Skip to content

perf(marlin): make the small-grid tile override K-aware (#5281) - #5292

Merged
Qubitium merged 1 commit into
mainfrom
fix/5281-marlin-smallgrid-k
Nov 18, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/5281-marlin-smallgrid-k

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

Fixes #5281. The #5216 small-grid override in marlin_mm counted only output
tiles (m_tiles * n_tiles * 4 <= sms) and ignored K and the 256-thread wide
tile it replaced. On 128-SM Ada parts N=4096 sits exactly on that boundary
(M=128: 32 tiles x 4 == 128; M<=16 is far below it), so large-K down
projections (K=11008/14336) launched the 128-thread {128,64,128} tile even
though the wide tile already had 8 split-K blocks per output tile. Decode and
small-batch calls were 1.07-1.31x slower than before #5216.

The rule now requires the narrow tile to pay for its halved warp count: on
128-SM parts it must still split K over at least two threadblocks per output
tile, and a 16-row wide tile is kept for long K unless the narrow tile still
splits K at least four ways. The 170-SM RTX 5090 keeps #5216's measured
choice (the narrow tile was faster for every 16-row shape there).

What Changed

  • gptqmodel_ext/marlin/gptq_marlin.cu: factor the small-grid decision into
    marlin_prefers_narrow_small_grid(). Keep perf(marlin): use 128x64 tiles when the output grid is small #5216's tile-count gate, keep the
    perf(marlin): use 128x64 tiles when the output grid is small #5216 choice on parts with more than 128 SMs, and on <=128-SM parts require
    a >=2-way narrow K split (plus short K or a >=4-way split for 16-row wide
    tiles). No kernel arithmetic changes.
  • gptqmodel/utils/marlin.py, marlin_torch_{fp16,bf16}.cpp: optional
    thread_k, thread_n, num_threads arguments forward an explicit tile
    through the existing manual branch in marlin_mm. Defaults keep the
    historical 18-argument ABI (and the 20-argument scratch ABI); validation
    rejects a partial tile or num_threads without a pair.
  • tests/kernels/test_marlin_small_grid_tiles.py: selection-contract tests for
    the large-K wide cases, the narrow cases that still split K, and a
    numerics test that forces both tiles on identical operands.
  • tests/test_marlin_scratch_interface.py: CPU forwarding test for the new
    optional arguments.
  • docs/marlin-tile-schedule.md: updated selection flow and measured table.

Logic Flow Documentation

Before (main @ 2665486):

flowchart TD
    A["auto tile from the small/large table"] --> B{"m_tiles * n_tiles * 4 <= sms?"}
    B -- "no" --> C["wide table tile"]
    B -- "yes" --> D["narrow {128, 64, 128}"]
Loading

After:

flowchart TD
    A["auto tile from the small/large table"] --> B{"m_tiles * n_tiles * 4 <= sms?"}
    B -- "no" --> C["wide table tile"]
    B -- "yes" --> D{"sms > 128?"}
    D -- "yes (RTX 5090)" --> E["narrow {128, 64, 128}"]
    D -- "no (128-SM Ada)" --> F{"narrow splits K over >= 2 blocks per tile?"}
    F -- "no" --> C
    F -- "yes" --> G{"thread_m_blocks == 1 and prob_k > 4096 and narrow split < 4?"}
    G -- "yes" --> C
    G -- "no" --> E
Loading

m_tiles = ceil(prob_m_split / (16 * thread_m_blocks)); the wide tile is the
one determine_exec_config() picked before the override. The full-SM sm_80
tables and the Ada narrow FP32 AWQ schedule keep their own choice, as before.

Measurements

Method: GPTQ 4-bit g128 MarlinLinear fp16, CUDA-graph replay of 100 calls,
minimum of 7 interleaved rounds per variant in ONE process (the host is shared
with other tenants; variants were alternated so drift hits both). wide is
the pre-#5216 tile, narrow is the #5216 tile, fixed is the tile the new
rule selects. Timings in microseconds per call.

sm_89 (RTX 4090, 128 SMs):

K x N M wide narrow narrow/wide fixed
14336x4096 1 10.53 10.66 1.01x wide
14336x4096 4 11.00 12.85 1.17x wide
14336x4096 16 14.54 19.11 1.31x wide
14336x4096 32 26.06 26.19 1.00x narrow
14336x4096 64 45.68 41.05 0.90x narrow
14336x4096 128 61.39 76.85 1.25x wide
11008x4096 4 9.40 9.99 1.06x wide
11008x4096 16 12.09 14.21 1.18x wide
11008x4096 32 23.32 19.22 0.82x narrow
11008x4096 64 38.66 31.09 0.80x narrow
11008x4096 128 49.94 58.34 1.17x wide
8192x4096 16 10.09 10.79 1.07x wide
8192x4096 32 20.41 15.15 0.74x narrow
8192x4096 64 36.13 25.03 0.69x narrow
8192x4096 128 38.84 43.57 1.12x wide
4096x4096 1 5.98 4.98 0.83x narrow
4096x4096 16 7.36 6.72 0.91x narrow
4096x4096 32 16.34 8.90 0.54x narrow
4096x4096 64 29.43 14.41 0.49x narrow
4096x4096 128 24.70 24.21 0.98x wide
4096x1024 16 13.46 7.41 0.55x narrow
4096x1024 64 86.50 13.45 0.15x narrow
4096x1024 128 46.97 11.48 0.24x narrow
3584x512 64 61.40 17.80 0.29x narrow
8192x2048 16 10.03 7.74 0.77x narrow
14336x2048 128 45.38 38.89 0.86x narrow
28672x8192 32 135.60 129.38 0.95x wide
28672x8192 64 146.24 169.83 1.16x wide

The #5216 wins stay visible: 4096x1024 (0.15-0.55x), 3584x512 (0.29x),
4096x4096 at M<=64 (0.49-0.91x), 8192x2048 (0.77x). BF16 spot check on the
same part: 14336x4096 M=16 15.22 -> 19.78 us (1.30x, fixed wide), M=128
64.90 -> 77.61 us (1.20x, fixed wide), 4096x1024 M=64 92.00 -> 14.04 us
(0.15x, fixed narrow).

sm_120 (RTX 5090, 170 SMs): the new rule is a no-op there by design; every
measured shape keeps #5216's narrow tile. wide -> narrow:

K x N M wide narrow narrow/wide
14336x4096 1 11.42 10.73 0.94x
14336x4096 16 16.39 15.30 0.93x
14336x4096 32 34.05 25.44 0.75x
14336x4096 64 57.59 44.10 0.77x
14336x4096 128 79.41 79.66 1.00x
11008x4096 16 14.18 12.74 0.90x
11008x4096 32 30.00 20.85 0.69x
11008x4096 64 50.67 35.67 0.70x
8192x4096 16 12.08 10.58 0.88x
8192x4096 128 52.02 49.13 0.94x
4096x4096 16 9.41 7.37 0.78x
4096x4096 128 34.49 28.77 0.83x
4096x1024 64 72.61 16.58 0.23x
3584x512 64 63.62 22.44 0.35x
28672x8192 32 84.82 87.99 1.04x
28672x8192 64 145.16 152.94 1.05x

The last two rows are the pre-existing #5216 behaviour (narrow with no K
split); they are not changed by this PR and are called out as a follow-up.

