Skip to content

perf(b12x/moe): balance W4A8 M128 tiles with atomic work stealing - #168

Merged
lukealonso merged 2 commits into
local-inference-lab:mainfrom
FujitsuPolycom:perf/b12x-w4a8-m128-worksteal-n3-swizzle
Nov 29, 2026
Merged

lukealonso merged 2 commits into
local-inference-lab:mainfrom
FujitsuPolycom:perf/b12x-w4a8-m128-worksteal-n3-swizzle

Conversation

@FujitsuPolycom

@FujitsuPolycom FujitsuPolycom commented Nov 28, 2026 •

Copy link
Copy Markdown

📌 Description

Why:
Static CTA strides leave a long tail with skewed expert routing in
SM120 W4A8 M128 prefill MoE. Four row buckets also pad sparse tiles,
and scalar epilogue loads add instructions on the M128xN64 path.

What:
Stacked on PR #161 (d8b6a60).

  • Claim tiles dynamically from atomic counters in
    W4A8MaterializedPhase1Kernel and W4A8MaterializedPhase2Kernel,
    group N64 tiles in strips of three for L2 reuse, dispatch to six
    row buckets (16, 32, 48, 64, 96, 128), and vectorize the M128xN64
    up-projection epilogue's shared loads.
  • Guarantee both work-stealing counters (task_head and
    producers_done_count) are zeroed across every launch and CUDA-graph
    replay path:
    • Zero task_head and p2_task_head in _prequant_route_init_zero_kernel
      and _dynamic_route_plan_kernel (plus matching _preparation.py
      compile_dynamic_route_plan warmups) and under volatile_launch_state
      when external_route_plan is disabled.
    • Reset p2_task_head in W4A8MaterializedPhase1Kernel and symmetrically
      reset p1_task_head in W4A8MaterializedPhase2Kernel.
    • Guard Phase 2 dynamic work stealing on fused_groups == 1 (with
      explicit assertions against tile_m == 128 and fused_groups > 1).
    • Skip initial work-steal atomic increments when task_tail == 0 in
      both Phase 1 and Phase 2 while still executing the opposite-phase
      reset.
    • Advance the integration.tp_moe.dynamic compile spec from 23 to 24.
  • Add CPU swizzle/bucket/reset-contract tests, direct Phase 1/2
    work-stealing and empty-launch tests, Triton route-plan counter-reset
    tests, and end-to-end poisoned-scratch replay tests covering
    mxfp8+triton+M128, bf16+triton+M16, and bf16+internal+M128 in
    tests/experimental/b12x/moe/test_w4a8_prefill_m128.py.

Result:
Single-GPU NVIDIA GeForce RTX 5090 (SM120) CUDA-graph comparison at
M=8192, H=5120, E=384, k=6, N=576, Zipf 1.1 with the Triton route
planner and inline scales (including the PR #161 epilogue prerequisite)
previously recorded 2697.8 -> 2424.5 us mean median, with zero BF16
bit differences across 41,943,040 deterministic outputs versus M64.

Evidence:
Verified locally on single-GPU RTX 5090 (SM120, PyTorch 2.14.1+cu132,
CUTLASS DSL 4.8.1) from exact git archive HEAD (multi-GPU / TP4 not
exercised here):

  • tests/experimental/b12x/moe/test_w4a8_prefill_m128.py: 47 passed,
    0 failed (including test_w4a8_m128_validation_and_heuristics,
    test_w4a8_m128_compute_m96_and_no_tail_barrier_invariants,
    test_w4a8_m128_worksteal_swizzle_buckets_and_reset_contracts,
    test_w4a8_m128_triton_route_plan_kernels_zero_worksteal_counters,
    test_w4a8_m128_phase1_worksteal_n3_swizzle_and_empty_direct,
    test_w4a8_m128_phase2_worksteal_counter_reset_and_empty_direct,
    test_w4a8_m128_phase2_alignment_fallback_and_compute_m96_direct
    [False/True], test_w4a8_m128_output_2byte_alignment_rejected,
    test_dynamic_monolithic_nvfp4_output_4byte_alignment_fallback
    [128-128, 512-16],
    test_w4a8_m128_output_alignment_fallback_and_rejection [False/True],
    and test_w4a8_m128_poisoned_scratch_replay across mxfp8-triton-m128,
    bf16-triton-m16, and bf16-internal-m128)
  • compile_dynamic_route_plan warmups: 3/3 passed for
    (preq=False, full=False), (preq=True, full=False), and
    (preq=True, full=True)
  • ruff 0.12.8 check clean across all 6 touched files;
    git merge-tree --write-tree 335459b HEAD clean;
    git diff --check clean.

Discord: Dooner

🧪 Tests

  • Tests have been added or updated as needed.

  • Exact-archive checks passed on candidate commit c9c5841aa90a9f5205b5278c26c212e81862dbb4: Stacked on fixed #161 (d8b6a60e). Verified on clean git archive (1x RTX 5090 SM120): ruff==0.12.8 clean, git merge-tree vs 335459b2 clean, test_w4a8_prefill_m128.py 47/47 passed + 3/3 compile_dynamic_route_plan warmups + monolithic NVFP4 4B/8B/12B Probe C PASS. Stays Draft pending #161 merge and exact-SHA stacked RTX 5090 A/B.

    Suggested CI test targets: tests/experimental/b12x/moe/test_w4a8_prefill_m128.py

🔬 Experimental Track

Experimental b12x backend change. Scope and hardware limits are stated above.

@coderabbitai

coderabbitai Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 59 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 25fe0344-99ba-4987-bec3-eea4d1e052b6
Commits

Reviewing files that changed from the base of the PR and between 335459b and c9c5841.

Files selected for processing: 10

  • flashinfer/experimental/b12x/_lib/intrinsics/atomics.py
  • flashinfer/experimental/b12x/moe/_shared/kernels/dynamic/kernel.py
  • flashinfer/experimental/b12x/moe/_shared/kernels/w4a16/kernel/sum.py
  • flashinfer/experimental/b12x/moe/_shared/kernels/w4a16/kernel/sum_launch.py
  • flashinfer/experimental/b12x/moe/_shared/kernels/w4a8_phase1.py
  • flashinfer/experimental/b12x/moe/_shared/kernels/w4a8_phase2.py
  • flashinfer/experimental/b12x/moe/fused_moe/_impl.py
  • flashinfer/experimental/b12x/moe/fused_moe/_preparation.py
  • tests/experimental/b12x/moe/test_w4a16_route_sum.py
  • tests/experimental/b12x/moe/test_w4a8_prefill_m128.py
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

Why:
SM120 W4A8 M128 materialized MoE uses scalar shared loads and BF16
stores or atomic reductions in the down-projection epilogue. Tasks
with 65 to 96 valid rows pad to 128 rows, and the epilogue retains a
tail barrier when the pipeline has three or more stages.

What:
- Load and write 128-bit vectors in W4A8MaterializedPhase2Kernel,
  using st_global_v4_u32 for deterministic output and
  red_add_global_v4_bf16x2 for routed accumulation when tile_n % 8 == 0,
  scatter_output base is 16-byte aligned, and scatter_n % 8 == 0,
  falling back to 4-byte st_global_u32 / red_add_global_bf16x2
  otherwise.
- Guard Phase 0 st_global_v4_u32 zeroing and the monolithic
  MoEDynamicKernelBackend scatter_add_v4_bf16x2 epilogue on 16-byte
  base alignment (and scatter_N % 8 == 0 / tile_shape_mnk[1] % 8 == 0
  with a 4x scatter_add_bf16x2 fallback at +0/+4/+8/+12 bytes), lower
  scatter_output assumed_align to 4 in _get_dynamic_kernel and
  _launch_dynamic_flat so LLVM preserves the runtime checks, and enforce
  output alignment in b12x_moe_fp4 (4-byte for dynamic non-raw-route
  paths, 16-byte fail-closed for micro and raw-route paths).
- Lower output assumed_align to 4 in W4A16TopKSumKernel (sum_launch.py),
  use 16-byte-aligned fc2_out for unused dummy pointer arguments, guard
  vec8_fast_path st_global_v4_u32 on 16-byte output alignment with a
  4x st_global_u32 fallback in sum.py, and bump the moe.w4a16.topk_sum
  compile spec from 7 to 8.
- Add the compute_m=96 bucket in both materialized kernels (asserting
  compute_m % 8 == 0 in Phase 2 epilogue), skip the tail barrier when
  stages >= 3 (preload_slices = stages - 2 keeps next_stage disjoint
  from the prior compute stage), and bump the integration.tp_moe.dynamic
  compile spec from 22 to 23.
- Add CPU invariant, direct Phase 2 alignment/bucket, monolithic NVFP4
  dynamic 4B/8B/12B alignment fallback, dynamic 4B/2B and micro 16B
  alignment, and direct W4A16TopKSumKernel 4B-aligned output tests in
  test_w4a8_prefill_m128.py and test_w4a16_route_sum.py.

Result:
Single-GPU NVIDIA GeForce RTX 5090 (SM120, driver 610.88, PyTorch
2.14.1+cu132, CUTLASS DSL 4.8.1) CUDA-graph A/B versus base 0ddf1a4
on W4A8 M128 prefill (E=384, H=5120, N=576, top-k 6, Zipf 1.1,
prequantized MXFP8 input, internal route planner, inline CSF scales,
3 interleaved trials x 30 replays with 128 MiB L2 flush; multi-GPU /
TP4 not exercised):
- M=4096, det=False: 1808.35 us (trials 1807.33/1808.35/1809.41) ->
  1774.40 us (trials 1703.71/1774.40/1776.40), -33.95 us (-1.88%)
- M=8192, det=False: 2596.64 us (trials 2591.71/2596.64/2632.18) ->
  2551.54 us (trials 2549.55/2551.54/2553.73), -45.10 us (-1.74%)
- M=8192, det=True:  2993.09 us (trials 2990.13/2993.09/3004.70) ->
  2975.62 us (trials 2975.52/2975.62/2983.70), -17.47 us (-0.58%)
Per-kernel CUDA profiler median on M=8192, det=False (15 L2-flushed
runs): W4A8MaterializedPhase2Kernel 972.24 -> 931.87 us (-4.15%),
W4A8MaterializedPhase1Kernel 1750.98 -> 1704.90 us (-2.63%).

Evidence:
Verified locally on single-GPU RTX 5090 (SM120, PyTorch 2.14.1+cu132,
CUTLASS DSL 4.8.1) from exact git archive HEAD (multi-GPU / TP4 not
exercised here):
- tests/experimental/b12x/moe/test_w4a8_prefill_m128.py: 37 passed,
  0 failed (including test_w4a8_m128_validation_and_heuristics,
  test_w4a8_m128_compute_m96_and_no_tail_barrier_invariants,
  test_w4a8_m128_phase2_alignment_fallback_and_compute_m96_direct
  [False/True], test_w4a8_m128_output_2byte_alignment_rejected
  [dynamic 4B + micro 16B fail-closed],
  test_dynamic_monolithic_nvfp4_output_4byte_alignment_fallback
  [128-128, 512-16] across 4B/8B/12B mod-16 output offsets, and
  test_w4a8_m128_output_alignment_fallback_and_rejection [False/True]
  across 4B/8B/12B mod-16 output offsets)
- tests/experimental/b12x/moe/test_w4a16_route_sum.py: 10 passed,
  0 failed (including test_route_sum_4byte_aligned_output [False/True])
- tests/experimental/b12x/moe/test_route_sum_compile.py: 14 passed,
  0 failed
- ruff 0.12.8 check clean across all 9 touched files;
  git merge-tree --write-tree 335459b HEAD clean;
  git diff --check clean.

Discord: Dooner
Signed-off-by: FujitsuPolycom <87842395+FujitsuPolycom@users.noreply.github.com>
Why:
Static CTA strides leave a long tail with skewed expert routing in
SM120 W4A8 M128 prefill MoE. Four row buckets also pad sparse tiles,
and scalar epilogue loads add instructions on the M128xN64 path.

What:
Stacked on PR local-inference-lab#161 (d8b6a60).
- Claim tiles dynamically from atomic counters in
  W4A8MaterializedPhase1Kernel and W4A8MaterializedPhase2Kernel,
  group N64 tiles in strips of three for L2 reuse, dispatch to six
  row buckets (16, 32, 48, 64, 96, 128), and vectorize the M128xN64
  up-projection epilogue's shared loads.
- Guarantee both work-stealing counters (task_head and
  producers_done_count) are zeroed across every launch and CUDA-graph
  replay path:
  - Zero task_head and p2_task_head in _prequant_route_init_zero_kernel
    and _dynamic_route_plan_kernel (plus matching _preparation.py
    compile_dynamic_route_plan warmups) and under volatile_launch_state
    when external_route_plan is disabled.
  - Reset p2_task_head in W4A8MaterializedPhase1Kernel and symmetrically
    reset p1_task_head in W4A8MaterializedPhase2Kernel.
  - Guard Phase 2 dynamic work stealing on fused_groups == 1 (with
    explicit assertions against tile_m == 128 and fused_groups > 1).
  - Skip initial work-steal atomic increments when task_tail == 0 in
    both Phase 1 and Phase 2 while still executing the opposite-phase
    reset.
  - Advance the integration.tp_moe.dynamic compile spec from 23 to 24.
- Add CPU swizzle/bucket/reset-contract tests, direct Phase 1/2
  work-stealing and empty-launch tests, Triton route-plan counter-reset
  tests, and end-to-end poisoned-scratch replay tests covering
  mxfp8+triton+M128, bf16+triton+M16, and bf16+internal+M128 in
  tests/experimental/b12x/moe/test_w4a8_prefill_m128.py.

Result:
Single-GPU NVIDIA GeForce RTX 5090 (SM120) CUDA-graph comparison at
M=8192, H=5120, E=384, k=6, N=576, Zipf 1.1 with the Triton route
planner and inline scales (including the PR local-inference-lab#161 epilogue prerequisite)
previously recorded 2697.8 -> 2424.5 us mean median, with zero BF16
bit differences across 41,943,040 deterministic outputs versus M64.

Evidence:
Verified locally on single-GPU RTX 5090 (SM120, PyTorch 2.14.1+cu132,
CUTLASS DSL 4.8.1) from exact git archive HEAD (multi-GPU / TP4 not
exercised here):
- tests/experimental/b12x/moe/test_w4a8_prefill_m128.py: 47 passed,
  0 failed (including test_w4a8_m128_validation_and_heuristics,
  test_w4a8_m128_compute_m96_and_no_tail_barrier_invariants,
  test_w4a8_m128_worksteal_swizzle_buckets_and_reset_contracts,
  test_w4a8_m128_triton_route_plan_kernels_zero_worksteal_counters,
  test_w4a8_m128_phase1_worksteal_n3_swizzle_and_empty_direct,
  test_w4a8_m128_phase2_worksteal_counter_reset_and_empty_direct,
  test_w4a8_m128_phase2_alignment_fallback_and_compute_m96_direct
  [False/True], test_w4a8_m128_output_2byte_alignment_rejected,
  test_dynamic_monolithic_nvfp4_output_4byte_alignment_fallback
  [128-128, 512-16],
  test_w4a8_m128_output_alignment_fallback_and_rejection [False/True],
  and test_w4a8_m128_poisoned_scratch_replay across mxfp8-triton-m128,
  bf16-triton-m16, and bf16-internal-m128)
- compile_dynamic_route_plan warmups: 3/3 passed for
  (preq=False, full=False), (preq=True, full=False), and
  (preq=True, full=True)
- ruff 0.12.8 check clean across all 6 touched files;
  git merge-tree --write-tree 335459b HEAD clean;
  git diff --check clean.

Discord: Dooner
Signed-off-by: FujitsuPolycom <87842395+FujitsuPolycom@users.noreply.github.com>
@FujitsuPolycom
FujitsuPolycom force-pushed the perf/b12x-w4a8-m128-worksteal-n3-swizzle branch from 51dec29 to c9c5841 Compare November 29, 2026 04:13

@voipmonitor voipmonitor left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Verified overnight commit c9c5841aa (cleanly stacked on #161 d8b6a60e2) on RTX PRO 6000 Blackwell (SM120) — high-impact prefill MoE win!

  • Zero VRAM & zero register-spill overhead: Identical dynamic workspace bytes (0 B added) and STACK:0 LOCAL:0 in cuobjdump across Phase 1 & Phase 2 (maintaining 2 CTAs/SM).
  • Isolated SM120 speedup over #161 (skewed DeepSeek E=384, H=5120, N=576, csf_inline=True): M=4096 -6.74% (1457.5 -> 1359.2 us), M=8192 -3.73% median / -7.5% p90.
  • Next step: Ready to un-draft and merge right after #161 lands.

4× RTX PRO 6000 Blackwell (SM120) E2E Stack Verification (gb202-preview, pure hyperflare-inference@19c098c8 + all 19 hardened PRs):

  • HF Preview Core: https://huggingface.co/local-inference-lab/DeepSeek-V4.1-Flash-1017-FLARE/tree/gb202-preview (e4ce7ebb)
  • Cold Prefill (wall / GPU vs gb202): 8K 47.6k / 50.4k (+9.4%), 16K 56.9k / 58.6k (+10.1%), 32K 62.5k / 63.7k (+10.7%), 64K 65.1k / 66.4k (+10.5%, 1.097s), 128K 64.4k / 65.9k (+10.4%)
  • Decode Concurrency (P=256 & P=2048): C=6 +7.5%, C=8 +4.0%, C=12 +11.2%, C=16 +14.2%, C=24 +4.8%, C=32 +4.2%, C=32@2K +5.4% (-5.8% step)
  • KV Cache / VRAM: 14.80 GiB/GPU (16,962,176 KV tokens = 99.64% of baseline 17,023,872) · -712 fewer kernels (40,347 vs 41,059) · 5/5 NIAH · 4/4 Vision · Greedy 391 PASS

@voipmonitor voipmonitor left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Marked ready for review and approved — verified on 4× RTX PRO 6000 Blackwell (SM120) and included in the tested gb202-preview stack. See tracking issue: local-inference-lab/hyperflare-recipes#90

@lukealonso
lukealonso merged commit 3346959 into local-inference-lab:main Nov 29, 2026
16 of 20 checks 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.

3 participants