Skip to content

[SM100] Mega MoE: optimize pre-dispatch with 1-group/thread NVFP4 mapping and PTX e2m1x2 - #159

Merged
zcnrex merged 2 commits into
sgl-project:devfrom
Denji-kk:perf/sm100-nvfp4-pre-dispatch
Nov 23, 2026
Merged

zcnrex merged 2 commits into
sgl-project:devfrom
Denji-kk:perf/sm100-nvfp4-pre-dispatch

Conversation

@Denji-kk

Copy link
Copy Markdown

mega_moe_pre_dispatch currently assigns two threads per 16-element NVFP4 group (kVecElems = 8), reduces the row-wise absmax through 32-float shared memory across warps, quantizes FP4 via a 7-branch scalar comparator (pre_dispatch_e2m1_encode), emits scalar byte scale stores, and fills padded topk slots one row per block.

This PR optimizes sm100_mega_moe_pre_dispatch.cuh while preserving exact bytewise output parity across FP8, MXFP4, and NVFP4:

  • 1-group/thread NVFP4 mapping (kVecElems = 16): when hidden % 512 == 0 and top_k <= hidden / 16 <= 1024, each thread loads one full 16-BF16 scale group via two uint4 loads (LDG.E.128), eliminating intra-group cross-lane shuffles and odd/even lane divergence and halving CTA thread count (896 -> 448 at H = 7168). Shapes outside that window keep the kVecElems = 8 fallback.
  • Hardware warp max reduction: use redux.sync.max.u32 across non-negative IEEE-754 float bit patterns within each warp before the cross-warp shared-memory reduction.
  • Native SM100+ PTX cvt.rn.satfinite.e2m1x2.f32 quantization: pack pairs of scaled float values directly into E2M1 nibbles on SM100+, followed by a 2-instruction branchless SWAR mask (packed &= ~((0x88888888u - (packed & 0x77777777u)) & 0x88888888u)) that canonicalizes -0 (0x8) to +0 (0x0) so outputs remain 100% byte-identical to deep_gemm's host reference.
  • Vectorized FP4 & UE4M3 scale writes + 4x pad-fill: write 16 FP4 nibbles per thread with a single uint64_t store, pack four adjacent lanes' UE4M3 scale bytes via __shfl_xor_sync + __byte_perm into one coalesced int32_t store, and process 4 padded rows per CTA using int4 / float4 stores when num_topk % 2 == 0 (% 4 == 0 for weights). expert_scales folding and PDL (cudaGridDependencySynchronize / cudaTriggerProgrammaticLaunchCompletion) are preserved unchanged.

Tests

Extended sgl_deep_gemm/tests/test_mega_moe_pre_dispatch.py to cover NVFP4 both on the kVecElems = 16 fast path (H = 1024, top_k = 8) and on the kVecElems = 8 fallback (H = 1280, top_k = 3), including buf_x_scales, expert_scales, pad-fill sentinels, and -0 -> +0 nibble canonicalization:

  PASS  [FP8] M=512 P=576 H=1024 K=8 G=32 — bytewise equal vs host helper + pad-fill correct
  PASS  [FP4] M=512 P=576 H=1024 K=8 G=32 — bytewise equal vs host helper + pad-fill correct
  PASS  [NVFP4] M=512 P=576 H=1024 K=8 G=16 — bytewise equal vs host helper + pad-fill correct
  PASS  [NVFP4] M=512 P=576 H=1280 K=3 G=16 — bytewise equal vs host helper + pad-fill correct
OK

Also ran the full Blackwell MegaMoE test suite (test_mega_moe_nvfp4_trtllm, test_mega_moe_nvfp4_alphas, test_mega_moe_l1_fp4_accuracy, test_mega_moe_l1_sentinel, test_mega_moe_situ, test_mega_moe_global_tokens) and verified the setup.py pybind build.

Microbenchmark & NCU

Standalone operator latency on 1x B300 (sm_103, median of 200 iterations after 30 warmup calls; M = num_tokens, P = max_tokens, H = hidden, K = top_k):

