Repository navigation
fix(cpu): align int4pack kernel contracts with the packed op - #4326
Merged
Merged
Conversation
- TorchAtenLinear and its AWQ variant declare the real N % 16 output-width contract and re-check group_size == in_features against the aten op's 32/64/128/256 group sizes, so AUTO falls through to TorchLinear instead of loading a layer that fails on the first forward. - TorchFusedLinear and its AWQ variant keep [8] for XPU and apply the CPU-only N % 16 check during validation. - dequantize_model no longer builds a shape-[0] bias for bias-less layers and keeps the checkpoint scale dtype instead of forcing FP16. - Document the dispatch contracts and the before/after mechanism in docs/kernel-dispatch-cpu.md.
This was referenced Nov 16, 2026
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
TL;DR
CPU
AUTOdispatch choseTorchAtenLinear/TorchFusedLinearfor layers that the packed int4 op rejects, and theirdequantize_modelcreated a shape-[0]bias for bias-less layers. This change declares the op's real output-width and group-size contracts, re-checks thegroup_size == in_featuresescape, and fixes the dequantization bias/dtype contract. 6 files, +493/-14.1. The problem
TorchAtenLinearandTorchFusedLineardeclaredSUPPORTS_OUT_FEATURES_DIVISIBLE_BY = [1], whileaten::_convert_weight_to_int4pack_for_cpurequiresN % 16 == 0.GroupedQuantLinear._validatealso letsgroup_size == in_featuresbypass the declared group-size set (qlinear/__init__.py:1023), sogroup_size = 512, in_features = 512reached an op that only accepts 32/64/128/256.AUTO therefore loaded the model and failed on the first forward:
dequantize_modelbuiltnn.Parameter(module.bias)even whenmodule.bias is None;nn.Parameter(None)is a shape-[0]tensor, so every bias-less layer failed in forward. It also forced FP16 weights regardless of the checkpoint scale dtype.flowchart TD A["AUTO on CPU"] --> B["TorchAtenLinear declares N % 1"] B --> C{"N % 16 == 0?"} C -- "no" --> D["kernel accepted by selector"] D --> E["first forward: ATen packer raises"] A --> F["group_size == in_features escape"] F --> G["ATen op raises qGroupSize"] style E fill:#f8d7da,stroke:#b02a37,stroke-width:2px style G fill:#f8d7da,stroke:#b02a37,stroke-width:2px2. The new flow
flowchart TD A["create_quant_layer(AUTO, CPU)"] --> B["validate_quant_linear"] B --> C["_validate_shape_constraints"] C --> D{"out_features divisible by<br/>declared divisor?"} D -- "no" --> E["NotImplementedError<br/>next AUTO candidate"] D -- "yes" --> F["kernel _validate"] F --> G{"class uses the<br/>aten int4pack op?"} G -- "no, AVX512 subclass" --> H["keep its wider contract"] G -- "yes" --> I["_validate_cpu_int4pack_group_size"] I --> J{"resolved group size<br/>in 32, 64, 128, 256?"} J -- "no" --> E J -- "yes" --> K["_validate_cpu_int4pack_out_features"] K --> L{"device is CPU and<br/>out_features % 16 == 0?"} L -- "no" --> E L -- "yes" --> M["candidate accepted"] E --> N["next candidate"] N --> B style E fill:#f8d7da,stroke:#b02a37,stroke-width:2px style M fill:#d1e7dd,stroke:#146c43,stroke-width:2pxThe same checks are applied to
TorchAtenAwqLinearandTorchFusedAwqLinear, which execute the same packed op.TorchCpuAvx512Linearkeeps its own[16, 32, 64, 128, 256, 512]group-size contract because it runs a different kernel.3. Change inventory
torch_aten_kernel.py[16]output width, CPU group-size re-check, bias/dtype-safe dequantizetorch_aten_kernel_awq.pytorch_fused.py[8]declaration plus CPU-only% 16check, dequantize fixtorch_fused_awq.pydocs/kernel-dispatch-cpu.md4. Tests / guarantees
test_aten_out_features_must_be_divisible_by_16test_aten_group_size_equal_to_in_features_is_rejectedtest_fused_cpu_out_features_must_be_divisible_by_16test_auto_falls_through_to_torch_for_odd_output_widthTorchLinearfor N=100test_avx512_keeps_its_wider_group_size_contracttest_dequantize_model_omits_bias_when_absentbias is None, BF16 weight stays BF16test_dequantize_model_preserves_bias_when_present5. Validation
tests/test_cpu_int4pack_dispatch_contracts.py: 11 passed.tests/test_torch_cpu_avx512.py tests/test_torch_fused_asym_qzeros.py tests/test_ret_idx_group_order.py tests/test_cpu_kernel_optimize.py tests/kernels/test_selection.py: 213 passed, 41 skipped. Twotest_aten_cpu_fused_forward_matches_fp32_oraclecases fail identically on the pre-change revision (host scale-dtype expectation), so they are not caused by this change.TorchLinear; bias-lessdequantize_modelreturnsbias=None.6. Reviewer notes
N % 16 == 0, notN % 8; N=8 still fails inside_convert_weight_to_int4pack_for_cpu.TorchFusedLinearkeeps[8]because it also serves XPU; the CPU-only 16-wide check is applied in_validate.group_size = -1remains unsupported byTorchAtenLinearas before; this change only closes thegs == Kescape.