Skip to content

fix(tritonv2): keep the range-safe decode in eval (#5738) - #5920

Merged
Qubitium merged 2 commits into
mainfrom
fix/5738-tritonv2-range-safe-decode
Nov 20, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/5738-tritonv2-range-safe-decode

Conversation

@Qubitium

@Qubitium Qubitium commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

TritonV2Linear (BACKEND.GPTQ_TRITON) skipped the #5606 range-safe decode in eval. QuantLinearFunction asks the Triton dequant kernel to store the decoded weight in the activation dtype, so a checkpoint whose per-group scale makes (code - zero) * scale exceed that dtype's finite range produced an inf weight and 0 * inf == NaN in the GEMM. The same checkpoint on BACKEND.TORCH returns the correct value. This PR routes the overflowing decode through the existing TorchLinear wide decode.

Fixes #5738

What Changed

  • gptqmodel/nn_modules/qlinear/tritonv2.py: after the fused GEMV/GEMM dispatch (which already range-gates through _can_use_rocm_fused), gate the whole-weight QuantLinearFunction decode on the inherited, eagerly precomputed _decoded_weight_fits(x.dtype). A decoded weight that does not fit the activation dtype now takes super().forward(x) (TorchLinear's decode_dtype_for_scales decode + linear_wide_bias reduction). The check sits after the fused dispatch so the hot fused path keeps its single existing range check.
  • tests/kernels/test_tritonv2_range_safe_decode_5738.py: new fast CUDA regression. Drives the overflowing fixture (4-bit asymmetric, code 15, zero 0, FP16 scale 8192 -> weight 122880) through TritonV2Linear and TorchLinear, asserts both are finite and exactly match an independent FP64 oracle, asserts no dense weight is retained, and asserts a range-safe fixture still takes the native QuantLinearFunction decode.
  • docs/triton-dispatch.md: new "Range-safe decode" section with old/new Mermaid flows (failure path marked), before/after file:line table, contract and validation command. Linked from README.md.

No API change, no new cache, no environment switch. Weights stay packed; AUTO still selects TritonV2Linear.

Tests

CUDA_DEVICE_ORDER=PCI_BUS_ID CUDA_VISIBLE_DEVICES=0 PYTHONPATH=$PWD HF_HUB_OFFLINE=1 \
  /root/venv/bin/python -m pytest \
  tests/kernels/test_tritonv2_range_safe_decode_5738.py \
  tests/kernels/test_tritonv2_dispatch.py \
  tests/kernels/test_tritonv2_bias_epilogue.py \
  tests/kernels/test_triton_fp32_reduce.py \
  tests/test_gptq_torch_eval_decode_nan_5606.py -q
# 74 passed, 1 warning in 5.75s

Related TritonV2/CUDA epilogue set (same env):

CUDA_DEVICE_ORDER=PCI_BUS_ID CUDA_VISIBLE_DEVICES=0 PYTHONPATH=$PWD HF_HUB_OFFLINE=1 \
  /root/venv/bin/python -m pytest \
  tests/kernels/test_tritonv2_range_safe_decode_5738.py \
  tests/kernels/test_tritonv2_dispatch.py \
  tests/kernels/test_tritonv2_bias_epilogue.py \
  tests/kernels/test_triton_fp32_reduce.py \
  tests/test_gptq_torch_eval_decode_nan_5606.py \
  tests/kernels/test_cuda_bias_epilogue.py \
  tests/kernels/test_uniform_group_lifecycle.py -q
# 144 passed, 1 warning in 7.64s

Broader kernel set (same env):

CUDA_DEVICE_ORDER=PCI_BUS_ID CUDA_VISIBLE_DEVICES=0 PYTHONPATH=$PWD HF_HUB_OFFLINE=1 \
  /root/venv/bin/python -m pytest \
  tests/kernels/test_cuda_bias_epilogue.py \
  tests/kernels/test_uniform_group_lifecycle.py \
  tests/kernels/test_kernel_compile_levels_gptq.py \
  tests/kernels/test_gptq.py -q
# 97 passed, 10 skipped, 10 warnings in 62.46s (0:01:02)

Red/green: with the code change reverted, test_tritonv2_eval_decode_matches_the_range_safe_torch_backend[float16] fails with TritonV2 returned tensor([nan, nan, nan, nan]); BF16 passes both ways.

/root/.local/bin/uvx [email protected] check gptqmodel docs/eora tests setup.py
# All checks passed!
git fetch origin main -q && git merge-tree --write-tree origin/main HEAD
# exit 0

Measured Impact

Hardware: RTX 4090 (SM 8.9), CUDA 0, torch 2.15.0+cu132, Triton 3.9.0. Fixture: 32x32, 4-bit asymmetric, gs=32, every code 15, zero 0, FP16 scale 8192 (x[0,0]=0.25). Oracle: FP64 x @ decoded_weight.

Metric (unit) Before After Change (after − before)
TritonV2 FP16 NaN outputs (count / 32) 32 0 −32
TritonV2 FP16 max abs error vs FP64 oracle (abs) NaN 0.0 non-finite → 0
TritonV2 FP16 output[0,0] (abs) NaN 30720.0 finite
BACKEND.TORCH FP16 NaN outputs (count / 32) 0 0 0
TritonV2 BF16 NaN outputs (count / 32) 0 0 0
Fused GEMV decode, K=N=4096, M=1, FP16 (ms, median of 100) 0.0570–0.0583 0.0555–0.0563 within noise (no regression)
Whole-weight decode, K=N=4096, M=1, FP16 (ms, median of 100) 0.1034–0.1037 0.1045–0.1096 +2.5 µs cached range lookup

Measurement commands:

# NaN/error table (run with and without the code change)
CUDA_DEVICE_ORDER=PCI_BUS_ID CUDA_VISIBLE_DEVICES=0 PYTHONPATH=$PWD HF_HUB_OFFLINE=1 \
  /root/venv/bin/python /tmp/5738-scratch/measure.py
# Latency: 20 warm-up + 100 timed forwards, CUDA events, same script

The whole-weight row is the non-fused CUDA path (for example A100/H100); the fused row is the Ada/Blackwell/ROCm hot path that the check order leaves untouched. The overflowing decode is slower by construction (wide FP32 reduction) and only applies when the activation dtype cannot hold the weight.

Model Support / Module Plan

Not applicable: no model definitions, MODEL_MAP entries, aliases, or module plans changed.

AWQ onboarding safety

Not applicable: no AWQ grouping/search/replay/folding change.

Logic Flow Documentation

  • This change alters dispatch selection; the required diagrams and docs page are included.
  • New-flow Mermaid diagram(s) describe the shipped code using real symbol names.
  • The removed/replaced mechanism is shown with file:line evidence (old QuantLinearFunction-only flow).
  • Failure and fallback paths are marked and visually distinct (classDef failure / classDef fallback).
  • docs/triton-dispatch.md repeats the diagrams, is titled for the mechanism, and is linked from README.md.

TritonV2Linear.forward dispatch after this change:

flowchart TD
    A["TritonV2Linear.forward"] --> B{"self.training"}
    B -->|"yes"| C["super().forward<br/>TorchLinear"]
    B -->|"no"| D["x2 = x.reshape(-1, K)"]
    D --> E{"_can_use_rocm_gemv(x2)<br/>or _can_use_rocm_gemm(x2)"}
    E -->|"yes"| F["_apply_rotation_to_input(x2)"]
    F --> G["TorchLinear._forward_eager<br/>gptq_gemv / gptq_gemm"]
    E -->|"no"| R{"_decoded_weight_fits(x.dtype)"}
    R -->|"no: decoded weight<br/>outside x.dtype"| W["super().forward<br/>wide decode + linear_wide_bias"]:::fallback
    R -->|"yes"| H["QuantLinearFunction.apply<br/>dequantize in x.dtype + matmul"]
    G --> I["adapter then out.to(x.dtype)"]
    H --> I
    C --> I
    W --> I

    classDef fallback fill:#ffdddd,stroke:#cc0000,color:#660000
Loading

Before #5738, the eval path had no range gate:

flowchart TD
    A["TritonV2Linear.forward<br/>eval"] --> B["QuantLinearFunction.apply"]
    B --> C["dequant<br/>out_dtype = input.dtype"]
    C --> D{"decoded weight fits<br/>input.dtype"}
    D -->|"yes"| E["finite matmul"]
    D -->|"no"| F["inf weight<br/>then 0 * inf = NaN"]:::failure

    classDef failure fill:#ffdddd,stroke:#cc0000,color:#660000
Loading

After #5738, an overflowing decode takes the #5606 wide decode:

flowchart TD
    A["TritonV2Linear.forward<br/>eval"] --> B{"fused GEMV/GEMM gate"}
    B -->|"yes"| F["gptq_gemv / gptq_gemm<br/>range-gated by #5606"]
    B -->|"no"| R{"_decoded_weight_fits(x.dtype)"}
    R -->|"yes"| C["QuantLinearFunction.apply<br/>dequant in x.dtype"]
    R -->|"no"| D["TorchLinear.forward<br/>wide decode + linear_wide_bias"]:::fallback
    C --> E["finite matmul"]
    D --> E

    classDef fallback fill:#ffdddd,stroke:#cc0000,color:#660000
Loading

Review Requirements

  • Reviewed this PR.
  • Checked that the code matches existing project structure, APIs, and conventions.
  • Avoided unnecessary monkeypatching and used the project's normal extension points where possible.
  • Minimized and compacted the impacted code surface.
  • Considered and eliminated potential regressions.

Kernel Accuracy Requirements

  • Accuracy measured against an independent Torch FP64 oracle (table above).
  • Fusion support changes: none. AUTO native class selection is preserved, and a test pins that a range-safe decode still takes QuantLinearFunction.
  • Kernel inventory/device report: unchanged; no new kernel, format, or architecture gate.

Notes

  • The fallback is the existing TorchLinear wide decode, not a new kernel. It also covers the FP64 decode the Triton kernel cannot store (pathological BF16 scales).
  • Follow-up (out of scope): making quant_matmul/QuantLinearFunction itself range-safe would help direct low-level callers, but the module forward is the contract this issue reports.

TritonV2Linear.forward sent every eval forward through
QuantLinearFunction, which asks the Triton dequant kernel to store the
decoded weight in the activation dtype. A checkpoint whose per-group
scale makes (code - zero) * scale exceed that dtype's finite range
produced an inf weight, and 0 * inf in the GEMM produced NaN, while
BACKEND.TORCH returned the correct value.

Gate the whole-weight Triton decode on the eager-precomputed
_decoded_weight_fits answer, the same range check #5606 added to the
fused GEMV/GEMM kernels and the Torch wide branch. An overflowing
decode now reuses TorchLinear's wide decode and linear_wide_bias
reduction; a fitting decode keeps the native Triton path unchanged.
The check sits after the fused dispatch, so the hot fused path keeps
its single existing range check. Weights stay packed and AUTO still
selects TritonV2Linear.

Add the FP16/BF16 overflow fixture test against an FP64 oracle, the
packed-storage assertion, and the mechanism flow in
docs/triton-dispatch.md.
@Qubitium
Qubitium marked this pull request as ready for review November 20, 2026 13:57
@Qubitium
Qubitium merged commit 8416a08 into main Nov 20, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/5738-tritonv2-range-safe-decode branch November 20, 2026 14:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant