[PyTorch] Fuse MoE chunk sorting and padding - #3509
Conversation
Signed-off-by: ryan.u <ryan.u@kakaocorp.com>
Signed-off-by: ryan.u <ryan.u@kakaocorp.com>
|
| # | ||
| # See LICENSE for license information. | ||
|
|
||
| """Output, gradient and graph coverage for fused chunk sorting and padding.""" |
There was a problem hiding this comment.
The new output, gradient, compilation, and CUDA graph tests are not included in the explicitly enumerated L0 PyTorch test runner. As a result, the main CI gate will not execute this dedicated test suite, allowing future regressions in this feature to go unnoticed. Please add test_chunk_padding.py to qa/L0_pytorch_unittest/test.sh.
Knowledge Base Used: Verification and CI matrix
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
There was a problem hiding this comment.
Added test_chunk_padding.py to the L0 PyTorch runner in 856167e, alongside the existing permutation tests. Shell syntax validation and all pre-commit checks pass.
Signed-off-by: ryan.u <ryan.u@kakaocorp.com>
Description
Sorting dispatched MoE tokens into expert order and padding them for grouped GEMM currently requires separate copies. Add a fused sort/pad operation that avoids the sorted, unpadded intermediate and copies tokens and probabilities together. A matching restore operation removes padding and restores the original order in one copy.
Chunk order and padded sizes are explicit, so callers can group rank chunks by expert and choose an alignment without depending on a GEMM backend. Routing metadata stays on the GPU; an explicit output size supports CUDA graph replay with changing routing at fixed buffer sizes. Token counts are runtime kernel arguments, allowing compiled kernels to be reused when routing changes the number of tokens.
Type of change
Changes
moe_sort_chunks_and_padandmoe_unpad_and_restore_chunksfor FP32, FP16 and BF16 tensors, with optional probabilities, autograd andtorch.compilesupport.Validation
On one B200, with BF16 tokens, FP32 probabilities, hidden size 2048, eight rank chunks per expert, 16 experts and alignment 128:
These are layout roundtrip measurements, excluding GEMM and communication. Each entry is the median of two run medians in A-B-B-A order, with 50 CUDA-event samples after warmup. Memory is incremental peak PyTorch allocation, not total GPU memory. The baseline uses existing TE chunk sorting and
Fp8Padding/Fp8Unpadding. Both paths run on a full source build of this branch (TE 2.20.0.dev0), with PyTorch 2.11.0a0 / CUDA 13.1 / cuDNN 9.20.A separate cold-cache probe over 25 routed token counts uses one map kernel and two copy kernels, compared with 25 and 50 variants when token counts are specialized.
All 63 new GPU tests and 12 selected existing permutation tests pass against the freshly built package and extension. Coverage includes exact outputs and gradients, empty chunks, mixed probability dtypes, and changed-routing graph replay with and without compilation. The build disables optional NCCL-EP. Test runs emit dependency deprecations and a pytest cache-path warning. Repository-wide pre-commit, production Python lint and license checks pass.
Checklist: