Skip to content

[VR] Add NVFP4 token staging for MegaMoE - #132

Open
andrewbriand wants to merge 1 commit into
sgl-project:devfrom
andrewbriand:abriand_muse_spark_k128_nvlink_staging
Open

andrewbriand wants to merge 1 commit into
sgl-project:devfrom
andrewbriand:abriand_muse_spark_k128_nvlink_staging

Conversation

@andrewbriand

Copy link
Copy Markdown

Pipeline NVFP4 activation pulls through shared-memory stages to improve performance at higher token counts on VR.

Extend the existing SGL MoE benchmark and NVFP4 regression tests for staging, localization, and post-expert normalization.

Benchmark

Vera Rubin @ EP4

unset DG_USE_NVIDIA_TOOLS
export DG_PRINT_CONFIGS=0 OMP_NUM_THREADS=1 NCCL_NVLS_ENABLE=1 NCCL_MNNVL_ENABLE=1 NCCL_CUMEM_ENABLE=1

(DG_MEGA_MOE_PULL_STAGES=0) \
python3 sgl_deep_gemm/tests/test_mega_moe_sgl.py --num-processes 4 \
  --mma-type nvfp4xnvfp4 --hidden 4096 --intermediate-hidden 4096 \
  --num-experts 256 --num-topk 8 --num-max-tokens-per-rank 8192 \
  --num-tokens <num-tokens-per-rank> --weight-layout trtllm \
  --activation swiglu --activation-clamp inf --normalize-topk-weights --x-scale 0.1 \
  --use-x-scales --post-expert-norm --post-expert-norm-weight-first --post-expert-norm-eps 1e-8 \
  (--use-localization)

Unlocalized

Total tokens Baseline (us) + NVLink staging (us) Performance improvement (%)
1K 213.15 210.63 1.20
2K 210.91 208.59 1.11
4K 244.65 231.32 5.76
8K 394.63 335.23 17.72
16K 677.84 517.56 30.97
32K 1261.00 1135.00 11.10

Localized

Total tokens Baseline (us) + NVLink staging (us) Performance improvement (%)
1K 168.60 162.41 3.81
2K 180.97 178.13 1.59
4K 233.09 208.50 11.79
8K 390.44 326.66 19.52
16K 672.29 515.94 30.30
32K 1257.00 1129.00 11.34

Test

set -euo pipefail
printf 'global tokens: '
python3 sgl_deep_gemm/tests/test_mega_moe_global_tokens.py --num-processes 4 | tail -n 1
printf 'outer scales: '
python3 sgl_deep_gemm/tests/test_mega_moe_nvfp4_alphas.py --num-processes 4 --check-staging | tail -n 1
for norm in 0 1; do
  opts=(); ((norm)) && opts=(--post-expert-norm)
  printf 'layouts, norm=%s: ' "$norm"
  python3 sgl_deep_gemm/tests/test_mega_moe_nvfp4_trtllm.py --num-processes 4 --check-staging \
    --num-tokens 65 --num-max-tokens-per-rank 256 --num-experts 4 --num-global-experts 16 --num-topk 4 \
    "${opts[@]}" | tail -n 1
done
for shared in 1 2; do
  for L in 0 1; do
    loc=(); ((L)) && loc=(--use-localization)
    printf 'shared=%s localized=%s: ' "$shared" "$L"
    python3 sgl_deep_gemm/tests/test_mega_moe_global_tokens.py --num-processes 4 \
      --num-max-tokens-per-rank 65 --num-tokens 65 --hidden 4096 --intermediate-hidden 4096 \
      --num-experts 16 --num-topk 4 --num-shared-experts "$shared" \
      --patterns balanced,uneven,all_idle,shared_only "${loc[@]}" | tail -n 1
  done
done
global tokens: OK
outer scales: OK
layouts, norm=0: OK
layouts, norm=1: OK
shared=1 localized=0: OK
shared=1 localized=1: OK
shared=2 localized=0: OK
shared=2 localized=1: OK

Pipeline NVFP4 activation pulls through shared-memory stages for native and TRT-LLM weight layouts, retaining the baseline fallback for unsupported pull shapes.

Extend the existing SGL MoE benchmark and NVFP4 regression tests for staging, localization, and post-expert normalization.
@zcnrex

zcnrex commented Nov 4, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the PR. We compared the staged TMA pull against the existing SIMT dispatch pull (#126), which today is gated to FP8 x FP4, by relaxing that gate for FP4 activations on top of this PR's head.

On VR, at this PR's benchmark shape (EP4, hidden/intermediate 4096, 256 experts, top-8, max 8192 tokens/rank), the SIMT pull was equal to or faster than staging in every cell we reran: NVFP4 (TRT-LLM layout, x-scales, post-expert norm) and MXFP4 at 256 / 512 / 2048 tokens per rank, 6 runs per arm, two separate launches. Against the baseline TMA pull, SIMT was 4-27% faster for NVFP4 at 1024+ tokens per rank, where staging was 10-23% faster. Staging reproduced your numbers in our runs.

Two more points:

  • Staging only engages when the ring covers the whole token pool, which the buffer sizing guarantees only at num_max_tokens_per_rank >= 6144. Decode-sized buffers fall back to the baseline pull. The SIMT pull works with ring wrap, and with a 256-token buffer it was 2-7% faster than the baseline.
  • On GB300 (SM103) the two were about equal (both ~3-4% over the baseline, near the noise floor), so this is a VR result.

#135 enables the SIMT pull for NVFP4/MXFP4 (default on CC 10.7, opt-in elsewhere) with a bitwise SIMT-vs-TMA test. Did you compare against the SIMT pull before adding staging? If there is a regime where staging wins that we did not cover (localization, other shapes), we'd like to know.

Two correctness notes if this lands anyway:

  1. retire_pull issues tma_store_arrive and tma_store_wait<0> + red_add_rel in two separate elect_one_sync() blocks. Bulk async-groups are per-thread, and elect.sync does not guarantee the same lane, so the wait can be a no-op: the token is published before its store lands and the slot is reused under an in-flight store. Merging the two blocks costs nothing, since the wait follows immediately.
  2. The new 16-byte TMA loads of input_topk_weights_buffer + (src_token_topk_idx & ~3) and input_x_scales_buffer + (src_token_idx & ~3) assume 16-byte-aligned bases, but the symmetric layout packs regions back to back without TMA alignment. The top-k weights base is 8 mod 16 when num_max_tokens_per_rank * num_topk is odd, and the x-scales base follows combine_sf, which with FP8 combine can be misaligned too. A guard in can_stage_pulls (or aligning those bases in the layout) would cover it.

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.

2 participants