Skip to content
FlashMask-4

FlashMask-4

github.com • November 5, 2026

Figure 1: FlashMask overview. (a) Supported mask types; (b) column-wise sparse representation; (c) efficient implementation

Figure 2: FlashMask-3 across the four layers of the execution stack

2026-09-25 FlashMask-4 : Supports Hopper and Blackwell. Kernels are JIT-compiled at runtime with CuTe DSL, so compilation is fast. Flexible masks are available at every supported head dim, with full support for sink bias and deterministic computation, plus large head dims. Both PaddlePaddle and PyTorch interfaces are provided.

2026-09-25 FlashMask-3 : Supports Hopper and introduces mask-aware distributed attention with communication/computation overlap and load balancing.

2026-09-25 FlashMask : Supports Ampere and introduces the column-wise sparse mask representation, which reduces memory complexity from $O(N^2)$ to $O(N)$ and uses mask sparsity to skip unnecessary computation.

FlashMask is an efficient attention kernel library for flexible sparse masks. It uses a column-wise sparse mask representation that encodes a 2D dense mask as 1D per-column row-index ranges, and it exploits mask sparsity to skip unnecessary computation.

This repository evolved from PaddlePaddle/flash-attention and is now maintained as the standalone FlashMask repository. It implements forward and backward kernels for Hopper (SM90) and Blackwell (SM100) with CuTe DSL , and provides both PaddlePaddle and PyTorch interfaces. The core code is JIT-compiled at runtime with CuTe DSL, so compilation is fast. Beyond flexible masks, it supports mask-aware distributed attention with communication/computation overlap and load balancing, and adds large head dim support on SM100.

$$ O = \mathrm{softmax}\left(\frac{QK^\mathsf{T}}{\sqrt{d}} + M\right)V $$

$Q,\ K,\ V$ are the input tensors of the attention module;

$d$ is the size of the last dimension of $Q$ (the head dim);

$M$ is the column-wise sparse mask introduced by FlashMask.

FlashMask-4 provides three capabilities:

memory-efficient, flexible, and fast masked attention kernels;

mask-aware distributed attention with communication/computation overlap and load balancing;

large head dim support.

All three ship in the same flashmask package. The core code is JIT-compiled at runtime with CuTe DSL, so compilation is fast, and the PaddlePaddle and PyTorch frontends the same kernel implementation .

Memory-efficient, flexible, and fast masked attention kernels

The table below compares FlashMask-4 with the mask_mod path of FlashAttention-4 , not with every FlashAttention-4 interface. d is the head dim of Q/K and dv is the head dim of V.

✅ supported, ⚠️ partially supported, ❌ not supported. On SM90, FlashAttention backward does not support GQA/MQA ( qhead_per_kvhead > 1 ) when d != dv , so 192/128 is MHA-only; FlashMask SM90 supports GQA/MQA in this configuration. The two FlashAttention columns were tested on 2026-09-23 at commit edb5c7 , the same baseline used in the performance section below.

✅ supported, ⚠️ partially supported, ❌ not supported. On SM90, FlashAttention backward does not support GQA/MQA ( qhead_per_kvhead > 1 ) when d != dv , so 192/128 is MHA-only; FlashMask SM90 supports GQA/MQA in this configuration.

The two FlashAttention columns were tested on 2026-09-23 at commit edb5c7 , the same baseline used in the performance section below.

FlashMask-4 does more than support larger head dims: it keeps flexible masks, sink bias, and deterministic computation fully available at those sizes.

Linear memory complexity : masks are described by a few boundary vectors, reducing memory complexity from $O(N^2)$ to $O(N)$ .

Sparse computation : blocks are classified as fully masked, partially masked, or unmasked. Fully masked blocks are skipped, and element-wise masking is applied only to partially masked blocks.

Broad flexible-mask support : 192/128 supports GQA/MQA on SM90, and custom masks are available at 256/256 , 512/512 , and 576/512 on SM100. FlashAttention-4 mask_mod is unavailable in these scenarios.

Complete feature set : every configuration in the table supports sink bias and deterministic computation.

The kernels live in flashmask/cute . They implement the SM90 and SM100 forward and backward passes in CuTe DSL and are JIT-compiled at runtime for the GPU and input configuration.

Distributed attention with communication/computation overlap and load balancing

FlashMask-4 uses mask information to distribute computation and schedule communication in context parallelism (CP): CPB (context-parallel balance) reduces load imbalance across GPUs, and Overlap reduces time spent waiting on communication . The two can be used independently or together.

CPB: mask-aware load balancing (open-sourcing in progress)

Under sparse masks, sequence chunks of the same length can require very different amounts of computation. CPB assigns work by the effective attention computation and optimizes the load with HCC-IPO (Hierarchical Compute-Communication Iterative Pairwise Optimal), reducing how much the slowest rank holds back overall throughput. It also builds each rank's local mask with a localized transformation that requires no extra mask communication .

The implementation lives in flashmask/cp_balance . It supports SM90 and SM100 and does not depend on Overlap.

Overlap: mask-aware communication/computation overlap

Overlap uses the mask to identify KV blocks that do not need to be transferred, cutting unnecessary communication, and organizes KV all-gather, attention computation, and gradient reduce-scatter into forward and backward pipelines that hide as much communication as possible.

Hierarchical intra-/inter-node communication : distinguishes intra-node (NVLink) from inter-node (RoCE / IB) links and schedules KV transfers by topology level, further reducing exposed cross-node latency.

DeepEP compatible : the communication implementation does not conflict with DeepEP, so both can be used in the same training process.

The implementation lives in flashmask/overlap . It communicates through NCCL GIN , is integrated with the CuTe DSL kernels, and currently supports SM100 only .

Overlap is enabled by passing a CP group (world size > 1) to flashmask_attention . Key/value are then the local S_local shard, and the mask key axis has length S_local * group_size in group-rank chunk order. The library converts it to its internal order, so callers must not pre-roll it. Hierarchical (multi-node) communication is selected automatically when the group spans more than one node.

The overlap-specific checks below reject unsupported calls before launching attention. Topology-dependent failures abort during communicator initialization:

positive S_local with S_local % 4 == 0 and S_local * H_kv * head_dim % 512 == 0 , which may change from call to call; hierarchical (multi-node) groups additionally require S_local % 512 == 0 ;

a power-of-two CP group of at most 64 ranks, with S_local * group_size =64 .

Automatically installed dependencies : nvidia-cutlass==4.2.0.0 , nvidia-cutlass-dsl[cu13]==4.5.2 , and apache-tvm-ffi>=0.1.5, = 2.30.5 (e.g. nvidia-nccl-cu13 , or point NCCL_HOME at an installation) and the cutlass C++ headers ( nvidia-cutlass , or FM4_OVERLAP_CUTLASS_INC ). If FLASHMASK_BUILD names ovl explicitly (e.g. ovl , utils+ovl ), a missing or too-old NCCL or missing cutlass headers fail the build with the reason. Under all , the bridge is skipped with a warning and the core kernels are unaffected. The NCCL version at runtime must equal the one the bridge was built against, otherwise communicator setup fails. The bridge loads the interpreter's nvidia-nccl wheel (or FLASHMASK_NCCL_LIB ), and the first libnccl.so.2 the process loads wins, so the framework's own NCCL must be that same version. Build and run in the same image. The target arch comes from FM4_OVERLAP_CUDA_ARCH , which takes one arch or a , / ; list such as 100a;103a (one SASS per arch in one .so ). The default is the local GPU, or 90a if no GPU is visible. sm_100a and sm_103a binaries are not interchangeable, so a build host without a GPU must set this variable.

NCCL >= 2.30.5 (e.g. nvidia-nccl-cu13 , or point NCCL_HOME at an installation) and the cutlass C++ headers ( nvidia-cutlass , or FM4_OVERLAP_CUTLASS_INC ).

If FLASHMASK_BUILD names ovl explicitly (e.g. ovl , utils+ovl ), a missing or too-old NCCL or missing cutlass headers fail the build with the reason. Under all , the bridge is skipped with a warning and the core kernels are unaffected.

The NCCL version at runtime must equal the one the bridge was built against, otherwise communicator setup fails. The bridge loads the interpreter's nvidia-nccl wheel (or FLASHMASK_NCCL_LIB ), and the first libnccl.so.2 the process loads wins, so the framework's own NCCL must be that same version. Build and run in the same image.

The target arch comes from FM4_OVERLAP_CUDA_ARCH , which takes one arch or a , / ; list such as 100a;103a (one SASS per arch in one .so ). The default is the local GPU, or 90a if no GPU is visible. sm_100a and sm_103a binaries are not interchangeable, so a build host without a GPU must set this variable.

The build is controlled by two environment variables, with defaults FLASHMASK_BUILD=all and FLASHMASK_FRAMEWORK=paddle . An invalid value makes the install fail and lists the allowed values.

FLASHMASK_BUILD : select components

Valid values are core , cpb , ovl , and all , combined with + or , (e.g. core+cpb ). core is always built and does not need to be listed.

FLASHMASK_FRAMEWORK : select the framework

The build targets PaddlePaddle by default. In a PyTorch environment, set FLASHMASK_FRAMEWORK=torch . core , cpb , and ovl one implementation across both frameworks.

The script picks head dims by GPU architecture (SM90: 128/192/256; SM100: additionally 576 with split and shared KV) and runs each on its own GPU in parallel, starting from GPU 0. Set DEVICES=2,3,4 to choose the GPUs.

The script picks head dims by GPU architecture (SM90: 128/192/256; SM100: additionally 576 with split and shared KV) and runs each on its own GPU in parallel, starting from GPU 0. Set DEVICES=2,3,4 to choose the GPUs.

Distributed attention with communication/computation overlap and load balancing

Run these commands from the repository root (each script changes into its own directory). The PaddlePaddle and PyTorch scripts the same names and live in tests/paddle/distributed/ and tests/torch/distributed/ respectively:

The scripts run on a single node by default; CPB uses GPUs 0–3 (CP=4). Use DEVICES to choose the GPUs on each node and TARGET_RANKS to choose the participating nodes for multi-node runs. See the header of each script for details.

The scripts run on a single node by default; CPB uses GPUs 0–3 (CP=4). Use DEVICES to choose the GPUs on each node and TARGET_RANKS to choose the participating nodes for multi-node runs. See the header of each script for details.

For more complete benchmarks, see the external repository test_flashmask .

If FlashMask helps your research, please cite:

FlashMask (ICLR 2025): FlashMask: Efficient and Rich Mask Extension of FlashAttention

FlashMask-3 (NeurIPS 2026): FlashMask-3: Efficient & Expressive Mask-Aware Distributed Attention

This repo benefits from FlashAttention . Thanks to Tri Dao and the team for their wonderful work.

FlashMask is released under the BSD 3-Clause License . Files derived from upstream projects retain their original copyright and license notices; for the files under Apache-2.0, see LICENSE.Apache-2.0 .

Extracted Entities

Domains (1)

Platforms (1)