Skip to content

[CPU] Enable DA8W4 on CPU #2128

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

Draft
wants to merge 16 commits into
base: main
Choose a base branch
from

Conversation

Xia-Weiwen
Copy link
Collaborator

@Xia-Weiwen Xia-Weiwen commented Apr 25, 2025

Summary
This PR enables DA8W4 on CPU.

  • It adds a new layout Int8DynamicActInt4WeightCPULayout and its implementation
  • It adds two custom ops: da8w4_linear_prepack_cpu for weight packing and da8w4_linear_cpu for DA8W4 GEMM.
  • It adds C++ kernels for the two new custom ops

The ops and kernels won't be available unless torchao is built from source with USE_CPP_KERNELS=1 on Linux only.

Test plan

pytest test/quantization/test_quant_api.py -k test_8da4w_cpu

Copy link

pytorch-bot bot commented Apr 25, 2025

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/ao/2128

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit 369000f with merge base 60d63a6 (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Apr 25, 2025
@Xia-Weiwen Xia-Weiwen added cpu quantize topic: new feature Use this tag if this PR adds a new feature labels Apr 25, 2025
torch.compile(m, fullgraph=True, dynamic=True),
*example_inputs,
)
assert "_weight_int4pack_mm_for_cpu" in code[0]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember this op is weight only quant?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. This op is used here because we don't have an op to compute da8w4 on CPU yet. So it will fallback to explicit dequantization and call of this op.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have re-implemented the path. Now we use a new layout for da8w4. Thanks.

@Xia-Weiwen Xia-Weiwen marked this pull request as ready for review April 29, 2025 02:01
@Xia-Weiwen Xia-Weiwen requested a review from jerryzh168 April 29, 2025 03:16
@Xia-Weiwen Xia-Weiwen marked this pull request as draft May 7, 2025 01:17
@Xia-Weiwen
Copy link
Collaborator Author

@leslie-fang-intel This PR is updated to use a new layout. Please review again. Thanks.

args[0].scales,
args[0].qzeros,
not args[0].transposed,
args[0]._layout,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain more about the implementation of aten.t.default for DA8W4CPUAQTTensorImpl? It seems we only changes the transposed flag here? Do we have testcase to cover it?

Copy link
Collaborator Author

@Xia-Weiwen Xia-Weiwen May 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the comment. There is a comment explaining this just above. And this is a copy from the Int4CPULayout. We have it here because Int4CPULayout has it. And I am afraid there is not a UT for it. And it seems unused when running models. Do you want me to remove it?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yean, how about we remove it at now and add it back with the UT when met the use case.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have removed it. Thanks.

@Xia-Weiwen Xia-Weiwen changed the title [CPU] enable int8_dynamic_activation_int4_weight with Int4CPULayout [CPU] enable int8_dynamic_activation_int4_weight on CPU May 16, 2025
@Xia-Weiwen Xia-Weiwen marked this pull request as ready for review May 16, 2025 05:59
@Xia-Weiwen Xia-Weiwen changed the title [CPU] enable int8_dynamic_activation_int4_weight on CPU [CPU] Add a new layout for int8_dynamic_activation_int4_weight on CPU May 16, 2025
@Xia-Weiwen
Copy link
Collaborator Author

Hi @jerryzh168 Could you please review this PR? Thanks.

2 similar comments
@Xia-Weiwen
Copy link
Collaborator Author

Hi @jerryzh168 Could you please review this PR? Thanks.

@Xia-Weiwen
Copy link
Collaborator Author

Hi @jerryzh168 Could you please review this PR? Thanks.

@Xia-Weiwen Xia-Weiwen marked this pull request as draft May 21, 2025 02:57
@Xia-Weiwen Xia-Weiwen removed the request for review from jerryzh168 May 21, 2025 02:57
@Xia-Weiwen Xia-Weiwen changed the title [CPU] Add a new layout for int8_dynamic_activation_int4_weight on CPU [CPU] Enable DA8W4 on CPU May 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. cpu quantize topic: new feature Use this tag if this PR adds a new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants