Repository navigation
fix(tritonv2): keep the range-safe decode in eval (#5738) - #5920
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
TritonV2Linear(BACKEND.GPTQ_TRITON) skipped the #5606 range-safe decode in eval.QuantLinearFunctionasks the Triton dequant kernel to store the decoded weight in the activation dtype, so a checkpoint whose per-group scale makes(code - zero) * scaleexceed that dtype's finite range produced aninfweight and0 * inf == NaNin the GEMM. The same checkpoint onBACKEND.TORCHreturns the correct value. This PR routes the overflowing decode through the existingTorchLinearwide 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-weightQuantLinearFunctiondecode on the inherited, eagerly precomputed_decoded_weight_fits(x.dtype). A decoded weight that does not fit the activation dtype now takessuper().forward(x)(TorchLinear'sdecode_dtype_for_scalesdecode +linear_wide_biasreduction). 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) throughTritonV2LinearandTorchLinear, 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 nativeQuantLinearFunctiondecode.docs/triton-dispatch.md: new "Range-safe decode" section with old/new Mermaid flows (failure path marked), before/afterfile:linetable, contract and validation command. Linked fromREADME.md.No API change, no new cache, no environment switch. Weights stay packed; AUTO still selects
TritonV2Linear.Tests
Related TritonV2/CUDA epilogue set (same env):
Broader kernel set (same env):
Red/green: with the code change reverted,
test_tritonv2_eval_decode_matches_the_range_safe_torch_backend[float16]fails withTritonV2 returned tensor([nan, nan, nan, nan]); BF16 passes both ways.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: FP64x @ decoded_weight.Measurement commands:
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_MAPentries, aliases, or module plans changed.AWQ onboarding safety
Not applicable: no AWQ grouping/search/replay/folding change.
Logic Flow Documentation
file:lineevidence (oldQuantLinearFunction-only flow).classDef failure/classDef fallback).docs/triton-dispatch.mdrepeats the diagrams, is titled for the mechanism, and is linked fromREADME.md.TritonV2Linear.forwarddispatch 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:#660000Before #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:#660000After #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:#660000Review Requirements
Kernel Accuracy Requirements
QuantLinearFunction.Notes
TorchLinearwide decode, not a new kernel. It also covers the FP64 decode the Triton kernel cannot store (pathological BF16 scales).quant_matmul/QuantLinearFunctionitself range-safe would help direct low-level callers, but the module forward is the contract this issue reports.