Repository navigation
fix(torch): keep a stored wide bias through the GPTQ fallback epilogue - #3786
Merged
Merged
Conversation
TorchLinear._forward_eager narrowed the stored bias to the activation dtype
before matmul_fp32_reduce, so an FP32 bias lost the value needed to cancel the
reduction. With K=256, N=128, an all-ones weight, activations of 256 and an
FP32 bias of -65535, the correct result is 1:
dtype rows before after
FP16 1 1 1
FP16 2 -inf 1
FP16 65 -inf 1
BF16 1 1 1
BF16 2 0 1
BF16 65 0 1
The row count only selected a branch: the fused GEMV/GEMM epilogues already add
the bias in FP32, while the dequantize + matmul fallback rounded the bias first.
The streaming path had the same narrowing cast in front of its per-tile addmm.
- torch.py eager fallback: move the bias to the activation device only, and let
matmul_fp32_reduce keep a wide bias out of the low-precision epilogue.
- torch.py streaming: a wide bias now takes the per-tile FP32 epilogue instead
of addmm on the output dtype; other biases keep the previous cast.
- _numerics.matmul_fp32_reduce: route a bias flagged by linear_needs_wide_bias
to linear_wide_bias, so HIP, CPU and an active autocast context do not round
the product before the bias cancels it.
- tests/kernels/test_torch_wide_bias_fallback.py pins M=1/2/16/17/65 for FP16
and BF16 across the eager, streaming and fused branches against an FP64
oracle, covers autocast and gradients, and checks that no dequantized weight
outlives the call.
- docs/low-precision-bias-epilogue.md documents the fallback flow.
Measured on one NVIDIA and one AMD device: before the change 28 of the 42 new
cases fail on the NVIDIA device and all 42 fail on the AMD device; all 42 pass
after it on both. On M=17 decode of a 4096x4096 layer an FP16 bias costs
0.026 ms and an FP32 bias 0.024 ms on the NVIDIA device, 0.046 ms and 0.051 ms
on the AMD device.
Collaborator
|
Reviewed: the fallback keeps FP32 bias through the sum. The targeted GPU tests pass (78), Ruff and CI are green, and I found no blocking issue. |
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.
TL;DR
TorchLinear._forward_eagercast the stored bias to the activation dtypebefore the reduction. An FP32 bias that must cancel the partial sum was
therefore rounded (or overflowed) first, and the result depended on which
branch the batch size selected. The fallback now keeps that bias at its stored
width, and
matmul_fp32_reduceadds it after a wide reduction and rounds once.Net diff:
+36/-9intorch.py,+12/-2in_numerics.py, one new testmodule, one docs section.
1. The problem
nn_modules/qlinear/torch.py:446onmain:Reproduction (
K=256,N=128, weight all1, activation256, FP32 bias-65535, oracle1), measured on both an NVIDIA and an AMD device:The row count never changed the arithmetic; it changed the branch. The fused
GEMV/GEMM epilogues already keep the bias wide (
_fused_bias_out_dtype), whilethe dequantize + matmul fallback narrowed it. On the AMD device the fused
kernels did not engage for this fixture, so every row count failed. The
streaming path had the same cast in front of its per-tile
torch.addmm.No open PR fixes this. The bias-wide series (
#3649,#3700,#3753,#3649-era helpers) covered CUDA/HIP fused and FP4/VPTQ/FP8/GGUF paths, and#3743/#3747covered CPU; the portable GPTQ fallback kept its narrowing castthrough all of them.
2. The new flow
flowchart TD A["TorchLinear.forward"] --> B{"_should_use_streaming?"} B -- "Yes" --> C["_forward_streaming"] B -- "No" --> D["_forward_eager"] D --> E{"_can_use_rocm_gemv or _can_use_rocm_gemm?"} E -- "Yes" --> F["fused kernel, _apply_fused_bias with wide=True"] E -- "No" --> G["dequantize_weight + matmul_fp32_reduce"] G --> H{"linear_needs_wide_bias(x, bias)?"} H -- "Yes" --> I["linear_wide_bias: FP32 sum, one rounding"] H -- "No" --> J["narrow epilogue, bias cast to activation dtype"] C --> K{"wide_bias or _wants_fp32_reduce?"} K -- "Yes" --> L["per-tile FP32 epilogue"] K -- "No" --> M{"autocast active?"} M -- "No" --> N["torch.addmm with per-tile bias"] M -- "Yes" --> O["post-loop out.add_(bias)"] classDef fail fill:#fce8e6,stroke:#a33,color:#111 P["removed: bias cast to activation dtype before the reduction"] --> Q["-65535 becomes -inf in FP16"] class P,Q failmatmul_fp32_reducegains the same gate, so every caller that reaches it witha wide bias (Torch GPTQ eager/streaming, Torch AWQ, FP8 dense, GGUF dense,
Triton dequant, ParoQuant dense) is protected even though only GPTQ narrowed
the bias itself.
3. Failure and ambiguity contract
torch.addmmlinear_wide_biasdisables autocast for its GEMMlinear_wide_biasreshapes before the add4. Change inventory
gptqmodel/nn_modules/qlinear/torch.pygptqmodel/nn_modules/qlinear/torch.pygptqmodel/nn_modules/qlinear/_numerics.pylinear_wide_biastests/kernels/test_torch_wide_bias_fallback.pydocs/low-precision-bias-epilogue.md5. Tests / guarantees
test_torch_fallback_keeps_stored_bias_wide[1,2,16,17,65]test_torch_streaming_keeps_stored_bias_wide[1,2,65]test_torch_fused_and_fallback_boundary[1,2,16,17,65]M=16/17produces the same valuetest_matmul_fp32_reduce_keeps_stored_bias_widetest_matmul_fp32_reduce_keeps_wide_bias_under_autocasttest_matmul_fp32_reduce_wide_bias_gradientstest_matmul_fp32_reduce_representable_wide_bias_stays_accurate2e-3qweight/qzeros/scales/g_idx/biassnapshot6. Validation
The new file fails 28 of 42 cases on the NVIDIA device and all 42 on the AMD
device when reverted to
main; all pass with the change. Storage: the modulestate after two forwards contains no tensor outside the pre-forward storage
set, and the tracked dequantized tensor is released.
7. Reviewer notes
TorchLinear._forward_eager's CPU branch, the fused GEMV/GEMM epilogues, andthe AWQ/FP8/GGUF/ParoQuant forward paths were already correct and are
untouched.
linear_needs_wide_bias, i.e. dtype-based, matchinglinear_fp32_reduce. No per-value synchronizing check is added, so CUDAgraph capture and
torch.compileare unaffected.so no new FP32 weight copy appears for the common checkpoint.