Repository navigation
Description
Describe the bug
AWQ's symmetric RTN grid is fitted as scale = amax / max_code and decodes as
code * scale in the checkpoint's decode dtype. The division rounds to the
nearest representable scale, so a finite amax can select a scale whose top
code is not finite. For FP16, 65504 / 7 rounds up to 9360, and
7 * 9360 == 65520 is exactly the value FP16 rounds to infinity.
The staging code then clamped the overflowing product back to 65504, so the
staged tensor and the saved checkpoint disagreed: the packer still kept code
15 and the scale 9360, and a narrow decode returned inf. The clamp cannot
repair this, because the grid was already chosen one step past what the
checkpoint can store.
gptqmodel/looper/awq_processor.py on the current main:
# symmetric RTN branch
scales = max_val / max_int
w = torch.clamp(torch.round(w / scales), min_int, max_int) * scales
# ... later, after the finite-scale assertion
if input_dtype.is_floating_point:
limits = torch.finfo(input_dtype)
w.clamp_(limits.min, limits.max)The staged value only becomes finite through the final clamp, which is not
what the packer stores.
The same unrepaired scale is used by the clip search in
_pseudo_quantize_tensor_into, so a candidate is scored on a grid the packer
would not store.
GPU Info
Not hardware-specific. This is a staging and grid-selection defect in the
quantization path and reproduces on a plain CPU Torch run. Some AWQ inference
wrappers have a range-aware wide decode and will select FP32, so not every
backend necessarily returns inf; the confirmed defect is the disagreement
between the staged/search values and the saved grid, plus a narrow decode of
the saved grid producing inf.
Software Info
main at 947390c25.
To Reproduce
32x32 FP16 constant weight, bits=4, group_size=32, sym=True, GEMM layout:
import torch
from gptqmodel.looper.awq_processor import _pseudo_quantize_tensor_into
w = torch.full((32, 32), 65504, dtype=torch.float16)
dst = torch.empty_like(w)
scales = _pseudo_quantize_tensor_into(w, dst, bits=4, sym=True, group_size=32,
format=FORMAT.GEMM)
print(scales[0, 0].item()) # 9360
print(dst[0, 0].item()) # 65504, but the packer stores code 15Packing that staged tensor yields qweight word 0xffffffff and qzeros word
0x88888888. Decoding with the stored scale gives 15 * 9360 = 65520, which
is inf in FP16.
Observed
| Source weight | Staged value | FP16 scale | Packed code | FP32 decode | FP16 decode |
|---|---|---|---|---|---|
| 60000 | 59968 | 8568 | 15 | 59976 | 59968 |
| 65472 | 65472 | 9352 | 15 | 65464 | 65472 |
| 65504 | 65504 | 9360 | 15 | 65520 | inf |
| -65504 | -65504 | 9360 | 1 | -65520 | -inf |
The first two rows differ between FP32 and FP16 only by ordinary output
rounding; the last row is the non-finite endpoint.
Expected behavior
The staged value, the stored scale and the code the packer recovers must name
the same grid point in the dtype the kernel decodes, so a finite source weight
never exports a non-finite grid.
Model/Datasets
Not needed; the reproduction is a synthetic tensor.
Additional context
Suggested direction (not prescriptive): use one set of representable
code/scale constraints for the ordinary symmetric path and the candidate
search, and derive the replay value from the grid that will actually be
stored. Two constraints worth checking before reusing existing helper logic:
- A
1 - epssafety margin is too conservative. A product at or below the
finite maximum rounds back into range, so a margin would discard a code that
fits (FP167 * 9352would drop to code 6). - The GEMV-fast and LLM-AWQ layouts additionally store a
-2 ** (bits - 1) * scaleoffset.-8 * 9360overflows FP16 even though the
top code alone would fit, and the existingscaled_zero_offsetcheck already
rejects that pack; lowering the scale enough to represent the offset would
cost roughly 8000 codes of usable range, so those layouts may reasonably stay
fail-closed.
Acceptance beyond this endpoint: full pseudo-quantize -> pack -> unpack chain,
positive and negative endpoints, subnormal scales, GEMM/GEMV/GEMV-fast, a wide
decode fallback, and the native kernel path on each target backend.
Activity