Skip to content

fix(cpu): align int4pack kernel contracts with the packed op - #4326

Merged
Qubitium merged 1 commit into
mainfrom
fix/4181-cpu-int4pack-contracts
Nov 16, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/4181-cpu-int4pack-contracts

Conversation

@ZX-ModelCloud

Copy link
Copy Markdown
Collaborator

TL;DR

CPU AUTO dispatch chose TorchAtenLinear / TorchFusedLinear for layers that the packed int4 op rejects, and their dequantize_model created a shape-[0] bias for bias-less layers. This change declares the op's real output-width and group-size contracts, re-checks the group_size == in_features escape, and fixes the dequantization bias/dtype contract. 6 files, +493/-14.

1. The problem

TorchAtenLinear and TorchFusedLinear declared SUPPORTS_OUT_FEATURES_DIVISIBLE_BY = [1], while aten::_convert_weight_to_int4pack_for_cpu requires N % 16 == 0. GroupedQuantLinear._validate also lets group_size == in_features bypass the declared group-size set (qlinear/__init__.py:1023), so group_size = 512, in_features = 512 reached an op that only accepts 32/64/128/256.

AUTO therefore loaded the model and failed on the first forward:

RuntimeError: _convert_weight_to_int4pack_cpu : expect N to be dividable by 16
RuntimeError: _weight_int4pack_mm_cpu: expect qGroupSize to be 32, 64, 128 or 256, got 512

dequantize_model built nn.Parameter(module.bias) even when module.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:2px
Loading

2. 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:2px
Loading

The same checks are applied to TorchAtenAwqLinear and TorchFusedAwqLinear, which execute the same packed op. TorchCpuAvx512Linear keeps its own [16, 32, 64, 128, 256, 512] group-size contract because it runs a different kernel.

3. Change inventory

Area File Purpose
ATen GPTQ torch_aten_kernel.py [16] output width, CPU group-size re-check, bias/dtype-safe dequantize
ATen AWQ torch_aten_kernel_awq.py same contracts
Fused GPTQ torch_fused.py [8] declaration plus CPU-only % 16 check, dequantize fix
Fused AWQ torch_fused_awq.py same CPU check
Docs docs/kernel-dispatch-cpu.md new/old flow, before/after table

4. Tests / guarantees

Test Invariant
test_aten_out_features_must_be_divisible_by_16 N=8/24/100 rejected, N=16 accepted
test_aten_group_size_equal_to_in_features_is_rejected gs=K=512 rejected, gs=K=256 accepted
test_fused_cpu_out_features_must_be_divisible_by_16 CPU N=8/24 rejected
test_auto_falls_through_to_torch_for_odd_output_width AUTO picks TorchLinear for N=100
test_avx512_keeps_its_wider_group_size_contract AVX512 gs=512 still valid
test_dequantize_model_omits_bias_when_absent bias is None, BF16 weight stays BF16
test_dequantize_model_preserves_bias_when_present real bias keeps its shape

5. 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. Two test_aten_cpu_fused_forward_matches_fp32_oracle cases fail identically on the pre-change revision (host scale-dtype expectation), so they are not caused by this change.
  • Manual repro before/after: N=100 and gs=K=512 now fail during selection; AUTO selects TorchLinear; bias-less dequantize_model returns bias=None.

6. Reviewer notes

  • The real ATen packer constraint is N % 16 == 0, not N % 8; N=8 still fails inside _convert_weight_to_int4pack_for_cpu.
  • TorchFusedLinear keeps [8] because it also serves XPU; the CPU-only 16-wide check is applied in _validate.
  • Per-channel group_size = -1 remains unsupported by TorchAtenLinear as before; this change only closes the gs == K escape.

- 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.
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.

2 participants