Repository navigation
Description
Found on main @ c1807f8, RTX 4090 (sm_89, 128 SMs) and RTX 5090 (sm_120, 170 SMs).
What is wrong
#5216 added a small-grid override to marlin_mm (gptqmodel_ext/marlin/gptq_marlin.cu:967-984). When the auto-selected tile leaves m_tiles * n_tiles * 4 <= sms output tiles, it switches to the narrow {thread_k=128, thread_n=64, threads=128} tile. The rule only counts output tiles. It ignores K and the 256-thread wide tile it replaces. On large-K projections the wide tile already spreads enough split-K work over the SMs, so swapping it for a 128-thread 128x64 tile makes the GEMM slower.
On sm_89 with 128 SMs, N=4096 sits exactly on the <= boundary (32 output tiles x 4 = 128), so the 128x64 tile is chosen for every decode/small-batch call of a Llama-3-8B down_proj (K=14336) or a Llama-2-7B down_proj (K=11008). These calls are 1.1-1.33x slower than on 280e2ff (the commit just before #5216).
Measurements
Method: graph-replayed calls (100 per graph, median of 7). Old = 280e2ff and new = main, each JIT-built from its own tree. Runs were interleaved old/new three times. The table shows the best of the three runs in µs; spread was below 3%.
sm_89 (RTX 4090), GPTQ 4-bit g128 MarlinLinear fp16 (AWQ Marlin and bf16 give the same ratios within 3%):
| K x N | M=1 | M=4 | M=16 | M=32 | M=64 | M=128 |
|---|---|---|---|---|---|---|
| 14336x4096 (Llama-3-8B down) | 11.4 -> 11.7 | 11.9 -> 13.3 (0.89x) | 15.0 -> 20.1 (0.75x) | 28.3 -> 28.6 | 49.6 -> 42.4 | 59.7 -> 73.6 (0.81x) |
| 11008x4096 (Llama-2-7B down) | 9.1 -> 8.5 | 9.2 -> 9.8 (0.94x) | 11.8 -> 13.9 (0.85x) | 22.6 -> 18.5 | 38.0 -> 29.7 | 48.5 -> 58.1 (0.84x) |
| 8192x4096 | 7.5 -> 7.0 | 7.8 -> 7.8 | 9.7 -> 10.8 (0.90x) | 19.8 -> 15.4 | 35.2 -> 23.9 | 37.5 -> 42.5 (0.88x) |
| 28672x8192 (Llama-70B down) | 127 -> 127 | 127 -> 127 | 129 -> 129 | 136 -> 130 | 148 -> 168 (0.88x) | 227 -> 225 |
| 4096x4096 (for contrast) | 6.4 -> 5.4 | 6.5 -> 5.7 | 8.0 -> 7.2 | 17.8 -> 10.0 | 31.6 -> 15.0 | 26.8 -> 23.4 |
sm_120 (RTX 5090): 28672x8192 at M=32/64 is 0.91-0.95x in every kind/dtype combination. 14336x4096 is not affected there, because 170 SMs push it onto the wide-tile path at M>=32.
The override also produces the large wins at small K, for example 4096x1024 at M=32-128, which is 4-6x faster. The regression is confined to large K.
Minimal repro
import torch
from gptqmodel.nn_modules.qlinear.marlin import MarlinLinear
from torch.profiler import profile, ProfilerActivity
torch.manual_seed(0)
for K, N in [(14336, 4096), (11008, 4096)]:
m = MarlinLinear(bits=4, group_size=128, desc_act=False, sym=True, in_features=K, out_features=N,
bias=False, pack_dtype=torch.int32, register_buffers=True, dtype=torch.float16)
with torch.no_grad():
m.qweight.copy_(torch.randint(-2**31, 2**31 - 1, m.qweight.shape, dtype=torch.int64).to(torch.int32))
m.scales.copy_(torch.rand_like(m.scales.float()).mul(0.01).half())
m.qzeros.fill_(0x77777777)
m = m.cuda(); m.post_init(); m.eval()
for M in (4, 16, 128):
x = torch.randn(M, K, device="cuda", dtype=torch.float16)
with torch.inference_mode():
for _ in range(5): m(x)
g = torch.cuda.CUDAGraph()
with torch.cuda.graph(g):
for _ in range(100): m(x)
ts = []
for _ in range(7):
s, e = torch.cuda.Event(True), torch.cuda.Event(True)
s.record(); g.replay(); e.record(); torch.cuda.synchronize(); ts.append(s.elapsed_time(e) * 10)
with profile(activities=[ProfilerActivity.CUDA]) as p:
m(x); torch.cuda.synchronize()
n = [e.name for e in p.events() if "Marlin<" in e.name][0]
tile = [t.strip() for t in n[n.find("Marlin<"):].split("(")[0].split(",")[3:7]]
print(f"K={K} N={N} M={M:3d}: {sorted(ts)[3]:6.2f} us/call (threads, m_blocks, n_blocks, k_blocks)={tile}")RTX 4090, main (c1807f8):
K=14336 N=4096 M= 4: 12.37 us/call (threads, m_blocks, n_blocks, k_blocks)=['128', '1', '4', '8']
K=14336 N=4096 M= 16: 18.96 us/call (threads, m_blocks, n_blocks, k_blocks)=['128', '1', '4', '8']
K=14336 N=4096 M=128: 72.63 us/call (threads, m_blocks, n_blocks, k_blocks)=['128', '4', '4', '8']
K=11008 N=4096 M= 4: 9.93 us/call (threads, m_blocks, n_blocks, k_blocks)=['128', '1', '4', '8']
K=11008 N=4096 M= 16: 14.60 us/call (threads, m_blocks, n_blocks, k_blocks)=['128', '1', '4', '8']
K=11008 N=4096 M=128: 57.35 us/call (threads, m_blocks, n_blocks, k_blocks)=['128', '4', '4', '8']
Same script on 280e2ff (before #5216):
K=14336 N=4096 M= 4: 11.88 us/call (threads, m_blocks, n_blocks, k_blocks)=['256', '1', '8', '8']
K=14336 N=4096 M= 16: 15.03 us/call (threads, m_blocks, n_blocks, k_blocks)=['256', '1', '8', '8']
K=14336 N=4096 M=128: 66.46 us/call (threads, m_blocks, n_blocks, k_blocks)=['256', '4', '16', '4']
K=11008 N=4096 M= 4: 9.10 us/call (threads, m_blocks, n_blocks, k_blocks)=['256', '1', '8', '8']
K=11008 N=4096 M= 16: 11.83 us/call (threads, m_blocks, n_blocks, k_blocks)=['256', '1', '8', '8']
K=11008 N=4096 M=128: 52.97 us/call (threads, m_blocks, n_blocks, k_blocks)=['256', '4', '16', '4']
The profiler shows the 128-thread 128x64 small-grid tile (n_blocks=4, k_blocks=8) replacing the 256-thread wide tile, and the extra time is all in that kernel.
Suggested fix
Make the override K-aware. For example, apply it only while the wide tile's K split per output tile stays small (k_tiles * wide_tiles / sms below some bound), or only when prob_k is at most about 8192. Alternatively, keep the wide tile whenever it already yields at least sms (tile x K-slice) work units. Then re-tune the threshold on sm_89 as well as sm_120. #5216 was measured only on an RTX 5090 (Llama-3-8B k/v at M=64). The current <= boundary puts every N=4096 projection on 128-SM Ada parts on the narrow tile.
Test that should have caught it
tests/kernels/test_marlin_small_grid_tiles.py pins the launched tile and checks numerics only. A perf guard over a few large-K decode shapes (14336x4096 and 11008x4096 at M=4/16/128), comparing the small-grid tile with the wide tile on the same device, would have shown the 1.2-1.33x loss.
Severity
Medium (PERF). There is no accuracy impact. Decode and small-batch latency of the MLP down projection, the largest GEMM per layer, rises 1.1-1.33x on 128-SM sm_89 cards for Llama-2/3-7/8B-class models at batch 4-16 and at M=128 (chunked prefill or batched decode). At M=32/64 the same shapes are faster, so the net effect depends on batch size.
🤖 Generated with Claude Code
Activity
Qubitium commentedon Nov 18, 2026
Agent claim:
agent-4e3a0639ecafis working on this issue now.Started 2026-11-18T17:26:42Z. The claim expires 4.0h after this comment is last edited (heartbeat). Other agents: do not start this issue while the claim is live.
Scope: fix the #5216 regression: the Marlin small-grid 128x64 tile override must account for K (and the 256-thread tile it replaces) so large-K projections keep the wide tile, with before/after timing on sm_89 and sm_120
Qubitium commentedon Nov 18, 2026
Agent claim released:
agent-4e3a0639ecaffinished this issue.PR: #5292 — base
main, headadd3b1321(branchfix/5281-marlin-smallgrid-k), 7 files, +348/−32.Fix
The #5216 override tested only
m_tiles * n_tiles * 4 <= sms, so on a 128-SM partN=4096sits exactly on the boundary and large-Kdown_projcalls launched the 128-thread{128,64,128}tile although the wide tile already had 8-way split-K work per SM. The rule is now K-aware (it accounts for the split-K work the wide tile already provides and the K-loop cost of the narrow tile), andgptq_marlin_gemmgained an explicitthread_k/thread_n/num_threadsoverride so tests can compare two tiles on the same operands. The default path keeps the existing ABI.Evidence (GPTQ 4-bit g128 fp16, CUDA-graph 100 calls, min of 7 interleaved rounds, one process)
sm_89 RTX 4090 (128 SMs) —
narrow/widebefore the fix, and the tile the new rule picks:sm_120 RTX 5090 (170 SMs): the new rule is a no-op — every measured shape keeps #5216's narrow tile (14336×4096 0.93–1.00x, 4096×1024 M=64 0.23x). BF16 on sm_89 shows the same 1.20–1.30x recoveries.
tests/kernels/test_marlin*.py327 passed;tests/test_marlin*.py215 passed, 2 skipped; newtests/kernels/test_marlin_small_grid_tiles.py30 passed (8 failed on pristinemainat the intended tile assertion); ruff clean.docs/marlin-tile-schedule.mddocuments the rule.Correction to the issue's ask: bitwise equality between the two tiles is not achievable — Marlin splits K across warps and threadblocks and reduces in a fixed order, so a tile change changes the FP32 reduction tree. Verified instead: both tiles are bitwise deterministic, have identical max FP64-oracle error, differ by at most 1 output ulp, and
autois bitwise identical to the forced selected tile.Not verified: Ampere/Hopper/datacenter-Blackwell (no hardware; the
sms > 128split, 4096-K cutoff and split bounds are empirical from sm_89 vs sm_120); timings come from a shared host; the 28672×8192 narrow-tile loss on sm_120 is pre-existing #5216 behaviour and left as a follow-up.perf(marlin): make the small-grid tile override K-aware (#5281) (#5292)
Merge origin/main into feat/anytorch-stable-abi
test(marlin): derive large-K tile expectation from SM count (#6672)