mma_type Shape (M, P, H, K) dev (us) This PR (us) Speedup Effective BW (dev -> PR)
nvfp4xnvfp4 M=128, P=4096, H=7168, K=4 14.50 14.30 1.01x 176.1 -> 178.4 GB/s
nvfp4xnvfp4 M=4096, P=4096, H=4096, K=8 33.82 16.13 2.10x 1,290.9 -> 2,707.3 GB/s
nvfp4xnvfp4 M=4096, P=4096, H=7168, K=8 54.91 22.37 2.45x 1,382.3 -> 3,393.6 GB/s
nvfp4xnvfp4 M=4096, P=8192, H=7168, K=4 54.72 22.32 2.45x 1,384.8 -> 3,395.0 GB/s
mxf4xmxf4 M=4096, P=4096, H=7168, K=8 44.22 25.28 1.75x 1,695.3 -> 2,965.7 GB/s

Nsight Compute (2026.3.0) hardware counters on 1x B300 for nvfp4xnvfp4 (M=4096, P=8192, H=7168, K=4):

Metric dev This PR Change
gpu__time_duration.sum 53.15 us 19.42 us 2.74x faster (-63.5%)
launch__grid_size x launch__block_size 4115 x 896 4106 x 448 -50% threads/CTA
smsp__inst_executed.sum 42,541,888 9,904,508 -76.7% (4.30x fewer insts)
dram__bytes_read.sum.per_second 1,107.5 GB/s 3,030.3 GB/s 2.74x
l1tex__t_sectors_pipe_lsu_mem_global_op_st.sum 591,872 538,624 -9.0%


__forceinline__ __device__ float pre_dispatch_warp_max_nonneg(float val) {
#if defined(__CUDA_ARCH__) && (__CUDA_ARCH__ >= 800)
const uint32_t mask = __activemask();

@zcnrex zcnrex Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

__activemask() returns the lanes that happen to be converged here, not the lanes meant to take part in the max. Both call sites reach this point uniformly today, so the result is correct. But if a future caller gets here under divergence, it would silently get a partial max.

Suggest passing the intended mask explicitly:

  • 16-elem path: 0xffffffff. The host gates it on hidden % 512 == 0, so warps are always full.
  • 8-elem path, where the last warp can be partial (e.g. hidden / 8 = 360):
    const uint32_t lanes = min(32u, blockDim.x - (threadIdx.x & ~31u));
    const uint32_t mask  = lanes == 32u ? 0xffffffffu : (1u << lanes) - 1u;

A constant mask also removes a little work: in the SASS, each __activemask() becomes a VOTE.ANY placed right before the CREDUX, so this drops two instructions per CTA.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 19e9177. Switched pre_dispatch_warp_max_nonneg to an explicit mask (0xffffffffu on the kVecElems == 16 path, and lanes-derived mask on kVecElems == 8). Also made the second-stage smem reduction stride by lanes on !kFullWarps so a partial tail warp (e.g. H=2880 -> 360 threads, num_warps=12, 8 lanes in warp 11) still folds smem[8..11], and added H=2880 test coverage.

const float2 p0, const float2 p1, const float2 p2, const float2 p3) {
uint32_t packed = 0;
#if defined(__CUDA_ARCH__) && (__CUDA_ARCH__ >= 1000)
asm volatile(

@zcnrex zcnrex Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some small cleanups for this helper and pre_dispatch_warp_max_nonneg:

  • Reuse math.cuh. It already has cvt_pack_f32_to_e2m1x2 and cvt_pack_f32x4_to_e2m1x4, which use the same cvt.rn.satfinite.e2m1x2.f32 pattern. An 8-wide variant with the -0 canonicalization would fit there better than as a file-local helper. Note that cast_into_e2m1x2_pairs can't be used as-is: it repeats its 2 bytes in the upper half of the return value.
  • Remove the dead fallbacks. The #else branches here (__CUDA_ARCH__ < 1000) and in pre_dispatch_warp_max_nonneg (< 800) can never be compiled for this SM100-only kernel. math.cuh already uses the e2m1x2 cvt without a guard. Removing them also leaves pre_dispatch_e2m1_encode unused, so that's a second encode path nobody has to keep in sync.

For reference, I compiled the PR with the JIT flags (-O3 --use_fast_math, sm_100f and sm_103f, nvcc 13.4). All variants use 28 registers with no spills, and the SASS shows native F2FP.SATFINITE.E2M1.F32.PACK_AB_MERGE_C and CREDUX, so the new fast path is the code that actually runs.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 19e9177. Moved the 8-wide E2M1 pack helper into math.cuh as math::cvt_pack_f32x8_to_e2m1x8<kCanonicalizeNegZero>, and removed the dead < sm_100 / < sm_80 fallback branches along with the unused pre_dispatch_e2m1_encode.

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