Repository navigation
Conversation
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.
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.
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 inflashinfer/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 asnvidia/Qwen3.8-Flash-Next-FP4-Localat TP2n=320or TP4n=160), passing{}on every call forces_get_weight_viewsto 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'w13scales) directly into every replay graph.Node-level
nsysprofiling ofQwen3.8-Flash-NextTP2 (MTP=0,C=1, 96 decode steps on 2× DGX Spark / GB10) showed those 5 ATen copy/fill kernels consuming123.8 ms/step(80.6%of GPU time) vs~29 ms/stepof actual model work.Fix
_binding_ops.py: Add_weight_views_cache(state, scale_buffers):scale_buffersis non-empty (ephemeral caller-owned CSF scale planes), return a fresh{}(unchanged behavior).scale_buffersis empty (immutable weights), reuse a single_weight_views_cachedict owned byexperts._impl(B12XFP4ExpertWeights), shared across all capacity variants in_FusedMoeCapacityStatewithout leaking into global_WEIGHT_CACHE.experts._weight_views_cacheinrelease_weight_views(experts)._impl.py: Guard_ensure_dynamic_nvfp4_tail_viewsto raiseRuntimeErrorif uncached split/padded tail views are requested whiletorch.cuda.is_current_stream_capturing()isTrue, preventing silent graph-pool allocations or captured scale repacking.tests/experimental/b12x/moe/test_scale_storage_lifetime.py: Add unit tests covering per-experts cache reuse/eviction, ephemeralscale_buffersisolation, capture-time rejection, and an end-to-end GPU CUDA-graph regression test forprepared_moe_outwithN=320non-CSF NVFP4.Verification
origin/maine34540cc):test_scale_storage_lifetime.py: 7/7 passed (includingtest_prepared_moe_out_reuses_nvfp4_tail_views_under_cuda_graph, verifyingsplit_calls == 1across eager warmup + CUDA graph capture, matching replay output, and cleanweakrefrelease onexperts.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 failuretest_a16_expanded_scales_reuse_the_planned_launch_for_live_countsreproduces identically on unpatchede34540cc).Qwen3.8-Flash-NextTP2 on 2× DGX Spark (SM121,--roce-ring,--embedding-table-storage ram,MTP-5,flareec957867/hyperflare-recipes89e52f1/flashinfer6ce11d2e+ this patch):RuntimeErrorguard active (6,821kernels vs7,020before); sanity 4/4 PASS (think=True,think=False,tool_call,32K needle).Qwen3.8-Flash-NextTP2 Spark)C=1(ctx=0, 256 out)12.5 tok/s61.1 tok/sC=446.5 tok/s(11.5/req)152.0 tok/s(38.1/req)C=880.6 tok/s(10.0/req)229.5 tok/s(28.5/req)C=16139.6 tok/s(8.8/req)326.8 tok/s(20.5/req)C=32225.6 tok/s(7.0/req)460.0 tok/s(14.3/req)8K(C=1)3,090 tok/s3,635 tok/s32K(C=1)3,318 tok/s3,742 tok/s64K(C=1)3,309 tok/s3,701 tok/s128K(C=1)3,219 tok/s3,583 tok/sDiscord: D-Rock