Repository navigation
perf(triton): fuse TritonV2 dispatch and pack-load small-M GEMM (#3982) - #3999
Merged
Merged
Conversation
This was referenced Nov 14, 2026
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.
Fixes #3982
TL;DR
Two changes, both measured on an RTX 4090 (SM 8.9), cold-L2, FP16/BF16, 4-bit gs=128:
TritonV2Linear.forwardnow reusesTorchLinear._forward_eagerwhen the existing fused GEMV / small-M GEMM gates allow. Decode and small-batch forward are 1.7x-5.4x faster and no longer allocate the denseK x Nweight._gptq_gemm_small_m_kernelloads each packed word once and unpacks it in registers, uses one scale/zero row per K-tile for identityg_idx, and optionally splits K deterministically. The Ada gate widens toM <= 64with no K/N caps; Blackwell gets a launch entry. The fused GEMM is 1.7x-6.8x faster than "best today" across 2048^2 .. 14336x4096.Split-K off is bitwise identical to the pre-rewrite kernel (
torch.equal, bits 2/4/8, gs128, act-order, FP16/BF16, CUDA and ROCm). Split-K on has lower FP32 drift than the single-pass kernel against an FP64 Torch oracle.Root cause
1. TritonV2 bypassed the fused dispatch
gptqmodel/nn_modules/qlinear/tritonv2.py:149-175(main) overrodeTorchLinear.forwardand always calledQuantLinearFunction.apply. That path dequantizes the wholeK x Nweight to the activation dtype and then runsmatmul_fp32_reduce; it never reaches_forward_eager, so the_can_use_rocm_gemv/_can_use_rocm_gemmdispatch (torch.py:534,:573) thatBACKEND.TORCHuses on Ada/ROCm was skipped.BACKEND.GPTQ_TRITONdecode was slower thanBACKEND.TORCHon the same GPU.2. The small-M kernel refetched packed data and gathered per element
gptqmodel/nn_modules/triton_utils/gptq_gemm.py:215-275(main) loadedqweightwith[block_k, block_n]addresses, so every int32 word was fetched32 / bitstimes (8x for 4-bit). It also loadedg_idx,scalesandqzerosper element even when the group was constant across the K tile. The tile was(16, 16, 256)with no split-K, so the Ada gate had to stop atM <= 16, K <= 2048, N <= 2048(gptq_gemm.py:144-146,arch.py:70-71); every 7B+ projection above 2048 fell back to writing and re-reading a 33-117 MB dense FP16 weight. Blackwell had no entry.Fix
TritonV2Linear.forward: reshape to 2-D, ask_can_use_rocm_gemv/_can_use_rocm_gemm, then callTorchLinear._forward_eager. Training, autograd, 3-bit, v1 qzeros, non-contiguous input and gate misses keep the existingQuantLinearFunctionpath._gptq_gemm_small_m_kernel: one[BLOCK_K / pack_factor, BLOCK_N]word load per K tile, register unpack +tl.reshapeto[BLOCK_K, BLOCK_N]; one[BLOCK_N]scale/zero row per K tile wheng_idxis the identity andgroup_size >= BLOCK_K; theg_idxgather is kept for act-order. Thetl.dotand FP32 accumulator are unchanged.[S, M, N]partials,.sum(0), no atomics.Sis the largest of{2, 4, 8}withK % (128*S) == 0andK/S >= 512, capped at 4 forM > 32. Auto-split is enabled only for the retiled CUDA entries; ROCm keeps its measured single-pass launch.(BLOCK_M, BLOCK_N)=(16, 32),(32, 64),(64, 64)forM <= 16,<= 32,<= 64.BLOCK_K=128, 4 warps, 2 stages.(small_m_max, max_in_features) = (64, 0)(0 = no cap) and no output cap; newblackwellentry with the same kernel.M == 1stays ongptq_gemv.TorchLinear.post_initcaches_gptq_gemm_uniform_grouponce, so the forward path does nog_idxcomparison.docs/triton-dispatch.md, linked fromREADME.md.Logic flow
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"| H["QuantLinearFunction.apply<br/>dequantize + matmul"] G --> I["adapter then out.to(x.dtype)"] H --> Iflowchart LR A["qweight words<br/>K/pack_factor x N"] --> B["load once<br/>BLOCK_K/pack_factor x BLOCK_N"] B --> C["shift and mask<br/>reshape to BLOCK_K x BLOCK_N"] D{"uniform_group and<br/>group_size >= BLOCK_K"} -->|"yes"| E["one scales row<br/>one qzeros row per K-tile"] D -->|"no"| F["g_idx gather<br/>per-row scales and zeros"] C --> G["weights = (code - zero) * scale<br/>cast to activation dtype"] E --> G F --> G G --> H["tl.dot with FP32 accumulator"] H --> I{"split_k > 1"} I -->|"yes"| J["FP32 partials S x M x N<br/>then sum over dim 0"] I -->|"no"| K["store in the output dtype"]flowchart TD A["forward gate"] --> B{"architecture has a launch entry"} B -->|"no"| X["dequantize + matmul"]:::fallback B -->|"yes"| C{"2 <= M <= small_m_max<br/>and dtype in fused set"} C -->|"no"| X C -->|"yes"| D{"bits in 2/4/8, int32 pack<br/>v2 qzeros, contiguous, no grad"} D -->|"no"| X D -->|"yes"| E["fused GEMV or small-M GEMM"] classDef fallback fill:#ffdddd,stroke:#cc0000,color:#660000Accuracy contract
gptq_gemm, for FP32 and the visible output, for bits 2/4/8, gs128, act-order on/off, FP16 and BF16. Proven withtorch.equalagainst a frozen copy of the old kernel (tests/kernels/_gptq_gemm_pre_rewrite.py):test_ada_split_off_is_bitwise_equal_to_pre_rewrite_kernel(24 cases).test_fused_gemm_split_off_is_bitwise_equal_to_pre_rewriteon gfx1201 (24 cases); a gfx1201 probe with the same tile also returned bitwise equality for all 24.1e-3 + 1e-5 * max|ref|. On K=14336, M=16, FP16: single-pass drift1.08e-3, split-K1.23e-4; M=64:1.39e-3vs2.81e-4. The test asserts both the bound andsplit_drift <= single_drift + 1e-6.M == 1never enters the small-M GEMM;gptq_gemvkeeps it.Fail-before / pass-after
Base
origin/main(105602e2b) with the new tests applied:Behaviour probe on the same base:
After (this branch):
Validation
CUDA RTX 4090 (
GPU-1ece7ec2-...):ROCm R9700 (
HIP_VISIBLE_DEVICES=1):The 92 skips are gfx950-only cases; the gfx1201 cases run and the new bitwise test passes.
tests/kernels/test_gptq.pycannot be collected on this host because the optionalparameterizedpackage is not installed (pre-existing, unrelated).Lint:
Benchmarks
tests/benchmark/benchmark_gptq_triton_small_m.py, RTX 4090, FP16, 4-bit gs=128,triton.testing.do_bench(L2 flushed before every timed iteration), medians in us.TritonV2Linear.forward(oldQuantLinearFunctionpath -> new dispatch):gptq_gemm("best today" = pre-rewrite kernel inside its old gate, otherwise dequantize + matmul):BF16 measured the same 1.4x-6.9x band (for example 4096x14336 M=2: 322.6 -> 47.1 us, 6.85x; M=64: 324.6 -> 78.9 us, 4.12x).
Reviewer notes
blackwellentry ships the same packed-load kernel and gate as Ada. This host has no Blackwell GPU, and the RTX 5090 on it was off-limits, so the entry follows the issue's indicative sm_120 report (1.3x-5.9x). No sm_120 timing was taken here.auto_splitis off forcdna4/rdna4, so their launch is unchanged except for the packed-load rewrite (which is bitwise equal). The ROCm gfx1201 suite and the new bitwise test pass.gptq_gemvowns that band.scripts/benchmark_ada_kernels.pynow passesuniform_groupand reports the M-banded tile.