Skip to content

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

Closed

Description

@Qubitium

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

Severity: high (silent wrong logits, no error). Status: confirmed on CPU at ef892ce9b. Kind: kernel-contract gap in the native INT8 path, not a regression from a specific commit.

Where

  • gptqmodel/nn_modules/qlinear/torch_int8.py: Int8PackedModule.forward calls torch.ops.aten._weight_int8pack_mm(flat[:, group, :], self.int8_weight_gnk[group], scale).
  • TorchInt8Linear.transform_cpu builds int8_weight_gnk as [group, out_features, max(group_lengths)].

Cause

aten::_weight_int8pack_mm reduces the contraction axis in 8-wide chunks and is only defined for K % 8 == 0. With any other width it reads past the end of each weight row and returns wrong values without raising. Measured on torch 2.11.0 (M=3, N=16, random codes, float oracle):

K 8 9 10 16 24 40 48 64
max abs error 3.8e-06 2.1e+01 1.3e+18 1.1e-05 7.6e-06 1.1e-05 1.5e-05 1.9e-05

N (out_features) and M (rows) are unconstrained; only K matters.

The group width is max(g_idx group lengths), so two supported layouts hit it:

  1. group_size=-1 — one group spanning in_features, so the width is in_features. Any model whose in_features % 8 != 0 miscomputes.
  2. Ragged g_idx groups — pack_block normalizes negative group indices to the last group, which turns group_size=32, in_features=96 with two -1 rows into lengths (30, 32, 34), width 34.

Repro

import torch
from gptqmodel.nn_modules.qlinear.torch_int8 import Int8PackedModule
weight = torch.zeros((2, 8, 4), dtype=torch.int8); weight[0] = 1; weight[1] = -1
scales = torch.tensor([[1.0] * 8, [2.0 ** -10] * 8])
m = Int8PackedModule(weight, scales.clone())
print(m(torch.ones(1, 8)))
# actual   [[7.9961, ..., 7.9961, -0.0039]]   (group 0 contributes 0)
# expected [[3.9961, ...]]                    (group 0 contributes 4.0)

The same defect is what makes two shipped tests fail on main:

tests/kernels/test_torch_int8.py::test_native_int8_negative_g_idx_matches_checkpoint_decode
tests/kernels/test_torch_int8.py::test_native_int8_scale_cast_does_not_destroy_canonical_precision

Both pass dequantize_weight() exactly (the storage repack is correct) and fail only the forward, which is why this went unnoticed: the exported weights are right, the runtime math is not.

Expected

The runtime group layout must satisfy the kernel's contraction alignment. Zero-padding the widest group (and the matching activation columns, which are already zero-padded for ragged groups) keeps the result exact because the extra products are 0 * 0.


Found while investigating two failing tests in tests/kernels/test_torch_int8.py after PR #4503; the failures are pre-existing on main and independent of that change.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions