Repository navigation
fix(int8): pad group codes to the kernel's 8-wide contraction alignment - #4512
Merged
Merged
Conversation
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.
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.
Summary
Fixes #4511.
aten::_weight_int8pack_mmreduces the contraction axis in 8-wide chunks and is only defined forK % 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 isin_features(in_features % 8 != 0);g_idxgroups, 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/32exact,K=4/12/15/17/33wrong (errors up to 1e18).This is also the root cause of the two
tests/kernels/test_torch_int8.pyfailures I flagged as pre-existing in #4503's description: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_INT8_KERNEL_CONTRACTION_ALIGN = 8and_align_int8_group_width()documenting the measured kernel contract.TorchInt8Linear.transform_cpu: pad the widest group up to the alignment when buildingint8_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_widthkeeps the unpadded width for the defaultgroup_lengthsderivation._activationsderives 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-endTorchInt8Linearchecks atgroup_size=-1within_features17/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 truegroup_lengths.No API change.
_int8_group_lengthskeeps the true per-group lengths.Tests
CPU-only (
GPTQ_BYPASS_SAFETY_CHECKS=1,CUDA_VISIBLE_DEVICES="",PYTHONPATH= worktree):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
tests/kernels/still has pre-existing environment failures unrelated to this change: 24test_gptq.py::TestKernelOutputerrors (CUDA-only kernels on a CPU host) andtest_gguf_torch_cpu.py::...q4_k-Q4_K-bf16(also fails on unmodifiedmain).