Numerics

The change is selection-only. Marlin splits K across warps and threadblocks
and reduces the partials in a fixed order, so two different tiles cannot be
bitwise equal: changing thread_k/thread_n changes the FP32 reduction
tree. Forced-tile evidence on the measured shapes (GPTQ builder, both dtypes):

shape dtype max abs(wide-narrow) max output ulp max FP64 oracle error (both tiles)
14336x4096, M=16 fp16 0.03125 0.03125 0.0222
14336x4096, M=128 fp16 0.03125 0.03125 0.0222
4096x1024, M=64 fp16 0.015625 0.015625 0.0103
14336x4096, M=16 bf16 0.125 0.25 0.1699
14336x4096, M=128 bf16 0.25 0.25 0.1731
4096x1024, M=64 bf16 0.00390625 0.125 0.0848

Both tiles are bitwise deterministic across repeats, both have the same
maximum FP64-oracle error, and auto is bitwise identical to the forced tile
the rule selects (asserted in the new test). The literal "bitwise equality
between the two tiles" requested by the issue is not achievable for this
kernel; the 1-ulp agreement above is the strongest true property.

Tests

Red on pristine main (worktree at 2665486, new test copied in):

PYTHONPATH=/root/work/issue-5281-pristine HF_HUB_OFFLINE=1 \
CUDA_DEVICE_ORDER=PCI_BUS_ID CUDA_VISIBLE_DEVICES=0 \
python -m pytest tests/kernels/test_marlin_small_grid_tiles.py -q -ra -k large_k_keeps_wide
# 8 failed (expected (256,256,64)/(256,128,128), got (128,64,128)), 22 deselected

