Skip to content

fix and add cache hint - #81

Merged
zcnrex merged 1 commit into
sgl-project:devfrom
zcnrex:megamoe-nvfp4-fix
Aug 31, 2026
Merged

zcnrex merged 1 commit into
sgl-project:devfrom
zcnrex:megamoe-nvfp4-fix

Conversation

@zcnrex

@zcnrex zcnrex commented Aug 31, 2026 •

Copy link
Copy Markdown
Collaborator

Two independent changes to the SM100 mega-MoE path.

1. use_x_scales becomes opt-in

fp8_fp4_mega_moe previously asserted use_x_scales == (mma_kind == NVFP4), i.e. every
NVFP4 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_scales region whether or
not the caller ever wrote it.

Now the flag defaults to False and is only permitted for nvfp4xnvfp4:

  • 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 is
    set on a non-NVFP4 buffer
  • test_mega_moe_nvfp4_alphas.py passes use_x_scales=True explicitly, so the
    per-token-scale path stays covered

That silent-uninitialized-read is not hypothetical. moe_ep_benchmark's NVFP4 mega
backend fills x, x_sf, topk_idx and topk_weights and never touches x_scales, so
under 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_scales with 1.0 reproduces the ~985 us of the
False path, confirming the gap is the degenerate data and not this change.

2. L2 cache hint on the activation TMA loads

tma::copy grows an optional cache_hint argument (default EVICT_NORMAL, so every
other caller is byte-identical). The mainloop activation load passes:

constexpr uint64_t kActsCacheHint = static_cast<uint64_t>(BLOCK_M <= 128 ?
    cute::TMA::CacheHintSm100::EVICT_LAST : cute::TMA::CacheHintSm100::EVICT_NORMAL);

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_LAST keeps the reused operand resident
against the weight stream. The BLOCK_M <= 128 gate 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 two
identical trees measured 512.8 vs 512.8 us, i.e. a sub-1% noise floor.

tok/rank BLOCK_M hint off hint on delta
1 16 107.6 / 107.6 107.6 / 107.7 —
8 16 310.4 / 310.4 310.2 / 310.3 —
32 16 476.2 / 476.2 476.1 / 476.2 —
128 32 492.3 / 492.3 491.5 / 491.8 +0.13%
512 96 510.9 / 510.9 508.8 / 508.1 +0.47%
1024 128 530.4 / 530.3 527.2 / 528.5 +0.47%
4096 192 985.8 unchanged (hint gated off) —

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.

variant 512 tok 2048 tok 4096 tok
activations EVICT_LAST + weights EVICT_FIRST -0.7% — -13%
activations EVICT_LAST, ungated +0.5% — -1.5%
weights EVICT_FIRST, prefill only (BLOCK_M > 128) — -2.2% -11.2%
final y TMA store EVICT_FIRST -0.2% — 0%
activations EVICT_LAST, gated BLOCK_M <= 128 (this PR) +0.47% unchanged unchanged

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, so EVICT_FIRST throws away exactly those hits.
The activation hint alone still costs 1.5% at prefill, hence the block-size gate.

Demoting the final y store — the write-once output, which is the object the analogous
GEMM-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 2 passes, rel-L2 0.18253 — bit-for-bit
the same value as before the change, as expected for a pure cache-policy hint.

@zcnrex
zcnrex merged commit ef19220 into sgl-project:dev Aug 31, 2026
Fridge003 pushed a commit that referenced this pull request Aug 31, 2026
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