Skip to content
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

[CPU] Fix registration of int4wo linear implementation on CPU #1578

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

Conversation

Xia-Weiwen
Copy link
Collaborator

@Xia-Weiwen Xia-Weiwen commented Jan 17, 2025

Summary
Int4wo on CPU does not run into expected mm op (torch.ops.aten._weight_int4pack_mm_for_cpu). It seems to be a regression after some refactoring of related code. This PR fixes it by registering a linear impl for the Int4CPULayout, which calls torch.ops.aten._weight_int4pack_mm_for_cpu for computation. The new impl is enabled for torch>=2.6. The new impl does not require dtype to be bfloat16. It supports fp32, fp16, bf16 for both weight and activation.

Test plan

python test/quantization/test_quant_api.py -k test_int4wo_cpu

Copy link

pytorch-bot bot commented Jan 17, 2025

🔗 Helpful Links

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

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

✅ No Failures

As of commit 6783a22 with merge base de5c6e1 (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 Jan 17, 2025
@Xia-Weiwen Xia-Weiwen added topic: not user facing Use this tag if you don't want this PR to show up in release notes topic: bug fix Use this tag for PRs that fix bugs labels Jan 17, 2025
if x_dim == 3:
example_inputs = (example_inputs[0].unsqueeze(0),)

with torch.no_grad(), torch.autocast(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since the model and inputs have already been converted to target data type, why we still need to enable autocast here?

y = torch.ops.aten._weight_int4pack_mm(
act_mat.contiguous(), packed_weight, groupsize, scale_and_zero
)
y = torch.ops.aten._weight_int4pack_mm(
Copy link
Collaborator

Choose a reason for hiding this comment

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

why we change _weight_int4pack_mm_for_cpu to _weight_int4pack_mm? I remember _weight_int4pack_mm didn't register for CPU.

Copy link
Contributor

@sanchitintel sanchitintel Jan 18, 2025

Choose a reason for hiding this comment

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

In this PR, @Xia-Weiwen moved the CPU implementation to torchao/dtypes/uintx/int4_cpu_layout.py, where support for more activation dtypes is also being added.

This code now exclusively pertains to CUDA.

@sanchitintel
Copy link
Contributor

sanchitintel commented Jan 18, 2025

Int4wo on CPU does not run into expected mm op (torch.ops.aten._weight_int4pack_mm_for_cpu)

The FX IR pattern you shared with me after running a toy model did have a call to torch.ops.aten._weight_int4pack_mm_for_cpu, but the whole pattern corresponding to it was very weird (even had a aten.mm call at the end), so something is indeed broken.
If we had run an LLM with torchchat with int4 WoQ & simply searched for torch.ops.aten._weight_int4pack_mm_for_cpu in the FX graph of the model, we may probably not have discovered this issue.

Is it possible to add a UT for a small model that uses torch.compile, and also somehow checks if the pattern is as expected?
Perhaps, the corresponding UT in test/integration/test_integration.py could be modified.

Thanks!

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. topic: bug fix Use this tag for PRs that fix bugs topic: not user facing Use this tag if you don't want this PR to show up in release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants