Skip to content

fix(int8): pad group codes to the kernel's 8-wide contraction alignment - #4512

Merged
Qubitium merged 1 commit into
mainfrom
fix/int8-contraction-alignment
Nov 17, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/int8-contraction-alignment

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

Fixes #4511. aten::_weight_int8pack_mm reduces the contraction axis in 8-wide chunks and is only defined for K % 8 == 0. With a narrower or misaligned width it reads past each weight row and returns wrong values without raising, so two supported INT8 layouts produced incorrect logits silently:

  • group_size=-1, where the group width is in_features (in_features % 8 != 0);
  • ragged g_idx groups, e.g. the (30, 32, 34) layout that negative group indices produce.

Measured on torch 2.11.0 (M=3, N=16, float oracle): K=8/16/24/32 exact, K=4/12/15/17/33 wrong (errors up to 1e18).

m = Int8PackedModule(weight, scales)   # 2 groups x 4 channels
m(torch.ones(1, 8))
# actual   [[7.9961, ..., 7.9961, -0.0039]]   group 0 contributes 0
# expected [[3.9961, ...]]                    group 0 contributes 4.0

This is also the root cause of the two tests/kernels/test_torch_int8.py failures I flagged as pre-existing in #4503's description:

::test_native_int8_negative_g_idx_matches_checkpoint_decode
::test_native_int8_scale_cast_does_not_destroy_canonical_precision

Both already matched dequantize_weight() exactly; only the forward was wrong, which is why the exported weights looked correct.

What Changed

  • gptqmodel/nn_modules/qlinear/torch_int8.py
    • New _INT8_KERNEL_CONTRACTION_ALIGN = 8 and _align_int8_group_width() documenting the measured kernel contract.
    • TorchInt8Linear.transform_cpu: pad the widest group up to the alignment when building int8_weight_gnk, so the runtime module never has to copy the codes.
    • Int8PackedModule.__init__: pad a copy when a directly constructed module receives an unaligned width, so the public class cannot silently miscompute. source_group_width keeps the unpadded width for the default group_lengths derivation.
    • _activations derives default group lengths from the unpadded width, and the existing zero-padding path pads each group to the aligned width.
  • tests/kernels/test_torch_int8_contraction_alignment.py (new): the alignment helper's rounding/rejection contract, exact float64 oracle checks for directly constructed modules at widths 4/12/34 (symmetric and asymmetric), and end-to-end TorchInt8Linear checks at group_size=-1 with in_features 17/20/34.

Padding is numerically exact: the extra code rows are zero and the matching activation columns are already zero, so they contribute nothing. dequantize_weight()/export are unchanged because they crop by the true group_lengths.

No API change. _int8_group_lengths keeps the true per-group lengths.

Tests

CPU-only (GPTQ_BYPASS_SAFETY_CHECKS=1, CUDA_VISIBLE_DEVICES="", PYTHONPATH = worktree):

pytest tests/kernels/test_torch_int8_contraction_alignment.py -q
# 20 passed

pytest tests/kernels/test_torch_int8.py tests/kernels/test_torch_int8_contraction_alignment.py \
       tests/test_torch_int8_tail_padding_4483.py -q
# 92 passed   (70 passed / 2 failed before)

pytest tests/kernels/test_torch_int8_awq.py tests/test_cpu_kernel_optimize.py \
       tests/test_rocm_kernel_contracts.py -q
# 448 passed

ruff check gptqmodel/nn_modules/qlinear/torch_int8.py \
           tests/kernels/test_torch_int8_contraction_alignment.py
# All checks passed

The 9 behavioural tests in the new file fail on unmodified main (with only the helper injected so the file imports) and pass with the fix.

Notes

  • The alignment constant is measured, not documented by PyTorch; it is centralized in one constant with the measurement recorded next to it.
  • tests/kernels/ still has pre-existing environment failures unrelated to this change: 24 test_gptq.py::TestKernelOutput errors (CUDA-only kernels on a CPU host) and test_gguf_torch_cpu.py::...q4_k-Q4_K-bf16 (also fails on unmodified main).

aten::_weight_int8pack_mm reduces the contraction axis in 8-wide chunks and is
only defined for K % 8 == 0. With a narrower or misaligned width it reads past
each weight row and silently returns wrong values, so two supported layouts
computed incorrect logits with no error:

  * group_size=-1 with in_features % 8 != 0 (the group width is in_features);
  * ragged g_idx groups such as the (30, 32, 34) layout that negative group
    indices produce.

Measured on torch 2.11: K=8/16/24/32 exact, K=4/12/15/17/33 wrong.

Zero pad the widest group up to the alignment in transform_cpu(), and make
Int8PackedModule pad its own copy when constructed directly. The activation
padding columns are already zero, so the extra products contribute exactly
nothing and the decoded weights are unchanged.

This also fixes the two pre-existing failures in tests/kernels/test_torch_int8.py
(negative_g_idx, scale_cast) that were unrelated to #4483.
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.

[BUG] CPU INT8 group matmul silently miscomputes when the widest group is not a multiple of 8

1 participant