Skip to content

[BUG] Rotation Hadamard crashes on CPU tensors; left-padding calibration truncation drops BOS; QQQ gs=128 export scale from W not Q; W4AFP8 Triton quantizer not bit-exact #4008

Closed

Description

@Qubitium

Audit of main @ 105602e. Four independent, smaller correctness issues. Each one was reproduced.

1. Rotation Hadamard ops ignore the tensor device: rotated models crash on CPU when the CUDA extension is built, and rotation can't be applied without CUDA (medium)

  • quantization/rotation/hadamard_utils.py import_fast_hadamard_transform: it picks the CUDA JIT _FastHadamardTransform whenever hadamard_available(), whatever device the input is on. It is used by matmul_hadU_cuda / apply_online_hadamard, which BaseQuantLinear._apply_rotation_to_input (nn_modules/qlinear/__init__.py:824) and the HookedLinear calibration path (hooked_linear.py:137) call. The CUDA op has no CPU kernel, and the portable _TorchHadamardTransform is only used when the extension is missing.
  • apply_exact_had_to_linear: does an unconditional W_.float().cuda(), even though models/base.py:2003-2010 explicitly routes MPS rotation to CPU.
GPU host, rotated GPTQ model, gptq_torch on CPU:
  cpu forward FAIL: NotImplementedError Could not run 'gptqmodel_hadamard::fast_hadamard_transform' with arguments from the 'CPU' backend
same script with CUDA_VISIBLE_DEVICES=  -> cpu forward ok
CPU-only quantize with rotation=:  exact had FAIL: RuntimeError No CUDA GPUs are available

Fix: dispatch per call on x.device.type (CUDA op only for CUDA tensors, _TorchHadamardTransform otherwise), and compute apply_exact_had_to_linear on the weight's own device. tests/test_rotation_persistence.py only checks norm fusion and config round trips. (Rotation numerics themselves are correct: logits reproduce to ~2e-7 on Llama/Qwen2 across all Hadamard table sizes.)

2. With padding_side="left", overlong calibration rows are front-truncated and lose BOS (low-medium)

gptqmodel/utils/calibration.py:471 trims rows longer than max_position_embeddings to row[-max:] whenever tokenizer.padding_side == "left", ignoring tokenizer.truncation_side (HF default: right). Every trimmed sample loses its BOS / attention-sink token, so calibration activations differ from inference.

With max_position_embeddings=32 and GPTQMODEL_LOG_CALIBRATION_SAMPLES=1:

left : trimmed 12 rows ... preview=' brown networks many ...'          (no BOS)
right:                     preview='<|begin_of_text|>integer fox ...'

Fix: truncate according to tokenizer.truncation_side, not padding_side.

3. QQQ grouped export takes the INT8 channel scale from W instead of the quantized Q, so packed weights saturate and differ from the in-memory wq (low)

gptqmodel/quantization/qqq.py:1075-1085 (main path) and :519-531 (fallback) run quantizer_extra.find_params(self.layer.weight.data) while the layer still holds W. Upstream QQQ assigns weight.data = Q first. GPTQ error propagation pushes max|Q| to ~1.10× max|W|, so int8 values reach ~140 and are clamped to 127. mlx_qqq.py:132-134 has the same issue, with a comment giving the wrong rationale.

gs=128: max|deq - Q| = 0.29 (7.1% of row max); pre-clamp int8 max 140.18 (48 values > 127.5)
gs=-1 : max diff 8e-4 (exact)          scale-from-Q: 0.016

The measured output-accuracy impact is small (calibration error 8.920e7 vs 8.928e7 over 5 seeds). The real problem is that the exported weights differ from the in-memory wq used for loss logging and next-layer replay, which departs from upstream. Fix: compute scale_extra from Q (after the Conv1D transpose/reshape). There is no QQQ quantize → pack → dequant round-trip test at gs=128.

4. W4AFP8 Triton token quantizer is not bit-exact with its Torch oracle (low)

gptqmodel/nn_modules/qlinear/w4a_triton.py:114: q = _to_e4m3_rn(clamp(x / scale, ±448)) uses Triton's approximate fp32 /. Near E4M3 rounding midpoints the result is one ULP off. The docstring promises bit-exact agreement with fp8_token_qdq, which is what GPTQ replay calibrates against.

elements 5242880   triton != f64ref: 102   torch != f64ref: 0      with tl.div_rn: 0

Fix: tl.div_rn(x, scale), keeping scale = amax / 448 as it is (switching the scale computation to div_rn too creates 139 mismatches). The existing test only packs zeros; add a bitwise fp8_pack vs fp8_token_qdq test on random input.

🤖 Generated with Claude Code

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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions