FlashMask-3: Efficient and Expressive Mask-Aware Distributed Attention
Organizations: Baidu Inc.
Abstract
Long-context training of large language and multimodal models couples structured attention masks (document, prefix-LM, share-question, sliding-window, block-sparse) with multi-node context parallelism, making the mask a dominant bottleneck. FlashMask's column-wise sparse mask compresses them from to via four query-axis boundary vectors, covering broad structured patterns. Implemented for Ampere GPUs, it neither exploits FlashAttention-3's TMA/WGMMA warp-specialized pipeline on Hopper nor supports distribution. The column-wise sparse mask is indexed by key positions but stores query-axis interval boundaries. Under any multi-chunk context-parallel assignment, it reduces per-rank mask localization to a provably zero-communication clip--shift--max on the boundary vectors, compatible with the single-GPU kernel. FlashMask-3 introduces a mask-aware Hopper kernel with three-role warp specialization atop FlashAttention-3's TMA/WGMMA pipeline, plus a mask-aware distributed extension: zero-communication mask localization, a sparsity-aware load balancer (HCC-IPO), and a mask-aware compute-communication overlap with mask-driven communication pruning, whose forward all-gather uses topology-aware hierarchical routing. On a single H100 across 12 masks at 4K--128K context, it improves achieved TFLOPs over FlashMask, FlexAttention, and MagiAttention by 45--141%, 34--71%, and 2--44%. At context-parallel degrees 4--32 with 8K sequence per device, it attains 31--75% compute-communication overlap efficiency and improves throughput over Megatron-Core CP ring, Megatron-Core CP all-gather, and MagiAttention by --, --, and --. In end-to-end training of a 118B-A14B MoE at 128K on 128 H100 GPUs, it raises throughput by 7.3--11.0% over a Dual Chunk all-gather baseline, with attention forward+backward 24.5--44.9% faster.
Figures & tables
| 70% sparsity | 80% sparsity | 86% sparsity | ||||
|---|---|---|---|---|---|---|
| Configuration | tokens/s/GPU | tokens/s/GPU | tokens/s/GPU | |||
| Baseline (Dual Chunk + all-gather) | 712.6 ± 8.7 | – | 753.3 ± 34.7 | – | 812.4 ± 57.1 | – |
| + HCC-IPO balancing | 732.2 ± 24.5 | +10.4% | 786.2 ± 65.2 | +19.1% | 858.5 ± 62.1 | +10.5% |
| + mask-aware overlap | 750.8 ± 6.4 | +15.9% | 785.0 ± 33.6 | +13.9% | 860.0 ± 55.0 | +20.5% |
| + both, hierarchical routing off | 700.6 ± 28.8 | +8.4% | 742.0 ± 72.6 | +21.5% | 828.0 ± 60.6 | +23.2% |
| + both, w/o chunk-skip pruning | 750.0 ± 20.6 | +20.5% | 788.4 ± 55.0 | +25.4% | 888.6 ± 69.6 | +27.9% |
Appendix figures & tables11 assets
Supplementary material from the paper’s appendix.
Appendix
| Operation | Time | Memory | Cross-rank comm. | Kernel interface |
|---|---|---|---|---|
| Single-chunk clip (Def. 1 ) | 0 | shared | ||
| Multi-chunk shift+merge (Def. 2 ) | 0 | shared | ||
| Total mask preprocessing | 0 | shared |
| LPT | LPT+P2 | LPT+P2+P3 | |||||
|---|---|---|---|---|---|---|---|
| Configuration | Subopt | Gap | Subopt | Gap | Subopt | P3 wins | |
| Uniform | 99.0% | 6.54% | 86.0% | 6.29% | 0% | 172 | |
| Uniform (wide) | 100% | 8.67% | 93.5% | 3.42% | 0% | 187 | |
| Causal | 93.5% | 2.51% | 53.5% | 1.39% | 0% | 107 | |
| Uniform † | 85.0% | 1.08% | 48.0% | 0.79% | 0% | 48 | |
| Baseline | Scope | Min | Max | Mean |
|---|---|---|---|---|
| FlexAttention | All | +33.6% | +70.5% | +55.3% |
| 8K | +33.6% | +62.9% | +50.6% | |
| 128K | +51.9% | +70.0% | +58.9% | |
| Magi-Attention | All | +2.0% | +44.1% | +15.0% |
| 8K | +12.0% | +44.1% | +25.0% | |
| 128K | +2.0% | +8.9% | +5.9% |
| vs. Baseline | Phase | Min | Max | Geo. Mean ± std |
|---|---|---|---|---|
| MagiAttention | FWD | 1.04 | 2.39 | 1.56 ± 0.53 |
| BWD | 1.10 | 1.19 | 1.15 ± 0.03 | |
| Total | 1.09 | 1.46 | 1.26 ± 0.15 | |
| Megatron Ring | FWD | 1.62 | 4.12 | 2.50 ± 0.97 |
| BWD | 1.86 | 3.57 | 2.38 ± 0.56 | |
| Total | 1.80 | 3.71 | 2.42 ± 0.65 |
| Seq | Mask | FA3 | FlashMask-3 | FlexAttention | MagiAttention |
|---|---|---|---|---|---|
| 8K | Full | 604.43 ± 1.27 | 569.84 ± 1.88 (0.94) | 394.15 ± 1.34 (0.65) | 513.66 ± 8.95 (0.85) |
| 8K | Causal | 579.73 ± 0.35 | 520.85 ± 0.54 (0.90) | 341.68 ± 1.45 (0.59) | 482.17 ± 1.71 (0.83) |
| 8K | Sliding Window | 407.23 ± 1.54 | 290.30 ± 1.04 (0.71) | 218.93 ± 1.73 (0.54) | 166.22 ± 0.15 (0.41) |
| 8K | Causal Document | 478.96 ± 27.20 | 377.86 ± 35.26 (0.79) | 264.06 ± 18.37 (0.55) | 247.92 ± 47.43 (0.52) |
| 8K | Document | 546.29 ± 17.93 | 460.49 ± 31.77 (0.84) | 299.46 ± 26.68 (0.55) | 352.82 ± 48.32 (0.65) |
| 32K | Full | 616.65 ± 0.65 | 603.63 ± 1.48 (0.98) | 408.22 ± 0.02 (0.66) | 593.40 ± 1.30 (0.96) |
| Seq | Mask | FlashMask-3 | FlashMask | FA3-style Hopper port |
|---|---|---|---|---|
| 8K | Full | 596.53 ± 2.26 | 384.88 ± 1.84 (0.65) | 214.01 ± 0.23 (0.36) |
| 8K | Causal | 553.31 ± 1.13 | 373.46 ± 1.37 (0.67) | 241.86 ± 0.13 (0.44) |
| 8K | Sliding Window | 432.15 ± 6.60 | 256.63 ± 0.07 (0.59) | 35.77 ± 0.07 (0.08) |
| 8K | Causal Document | 483.97 ± 29.40 | 302.03 ± 15.27 (0.62) | 64.77 ± 15.83 (0.13) |
| 8K | Document | 532.26 ± 24.90 | 287.89 ± 34.54 (0.54) | 55.03 ± 14.14 (0.10) |
| 32K | Full | 614.19 ± 1.15 | 388.92 ± 1.84 (0.63) | 214.22 ± 0.08 (0.35) |