Control on the same pristine tree:

... -k large_k_keeps_narrow
# 5 passed, 25 deselected

Green after the fix (RTX 4090):

PYTHONPATH=/root/work/issue-5281 HF_HUB_OFFLINE=1 \
CUDA_DEVICE_ORDER=PCI_BUS_ID CUDA_VISIBLE_DEVICES=0 \
python -m pytest tests/kernels/test_marlin*.py -q -ra
# 327 passed

... python -m pytest tests/test_marlin*.py -q -ra -k 'not test_marlin_cuda_smoke_build_and_forward'
# 215 passed, 2 skipped (two CUDA devices required), 1 deselected

... python -m pytest tests/test_marlin_jit.py::test_marlin_cuda_smoke_build_and_forward -q
# 1 passed in isolation

test_marlin_jit.py::test_marlin_cuda_smoke_build_and_forward fails when run
after other marlin tests ("already loaded in this Python process"); the same
failure reproduces on pristine 2665486, so it is a pre-existing test
isolation issue, not this change.

Lint:

uvx [email protected] check gptqmodel docs/eora tests setup.py
# All checks passed!

Unverified

  • Ampere A100 (108 SMs), Hopper, and datacenter Blackwell: no hardware here.
    The sms > 128 split and the 4096 K cutoff are tuned on sm_89 (128 SMs) vs
    sm_120 (170 SMs) only.
  • The 4096 K cutoff and the >=2/>=4 narrow-split bounds are empirical; other
    K/N combinations around them were not exhaustively measured.
  • Timings are from a shared host (another tenant active on GPU0); absolute
    microseconds move a few percent between runs. All comparisons were
    interleaved in one process.
  • The 28672x8192 M=32/64 narrow-tile loss on sm_120 is pre-existing perf(marlin): use 128x64 tiles when the output grid is small #5216
    behaviour and is left unchanged; it needs its own retune.

#5216 switched to the narrow {thread_k=128, thread_n=64, threads=128} tile
whenever the auto-selected tile left fewer than four output tiles per SM. That
test counted output tiles only: it ignored K and the 256-thread tile it
replaced. On 128-SM Ada parts N=4096 sits exactly on the boundary, so every
large-K down projection took the narrow tile even though the wide tile
already had enough split-K work per SM (1.07-1.31x slower at M=4/16/128).

Keep #5216's tile-count gate, but on parts with at most 128 SMs require the
narrow tile to still split K over at least two threadblocks per output tile.
A 16-row wide tile is kept for long K unless the narrow tile still splits K
at least four ways. The 170-SM RTX 5090 keeps #5216's measured choice; the
narrow tile was faster there for every 16-row shape, including large K.

Also forward optional thread_k/thread_n/num_threads through the existing
manual branch so tests and tuning can compare both tiles on identical
operands; the defaults keep the 18-argument ABI (and the 20-argument scratch
ABI). Selection is unchanged numerically: both tiles are bitwise
deterministic, agree to one output ulp, and auto is bitwise identical to the
forced tile the rule selects.
@Qubitium
Qubitium marked this pull request as ready for review November 18, 2026 17:57
@Qubitium
Qubitium merged commit 313df25 into main Nov 18, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/5281-marlin-smallgrid-k branch November 18, 2026 23:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant