Skip to content

fix(marlin): stop re-reading a group's scale slot after it is refilled (#4913) - #5188

Merged
Qubitium merged 1 commit into
mainfrom
fix/4913-marlin-scale-slot-race
Nov 18, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/4913-marlin-scale-slot-race

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

Fixes #4913

Marlin returned silently wrong 64-row output blocks when another process shared the GPU. The cause is a write-after-read race on the group scale and zero-point slot in shared memory. It is not a driver, CUDA or cuBLAS problem, and not hardware.

For group_blocks > thread_k_blocks (g128 with thread_k = 64), one 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 (sh_s + s_sh_stage * g * (pipe / g)). That read ran in iteration p-1 at k = 1, after the last wait_for_stage() barrier that protects the slot.
  • In iteration p, before its own barrier, faster warps refill the same slot with stage p + stages, which belongs to group g + 2.
  • cp.async latency normally hides the race. When the GPU time-slices to another context, in-flight copies drain before the save, so a lagging warp reads group g + 2's scales.

This affects GPTQ (kU4B8) and AWQ (kU4 with zero points), in fp16 and bf16, on sm_89 (RTX 4090) and sm_120 (RTX 5090). It explains every observation in #4913: whole 64x64 blocks wrong, a new location each time, trigger only under another tenant, and a rate that grows with thread_m_blocks.

vLLM's Marlin had the identical code up to v0.11.x. It was fixed there, without being labelled a race fix, by vllm-project/vllm#24722 (v0.12.0, dense and MoE). GPT-QModel forked before that change.

What Changed

  • gptqmodel_ext/marlin/marlin_template.h: port vLLM's if (pipe % g == 0) guard to fetch_scales_to_registers and to both fetch_zp_to_registers paths (integer and float zero points). Only the group's first stage reads shared memory. Later stages keep frag_s / frag_qzp / frag_zpf, which already hold that group's values.
    • When the guard fires, the slot address is unchanged (pipe == g * (pipe / g)).
    • matmul() re-dequantizes zero points only at k == 0, from frag_qzp[0], so register reuse supplies the same group.
    • In-place fp8 scale dequant can't double-apply: fp8 scales occur only with fp4 groups of 16/32, which take the group_blocks < thread_k_blocks branch.
  • tests/kernels/test_marlin_scale_slot_race_4913.py:
    • A source check that runs on any host.
    • A real-kernel soak: a second process keeps the same GPU busy with torch.cuda._sleep, and 10,000 launches must stay bitwise identical to the first launch. The first launch is also checked against an FP64 oracle.
  • No host-side, API or behavior change beyond the fix.

Tests

  • Unit tests have been executed and passed, or the reason they are not applicable is documented.
  • I added a new simple/fast unit test for this change, or documented why that is not applicable.

Red, the new test against the base (a13fe7aa2):

pytest -q tests/kernels/test_marlin_scale_slot_race_4913.py
# RTX 5090 (sm_120): source check FAILED; gptq-fp16 FAILED 57/10000; awq-zp-bf16 passed (this card does not trigger bf16 AWQ)
# RTX 4090 (sm_89):  source check FAILED; gptq-fp16 FAILED 1/10000;  awq-zp-bf16 FAILED 10/10000

Green, this branch, on each card:

pytest -q 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
# RTX 5090: 224 passed, 2 skipped (179s)
# RTX 4090: 224 passed, 2 skipped (207s)

Skips are architecture gates unrelated to Marlin: Machete is Hopper-only, and Swordfish supports sm100/103/110 only.

Loaded A/B soak (same synthetic modules, a torch.cuda._sleep tenant process on the same GPU, 2 x 20,000 calls per cell, original and fixed interleaved):

card case original this PR
RTX 5090 GPTQ fp16, M=2048, K=2048, N=8192 99 + 100 / 40,000 0 / 40,000
RTX 4090 AWQ zp bf16, M=1024, K=2048, N=8192 29 + 22 / 40,000 0 / 40,000

An earlier per-stage-copy variant of the same fix was also clean: 0 bad in 220,000 loaded calls.

ruff check gptqmodel docs/eora tests setup.py (ruff 0.14.2, the format/check.sh invocation): all checks passed.

Root-cause evidence

  • Forensics on 12 captured bad outputs (fit the delta against an FP64 oracle):
    • Each bad block's error lives in exactly 32 K indices of one 64-wide stage: k%64 in {0-15, 32-47} or {16-31, 48-63}, which is one k-warp-group.
    • It is always the second stage of a group.
    • The weights used equal the true weights times a per-column factor (residual 0.002), so the codes were right and only the scales were wrong.
    • The scales used match group g+2 exactly (error 0.000).
  • Not stale memory: filling all shared memory with junk before every call gave 0 bad.
  • The trigger is the context switch: a pure ALU spin kernel in another process raised the rate about 5x compared with a GEMM tenant. So it is time-slicing, not memory or cache contention.
  • Standalone CUDA reproducer (no torch, no Marlin): a 4-stage cp.async ring with Marlin's 2*(pipe/2) scale slot. Delaying one warp by 2 µs before its scale read corrupts 200 of 200 launches. With a per-stage slot, it corrupts 0 of 200. A legal scheduling delay changes the result, which proves the race is in the code.

Model Support / Module Plan

Not applicable: no model or definition changes.

AWQ onboarding safety

Not applicable: kernel fix only, with no AWQ grouping, search, replay or fold changes.

Logic Flow Documentation

  • This change does not alter a major logic flow, or the required diagrams and docs page are included below.

Not applicable: three guarded shared-memory reads inside existing kernel lambdas; no control-flow, selection or lifecycle change.

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

Notes

  • The test's soak half depends on hardware and timing. On the base kernel it fails reliably on an RTX 5090 (about 57 per 10,000) and is borderline on an RTX 4090 for GPTQ fp16 (1 per 10,000). The source check fails deterministically on the base. Cards or drivers that don't time-slice will pass the soak trivially.
  • A survey of other vLLM Marlin fixes since the fork is in progress. Any further ports will be separate PRs.

🤖 Generated with Claude Code

#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]>
@Qubitium
Qubitium marked this pull request as ready for review November 18, 2026 10:21
@Qubitium
Qubitium merged commit 396b5f0 into main Nov 18, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/4913-marlin-scale-slot-race branch November 18, 2026 10:21
Qubitium added a commit that referenced this pull request Nov 18, 2026
* fix(marlin): stop re-reading a group's scale slot after it is refilled (#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]>

* perf(marlin): use 128x64 tiles when the output grid is small

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]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
Qubitium added a commit that referenced this pull request Nov 18, 2026
…ule) (#5222)

* fix(marlin): stop re-reading a group's scale slot after it is refilled (#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]>

* perf(marlin): take whole output tiles before striping K (hybrid schedule)

Port the hybrid work schedule from vLLM's Marlin kernel
(vllm-project/vllm#24722, scheduling part only). With more output tiles
than threadblocks, each block first takes part1_mn_iters rounds of whole
tiles (all of K, slice_count 1, no lock or global reduction). The remaining
part2_mn_tiles are then striped along K as before. The striped remainder
is kept at least a third of the grid.

The first part-2 slice of a block usually starts mid-K. The slice re-init
now rebuilds a_gl_rd, b_gl_rd/B_ptr, s_gl_rd and zp_gl_rd from
(slice_row, slice_col) instead of assuming slice_row == 0.

#24722's contiguous-K warp layout and single-copy scale fetch are not
ported. Measured on an RTX 5090, they regressed M=16 by up to 9%, and they
reshape the scale-read lines that #5193 changes.

Adds test_marlin_hybrid_schedule.py, which drives the part-1 -> part-2
transition with g128 scales, AWQ zero points, bias and FP32/FP16 reductions
against an FP64 oracle, plus docs/marlin-hybrid-schedule.md.

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

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(marlin): rebuild the 8-bit scale row on hybrid part-2 restarts

The part-2 slice restart copied the initial s_gl_rd row term from before
#5193, so it divided by 2 only for FP4 weights (w_type == kFE2M1f). Since
#5193 the 8-bit (paired) scale layout keys on the scale width, so MXFP8
restarts landed on twice the right scale row. On an RTX 4090, MXFP8 at
M=1000, K=4096, N=4096 came out with errors as large as the outputs. Use
is_8bit_scale like the initial s_gl_rd.

Add MXFP8 and MXFP4 cases to the part-1 -> part-2 transition test. They fail
with the old divisor and pass with this one.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* perf(marlin): make the hybrid schedule a prefill-only kernel (>= 512 rows)

The hybrid schedule is now a compile-time kernel variant (`hybrid` template
argument), instantiated only for 64-row tiles. The host launches it only for
calls with at least MARLIN_HYBRID_MIN_ROWS (512) rows; every smaller call runs
the hybrid=false kernel, which compiles to the original schedule.

A clean A/B on a 124-SM A100 measured the ungated hybrid kernel ~1 us slower
per call at M=1 and up to 14% slower at M=64, and 1.06x median (up to 1.20x)
faster at M >= 1024. The small-M cost shows even where no part-1 round runs,
so it is kernel code, not schedule; hence a compile-time gate.

Adds a source-contract test for the gate and FP64-oracle tests at 511 rows
(original kernel) and 512 rows (hybrid kernel).

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
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