perf(minimax-h3): optimize Ulysses attention and DiT kernels - #37
Open
L17807593 wants to merge 2 commits into
Open
perf(minimax-h3): optimize Ulysses attention and DiT kernels#37L17807593 wants to merge 2 commits into
L17807593 wants to merge 2 commits into
Conversation
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.
TeleFuser MiniMax-H3: Five Lossless Optimizations
1. Summary
Under a four-GPU setup with five prompt/seed pairs, the five lossless optimizations reduce the average wall time from 78.546 s to 75.766 s, achieving a 3.539% average speedup. The generated outputs remain bit-exact with the baseline.
2. Optimizations
2.1 Overlap Ulysses communication with FlashAttention 4
Added
AttentionConfig.attention_chunks. The generic default remains 1 to preserve existing behavior; the H100 example uses 2.MiniMax-H3 attention splits local heads into two chunks. Each chunk enters FA4 immediately after the fused scatter, while its gather is submitted asynchronously, allowing computation for the next chunk to overlap with communication for the previous chunk.
New interfaces include:
ulysses_scatter_qkv_qknorm_rope_chunk_asynculysses_gather_heads_chunk_asyncThe new path is enabled only when Ulysses, FA4, CUDA BF16, and the supported tensor layout are all available. Otherwise, execution falls back to the original attention path.
2.2 Fuse Q/K RMSNorm, RoPE, and Ulysses Pack
Added the Triton kernel
pack_qkv_qknorm_rope_destination_major, which performs the following in a single kernel:This reduces kernel launches, intermediate layout conversions, and temporary tensors. Guards on dtype, device, weights, RoPE cache, head topology, and packed shape ensure that unsupported cases fall back to the original implementation.
2.3 Add fixed-valid and valid-only attention modes
FA4 packed attention now supports:
fixed_validpad_fixed_valid_outputFor MiniMax-H3, valid video tokens are at the front of the packed tensor, while the tail contains alignment padding or auxiliary short sequences. The fixed-valid path runs FA4 only on the valid sequence with a fixed shape. The valid-only mode does not restore the invalid tail after attention, reducing attention and subsequent communication work.
The generic defaults remain:
Therefore, existing call sites retain their original behavior.
2.4 Fuse valid-only gather with zero-tail merge
Added the Triton kernel
merge_ulysses_head_chunk. It writes each received All-to-All head chunk directly into the final destination tensor and clears the invalid tail in the same kernel, avoiding separate relayout, copy, and zero-tail operations.2.5 Fuse RMSNorm with indexed AdaLN modulation
Added the Triton kernel
indexed_rmsnorm_scale_shift_bf16, which fuses the following sequence in DiT blocks and the final layer:The implementation preserves the original BF16 store/load rounding boundaries. CUDA unit tests verify element-wise bit-exact equivalence with the previous path. The fused kernel is enabled only for eager CUDA BF16, contiguous tensors, and valid shapes; other cases fall back to the original
_modulate(norm(hidden), ...)implementation.3. Per-case performance
4. Quality verification
All quality comparisons use the baseline generated with the same prompt and seed, comparing the final video and audio MP4 outputs.
Additional hash check:
baseline == lossless: 5/5.The five optimizations are therefore bit-exact with the baseline at the final video and audio file level, while providing a 3.539% average wall-time speedup.