Skip to content

[torchao] Support quantization configs using module swap #21982

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jerryzh168
Copy link
Contributor

@jerryzh168 jerryzh168 commented Jul 31, 2025

Summary:

Current torchao integration quantizes the weights by wrapping weights in a top level linear module and use quantize_ to quantize it, this works for quantization methods that do inplace changes to the weight itself, such as int4, float8, since these only do inplace changes to the linear module itself

but there are quantization configs that would need module swap, such as awq, that's not supported, in order to support these, we wrap the linear in nn.Sequential so it is no longer a top level module and can be swapped to another module.

Test Plan:
uplodated an awq checkpoint: https://huggingface.co/torchao-testing/Phi-4-mini-instruct-int4wo-awq-0.13-dev and we test by loading the checkpoint

python tests/quantization/test_torchao.py

Reviewers:

Subscribers:

Tasks:

Tags:

@jerryzh168 jerryzh168 force-pushed the allow-torchao-module-swap-config branch from d9ce8c2 to 4de8f17 Compare July 31, 2025 01:58
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request enables support for torchao quantization methods that require module swaps, like AWQ, by wrapping the linear layer in an nn.Sequential. The change is well-contained and accompanied by a relevant test case. I've suggested one improvement to make the code more robust against future changes in torchao or different quantization configurations.

Comment on lines +171 to +168
dummy_linear[0].weight = param
quantize_(dummy_linear, torchao_config)
return dummy_linear.weight
return dummy_linear[0].weight
Copy link
Contributor

Choose a reason for hiding this comment

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

high

After quantization, the module may have been swapped. Instead of directly accessing dummy_linear[0].weight, retrieve the weight by inspecting the module's parameters. This avoids making fragile assumptions about the internal structure of the quantized module, which may change in future torchao versions.

    dummy_linear[0].weight = param
    quantize_(dummy_linear, torchao_config)
    # After quantization, the module may have been swapped.
    # We retrieve the single parameter, which is the quantized weight.
    params = list(dummy_linear.parameters())
    assert len(params) == 1, (
        "Expected the dummy module to have exactly one parameter after "
        f"quantization, but found {len(params)}."
    )
    return params[0].data

Copy link

👋 Hi! Thank you for contributing to the vLLM project.

💬 Join our developer Slack at https://slack.vllm.ai to discuss your PR in #pr-reviews, coordinate on features in #feat- channels, or join special interest groups in #sig- channels.

Just a reminder: PRs would not trigger full CI run by default. Instead, it would only run fastcheck CI which starts running only a small and essential subset of CI tests to quickly catch errors. You can run other CI tests on top of those by going to your fastcheck build on Buildkite UI (linked in the PR checks section) and unblock them. If you do not have permission to unblock, ping simon-mo or khluu to add you in our Buildkite org.

Once the PR is approved and ready to go, your PR reviewer(s) can run CI to test the changes comprehensively before merging.

To run CI, PR reviewers can either: Add ready label to the PR or enable auto-merge.

🚀

@jerryzh168 jerryzh168 force-pushed the allow-torchao-module-swap-config branch from 4de8f17 to f1473ee Compare July 31, 2025 03:47
Copy link
Member

@mgoin mgoin left a comment

Choose a reason for hiding this comment

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

Looks reasonable to me, thanks!

@mgoin mgoin enabled auto-merge (squash) July 31, 2025 20:13
@github-actions github-actions bot added the ready ONLY add when PR is ready to merge/full CI is needed label Jul 31, 2025
Copy link
Collaborator

@houseroad houseroad left a comment

Choose a reason for hiding this comment

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

Looks good.

@DarkLight1337
Copy link
Member

Can you merge from main to fix the CI failures?

auto-merge was automatically disabled August 1, 2025 18:27

Head branch was pushed to by a user without write access

@jerryzh168 jerryzh168 force-pushed the allow-torchao-module-swap-config branch from f1473ee to a3112b0 Compare August 1, 2025 18:27
… swap

Summary:
Current torchao integration quantizes the weights by wrapping weights
in a top level linear module and use quantize_ to quantize it, this works
for quantization methods that do inplace changes to the weight itself, such as
int4, float8, but there are quantization configs that would need module swap,
such as awq, that's not supported, in order to support these, we wrap the linear
in nn.Sequential so it is no longer a top level module and can be swapped to another module.

Test Plan:
uplodated an awq checkpoint: https://huggingface.co/torchao-testing/Phi-4-mini-instruct-int4wo-awq-0.13-dev
and we test by loading the checkpoint

```
python tests/quantization/test_torchao.py
```

Reviewers:

Subscribers:

Tasks:

Tags:
Signed-off-by: Jerry Zhang <[email protected]>
@jerryzh168 jerryzh168 force-pushed the allow-torchao-module-swap-config branch from a3112b0 to f511cfd Compare August 1, 2025 23:36
@jerryzh168
Copy link
Contributor Author

jerryzh168 commented Aug 2, 2025

the current error is because we just landed AWQ updates in torchao recently and it's not picked up by nightly yet
there is actually one more update to awq I plan to do next week, we can land this one after that

@jerryzh168 jerryzh168 changed the title [torchao] Support quantization configs using module support [torchao] Support quantization configs using module swap Aug 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
quantization ready ONLY add when PR is ready to merge/full CI is needed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants