Repository navigation
fix(marlin): stop re-reading a group's scale slot after it is refilled (#4913) - #5188
Merged
Merged
Conversation
#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
marked this pull request as ready for review
November 18, 2026 10:21
This was referenced Nov 18, 2026
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]>
11 tasks done
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 withthread_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.sh_s + s_sh_stage * g * (pipe / g)). That read ran in iterationp-1atk = 1, after the lastwait_for_stage()barrier that protects the slot.p, before its own barrier, faster warps refill the same slot with stagep + stages, which belongs to groupg + 2.cp.asynclatency normally hides the race. When the GPU time-slices to another context, in-flight copies drain before the save, so a lagging warp reads groupg + 2's scales.This affects GPTQ (
kU4B8) and AWQ (kU4with 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 withthread_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'sif (pipe % g == 0)guard tofetch_scales_to_registersand to bothfetch_zp_to_registerspaths (integer and float zero points). Only the group's first stage reads shared memory. Later stages keepfrag_s/frag_qzp/frag_zpf, which already hold that group's values.pipe == g * (pipe / g)).matmul()re-dequantizes zero points only atk == 0, fromfrag_qzp[0], so register reuse supplies the same group.group_blocks < thread_k_blocksbranch.tests/kernels/test_marlin_scale_slot_race_4913.py: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.Tests
Red, the new test against the base (
a13fe7aa2):Green, this branch, on each card:
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._sleeptenant process on the same GPU, 2 x 20,000 calls per cell, original and fixed interleaved):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, theformat/check.shinvocation): all checks passed.Root-cause evidence
k%64in {0-15, 32-47} or {16-31, 48-63}, which is one k-warp-group.g+2exactly (error 0.000).cp.asyncring with Marlin's2*(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
Not applicable: three guarded shared-memory reads inside existing kernel lambdas; no control-flow, selection or lifecycle change.
Review Requirements
Kernel Accuracy Requirements
Notes
🤖 Generated with Claude Code