Skip to content

fix(int8): crop padded input rows in the cold GPTQ source decode - #4503

Merged
Qubitium merged 1 commit into
mainfrom
fix/issue-4483-int8-tail-cache
Nov 16, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/issue-4483-int8-tail-cache

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

Fixes #4483. The packed INT8 layout pads the input-channel axis up to the pack factor, so in_features=17 stores 20 decoded rows. transform_cpu() cropped them to in_features when it built the runtime view, but _dequantize_gptq_source_weight() reshaped all 20 and then subtracted per-channel zeros with 17 rows:

m = TorchInt8Linear(bits=8, group_size=-1, sym=False, desc_act=False,
                    in_features=17, out_features=8, bias=False).eval()
m.scales.fill_(1); m.transform_cpu(dtype=torch.float32)
m.dequantize_weight().shape   # (17, 8)
m.to(dtype=torch.float32)     # drops the runtime view
m.dequantize_weight()         # RuntimeError: 20 must match 17 at dim 0

BaseQuantLinear._apply drops the runtime view on any dtype/device apply, so explicit dequantization and export failed depending on call order. K=16 was unaffected, which is why the aligned widths in the suite never hit it.

What Changed

  • gptqmodel/nn_modules/qlinear/torch_int8.py: crop the decoded rows to self.in_features before indexing the per-channel groups, matching what transform_cpu() already does for the runtime view. If the buffers decode fewer rows than in_features, raise a clear RuntimeError instead of the opaque size-mismatch error.
  • tests/test_torch_int8_tail_padding_4483.py (new): K=17/18/19 cold-vs-warm decode equality plus an independent value oracle, forward oracle after cache invalidation, and an aligned K=16 control.

No API change.

Tests

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

pytest tests/test_torch_int8_tail_padding_4483.py -q
# 7 passed

pytest tests/kernels/test_torch_int8.py tests/test_torch_int8_tail_padding_4483.py -q
# 70 passed, 2 failed

The three cold-decode cases fail on unmodified main with the reported
RuntimeError: The size of tensor a (20) must match the size of tensor b (19) and pass with the fix.

tests/kernels/test_torch_int8.py::test_native_int8_negative_g_idx_matches_checkpoint_decode and
::test_native_int8_scale_cast_does_not_destroy_canonical_precision also fail on unmodified main in this environment (same assertions, test_torch_int8.py:570); they are pre-existing and unrelated to this diff.

Notes

  • dequantize_weight() is the only caller of _dequantize_gptq_source_weight(), and the forward path rebuilds the runtime view before decoding, so nothing else depended on the padded row count.
  • Group-contiguous runtime decoding (_cached_int8_dequantize) is unchanged; this only aligns the cold source path with it.

The packed INT8 layout pads the input-channel axis to the pack factor, so
in_features=17 stores 20 decoded rows. transform_cpu() cropped them when it
built the runtime view, but _dequantize_gptq_source_weight() reshaped all 20
and then subtracted per-channel zeros with 17 rows:

  RuntimeError: The size of tensor a (20) must match the size of tensor b (17)

Any .to(...)/_apply drops the runtime view, so dequantize_weight() (and
therefore explicit export) failed depending on call order. Crop to
in_features and fail loudly if the buffers decode fewer rows than that.
@Qubitium
Qubitium merged commit d654d22 into main Nov 16, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/issue-4483-int8-tail-cache branch November 16, 2026 23:41
@Qubitium

Copy link
Copy Markdown
Collaborator Author

Follow-up: the two pre-existing tests/kernels/test_torch_int8.py failures noted in the description (negative_g_idx, scale_cast) turned out to be a real product bug, not test debt. aten::_weight_int8pack_mm only supports a contraction width that is a multiple of 8, and both layouts fed it an unaligned group width (4, and the ragged (30, 32, 34)), so the forward silently miscomputed while dequantize_weight() stayed exact.

Tracked in #4511 and fixed by #4512: group codes are now zero-padded to the kernel alignment. tests/kernels/test_torch_int8.py is 72/72 on current main.

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 dequantization loses tail padding after source-cache invalidation

1 participant