Skip to content

[Audit][tritonv2] TritonV2Linear skips the #5606 range-safe decode: FP16 forward returns NaN where BACKEND.TORCH is finite #5738

Closed

Description

@Qubitium

Summary

#5606 made the TorchLinear decode range-safe. When decoded weights do not fit the activation dtype, it decodes in a wide dtype and reduces with linear_wide_bias, and the fused ROCm/Ada kernels are gated by _decoded_weight_fits(x.dtype). The issue said "Check the separate streaming/Triton paths before claiming parity there". TritonV2Linear.forward (BACKEND.GPTQ_TRITON, AUTO priority 40 on CUDA) never makes that check. It calls QuantLinearFunction → quant_matmul → dequant(input.dtype, ...), and that always narrows the FP32-decoded weight to the activation dtype. For FP16 activations, any decoded weight |(q - z) * s| > 65504 becomes inf, and 0 * inf gives NaN in the GEMM. The same checkpoint gives a finite, correct output on BACKEND.TORCH.

Impact

  • Post-quant accuracy. Severity is low: only checkpoints with large per-group scales are affected (for 4-bit, s * 15 > 65504, i.e. s > ~4367 in FP16). The failure is NaN/inf output, where the Torch backend returns the correct value.
  • BF16 activations are not affected, because the kernel decodes in FP32 and BF16 has FP32's range.

Location (@ 85caf6c)

  • gptqmodel/nn_modules/qlinear/tritonv2.py:185: out = QuantLinearFunction.apply(x, self.qweight, self.scales, ...), with no _decoded_weight_fits check. The class inherits that method from TorchLinear.
  • gptqmodel/nn_modules/triton_utils/dequant.py:352: W = dequant(input.dtype, qweight, scales, qzeros, g_idx, bits, pack_bits, maxq). The kernel computes in FP32, then tl.cast(weights, out_dtype).
  • For comparison, the gated paths are torch.py:740 (and self._decoded_weight_fits(x.dtype) on the fused kernels), the _forward_eager wide branch, and _dequantize_weight_triton (which asks for self._decode_dtype()).

Mechanism

The decode dtype is taken from the activation, not from the stored scale range, so the overflow happens inside the decoder before any FP32 reduction can help. This is the same failure #5606 fixed for TorchLinear.

Reproduction / Validation

audit_kernels/repro_tritonv2_range.py, executed on CPU. It uses the #5606 recipe (4-bit, asym, code 15, zero 0, FP16 scale 8192) with FP16 activations. The TorchLinear output is real. The TritonV2 result is a torch emulation of dequant_kernel (FP32 decode, then cast to input.dtype) followed by the same matmul_fp32_reduce that quant_matmul calls. The Triton kernel itself was not executed: requires CUDA.

TorchLinear fp16 out[0,0] = 30720.0 (expected 0.25*15*8192 = 30720)
TritonV2 emulated out[0,0] = nan

Suggested fix

In TritonV2Linear.forward, route to the already range-safe parent path when the decode does not fit:

if self.training or not self._decoded_weight_fits(x.dtype):
    return super().forward(x)   # TorchLinear: wide decode + linear_wide_bias

Alternatively, pass self._decode_dtype() into quant_matmul/dequant and reduce with linear_wide_bias when it differs from x.dtype. _decoded_weight_fits reads an eagerly precomputed answer, so the check adds no host sync per forward.

Notes

Found by automated code audit (Claude Code).

Activity

  1. added
    bug
    on Nov 20, 2026
  2. added
    scanned
    safe
    on Nov 20, 2026
  3. Qubitium commented on Nov 20, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim released: agent-4e3a0639ecaf finished this issue.

    Pull request: #5920

    Fixing TritonV2Linear's missing range-safe decode so FP16 forwards stop returning NaN

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

    bugsafescanned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions