Repository navigation
fix(triton): keep the GPTQ reduction in FP32 and repair kernel test imports - #3526
Conversation
…mports The Triton dequant path computed `input @ dequantized_weight` in the activation dtype, so every output drifted by about one bf16 ulp relative to the Torch backend, which rounds once after an FP32 reduction. PR ModelCloud#3520 fixed the Torch, GGUF, ExLlamaV2 and Marlin epilogues but missed this path. - Route both quant_matmul branches through matmul_fp32_reduce so Triton and Torch agree bit-for-bit on CUDA. HIP already accumulates in FP32 for low-precision operands, so the ROCm result is unchanged. - tests/kernels/test_triton_fp32_reduce.py pins the FP32 epilogue against an independent FP64 oracle and asserts Triton/Torch bit equality; arithmetic error is reported separately from the visible low-precision output error. - test_inference_numeric_{fusion,range}.py referenced sibling helpers through bare module names that only resolve when tests/ is on sys.path, so `pytest tests/kernels/test_inference_numeric_fusion.py` (the exact CI form) died with ModuleNotFoundError and collected nothing. - test_torch_int8.py pre-rounded the channel scale to the activation dtype in its oracle. The kernel multiplies the canonical FP32 scale and rounds only the output, so the oracle disagreed with a correct kernel for every bf16 case; compare arithmetic in FP64 and keep the visible rounding separate.
Review of PR #3526 — measured on real hardwareReviewed head Verdict: the numerics fix is correct; the perf/memory cost and two test issues should be addressed before merge. Correctness — confirmed
Blocker: latency and peak-memory regression, and zero benefit on ROCm
Two facts that matter for the fix's scope:
Suggested resolution (either is fine):
Test issues
Minor
Verification run
No dense/dequantized weight cache is retained by this change — the FP32 reduction output is the same forward result tensor, freed with the normal output lifetime. |
Follow-up pushed:
|
| Claim | Reproduction |
|---|---|
| narrow == wide on gfx1201 | 400/400 configs bitwise identical (matmul + linear w/ bias, incl. the transpose=True backward branch), plus 96 shape/dtype/bit cases and 150 random matmuls — 0 differing elements |
| narrow is not worse there | 150 matmuls / 90.3M elements vs an FP64 oracle: narrow worse on 0, wide worse on 0 |
| backward was not bitwise-identical on ROCm | with the gate, transpose=True is bitwise identical to main — 0/48 cases differ |
| wide costs latency/memory | confirmed +4.1%…+39.5% latency and +144 MiB peak on gfx1201; both are now gone (peak delta 0.0 MiB on every shape) |
_numerics.py is shared by Torch/GGUF/AWQ/FP8/VPTQ, so I re-ran those consumers on both backends as you suggested: 77 passed / 7 skipped on CUDA, 72 passed / 12 skipped on ROCm. The gate does not change any current output.
CUDA is unchanged, so the test_gptq.py -k TRITON fix and the bit-for-bit Triton/Torch agreement still hold there.
I did not gate on x.shape[0] > 16. Your M ≤ 16 observation is correct on the shapes you measured, but it is not stable: on 3072x1024 the split reduction also fires at M=2 and M=8 (bf16 M=8: 3515/8192 differing elements) and not at M=64 or M=512. A row-count gate would silently drop the fix on decode shapes, so the commit comment records this.
Test issues — both fixed
- Unreachable
importorskip: confirmedcollected 0 items / 1 error. The import is now lazy; a Triton-less run gives 12 skipped. - Self-referential "arithmetic error" assertions: you were right that they compared Torch against itself. Both now assert the actual kernel constraint against an independent FP64 oracle — half an output-dtype ulp plus the FP32 accumulation slack,
8·eps32·(|x|@|W|). Evidence it bites: injecting the old narrow matmul fails 8 cases (e.g.M=32: 4091/16384 outputs exceed, max excess 3.8e-4). - Backward coverage: added
test_quant_matmul_backward_matches_fp64_oracle(3 row counts × 2 dtypes), sinceTritonV2Linear.SUPPORTS_TRAINING = Trueand that branch had none. - Visible-error check:
atol=2e-3alone rejected a correct kernel — a bf16 half-ulp at magnitude 2 is 7.8e-3. The visible check now allows the mandatory output rounding on top of the2e-3/2e-3limit, which is what caught this on ROCm.
On your minor points
- "17 passed / 8 skipped" — agreed, differs with BitBLAS availability; the message no longer quotes a single count as canonical.
- The
_dequantizestorage claim re-verified directly: no dense weight retained (1 MiB layer → only 8 KiB of scales), anddequant()temporaries are collected by the time the forward returns (weakref: 0 alive).
Verification on this head
RTX 4090 / torch 2.15.0+cu132 / triton 3.9.0; gfx1201 / torch 2.16.0.dev+rocm10.1.
test_triton_fp32_reduce.py+test_torch_int8.py: 51 passed (CUDA), 39 passed / 12 skipped (ROCm)test_gptq.py,test_inference_numeric_{range,fusion}.py,test_awq_triton_accum.py: 464 passed, 16 skippedtest_torch_int8.pyon main: 15 failed → 29 passedruff check gptqmodel testsclean; CIruff-checkgreen on this commit
One note on the PR itself: the head ref stayed pinned to 679c359e9 after pushing (the branch was already correct upstream), so I re-opened the PR to get GitHub to recompute it. It now points at 5f4b0bbc9; no force-push was involved.
…cles Follow-up to ModelCloud#3526 addressing review measurements. The wide `out_dtype=float32` request is only needed on CUDA, where cuBLAS starts accumulating fp16/bf16 GEMMs in the operand dtype once it splits the reduction. HIP already accumulates these operands in FP32, so the request only adds a cast and a transient FP32 output. Measured on gfx1201 (Radeon AI PRO R9700, HIP 7.16.26385): - narrow vs wide bitwise identical across 96 shape/dtype/bit cases and 150 random matmuls, and 400/400 matmul+linear configs including the `transpose=True` backward branch. - wide cost was +4.1% .. +39.5% latency and up to +144 MiB peak allocation; both now gone. CUDA behaviour is unchanged, so the ModelCloud#3526 numerics fix and its bit-for-bit Torch agreement still hold there. Test hardening: - `test_triton_fp32_reduce.py` imported `quant_matmul` at module scope, so `pytest.importorskip("triton")` was unreachable and a Triton-less environment got a collection error instead of a skip. The import is now lazy; verified `1 error` -> `12 skipped`. - Replace the self-referential "arithmetic error" assertions, which compared Torch against itself, with the real constraint the kernels must satisfy: half an output-dtype ulp plus the FP32 accumulation slack, measured against an independent FP64 oracle. Injecting the old narrow matmul fails 8 cases. - Add backward (`transpose=True`) coverage; `TritonV2Linear.SUPPORTS_TRAINING` is True and this branch had none. - Allow mandatory output rounding in the visible-error check: a bf16 half-ulp at magnitude 2 is 7.8e-3, which `atol=2e-3` rejected on a correct kernel. Verification (RTX 4090 / torch 2.15.0+cu132 / triton 3.9.0, and gfx1201 / torch 2.16.0.dev+rocm10.1): - `test_triton_fp32_reduce.py` + `test_torch_int8.py`: CUDA 45 passed, ROCm 39 passed / 12 skipped. - `test_gptq.py`, `test_inference_numeric_{range,fusion}.py`, `test_awq_triton_accum.py`: 464 passed, 16 skipped. - Torch/GGUF/AWQ/FP8 consumers of the shared helper: 77 passed / 7 skipped (CUDA), 72 passed / 12 skipped (ROCm). - `test_torch_int8.py` on main: 15 failed -> 29 passed. - `ruff check gptqmodel tests` clean. No weight cache is introduced: the dequantized tile is still consumed by the matmul and released (verified by weakref after forward), and a 1 MiB dense layer weight is not retained -- only 8 KiB of scales.
b67050f to
80438ac
Compare
|
Review of P1 — the ROCm bypass loses the FP32 bias epilogue for non-contiguous batched inputs. P2 — the new backward oracle accepts NaN outputs. Baseline verification: the existing four numerical suites pass 460 / 8 skipped on CUDA and 381 / 87 skipped on ROCm. These results do not cover the reproduced biased non-contiguous case. The Triton CUDA reduction fix and import repairs otherwise checked out. Fixed and pushed in
Final validation: ROCm latency and transient memory: physical GPU 3, gfx1201; non-contiguous batched inputs, resident operands, 10 warmups, median of five 30-call CUDA-event batches with synchronization. Shapes below are original
Peak allocator increments, including backend workspace, were respectively 80.25 / 80.75 / 81 MiB for the smaller shape and 84 / 92 / 96 MiB for the larger shape (prior PR / fixed / wide). Flattening may require a temporary activation copy, hence +0.5/+8 MiB versus the unsafe path; it still saves 0.25/4 MiB versus wide output. Random-input max error versus raw FP64 was 3.06e-5 (FP16) / 2.36e-4 (BF16); versus dtype-rounded FP64 it was 3.06e-5 / 2.45e-4, within Storage: the 128→64 VPTQ regression retained the same 1,280 bytes of packed/codebook/bias storage before and after forward, with zero additional retained bytes; its 16,384-byte temporary dense reconstruction was released after each call. No dense or expanded weight cache was introduced. |
Problem
TritonV2Linearcomputedinput @ dequantized_weightin the activation dtype, while every other GPTQ backend rounds once after an FP32 reduction. PR #3520 fixed the Torch, GGUF, ExLlamaV2 and Marlin epilogues but missed this path.On
sliuau/Llama-3.2-3B_4bits_128group_size(model.layers.6.self_attn.v_proj, bf16) the dequantized weights are bit-identical between backends, yet the outputs differ by exactly one bf16 ulp (0.0078125) on 32 of 112 samples. Against an independent FP64 oracle:input @ W(old Triton)matmul_fp32_reduce(Torch)The narrow path is strictly worse on 3523 of 8192 elements, so Torch was right and Triton was wrong.
Fix
Both
quant_matmulbranches now route through the existingmatmul_fp32_reducehelper, so Triton and Torch agree bit-for-bit on CUDA. HIP already accumulates in FP32 for low-precision operands, so the ROCm result is unchanged (verified on gfx1201).Test-collection bugs fixed alongside
These made the CI command
pytest tests/kernels/<file>.pycollect nothing:test_inference_numeric_{fusion,range}.pyimported sibling helpers by bare module name. Those only resolve viatests/conftest.py'ssys.pathinjection, which does not covertests/kernels/. The fusion file therefore died withModuleNotFoundErrorand collected 0 tests.test_inference_numeric_range.pypointed attest_exllamav3_cpu, which lives intests/, nottests/kernels/— all 4test_exl3_transform_rangecases failed.Oracle bug in
test_torch_int8.pyThe oracle pre-rounded the channel scale to the activation dtype:
but
Int8PackedModule.forwardpassesint8_channel_scale.float()and rounds only the final output. The kernel was correct; the test was not. Every bf16 case failed (15 failed → 29 passed after the fix). The oracle now computes in FP64 and reports arithmetic error separately from visible low-precision output error, per the repo's numerics guidance.Verification
tests/kernels/test_gptq.py::TestKernelOutput— 16 passed, 8 skipped (was failing for both Triton dtypes)tests/kernels/test_triton_fp32_reduce.py— 10 passed; reverting the fix makes 2 fail, so the regression is pinnedtest_torch_int8.py29 passed,test_inference_numeric_range.py320 passed,test_inference_numeric_fusion.py89 passedruff check gptqmodel testscleanNo dense/dequantized weight cache is introduced or retained; the dequantized tile is consumed by the matmul and released.