Repository navigation
fix and add cache hint - #81
Merged
Merged
Conversation
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.
Two independent changes to the SM100 mega-MoE path.
1.
use_x_scalesbecomes opt-infp8_fp4_mega_moepreviously asserteduse_x_scales == (mma_kind == NVFP4), i.e. everyNVFP4 call was forced to supply per-token FP32 outer scales, and the Python wrapper
derived the flag from the buffer type. That makes the common NVFP4 case impossible to
express — the one where the outer scale is already folded into the weight SFs — and it
forces every default NVFP4 call onto a path that reads the
x_scalesregion whether ornot the caller ever wrote it.
Now the flag defaults to
Falseand is only permitted fornvfp4xnvfp4:csrc/apis/mega.hpp:DG_HOST_ASSERT(not use_x_scales or mma_kind == MmaKind::NVFP4)deep_gemm/mega/__init__.py:use_x_scales: bool = False, raises only when the flag isset on a non-NVFP4 buffer
test_mega_moe_nvfp4_alphas.pypassesuse_x_scales=Trueexplicitly, so theper-token-scale path stays covered
That silent-uninitialized-read is not hypothetical.
moe_ep_benchmark's NVFP4 megabackend fills
x,x_sf,topk_idxandtopk_weightsand never touchesx_scales, sounder the old default it multiplied every activation by a zero scale: the L1 intermediate
came out all-zero and fc2 ran on zero-valued FP4. That is measurably faster on a
power-limited part (~937 vs ~985 us at 4096 tok/rank), so the old default was quietly
inflating prefill numbers. Filling
x_scaleswith 1.0 reproduces the ~985 us of theFalsepath, confirming the gap is the degenerate data and not this change.2. L2 cache hint on the activation TMA loads
tma::copygrows an optionalcache_hintargument (defaultEVICT_NORMAL, so everyother caller is byte-identical). The mainloop activation load passes:
Rationale: one activation tile is re-read by every N block of the same token block
(80 N blocks at the shape below), while the weight stream is read once per
(expert, N block). Marking the activations
EVICT_LASTkeeps the reused operand residentagainst the weight stream. The
BLOCK_M <= 128gate restricts this to decode block sizes— see the rejected variants for why prefill is excluded.
Benchmark
bench_moe_ep_mega.py --mega-backend deep_gemm_nvfp4_mega,MEGA_TIMING=e2e_pipelined(GPU time, back-to-back enqueued — excludes host launch overhead), 8x B300, EP8,
h7168 / i5120 / 384 experts / top-4, 50 iters after 20 warmup, p50 in us.
A/B is interleaved off/on/off/on in a single session between two copies of this branch
that differ only in the hint, both with their own freshly built
_C.so. Null A/B on twoidentical trees measured 512.8 vs 512.8 us, i.e. a sub-1% noise floor.
The win is confined to the larger decode block sizes (BLOCK_M 96/128), where the
activation tile is big enough and re-read often enough for L2 residency to matter. At
BLOCK_M 16/32 it is flat: the working set is small and the fixed dispatch/combine/barrier
cost dominates. An earlier sweep over 1/2/4/8/16/32/64/128 tok/rank was flat at every
point, which is why only a subset is listed here.
Variants measured and rejected
All at the same shape, same protocol.
EVICT_LAST+ weightsEVICT_FIRSTEVICT_LAST, ungatedEVICT_FIRST, prefill only (BLOCK_M > 128)yTMA storeEVICT_FIRSTEVICT_LAST, gatedBLOCK_M <= 128(this PR)Demoting the weight stream is clearly wrong, and gating it to prefill does not rescue it:
at prefill many m-blocks of one expert are in flight across SMs at once and all re-fetch
the same
(n_block, k)weight tiles, soEVICT_FIRSTthrows away exactly those hits.The activation hint alone still costs 1.5% at prefill, hence the block-size gate.
Demoting the final
ystore — the write-once output, which is the object the analogousGEMM-kernel optimization targets — does nothing here, because that store happens in the
post-NVLink-barrier combine phase after all GEMM work is done, so there is no live
operand left in L2 for it to protect.
Correctness
test_mega_moe_nvfp4_alphas.py --num-processes 2passes, rel-L2 0.18253 — bit-for-bitthe same value as before the change, as expected for a pure cache-policy hint.