Skip to content

[PERF] GPTQ Triton decode/small-batch: TritonV2 bypasses the fused GEMV (4-5x at M=1); small-M GEMM gate excludes all 7B+ shapes (2.4-5.8x at M=2..64) #3982

Closed

Description

@Qubitium

Perf audit of main @ 93d3ae7 on RTX 4090 (sm_89), fp16/bf16, 4-bit gs=128 unless noted. Timings are cold-L2 GPU time (triton.testing.do_bench or CUDA events with an L2 flush). Neither change trades accuracy for speed; the evidence is listed under each item.

1. TritonV2Linear.forward never uses the fused GEMV/GEMM its parent already dispatches to

gptqmodel/nn_modules/qlinear/tritonv2.py:149-175 overrides TorchLinear.forward and always calls QuantLinearFunction.apply. That path dequantizes the full K×N weight to fp16, then runs matmul_fp32_reduce. It never reaches TorchLinear._forward_eager, so the _can_use_rocm_gemv / _can_use_rocm_gemm dispatch (torch.py:534, :573) that BACKEND.TORCH already uses on Ada and ROCm is skipped. BACKEND.GPTQ_TRITON decode is therefore slower than BACKEND.TORCH on the same hardware.

M=1, module forward today vs. the existing gptq_gemv (no kernel change):

K×N TritonV2 today gptq_gemv speedup
4096×4096 75–80 µs 19.5–21 µs 3.8×
4096×11008 200–205 µs 43–46 µs 4.5×
11008×4096 233 µs 47–49 µs 4.9×
4096×14336 267–270 µs 56–66 µs 4.0–4.8×
14336×4096, act-order, bias 324 µs 192 µs 1.7×

Two side effects of today's path:

  • Every call allocates a dense-weight temporary: transient peak goes from 120 MiB to 0.03 MiB at 4096×14336.
  • Host time per call drops from 43–125 µs to 32–46 µs with the reroute.

Accuracy: not bitwise. The gemv path has the same max-abs error against an fp64 oracle as today's TritonV2 output on every shape. The share of elements that miss round(fp64) by 1 ulp goes down (e.g. 0.0037 → 0.0007). This is already the tested contract for TorchLinear (tests/kernels/test_gptq_ada_gemv.py, TOL 2e-3, fp32 accumulate, bias fused before rounding).

Sketch:

def forward(self, x):
    if not self.training:
        x2 = x.reshape(-1, x.shape[-1])
        if self._can_use_rocm_gemv(x2) or self._can_use_rocm_gemm(x2):
            x2 = self._apply_rotation_to_input(x2)
            return TorchLinear._forward_eager(self, x2, x.shape[:-1] + (self.out_features,))
    ...existing QuantLinearFunction path (training / autograd / 3-bit / gate miss)...

The gates already reject training, requires_grad, 3-bit, non-v2 qzeros and dtype mismatch. One behaviour difference: the adapter gets the 2-D x, as it already does in TorchLinear. Related: #3661, which moved TritonV2's bias before rounding; this keeps that guarantee via _apply_fused_bias.

2. The fused small-M GPTQ GEMM refetches packed data per element, so its gate excludes every 7B+ shape

  • gptqmodel/nn_modules/triton_utils/gptq_gemm.py:215-275 (_gptq_gemm_small_m_kernel) loads qweight with [block_k, block_n] addresses, so each int32 word is fetched 8× for 4-bit. It also loads g_idx, scales and qzeros per element even when the group is constant over the K-tile (no act-order). The tile is BLOCK_N=16 with no split-K.
  • Because of that the kernel loses to dequant + cuBLAS above 2048, so the Ada table caps it at K ≤ 2048, N ≤ 2048, M ≤ 16 (gptq_gemm.py:144-146).
  • For every real 7B+ projection, M=2..64 therefore takes triton dequant + torch.mm(out_dtype=fp32). That writes and re-reads a dense fp16 weight of 33–117 MB per call, which exceeds the 72 MB L2 from 4096×11008 upward.
  • Blackwell has no entry, so it always takes the split path.

Prototype (packed-load rewrite of the same kernel):

  • Load packed words [BLOCK_K/pf, BLOCK_N] once and unpack in registers (words[:, None, :] >> shifts → tl.reshape).
  • Load scales/zeros as one [BLOCK_N] row per K-tile when GROUP_SIZE >= BLOCK_K (new constexpr, set when g_idx == arange // group_size, checked once in post_init). Keep the g_idx gather only for act-order.
  • Keep the exact same tl.dot(fp16/bf16, fp16/bf16, fp32 acc).
  • Optional deterministic split-K: fp32 partials [S, M, N] → .sum(0), no atomics. S = largest of {2, 4, 8} with K % (128·S) == 0 and K/S ≥ 512 (capped at 4 when M > 32).
  • Tiles: BK=128, BN=32 (64 when M > 16), BM=16/32/64, 4 warps, 2 stages.

4090, fp16, µs. "Best today" is gptq_gemm inside the gate, otherwise dequant+mm:

K×N M=2 M=8 M=16 M=64
4096×4096 58.5 → 17.0 (3.4×) 52.0 → 18.6 (2.8×) 67.5 → 20.0 (3.4×) 63.5 → 26.3 (2.4×)
4096×11008 215 → 37.9 (5.7×) – 220 → 42.9 (5.1×) 246 → 66.9 (3.7×)
11008×4096 195 → 52.8 (3.7×) – 200 → 54.5 (3.7×) 206 → 76.1 (2.7×)
4096×14336 269 → 46.1 (5.8×) 271 → 50.1 (5.4×) 273 → 53.7 (5.1×) 323 → 85.8 (3.8×)
14336×4096 261 → 49.9 (5.2×) – 266 → 55.9 (4.8×) 299 → 69.9 (4.3×)
2048×2048 21.9 → 10.5 (2.1×) 22.0 → 11.0 (2.0×) – –
  • bf16: same 1.9–5.7×.
  • Act-order (g_idx gather kept): 1.2–2.1×.
  • RTX 5090 (sm_120, shared/noisy card, indicative only): 1.3–5.9×, e.g. 4096×14336 M=8 266 → 45 µs.
  • The split-K scratch is S·M·N·4 B (≤ 15 MB) instead of a K·N·2 B dense weight (117 MB at 4096×14336).

Accuracy:

  • With split-K off, the rewrite is bitwise equal to today's gptq_gemm (fp32 and visible output) for bits 2/4/8, gs128 and act-order, fp16 and bf16.
  • With split-K, fp32 drift against the fp64 oracle is lower than today's kernel (K=14336, M=16: 2.7e-4 vs 9.5e-4). That is within the test_gptq_ada_gemm bound 1e-3 + 1e-5·max|ref|.
  • Visible-dtype max error equals today's split path on every shape.

Proposed: replace the body of _gptq_gemm_small_m_kernel as above, widen the Ada gate to M ≤ 64 with no K/N caps, and add a blackwell entry. Optionally route M=1 through it where it wins (1.1–1.25× over gptq_gemv on 4096×{4096, 11008, 14336}, but 0.90× on 11008×4096, so keep gemv there).

Combined with item 1, this would make GPTQ Triton decode, speculative decoding and small-batch serving on Ada/Blackwell 2.4–5.8× faster per linear.

🤖 Generated with Claude Code

Activity

  1. Qubitium commented on Nov 14, 2026

    @Qubitium
    CollaboratorAuthor

    Follow-up review of #3999 found a stale-flag correctness bug: after load_state_dict with a non-identity g_idx, M=2..64 output is wrong (rel err 0.59–0.70). See #4028 item 1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions