-
Notifications
You must be signed in to change notification settings - Fork 349
Add NPU (Ascend) backend support for INT4 weight-only quantization workflow #3172
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/ao/3172
Note: Links to docs will display an error until the docs builds have been completed. This comment was automatically generated by Dr. CI and updates every 15 minutes. |
try: | ||
import torch_npu | ||
except ImportError: | ||
torch_npu = None | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PyTorch provide Autoload
mechinasm, so we do not need to import it explicitly.
@unittest.skipIf(torch_npu is None, "torch_npu is not available") | ||
@unittest.skipIf(not torch_npu.npu.is_available(), "NPU not available") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@unittest.skipIf(torch_npu is None, "torch_npu is not available") | |
@unittest.skipIf(not torch_npu.npu.is_available(), "NPU not available") | |
@unittest.skipIf(torch.accelerator.current_accelerator(True).type == "npu" and torch.accelerator.is_available(), "NPU not available") |
@unittest.skipIf( | ||
version.parse(torch_npu.__version__) < version.parse("2.7.1rc1"), | ||
"Need torch_npu 2.7.1rc1+", | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can remove it because there are some strcit version mapping between PyTorch and Torch_NPU
) | ||
|
||
assert int_data.dtype == torch.int32, ( | ||
f"torch_npu.npu_convert_weight_to_int4pack expects `int32` dtype" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
f"torch_npu.npu_convert_weight_to_int4pack expects `int32` dtype" | |
f"torch.ops.npu.npu_convert_weight_to_int4pack expects `int32` dtype" |
) | ||
|
||
assert int_data.shape[-1] % 8 == 0, ( | ||
f"torch_npu.npu_convert_weight_to_int4pack expects last dim must be aligned to 8,but got {int_data.shape[-1]}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
f"torch_npu.npu_convert_weight_to_int4pack expects last dim must be aligned to 8,but got {int_data.shape[-1]}" | |
f"torch.ops.npu.npu_convert_weight_to_int4pack expects last dim must be aligned to 8,but got {int_data.shape[-1]}" |
Hi @jcaip @jerryzh168 , please help to review it, thanks! |
Related to #3044
Summary
This PR adds NPU (Ascend) backend support for the INT4 weight-only quantization workflow.
It introduces a new tensor subclass,
Int4PlainInt32TensorNPU
, aligned with the existingInt4PlainInt32Tensor
for theplain_int32
packing format.Environment
Files changed
Modified
torchao/quantization/__init__.py
torchao/quantization/quant_api.py
torchao/quantization/quantize_/workflows/__init__.py
Added
torchao/quantization/quantize_/workflows/int4/int4_plain_int32_tensor_npu.py
test/quantization/quantize_/workflows/int4/test_int4_plain_int32_tensor_npu.py
Implementation Overview
Int4PlainInt32TensorNPU
to enable NPU backend support for INT4 weight-only quantization.quant_api.py
for dispatch.__init__.py
files to ensure proper import and exposure.Test Case
test/quantization/quantize_/workflows/int4/test_int4_plain_int32_tensor_npu.py