Repository navigation
Add SM100 NVFP4 MegaMoE with fused BF16 shared experts - #409
Conversation
d69d22b to
cb1f99d
Compare
26478dc to
e425dec
Compare
e425dec to
2deb26a
Compare
| # Returns `((packed_weights, mn_major_packed_sf), dequantized_weights)` | ||
| num_groups, n, k = bf16_weights.shape | ||
| w = torch.empty((num_groups, n, k // 2), device='cuda', dtype=torch.int8) | ||
| w_sf = torch.empty((num_groups, n, k // 64), device='cuda', dtype=torch.int32) |
There was a problem hiding this comment.
🔵 suggestion: _quantize_weights_to_nvfp4 loops per expert group in Python and calls cast_back_from_fp4 per group. Fine for correctness tests, but consider vectorizing across the group dimension if suite runtime becomes an issue for large expert counts.
🤖 v5
There was a problem hiding this comment.
Leaving this unchanged for now: it is correctness-test-only setup outside the measured region, and the current suite runtime is acceptable. We can vectorize it later if it becomes a material test-runtime bottleneck.
🤖 ds-review-bot Code Reviewv6新增 FP4 API 未验证对称缓冲区布局,使用默认缓冲区配置时可能通过大小检查却读取错误偏移,导致静默错误输出。 v5This MR adds an SM100 NVFP4 x NVFP4 MegaMoE kernel on nv_dev, with an optional fused BF16 shared expert whose output is added after routed scaling, while preserving ModelOpt NVFP4 scale semantics (per-16-element E4M3 block SF, per-expert weight_scale_2 alphas, and the down-proj input_scale folded into the L2 alpha) and reusing the existing MegaMoE scheduler and public API. The change is well layered: a new device kernel (deep_gemm/include/deep_gemm/impls/sm100_fp4_fp4_mega_moe.cuh, ~1.8K lines), a JIT wrapper (csrc/jit_kernels/impls/sm100_fp4_fp4_mega_moe.hpp), a validated host API with pybind registration (csrc/apis/mega.hpp), Python bindings and exports (deep_gemm/mega/init.py, deep_gemm/init.py), NVFP4-aware heuristics/scheduler/layout extensions, and a thorough reference test (tests/test_nvfp4_mega_moe.py) whose _quantize_to_fp4_e2m1_rne reproduces cvt.rn.satfinite.e2m1x2.f32 round-to-nearest-even exactly. Verified during review: the pybind signature (21 args) matches the Python call site and fp4_fp4_mega_moe is exported; host-side validation is strict (SF layout, shared-triple co-presence enforced on both the Python and C++ sides, alpha/a2-scale shapes, finiteness of routed_scaling_factor); the combine epilogue preserves the serial pipeline's BF16 -> scale -> BF16 ordering before the shared add in both kHasShared branches; SHARED_BLOCK_K in the scheduler is a clean backward-compatible relaxation for the BF16 shared expert's K tiling; and get_byte_addressable_element_size() correctly guards all byte-based SM90/SM100/SM120 heuristic call sites from sub-byte NVFP4 (no remaining unguarded get_element_size() uses outside the bit-aware MegaMoE helper). Reported validation (boundary/fuzz/shared-only accuracy, GLM layer replay, deterministic E2E, 4-rank correctness) and the ~10-20% fusion gains are consistent with the implementation. Overall the MR is in good shape and approvable; the comments below are non-blocking suggestions. v4p本 MR 为 SM100 新增 NVFP4 x NVFP4 MegaMoE 实现,并在核内融合 BF16 shared expert:路由输出在 combine 阶段先做 routed scaling,再与 shared 输出相加;同时保持 ModelOpt NVFP4 scale 与既有 MegaMoE scheduler/API。整体实现与现有 FP8/BF16 MegaMoE 模式一致,主机侧 buffer/启发式布局、设备侧 TMA/MMA/epilogue/combine 路径以及 Python 封装均同步完整,未发现明确的正确性问题。 Files reviewed: 15 📍 未定位到 diff 的评论🔵 suggestion |
| # Returns `((packed_weights, mn_major_packed_sf), dequantized_weights)` | ||
| num_groups, n, k = bf16_weights.shape | ||
| w = torch.empty((num_groups, n, k // 2), device='cuda', dtype=torch.int8) | ||
| w_sf = torch.empty((num_groups, n, k // 64), device='cuda', dtype=torch.int32) |
There was a problem hiding this comment.
🔵 suggestion: Looping per expert group and calling cast_back_from_fp4 per group is fine for correctness tests, but could be vectorized across the group dimension if suite runtime becomes an issue for large expert counts.
🤖 v5
🤖 ds-review-bot Code Reviewv6未发现新的非重复问题,但先前指出的 FP4 API 未校验对称缓冲区布局问题仍然存在,传入默认 FP8 布局缓冲区时可能静默产生错误结果。 v5The NVFP4 x NVFP4 MegaMoE implementation is well layered and the fused BF16 shared-expert design (post-L2 top-k weighting, routed scaling before the shared add, ModelOpt scale folding l2_scale = l2_alpha * a2_scale) matches the description and is consistent across the device kernel, JIT wrapper, host API, Python bindings, and test. However, the current HEAD is the same revision that the v6 review targeted, and none of the v6 comments have been addressed yet. One issue is blocking: fp4_fp4_mega_moe does not validate the symmetric-buffer layout, so a default fp8xfp4-layout buffer passes the byte-size check but the kernel reads wrong offsets (routing-weight sidecar shifts the input region), silently producing incorrect results. v4p本 MR 为 SM100 新增 NVFP4 x NVFP4 MegaMoE 内核,并在同一持久化内核中融合 BF16 shared expert:routed 输出在 combine 阶段先按 routed_scaling_factor 缩放,再与 shared-expert 输出相加,同时保持既有 BF16 / FP8xFP4 MegaMoE 路径不受影响。整体实现分层清晰,主机侧校验、布局/启发式/调度器扩展以及测试覆盖均较完整,本次复审未发现新的明确正确性缺陷。需要注意的是,讨论中已提出的关键问题在当前 diff 中仍未修复:fp4_fp4_mega_moe 未保存/校验 SymmBuffer 的 mma_type 或布局,使用默认 fp8xfp4 缓冲区时可通过 C++ 字节数检查,但 FP4 布局在 input 前额外放置了 routing-weight sidecar,内核会按错误偏移读取缓冲区,导致静默错误结果;建议合入前在 Python 层保存 mma_type 并校验,或至少检查 sym_buffer.x 的 dtype 与形状。 Files reviewed: 15 📍 未定位到 diff 的评论🔵 suggestion |
|
Follow-up on the shared-slot ordering suggestion: addressed structurally in 814d9bc and clarified in 0c067ed. The shared producer and combine consumer both use kSharedCombineSlot, defined as the slot immediately after routed top-k. The combine path separately asserts that routed plus shared slots fit in the 32-bit ballot, and the reference test verifies BF16 routed rounding, routed scaling, then shared addition. The previous equality static assert was tautological and has been removed. |
Signed-off-by: zhou9402 <[email protected]>
|
Addressed the symmetric-buffer layout concern in 4d789d4. The layout metadata guard was originally added in 814d9bc; this follow-up makes the MMA type, activation, shared-tensor set, and shared-expert-count checks explicit |
Summary
Design highlights
Native NVFP4 routed path. Both routed activations and weights use packed E2M1 data with per-16-element E4M3 scale factors. This requires a bit-aware symmetric-buffer layout, NVFP4-specific TMA/UTCCP handling, and an in-kernel L1 requantization epilogue.
Post-L2 top-k weighting. Top-k routing weights are stored in a new NVFP4-specific buffer and applied to the BF16 routed output after L2, immediately before the combine write. Applying top-k weights directly in the L1 epilogue can reduce the intermediate magnitude before NVFP4 quantization and introduce additional precision loss.
ModelOpt scale handling. Per-expert gate/up scales are applied before SwiGLU. The down-projection input scale normalizes the in-kernel NVFP4 intermediate quantization and is folded back into the L2 output scale:
l2_scale = l2_alpha * a2_scale.Correct routed/shared ordering. The kernel accumulates the routed top-k outputs, rounds the routed sum to BF16, applies
routed_scaling_factor, and then adds the BF16 shared-expert output.BF16 shared expert inside MegaMoE. Optional BF16 shared L1 and L2 work is inserted into the existing scheduler as
SharedLinear1andSharedLinear2phases. These tasks reuse the existing TMA-load, MMA, and epilogue warp roles; shared computation uses ordinary BF16 UMMA without scale factors or UTCCP.Separate shared K tiling. Routed NVFP4 and shared BF16 tasks reuse the same shared-memory byte tiles but contain different numbers of elements, so
SHARED_BLOCK_Kdescribes the BF16 K extent independently from the routed NVFP4 K extent.Fused shared epilogues. Shared L1 applies SwiGLU and stores a BF16 intermediate. Shared L2 writes to an additional local combine slot, allowing the final combine phase to perform routed scaling and the shared add without separate shared-expert launches.
Compatibility. The new NVFP4 path does not affect the existing BF16 or FP8 x FP4 MegaMoE paths.
Validation
Performance
Pure CUDA-kernel time on 8x GB300 with EP8. All three arms have the same BF16-input to BF16-output model boundary and include routed computation, dispatch/combine, routed scaling, the BF16 shared expert, and the final add.
sm100_fp4_fp4_mega_moe-> routed scaling -> serial DeepGEMM BF16 shared expert -> add.sm100_fp4_fp4_mega_moekernel containing the routed path, routed scaling, BF16 shared expert, and final add.Fusion gaincompares the two DeepGEMM arms.Fused vs FIcompares against the modular, non-MegaMoE FlashInfer baseline.Complete test recipe
Revisions and environment
2.13.0+cu130, vLLM0.17.2rc1.dev4473+g3ada0d7f1, FlashInfer0.6.16rc5Build and correctness
The boundary, zero-token, masked-routing, and shared-only cases use the same command with the corresponding
--num-tokens,--zero-token-rank,--masked-ratio,--routing-hot-rank, and--num-shared-expertssettings.Performance shape and inputs
61442048256; top-k:8; BF16 shared experts:10.751, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 163841651220260811Timed call graphs
Timing
deep_gemm.testing.bench_kinetoand reports selected CUDA-kernel time only.wait=0, warmup=1, active=1with 30 calls in the measured phase.