Skip to content

[Audit][torch/tritonv2] Fused GPTQ GEMV/small-M GEMM unreachable for BF16 models: gate requires scales.dtype == x.dtype but GPTQ scales load as FP16 #5740

Closed

Description

@Qubitium

Summary

TorchLinear._can_use_rocm_fused requires self.scales.dtype == x.dtype before it dispatches the fused GPTQ GEMV (M=1) and the small-M GEMM (M=2..64) on Ada/Blackwell/ROCm. TritonV2Linear delegates to the same gate (#3982/#3999).

In a real load those two dtypes differ for every BF16 model:

  • GPTQ checkpoints always store FP16 scales (_scales_for_storage casts to FP16, nn_modules/qlinear/__init__.py:1612).
  • After Accelerate casts the buffers to the BF16 model dtype, the loader restores every narrowed quantization buffer to its checkpoint dtype (utils/model.py:4449-4470, _load_narrowed(fp16, bf16) == True).

So a BF16 model (the auto dtype for Qwen2.5/3, Llama-3 and most current configs) runs with FP16 scales and BF16 activations. The fused paths are never taken. Decode and small-batch then fall back to the full dequantize + GEMM. That fallback also makes an extra full-weight cast, because the Triton decode emits the scale dtype (FP16) and _forward_eager then calls weights.to(dtype=x.dtype).

The fused kernels do not need the scale dtype to match. Both load scales and immediately do .to(tl.float32) (gptq_gemv.py and gptq_gemm.py), and they only use x_ptr.dtype.element_ty for the activation-side rounding. The kernel tests never hit this case, because they build the module with .to(device="cuda", dtype=dtype), which makes the scales BF16 too (tests/kernels/test_gptq_ada_gemv.py:95).

Impact

Location (@ 85caf6c)

  • gptqmodel/nn_modules/qlinear/torch.py:714-743 (_can_use_rocm_fused):
            and not x.requires_grad
            and self.scales.dtype == x.dtype      # <- false for every BF16 model with FP16 checkpoint scales
            ...
            and self._decoded_weight_fits(x.dtype)
  • gptqmodel/nn_modules/qlinear/torch.py:614: weights = weights.to(device=x.device, dtype=x.dtype). This is the per-forward full-weight cast on the fallback. The Triton decode returns self._decode_dtype(), i.e. FP16 (torch.py:1166).
  • gptqmodel/nn_modules/qlinear/tritonv2.py:175-180: delegates to the same gate.

Mechanism

The gate is stricter than what the kernels require. Combined with the loader's (correct) restore of FP16 checkpoint scales, the fused paths become unreachable for BF16 models.

Reproduction / Validation

audit_kernels/repro_fused_gate_bf16.py, executed on CPU. The device and arch predicates (_arch_is_cuda_fused, _rocm_arch_is_fused, _can_use_triton_dequant) are monkeypatched to True so that only the dtype predicate differs. The module is a 2048x2048 4-bit GPTQ v2 layer.

fp16 checkpoint scale restored after a bf16 load: True
scales=torch.float16 x=torch.float16: fused GEMV eligible = True
scales=torch.float16 x=torch.bfloat16: fused GEMV eligible = False
scales=torch.bfloat16 x=torch.bfloat16: fused GEMV eligible = True

The end-to-end speed impact was not executed: requires an Ada/Blackwell/ROCm GPU.

Suggested fix

Relax the predicate to what the kernels support, and keep the range check:

            and self.scales.dtype in (torch.float16, torch.bfloat16)
            and self._decoded_weight_fits(x.dtype)

Then add GEMV/GEMM tests that load FP16 scales with BF16 activations against the FP64 oracle. Today's tests cast the scales along with the module. For the dense fallback, ask the Triton decoder for x.dtype directly when _decoded_weight_fits(x.dtype) holds, instead of decoding to FP16 and casting the whole weight.

Notes

  • Not verified on hardware. Parity of the kernels with FP16 scales and BF16 activations is inferred from the kernel source (scales are loaded and widened to FP32 before use) and should be confirmed by the suggested test.

Found by automated code audit (Claude Code).

Activity

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

    @Qubitium
    CollaboratorAuthor

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

    Pull request: #5921

    Unblocking the fused GPTQ GEMV/small-M GEMM gate for BF16 models

  3. added a commit that references this issue on Nov 22, 2026
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

    performancesafescanned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions