Repository navigation
[SM100] Mega MoE: optimize pre-dispatch with 1-group/thread NVFP4 mapping and PTX e2m1x2 - #159
Conversation
…ping and PTX e2m1x2
|
|
||
| __forceinline__ __device__ float pre_dispatch_warp_max_nonneg(float val) { | ||
| #if defined(__CUDA_ARCH__) && (__CUDA_ARCH__ >= 800) | ||
| const uint32_t mask = __activemask(); |
There was a problem hiding this comment.
__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 onhidden % 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.
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
Some small cleanups for this helper and pre_dispatch_warp_max_nonneg:
- Reuse
math.cuh. It already hascvt_pack_f32_to_e2m1x2andcvt_pack_f32x4_to_e2m1x4, which use the samecvt.rn.satfinite.e2m1x2.f32pattern. An 8-wide variant with the -0 canonicalization would fit there better than as a file-local helper. Note thatcast_into_e2m1x2_pairscan't be used as-is: it repeats its 2 bytes in the upper half of the return value. - Remove the dead fallbacks. The
#elsebranches here (__CUDA_ARCH__ < 1000) and inpre_dispatch_warp_max_nonneg(< 800) can never be compiled for this SM100-only kernel.math.cuhalready uses the e2m1x2 cvt without a guard. Removing them also leavespre_dispatch_e2m1_encodeunused, 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.
There was a problem hiding this comment.
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.
mega_moe_pre_dispatchcurrently 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 paddedtopkslots one row per block.This PR optimizes
sm100_mega_moe_pre_dispatch.cuhwhile preserving exact bytewise output parity across FP8, MXFP4, and NVFP4:kVecElems = 16): whenhidden % 512 == 0andtop_k <= hidden / 16 <= 1024, each thread loads one full 16-BF16 scale group via twouint4loads (LDG.E.128), eliminating intra-group cross-lane shuffles and odd/even lane divergence and halving CTA thread count (896 -> 448atH = 7168). Shapes outside that window keep thekVecElems = 8fallback.redux.sync.max.u32across non-negative IEEE-754 float bit patterns within each warp before the cross-warp shared-memory reduction.cvt.rn.satfinite.e2m1x2.f32quantization: pack pairs of scaledfloatvalues directly into E2M1 nibbles onSM100+, 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 todeep_gemm's host reference.uint64_tstore, pack four adjacent lanes'UE4M3scale bytes via__shfl_xor_sync+__byte_perminto one coalescedint32_tstore, and process 4 padded rows per CTA usingint4/float4stores whennum_topk % 2 == 0(% 4 == 0for weights).expert_scalesfolding and PDL (cudaGridDependencySynchronize/cudaTriggerProgrammaticLaunchCompletion) are preserved unchanged.Tests
Extended
sgl_deep_gemm/tests/test_mega_moe_pre_dispatch.pyto cover NVFP4 both on thekVecElems = 16fast path (H = 1024, top_k = 8) and on thekVecElems = 8fallback (H = 1280, top_k = 3), includingbuf_x_scales,expert_scales, pad-fill sentinels, and-0 -> +0nibble canonicalization: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 thesetup.pypybind 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_typeM, P, H, K)dev(us)dev-> PR)nvfp4xnvfp4M=128, P=4096, H=7168, K=4nvfp4xnvfp4M=4096, P=4096, H=4096, K=8nvfp4xnvfp4M=4096, P=4096, H=7168, K=8nvfp4xnvfp4M=4096, P=8192, H=7168, K=4mxf4xmxf4M=4096, P=4096, H=7168, K=8Nsight Compute (
2026.3.0) hardware counters on 1x B300 fornvfp4xnvfp4(M=4096, P=8192, H=7168, K=4):devgpu__time_duration.sumlaunch__grid_sizexlaunch__block_size4115 x 8964106 x 448smsp__inst_executed.sumdram__bytes_read.sum.per_secondl1tex__t_sectors_pipe_lsu_mem_global_op_st.sum