perf(pytorch): optimize compact blocked FP8 MoE and route preparation - #4857
Open
grimoire wants to merge 13 commits into
Open
perf(pytorch): optimize compact blocked FP8 MoE and route preparation#4857grimoire wants to merge 13 commits into
grimoire wants to merge 13 commits into
Conversation
…-moe # Conflicts: # lmdeploy/pytorch/kernels/cuda/moe/blocked_fp8.py # tests/pytorch/kernel/test_fuse_moe_blocked_fp8.py # tests/pytorch/kernel/test_fused_moe.py
Contributor
There was a problem hiding this comment.
Pull request overview
This PR optimizes PyTorch CUDA MoE execution by reducing route-preparation overhead (including a Hopper single-CTA path) and improving compact blocked FP8 MoE dispatch decisions, while reorganizing MoE CUDA kernels under lmdeploy/pytorch/kernels/cuda/moe and updating downstream imports/tests accordingly.
Changes:
- Adds a shared Triton route-preparation implementation (single-CTA on Hopper for small workloads, parallel scalable path otherwise) while preserving
_get_sorted_idx*APIs. - Extends compact blocked FP8 MoE dispatch heuristics and adds an optional transposed MMA path for measured small-M / long-K gate-up tiles.
- Moves/organizes MoE kernels under
lmdeploy/pytorch/kernels/cuda/moe, updates backend wiring, and expands tests to cover new routing and config-selection behavior.
Reviewed changes
Copilot reviewed 21 out of 23 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| tests/pytorch/kernel/test_v4_fp4_grouped_gemm.py | Updates imports to new cuda.moe.* module layout for v4 FP4 grouped GEMM/SwiGLU quant tests. |
| tests/pytorch/kernel/test_moe_route.py | Updates noaux-TC routing import to the new MoE routing module. |
| tests/pytorch/kernel/test_fused_moe.py | Updates imports and adds extensive coverage for new sorted-index/compact-block route preparation policies and implementations. |
| tests/pytorch/kernel/test_fused_moe_static_fp8.py | Updates imports for static FP8 MoE path to new cuda.moe.* modules. |
| tests/pytorch/kernel/test_fuse_moe_v4_fp4.py | Updates v4 FP4 fused MoE imports to new module locations. |
| tests/pytorch/kernel/test_fuse_moe_blocked_fp8.py | Adds coverage for compact blocked FP8 gate config (incl. transposed MMA) and updates imports to new cuda.moe.* modules. |
| lmdeploy/pytorch/kernels/cuda/moe/w8a8.py | Fixes relative imports after move; keeps w8a8 MoE implementation under cuda/moe. |
| lmdeploy/pytorch/kernels/cuda/moe/v4_swiglu_quant.py | Updates imports to reference parent CUDA utilities after move under cuda/moe. |
| lmdeploy/pytorch/kernels/cuda/moe/v4_fp4.py | Updates imports to reference parent CUDA utilities after move under cuda/moe. |
| lmdeploy/pytorch/kernels/cuda/moe/v4_fp4_grouped_gemm.py | Updates imports to reference relocated v4 FP4 fused launcher and parent utilities. |
| lmdeploy/pytorch/kernels/cuda/moe/route_single_group.py | Introduces a Triton fused single-group top-k router specialized for the supported configuration. |
| lmdeploy/pytorch/kernels/cuda/moe/route_noaux_tc.py | Introduces Triton noaux-TC routing kernel with autotuning keyed by routing shape parameters. |
| lmdeploy/pytorch/kernels/cuda/moe/fused_moe.py | Implements optimized route preparation (single-CTA + parallel paths), reorganizes kernels, and updates compact route metadata building. |
| lmdeploy/pytorch/kernels/cuda/moe/ep.py | Updates import paths after module move under cuda/moe. |
| lmdeploy/pytorch/kernels/cuda/moe/ep_fp8.py | Updates import paths and EP gather import after module move under cuda/moe. |
| lmdeploy/pytorch/kernels/cuda/moe/blocked_fp8.py | Extends compact blocked FP8 selection logic and adds transposed MMA option for gate/up compact kernel. |
| lmdeploy/pytorch/kernels/cuda/moe/init.py | Adds package marker for the new cuda.moe module tree. |
| lmdeploy/pytorch/kernels/cuda/init.py | Re-exports fused_moe/fused_moe_w8a8 from new cuda.moe.* locations to preserve import surface. |
| lmdeploy/pytorch/backends/cuda/moe/v4_fp4.py | Updates backend to import v4 FP4 fused MoE from cuda.moe.*. |
| lmdeploy/pytorch/backends/cuda/moe/static_fp8.py | Updates backend to import static FP8 MoE from cuda.moe.w8a8. |
| lmdeploy/pytorch/backends/cuda/moe/default.py | Updates EP and renormalize imports to cuda.moe.*. |
| lmdeploy/pytorch/backends/cuda/moe/blocked_fp8.py | Updates backend imports to new compact blocked FP8 / EP-FP8 / renormalize module locations. |
| lmdeploy/pytorch/backends/cuda/moe_router.py | Switches router implementation to new cuda.moe.route_* routing kernels and retains gating logic for the specialized fused path. |
Suppressed comments (1)
lmdeploy/pytorch/kernels/cuda/moe/fused_moe.py:666
- _get_sorted_idx_blocks_parallel launches Triton kernels that assume
expert_offset/local_num_expertsdescribe a valid slice of[0, num_experts), and thatblock_m > 0. If a caller passes invalid values, the kernels can read/write out of bounds (e.g., in_route_prefix_kernelwhen loadingCounts + expert_offset + local_expert_offsets). Add explicit argument validation before launching kernels to fail fast with a clear error.
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
MoE execution has two avoidable overheads:
Changes
_get_sorted_idxand_get_sorted_idx_blocksAPIs.block_m <= 32;block_m >= 64.lmdeploy/pytorch/kernels/cuda/moeand update imports.No public operator API is changed.
Performance
Measured on one NVIDIA H200 inside CUDA graphs.
Route preparation with random top-k-8 routing:
Representative exact-checkpoint blocked FP8 results:
Checkpoint weights and FP8 scales are real; activations and random/hot routing inputs are synthetic.