Skip to content

perf(marlin): use 128x64 tiles when the output grid is small - #5216

Merged
Qubitium merged 2 commits into
mainfrom
perf/marlin-small-grid-tiles
Nov 18, 2026
Merged

Qubitium merged 2 commits into
mainfrom
perf/marlin-small-grid-tiles

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

Port the small-grid thread-tile override from vLLM's Marlin planner
(vllm-project/vllm#24722, csrc/quantization/marlin/marlin.cu). When the
auto-selected tile leaves fewer than four output tiles per SM
(m_tiles * n_tiles * 4 <= sms), marlin_mm now launches the narrow
{thread_k=128, thread_n=64, threads=128} tile. The old planner kept the wide
tile. Most SMs then split K for the same few output tiles and queued on their
reduction locks: Llama-3-8B k_proj/v_proj at M=64 took 72.6 us on an
RTX 5090, and 16.9 us with this change.

Correctness evidence (summary):

Depends on #5188 (the #4913 scale-slot race fix). This branch carries that
commit (f66a4fc6c) and should be rebased once #5188 lands. vLLM #24722
contains the same guard.

Net diff: one 24-line hunk in gptq_marlin.cu, plus a test and a docs page.

What Changed

  • gptqmodel_ext/marlin/gptq_marlin.cu: in the auto-config branch of
    marlin_mm, after determine_exec_config, switch to {128, 64, 128} when
    ceil(prob_m_split / (16 * thread_m_blocks)) * (prob_n / thread_n) * 4 <= sms,
    is_valid_config accepts the narrow tile, and get_marlin_kernel has an
    instantiation for it. The kernel itself does not change.
  • GPT-QModel's own tuning is untouched. The full-SM sm_80 tables
    (marlin_prefers_full_sm80) and Ada's narrow FP32 AWQ schedule
    (narrow_splitk_arch / narrow_fp32_awq, including narrow_small_m_tiles)
    keep their tile; the override is skipped for them. Manual
    thread_k/thread_n, the force_exact_split retry and exec_config_t /
    blocks_per_sm are unchanged.
  • tests/kernels/test_marlin_small_grid_tiles.py (new): reads the launched
    kernel's template arguments from the profiler. It asserts that small-grid
    shapes launch {128, 64, 128} and that a large-grid shape does not, and
    checks every launch against an FP64 oracle (GPTQ and AWQ, FP16/BF16).
  • docs/marlin-tile-schedule.md (new, linked from docs/README.md):
    selection flow, before/after table and measurements.
  • No API change, and no sm_75-specific code (keeps clear of chore(marlin): drop the sm_75 (Turing) path; require sm_80+ #5200).

Out of scope: the rest of vllm-project/vllm#24722. The hybrid scheduling /
contiguous-K restructure was evaluated separately and is not part of this PR.
W4A8 and the FP4/FP8 8-bit scale layout are excluded.

Tests

  • Unit tests have been executed and passed.
  • I added a new simple/fast unit test for this change.

Red (baseline a706c646e + #5188, RTX 4090 sm_89, new test copied in):

CUDA_DEVICE_ORDER=PCI_BUS_ID CUDA_VISIBLE_DEVICES=0 TORCH_CUDA_ARCH_LIST=8.9 \
  python -m pytest -q tests/kernels/test_marlin_small_grid_tiles.py
# 8 failed, 3 passed
# E AssertionError: assert {(256, 128, 128)} == {(128, 64, 128)}   (M<=16)
# E AssertionError: assert {(256, 256, 64)} == {(128, 64, 128)}    (M=64)

Green, both cards (TORCH_CUDA_ARCH_LIST=8.9 on GPU 0 / RTX 4090, 12.0 on
GPU 2 / RTX 5090, CUDA_DEVICE_ORDER=PCI_BUS_ID):

python -m pytest -q -rs tests/kernels/test_marlin_small_grid_tiles.py \
  tests/kernels/test_marlin_scale_slot_race_4913.py tests/kernels/test_marlin_tile_determinism_4735.py \
  tests/kernels/test_awq_marlin_determinism_4683.py tests/kernels/test_marlin_bias_epilogue.py \
  tests/kernels/test_marlin_fp32_schedule.py tests/kernels/test_marlin_quant_dtype_boundaries.py \
  tests/kernels/test_marlin_strided_activation_4563.py tests/kernels/test_awq_marlin_stream_4683.py \
  tests/kernels/test_awq_machete_marlin.py
# sm_89 : 235 passed, 2 skipped   (first combined session incl. test_marlin_jit: 299 passed, 1 failed =
#          test_marlin_cuda_smoke_build_and_forward, which needs a fresh process; it passes on its own below)
# sm_120: 235 passed, 2 skipped
python -m pytest -q tests/test_marlin_jit.py      # separate process (its smoke test builds the extension)
# sm_89 : 65 passed;  sm_120: 65 passed
/root/venv/bin/ruff check gptqmodel docs/eora tests setup.py   # All checks passed!

The #4913 time-slicing soak (test_group_scales_survive_gpu_time_slicing,
gptq-fp16 and awq-zp-bf16, 10,000 launches each against a GPU tenant) passed on
both cards. The source-parsing tests in test_marlin_tile_determinism_4735.py
and test_marlin_fp32_schedule.py pass unchanged; this change does not move
any planner constant they pin.

Skips (both cards): test_awq_machete_marlin.py:290 (Machete is SM90-only) and
:308 (Swordfish needs sm100/103/110). No skip touches the changed path.

Not verified: performance on sm_89/sm_80/sm_86/sm_90. GPU 0 (4090) is shared
with outside load, so it was used for correctness only. No other idle card was
available.

Model Support / Module Plan

Not applicable: no model definitions change.

Logic Flow Documentation

  • The required diagrams and docs page are included below.

The change adds one decision to the tile planner. Diagram (also in
docs/marlin-tile-schedule.md):

flowchart TD
    A["marlin_mm: next M split"] --> B{"narrow_small_m_tiles?"}
    B -- "yes" --> B1["{128, 128, 256} (unchanged)"]
    B -- "no" --> C{"manual thread_k/thread_n?"}
    C -- "yes" --> C1["manual tile (unchanged)"]
    C -- "no" --> D["determine_exec_config (table tile)"]
    D --> E{"tile found, !full_sm80, !narrow_fp32_awq?"}
    E -- "no" --> F["table tile"]
    E -- "yes" --> G{"m_tiles * n_tiles * 4 <= sms?"}
    G -- "no" --> F
    G -- "yes" --> H{"{128,64,128} valid + instantiated?"}
    H -- "no" --> F
    H -- "yes" --> I["{128, 64, 128}"]
    D -- "no valid tile" --> X["retry with fewer thread_m_blocks (unchanged)"]
    classDef new fill:#e6f4ea,stroke:#137333,color:#111
    classDef fail fill:#fce8e6,stroke:#c5221f,color:#111
    class G,H,I new
    class X fail
Loading
Situation Before (gptq_marlin.cu, auto branch: thread_tfg = exec_cfg.tb_cfg) After
auto tile, m_tiles*n_tiles*4 <= sms table tile {128, 64, 128}
narrow tile invalid / not instantiated table tile table tile
full-SM sm_80, sm_89 narrow FP32 AWQ own tile unchanged
manual tile manual unchanged

Review Requirements

  • Reviewed this PR.
  • Checked that the code matches existing project structure, APIs, and conventions.
  • Avoided unnecessary monkeypatching and used the project's normal extension points where possible.
  • Minimized and compacted the impacted code surface.
  • Considered and eliminated potential regressions.

Kernel Accuracy Requirements

  • Accuracy drift was measured against an independent Torch FP64 oracle.
  • Not a fusion change.
  • Skips are listed above.

FP64 oracle sweep: GPTQ sym g128 (desc_act False and True) and AWQ zp g128,
FP16 and BF16, with and without bias, M in {1, 7, 16, 64, 128, 512, 2048}.
Shapes: Llama-3-8B (4096x6144, 4096x4096, 4096x1024, 4096x28672, 14336x4096)
and Qwen2.5-7B (3584x3584, 3584x512, 18944x3584). That is 672 cases per card.

card cases overall max abs err (base / change) mean of per-case max err cases with higher max err
RTX 4090 sm_89 672 0.2983 / 0.2983 0.05693 / 0.05693 2: 0.00815→0.00958 at abs ref max 19.7, 0.0189→0.0201 at abs ref max 49.3 (both under half an FP16 ulp)
RTX 5090 sm_120 672 0.2794 / 0.2794 0.05651 / 0.05651 1: 0.02069→0.02173 at abs ref max 65.6 (under half an FP16 ulp)

All outputs are finite, and every case is bitwise identical across repeated
launches. Storage is unchanged: no new buffers or caches, and the narrow tile
uses less shared memory than the wide one.

Performance

RTX 5090 (sm_120, 170 SMs), GPU 2, CUDA_DEVICE_ORDER=PCI_BUS_ID. Both
sides use the same harness: the raw apply_{gptq,awq}_marlin_linear call is
captured as a CUDA graph of 20 launches; 10 warmup replays, then the median of
60 replays timed with CUDA events. Baseline and change ran from separate
worktrees with separate JIT build roots. Each cell is the GPTQ-FP16 per-call
time in microseconds (before → after), then the range of the change over
{GPTQ, AWQ} × {FP16, BF16}:

shape (K x N) M=1 M=7 M=16 M=64 M=128 M=512 M=2048
llama_qkv (4096x6144) 7.3→7.3 (-0..+0%) 7.3→7.4 (+0..+1%) 9.8→9.9 (-0..+1%) 33.1→23.8 (-29..-26%) 40.4→40.5 (-0..+0%) 120.4→120.5 (-0..+0%) 458.8→461.0 (+0..+0%)
llama_o (4096x4096) 7.4→5.9 (-20..-19%) 7.5→6.0 (-19..-18%) 9.7→7.8 (-21..-19%) 31.4→19.1 (-40..-39%) 34.7→29.2 (-17..-14%) 82.3→82.2 (-1..-0%) 302.2→302.3 (-0..+0%)
llama_kv (4096x1024) 13.9→7.7 (-46..-44%) 13.9→7.8 (-45..-44%) 17.2→8.9 (-49..-46%) 72.6→16.9 (-77..-76%) 42.0→16.4 (-61..-61%) 34.5→29.0 (-17..-14%) 82.5→82.6 (-1..+0%)
llama_gateup (4096x28672) 15.7→15.6 (-0..+0%) 16.3→16.3 (-1..+0%) 24.3→24.3 (-0..+0%) 73.1→73.5 (-1..+1%) 138.3→138.4 (+0..+0%) 525.2→525.2 (-0..+0%) 2102.7→2105.4 (+0..+0%)
llama_down (14336x4096) 11.8→11.0 (-8..-7%) 12.1→11.2 (-8..-7%) 16.9→15.7 (-8..-7%) 58.6→44.6 (-24..-23%) 79.8→81.4 (+0..+3%) 260.9→261.0 (-0..+0%) 1033.1→1035.1 (+0..+0%)
qwen_q (3584x3584) 7.6→5.3 (-31..-29%) 7.7→5.5 (-30..-28%) 9.8→6.9 (-31..-28%) 32.6→16.4 (-50..-49%) 31.0→24.0 (-24..-21%) 64.7→64.3 (-1..+0%) 233.8→233.8 (+0..+0%)
qwen_kv (3584x512) 21.9→11.1 (-49..-48%) 22.0→11.2 (-49..-48%) 27.1→12.6 (-54..-48%) 63.4→22.4 (-65..-63%) 63.6→18.7 (-71..-70%) 32.5→17.3 (-47..-46%) 42.5→42.6 (+0..+0%)
qwen_down (18944x3584) 12.9→12.3 (-7..-5%) 13.3→12.5 (-7..-6%) 18.6→17.4 (-7..-6%) 67.0→49.9 (-25..-24%) 90.1→91.3 (+1..+4%) 299.5→299.5 (-0..+0%) 1233.2→1237.5 (+0..+1%)

Over the 224 cases: 124 improved by more than 2%, 96 are within ±2%, and 4 regressed by more than 2% (listed below).

Unverified outliers. Down projections at M=128: Llama 14336x4096 measured
+0..+3% and Qwen 18944x3584 +1..+4%:

  ('qwen_down', 'gptq', 'bfloat16', 128): 89.71 -> 93.18 us (+3.9%)
  ('llama_down', 'gptq', 'bfloat16', 128): 79.71 -> 81.71 us (+2.5%)
  ('llama_down', 'gptq', 'float16', 128): 79.78 -> 81.42 us (+2.1%)
  ('qwen_down', 'awq', 'float16', 128): 91.63 -> 93.47 us (+2.0%)

These runs (10:20 to 10:35 UTC+0) finished before I observed any other
process on GPU 2. However, the harness did not yet check for other processes,
and from about 10:45 GPU 2 was continuously shared with other sessions' jobs.
The interleaved re-runs of these cells never got an idle window, so the
outliers are unverified (GPU contended). sm_89/sm_80/sm_86/sm_90
performance is unmeasured.

Commands (per side, <wt> = baseline or change worktree):

export PATH=/usr/local/cuda-13.4/bin:$PATH CUDA_HOME=/usr/local/cuda-13.4 CUDA_DEVICE_ORDER=PCI_BUS_ID \
  CUDA_VISIBLE_DEVICES=2 TORCH_NO_COMPILER_WRAPPER=1 MAX_JOBS=64 TORCH_CUDA_ARCH_LIST=12.0 \
  GPTQMODEL_MARLIN_FP16_BUILD_ROOT=<own>/fp16 GPTQMODEL_MARLIN_BF16_BUILD_ROOT=<own>/bf16 PYTHONPATH=<wt>
python harness.py bench --out <side>.json      # accuracy: python harness.py acc --out <side>.json

The harness is a local script, not part of this PR. It builds modules through
MarlinLinear/AwqMarlinLinear.post_init() and times the raw apply call; the
accuracy mode compares module(x) with x.double() @ W.double() + bias.

Notes

🤖 Generated with Claude Code

Qubitium and others added 2 commits November 18, 2026 10:06
#4913)

For group_blocks > thread_k_blocks (g128 with thread_k = 64) a quantization
group spans two pipeline stages. fetch_to_shared() copies the group's scales
and zero points only into the slot of the group's first stage. The second stage
re-read that slot (`2 * (pipe / 2)`) after the last wait_for_stage() barrier
that protects it. In the same window, faster warps refill that slot with stage
pipe + stages, which belongs to group g + 2.

cp.async latency normally hides the race. When the GPU time-slices to another
process, in-flight copies drain before the context is saved, so a lagging warp
applies group g+2's scales. One k-warp-group then gets a whole 64-row output
block wrong, with no error raised. This affects GPTQ and AWQ in fp16 and bf16
on sm_89 and sm_120.

Port vLLM's guard from vllm-project/vllm#24722 (vLLM >= 0.12.0): only the
group's first stage reads shared memory for scales and integer or float zero
points; the later stages keep frag_s / frag_qzp / frag_zpf.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Port the small-grid tile override from vLLM's Marlin planner
(vllm-project/vllm#24722). When the auto-selected thread tile leaves fewer
than four output tiles per SM (m_tiles * n_tiles * 4 <= sms), launch the
narrow {thread_k=128, thread_n=64, threads=128} tile instead. The wide tile
made most SMs split K for the same few output tiles and then queue on their
reduction locks. Example: Llama-3-8B k/v projection at M=64 drops from
72.6 us to 16.9 us on an RTX 5090.

The override applies only in the auto-config branch. It requires
is_valid_config and an instantiated kernel. The full-SM sm_80 tables and
Ada's narrow FP32 AWQ schedule keep their own tuned tile.

Adds a profiler-based test that pins the launched tile and checks each
launch against an FP64 oracle, plus docs/marlin-tile-schedule.md.

Depends on #5188 (#4913 scale-slot race fix), which this branch carries.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Qubitium
Qubitium marked this pull request as ready for review November 18, 2026 11:26
@Qubitium
Qubitium merged commit 92c6ce0 into main Nov 18, 2026
6 checks passed
@Qubitium
Qubitium deleted the perf/marlin-small-grid-tiles branch November 18, 2026 11:26
Qubitium added a commit that referenced this pull request Nov 18, 2026
#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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant