Skip to content

fix(cuda): add GPTQ TritonV2 bias before rounding and repair backward arity - #3661

Merged
Qubitium merged 2 commits into
mainfrom
audit/cuda-tritonv2-bias-20261108
Nov 8, 2026
Merged

Qubitium merged 2 commits into
mainfrom
audit/cuda-tritonv2-bias-20261108

Conversation

@Qubitium

@Qubitium Qubitium commented Nov 8, 2026

Copy link
Copy Markdown
Collaborator

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.backward returned seven gradients for eight inputs and raised on an eval forward with a differentiable input. Bias now enters the wide matmul_fp32_reduce epilogue, 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

Path Previous code New code
Eval bias tritonv2.py:190-202 added bias after the low precision output Pass bias into QuantLinearFunction; matmul_fp32_reduce adds it before rounding
Direct backward dequant.py:375 returned seven gradients for eight inputs Return the exact number of slots supplied to the custom function
Bias backward Bias sat outside the custom function, but backward failed due the arity mismatch Reduce bias gradient in FP32 inside QuantLinearFunction.backward

New 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 failure
Loading

The 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.
  • Packed unit-weight FP16/BF16 1×128×128 and 2×3×128 biased outputs matched an independent FP64 Torch oracle after rounding. Old FP16 output: inf; fixed output: 65024. Biased and unbiased input gradients, bias gradients, and empty-row bias gradients passed.
  • Random packed 2×3×256 inputs with 128 outputs matched the independently built Torch oracle after rounding: maximum visible-output difference 0 for both dtypes; maximum input-gradient difference 0.001953125 FP16 and 0 BF16; bias-gradient difference 0.
  • Repeated forwards and backward retained 0 bytes of expanded weight in the module. The dequantized matrix is temporary.
  • 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_matmul calls. Peak is allocator delta including dequantization, output, and library scratch.

Shape (M×K×N) Old bias after rounding New bias before rounding Old peak New peak
1×1024×1024 0.1001 ms 0.1061 ms 34.00 MiB 3.01 MiB
128×1024×1024 0.1059 ms 0.1082 ms 34.50 MiB 3.50 MiB

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.

@Qubitium
Qubitium marked this pull request as ready for review November 8, 2026 18:54
@Qubitium
Qubitium merged commit 72f35ee into main Nov 8, 2026
6 checks passed
@Qubitium
Qubitium deleted the audit/cuda-tritonv2-bias-20261108 branch November 8, 2026 18:57
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.

1 participant