Skip to content

fix(moe): cooperatively launch fused W4A16 grids#73

Merged
lukealonso merged 1 commit into
local-inference-lab:masterfrom
yatesdr:fix/w4a16-cooperative-grid-pr-20260723
Jul 23, 2026
Merged

fix(moe): cooperatively launch fused W4A16 grids#73
lukealonso merged 1 commit into
local-inference-lab:masterfrom
yatesdr:fix/w4a16-cooperative-grid-pr-20260723

Conversation

@yatesdr

@yatesdr yatesdr commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

Summary

Launch the two barrier-bearing fused W4A16 MoE grids cooperatively.

Both W4A16FusedMoeKernel and W4A16FusedMoeHybridKernel synchronize every
CTA between FC1, activation, output initialization and FC2. Their planners
already cap the grid to SM count * blocks_per_sm, but the launch itself did
not request whole-grid cooperative admission.

This patch adds cooperative=True to those two launch sites. It does not
change the standalone W4A16 GEMM, launch geometry, tile selection,
quantization, routing or output math.

Why

A software whole-grid barrier requires every participating CTA to be
resident. A bounded grid alone does not guarantee that: unrelated work on
another stream can occupy an SM after only part of the fused grid has been
admitted, leaving resident CTAs waiting for peers that cannot be scheduled.

SparkInfer already uses cooperative admission for the fused-micro W4A16 decode
path. The route-packed fallback entered above that micro range and the
two-tier hybrid path use the same barrier pattern but were missing the launch
contract.

The production workload that motivated the audit runs shared-expert and other
auxiliary-stream work beside MTP decode graph capture. This PR is deliberately
not presented as the fix for that workload's eventual CUDA illegal-address
failure: launch-blocking subsequently localized that separate failure to an
MLA query-absorption BMM, which has its own vLLM fix. Cooperative admission is
an independent correctness requirement visible directly in these fused
kernels.

Validation

Completed:

  • forward-port onto current SparkInfer master;
  • python -m py_compile for both changed files;
  • git diff --check;
  • exact patched W4A16 source was byte-verified in the GLM-5.2 v20 candidate
    that completed profiling and production decode capture at sizes 16 through
    1, with 624/624 diagnostic boundaries passing and no CUDA/cuBLAS/OOM/Xid
    failure.

The engine result is combined-stack evidence, not an isolated A/B proof of
this change.

This PR adds a focused SM120 GPU regression:

pytest -q tests/moe/test_fused_moe.py \
  -k test_run_w4a16_m9_graph_replay_with_prequeued_aux_work

It captures the first route-packed size above the fused-micro range using the
GLM shard geometry, prequeues large BF16 GEMMs on an auxiliary stream, replays
the W4A16 graph concurrently, and checks finite, nonzero, stable output.

The test cannot run on the contributor's macOS host because CUTLASS DSL and
SM120 CUDA are unavailable. This PR remains draft until the focused GPU test
and the ongoing decode-throughput qualification are recorded.

Tradeoff

Cooperative admission can alter scheduling latency when unrelated stream work
is queued. It does not serialize the streams globally or disable
shared-expert overlap; it only requires the complete bounded W4A16 grid to be
admitted as a unit, which is the execution contract required by its software
barriers.

AI assistance

OpenAI Codex assisted with source tracing, forward-porting, regression-test
design and PR drafting. The submitted code and runtime evidence are being
reviewed by the human contributor and target-system operator before the draft
is marked ready.

Summary by CodeRabbit

  • Bug Fixes

    • Improved reliability of fused W4A16 Mixture-of-Experts operations, particularly during CUDA graph replay and concurrent GPU workloads.
    • Enhanced synchronization for supported cooperative execution scenarios.
  • Tests

    • Added coverage for route-packed workloads and CUDA graph replay while auxiliary GPU work is running.
    • Verified results remain finite and numerically consistent under these conditions.

@coderabbitai

coderabbitai Bot commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

W4A16 fused and hybrid MoE kernel launches now request cooperative CUDA execution. A new test validates CUDA graph replay at m=9 while auxiliary GEMM work is queued on another stream.

Changes

W4A16 cooperative execution

Layer / File(s) Summary
Cooperative kernel launches
sparkinfer/moe/_shared/kernels/w4a16/kernel.py
Single-tier and hybrid W4A16 fused MoE launches now pass cooperative=True.
Graph replay validation
tests/moe/test_fused_moe.py
Adds an end-to-end test that captures and replays W4A16 fused MoE alongside auxiliary stream GEMMs, then checks finite and numerically matching output.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Test as W4A16 graph replay test
  participant Graph as CUDA graph
  participant MoE as fused_moe.run
  participant Aux as auxiliary CUDA stream
  Test->>MoE: Compute eager expected output
  Test->>Graph: Capture fused_moe.run
  Test->>Aux: Queue auxiliary GEMMs
  Test->>Graph: Replay captured graph
  Graph->>MoE: Execute cooperative W4A16 launch
  Test->>Graph: Synchronize and compare output
Loading

Possibly related PRs

Suggested reviewers: voipmonitor

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main change: enabling cooperative launches for fused W4A16 MoE grids.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@yatesdr
yatesdr marked this pull request as ready for review July 23, 2026 05:34
@yatesdr

yatesdr commented Jul 23, 2026

Copy link
Copy Markdown
Contributor Author

Runtime qualification update: the exact cooperative-launch source was byte-verified in the final v20 candidate. With shared-expert and MTP activity enabled, that process completed profiling and production graph capture for sizes 16 through 1, passed 624/624 diagnostic boundaries, then completed cold prefill plus ten decode cells through concurrency 16 with zero errors and RestartCount 0. Decode stayed within 5 percent of the v19 production baseline at the measured comparable cells. This is combined-stack rather than isolated A/B evidence, but it exercises the production concurrent-stream setting and closes the pending throughput/stability gate. The patch remains narrowly scoped to supplying the cooperative-admission contract required by the existing whole-grid barriers.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
tests/moe/test_fused_moe.py (1)

172-182: 🩺 Stability & Availability | 🔵 Trivial | 💤 Low value

Warmup runs on the default stream instead of the capture stream.

PyTorch's documented CUDA-graph pattern requires warmup to run on the same side stream used for capture (to stabilize allocator/algorithm-selection state before recording), but the eager fused_moe.run(binding=binding) at line 172 runs on the default stream, while capture happens on a separate capture_stream. Since this test is inference-only (no autograd), this is unlikely to break capture, but it doesn't mirror the officially recommended pattern nor necessarily the production warmup path this test is meant to validate.

As per coding guidelines, "Treat CUDA graph capture/replay, warmup behavior, stable allocation, and fixed or preplanned workspace capacity as serving requirements."

♻️ Align warmup with the capture stream
-    fused_moe.run(binding=binding)
-    torch.cuda.synchronize()
-    expected = output.clone()
-
-    graph = torch.cuda.CUDAGraph()
-    capture_stream = torch.cuda.Stream()
-    capture_stream.wait_stream(torch.cuda.current_stream())
-    with torch.cuda.stream(capture_stream), torch.cuda.graph(graph):
-        fused_moe.run(binding=binding)
+    graph = torch.cuda.CUDAGraph()
+    capture_stream = torch.cuda.Stream()
+    capture_stream.wait_stream(torch.cuda.current_stream())
+    with torch.cuda.stream(capture_stream):
+        fused_moe.run(binding=binding)
+    torch.cuda.current_stream().wait_stream(capture_stream)
+    torch.cuda.synchronize()
+    expected = output.clone()
+
+    with torch.cuda.stream(capture_stream), torch.cuda.graph(graph):
+        fused_moe.run(binding=binding)
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/moe/test_fused_moe.py` around lines 172 - 182, Move the warmup
fused_moe.run call in the test to the capture_stream, ensuring the stream is
initialized and synchronized appropriately before capture. Keep expected output
generation and the existing capture/replay synchronization behavior unchanged,
while making warmup use the same side stream as torch.cuda.graph.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@tests/moe/test_fused_moe.py`:
- Around line 172-182: Move the warmup fused_moe.run call in the test to the
capture_stream, ensuring the stream is initialized and synchronized
appropriately before capture. Keep expected output generation and the existing
capture/replay synchronization behavior unchanged, while making warmup use the
same side stream as torch.cuda.graph.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 5e19bc02-ab58-4ba4-8672-fc8781db5e98

📥 Commits

Reviewing files that changed from the base of the PR and between 1a88b38 and f309b27.

📒 Files selected for processing (2)
  • sparkinfer/moe/_shared/kernels/w4a16/kernel.py
  • tests/moe/test_fused_moe.py

@lukealonso
lukealonso merged commit cc9b476 into local-inference-lab:master Jul 23, 2026
1 check passed
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.

2 participants