Skip to content

Add dequantize, 3D from_hp, transpose, and _grouped_mm support to Int4 tensors - #4731

Draft
eryk-roch wants to merge 1 commit into
pytorch:mainfrom
eryk-roch:add-grouped-mm-int4
Draft

Add dequantize, 3D from_hp, transpose, and _grouped_mm support to Int4 tensors#4731
eryk-roch wants to merge 1 commit into
pytorch:mainfrom
eryk-roch:add-grouped-mm-int4

Conversation

@eryk-roch

Copy link
Copy Markdown

Add support for MoE (Mixture of Experts) models with int4 weight-only quantization by enabling 3D expert weight stacks and grouped_mm dispatch.

MoE models like Gemma-4 and Qwen3-Coder-Next store expert weights as 3D stacked tensors (e.g. [num_experts, N, K]) and use torch._grouped_mm for the forward pass. Previously, Int4PlainInt32Tensor only supported 2D tensors and neither Int4 tensor class implemented aten._grouped_mm.default, so these expert weights could not be quantized.

Changes to Int4PlainInt32Tensor (XPU/NPU):

  • Add dequantize() using identity-matrix trick with existing int4pack kernel
  • Add 3D support to from_hp() via per-slice quantization
  • Add aten.transpose.int handler (logical-only, packed data stays in place)
  • Add aten._grouped_mm.default handler (dequant-then-native fallback)

Changes to Int4Tensor (CUDA/mslk):

  • Add dequantize() with manual int4 unpack and affine dequantization
  • Add aten._grouped_mm.default handler (dequant-then-native fallback)

Tests added for both tensor classes: dequantize roundtrip, 3D from_hp, and grouped_mm end-to-end with quantize_ API.

…4 tensors

Add support for MoE (Mixture of Experts) models with int4 weight-only
quantization by enabling 3D expert weight stacks and grouped_mm dispatch.

MoE models like Gemma-4 and Qwen3-Coder-Next store expert weights as 3D
stacked tensors (e.g. [num_experts, N, K]) and use torch._grouped_mm for
the forward pass. Previously, Int4PlainInt32Tensor only supported 2D
tensors and neither Int4 tensor class implemented aten._grouped_mm.default,
so these expert weights could not be quantized.

Changes to Int4PlainInt32Tensor (XPU/NPU):
- Add dequantize() using identity-matrix trick with existing int4pack kernel
- Add 3D support to from_hp() via per-slice quantization
- Add aten.transpose.int handler (logical-only, packed data stays in place)
- Add aten._grouped_mm.default handler (dequant-then-native fallback)

Changes to Int4Tensor (CUDA/mslk):
- Add dequantize() with manual int4 unpack and affine dequantization
- Add aten._grouped_mm.default handler (dequant-then-native fallback)

Tests added for both tensor classes: dequantize roundtrip, 3D from_hp,
and grouped_mm end-to-end with quantize_ API.
@pytorch-bot

pytorch-bot Bot commented Aug 10, 2026

Copy link
Copy Markdown

🔗 Helpful Links

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

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.

@meta-cla

meta-cla Bot commented Aug 10, 2026

Copy link
Copy Markdown

Hi @eryk-roch!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@eryk-roch
eryk-roch marked this pull request as draft August 10, 2026 14:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant