Skip to content

fix(b12x/moe): reuse per-experts weight view cache for immutable scales - #196

Open
yatesdr wants to merge 1 commit into
local-inference-lab:mainfrom
yatesdr:fix/b12x-moe-weight-views-cache
Open

yatesdr wants to merge 1 commit into
local-inference-lab:mainfrom
yatesdr:fix/b12x-moe-weight-views-cache

Conversation

@yatesdr

@yatesdr yatesdr commented Nov 30, 2026

Copy link
Copy Markdown

Problem

Commit 810c0849fe ("Expose caller-owned storage for prepared operators") passed a brand-new _weight_views_cache={} on every invocation of the four custom ops in flashinfer/experimental/b12x/moe/fused_moe/_binding_ops.py (prepared_moe_out, prepared_moe_prequant_out, prepared_routed_moe_out, prepared_moe_mxfp8_out) so ephemeral CSF scale buffers would not retain references in the global _WEIGHT_CACHE.

For non-CSF NVFP4 MoE experts with n % 128 != 0 (such as nvidia/Qwen3.8-Flash-Next-FP4-Local at TP2 n=320 or TP4 n=160), passing {} on every call forces _get_weight_views to miss on every invocation—including during CUDA graph capture—so _ensure_dynamic_nvfp4_tail_views (_impl.py:15052) captures _split_gated_nvfp4_scale_views (unswizzle, split, zero-fill, and re-swizzle of all 512 experts' w13 scales) directly into every replay graph.

Node-level nsys profiling of Qwen3.8-Flash-Next TP2 (MTP=0, C=1, 96 decode steps on 2× DGX Spark / GB10) showed those 5 ATen copy/fill kernels consuming 123.8 ms/step (80.6% of GPU time) vs ~29 ms/step of actual model work.

Fix

  1. _binding_ops.py: Add _weight_views_cache(state, scale_buffers):
    • When scale_buffers is non-empty (ephemeral caller-owned CSF scale planes), return a fresh {} (unchanged behavior).
    • When scale_buffers is empty (immutable weights), reuse a single _weight_views_cache dict owned by experts._impl (B12XFP4ExpertWeights), shared across all capacity variants in _FusedMoeCapacityState without leaking into global _WEIGHT_CACHE.
    • Clear experts._weight_views_cache in release_weight_views(experts).
  2. _impl.py: Guard _ensure_dynamic_nvfp4_tail_views to raise RuntimeError if uncached split/padded tail views are requested while torch.cuda.is_current_stream_capturing() is True, preventing silent graph-pool allocations or captured scale repacking.
  3. tests/experimental/b12x/moe/test_scale_storage_lifetime.py: Add unit tests covering per-experts cache reuse/eviction, ephemeral scale_buffers isolation, capture-time rejection, and an end-to-end GPU CUDA-graph regression test for prepared_moe_out with N=320 non-CSF NVFP4.

Verification

  • SM120 pytest (RTX PRO 6000, against origin/main e34540cc):
    • test_scale_storage_lifetime.py: 7/7 passed (including test_prepared_moe_out_reuses_nvfp4_tail_views_under_cuda_graph, verifying split_calls == 1 across eager warmup + CUDA graph capture, matching replay output, and clean weakref release on experts.release_weight_views()).
    • test_nvfp4_split_backend.py: 6/6 passed.
    • test_fused_moe.py: 2/2 passed.
    • test_nvfp4_csf.py: 67/68 passed (the 1 failure test_a16_expanded_scales_reuse_the_planned_launch_for_live_counts reproduces identically on unpatched e34540cc).
  • End-to-end Qwen3.8-Flash-Next TP2 on 2× DGX Spark (SM121, --roce-ring, --embedding-table-storage ram, MTP-5, flare ec957867 / hyperflare-recipes 89e52f1 / flashinfer 6ce11d2e + this patch):
    • All 158 CUDA graphs captured cleanly with the capture-time RuntimeError guard active (6,821 kernels vs 7,020 before); sanity 4/4 PASS (think=True, think=False, tool_call, 32K needle).
Benchmark (Qwen3.8-Flash-Next TP2 Spark) Before After Delta
Decode C=1 (ctx=0, 256 out) 12.5 tok/s 61.1 tok/s 4.89×
Decode C=4 46.5 tok/s (11.5/req) 152.0 tok/s (38.1/req) 3.27×
Decode C=8 80.6 tok/s (10.0/req) 229.5 tok/s (28.5/req) 2.85×
Decode C=16 139.6 tok/s (8.8/req) 326.8 tok/s (20.5/req) 2.34×
Decode C=32 225.6 tok/s (7.0/req) 460.0 tok/s (14.3/req) 2.04×
Cold Prefill 8K (C=1) 3,090 tok/s 3,635 tok/s +17.6%
Cold Prefill 32K (C=1) 3,318 tok/s 3,742 tok/s +12.8%
Cold Prefill 64K (C=1) 3,309 tok/s 3,701 tok/s +11.9%
Cold Prefill 128K (C=1) 3,219 tok/s 3,583 tok/s +11.3%

Discord: D-Rock

Commit 810c084 passed a fresh `_weight_views_cache={}` on every call
to `prepared_moe_out` (and the sibling custom ops in `_binding_ops.py`)
so ephemeral CSF scale planes would not leak into the global
`_WEIGHT_CACHE`.

For immutable non-CSF NVFP4 experts whose per-rank intermediate width
is not 128-aligned (e.g. Qwen3.8-Flash-Next TP2 N=320, TP4 N=160), that
forced a cache miss on every invocation including CUDA graph capture,
recording `_split_gated_nvfp4_scale_views` (unswizzle, split, zero-pad,
and reswizzle of all 512 experts' FC1 scales) into every captured graph.
On 2x DGX Spark Qwen3.8-Flash-Next TP2, those ATen copy/fill kernels
accounted for 123.8 ms/step (80.6% of decode GPU time).

Fix:
- Attach the weight-views cache to the owning `B12XFP4ExpertWeights`
  instance when `scale_buffers` is empty (immutable scales shared across
  all capacity variants), while continuing to pass a fresh `{}` when
  ephemeral caller-owned CSF `scale_buffers` are supplied.
- Clear `experts._weight_views_cache` in `release_weight_views`.
- Reject lazy `_ensure_dynamic_nvfp4_tail_views` materialization during
  `torch.cuda.is_current_stream_capturing()` so tail views can never
  silently allocate in a graph pool or capture scale-repacking work.
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