Repository navigation
fix(cuda): add GPTQ TritonV2 bias before rounding and repair backward arity - #3661
Merged
Merged
Conversation
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
Fix two reproduced GPTQ TritonV2 CUDA failures. Bias was added after the FP16/BF16 matmul output rounded, so a finite result could become
inf.QuantLinearFunction.backwardreturned seven gradients for eight inputs and raised on an eval forward with a differentiable input. Bias now enters the widematmul_fp32_reduceepilogue, and backward returns the requested input and bias gradients for both the eight-argument direct form and the nine-argument module form.Previous and current behavior
tritonv2.py:190-202added bias after the low precision outputQuantLinearFunction;matmul_fp32_reduceadds it before roundingdequant.py:375returned seven gradients for eight inputsQuantLinearFunction.backwardNew flow
flowchart TD P["TritonV2Linear.post_init"] --> Q{"g_idx in range?"} Q -- "No" --> X["Raise ValueError"] Q -- "Yes" --> A["TritonV2Linear.forward"] A --> B{"Training mode?"} B -- "Yes" --> C["TorchLinear.forward"] B -- "No" --> D["QuantLinearFunction.apply with optional bias"] D --> E["quant_matmul dequantizes temporary weight"] E --> F{"Bias present?"} F -- "Yes" --> G["matmul_fp32_reduce uses FP32-output addmm"] F -- "No" --> H["matmul_fp32_reduce uses FP32-output mm"] G --> I["Cast once to input dtype"] H --> I I --> J{"Backward called?"} J -- "No" --> K["Finish"] J -- "Yes" --> L["QuantLinearFunction.backward"] L --> M{"Input gradient requested?"} M -- "Yes" --> N["quant_matmul with transpose"] M -- "No" --> O{"Bias gradient requested?"} N --> O O -- "Yes" --> R["FP32 sum_to_size for bias"] O -- "No" --> S["Return matching gradient slots"] R --> S classDef failure fill:#fce8e6,stroke:#a33,color:#111 class X failureThe reference page is CUDA quantized linear epilogue, already linked from the README.
Validation
NVIDIA GB10 (SM 12.1), PyTorch 2.15.0+cu132, Triton 3.9.0:
PYTHON_GIL=1 pytest -q tests/kernels/test_tritonv2_bias_epilogue.py tests/kernels/test_triton_fp32_reduce.py tests/test_triton_g_idx_bounds.py tests/kernels/test_cuda_bias_epilogue.py: 89 passed.inf; fixed output:65024. Biased and unbiased input gradients, bias gradients, and empty-row bias gradients passed.0for both dtypes; maximum input-gradient difference0.001953125FP16 and0BF16; bias-gradient difference0.ruff check,git diff --check, and Mermaid fence validation passed.Resident FP16 timing: five warmups, median of seven sets of 30 CUDA-event timed direct
quant_matmulcalls. Peak is allocator delta including dequantization, output, and library scratch.The newly working 6×1024×1024 backward took 1.96–2.52 ms across two process runs, with a 34.03 MiB peak. There was no valid old backward to compare.