CompileMask: Bridging the Gap Between Expressiveness and Efficiency in Attention Masking

Hongzheng Li, Jiacheng Wan, Zhaolong Xing, Zhida Jiang, Yuanhang Yang, Junhua Shu, Jiaxing Wang, Xupeng Miao, Yingxia Shao, Zhen Chen, Ke ZhangarXiv 2026

TL;DR

CompileMask is a new framework that solves the "expressiveness-efficiency trade-off" in Transformer attention masking. By using an offline compiler to translate high-level, region-based mask specifications into optimized, interval-based execution descriptors, it achieves up to 3.8× speedup over state-of-the-art methods like FlexAttention while supporting complex, dynamic masking patterns.

What Problem Does This Paper Address?

Attention masking is the mechanism that tells a Transformer which tokens can "see" each other. While standard patterns like "causal" (looking only at the past) are well-supported by hardware-efficient kernels like FlashAttention, modern tasks—such as prefix sharing, multimodal generation, and recommendation systems—require much more complex, data-dependent masks.

Currently, researchers face a frustrating choice:

  1. Predicate-based methods (e.g., FlexAttention): Highly expressive (you can write any logic), but slow because they evaluate complex boolean logic for every single element in the attention matrix.
  2. Interval-based methods (e.g., FlashMask): Very fast, but rigid. They rely on a fixed number of "visible intervals," making them unable to represent complex patterns like the CLEAR multimodal mask, which might have many disjoint visible segments per row.

CompileMask bridges this gap by decoupling how a user defines a mask from how the hardware executes it.

Methods

The core innovation of CompileMask is a two-stage pipeline that moves the heavy lifting of mask analysis from the training loop to an offline compilation step.

1. The MaskSpec DSL

Users define masks using MaskSpec, a region-based Domain Specific Language (DSL). Instead of writing a single global function, users divide the mask into "Regions" (e.g., Context, History, Target). Each region has a "row guard" (which rows this applies to) and a "visibility rule."

2. Offline Symbolic Compilation

The CompileMask compiler symbolically analyzes these rules. It transforms Python-like logic into a Disjunctive Normal Form (DNF) and extracts symbolic intervals.

Figure 2. The COMPILEMASK pipeline. Pink blocks denote the two mask representations, and green blocks denote the computa- tional components.

As shown in the figure above, the compiler generates a specialized preprocess kernel. This kernel is "symbolic" because it knows the structure of the mask but waits for runtime metadata (like actual sequence lengths) to calculate the exact boundary values.

3. Online Execution Descriptor

During training or inference, the generated preprocess kernel runs once per batch. It produces an Execution Descriptor consisting of:

  • Interval Table: Precomputed start and end points for visible segments.
  • Tile-State Bitmaps: A compact 2-bit-per-tile map that tells the attention kernel if a tile is "Empty" (skip it), "Full" (no masking needed), or "Partial" (use the Interval Table).

The attention kernel then uses a clever unsigned arithmetic trick—Uint(column_index - start) < Uint(length)—to check visibility with a single operation, making the "partial" tiles extremely fast to process.

Datasets

The authors evaluated CompileMask using both synthetic workloads and real-world data:

  • Synthetic Patterns: HSTU (recommendation), Shared-prompt Reward Models (RLHF), CLEAR (multimodal), and standard Causal masks.
  • Real-world Traces: Variable-length sequences derived from the KuaiRand-1K interaction logs to simulate production recommendation workloads.
  • Models: End-to-end training was tested on Qwen3-8B and Qwen3-14B architectures.

Key Insights and Ideas

  • Structural Separation: The key "aha!" moment is that while mask boundaries change per batch, the structure of the rules is usually static. By analyzing this structure offline, you can eliminate almost all the logical overhead during the actual attention computation.
  • Unsigned Arithmetic for Intervals: By representing intervals as a start and a length, the kernel can check if a token is visible using one subtraction and one comparison. This is significantly faster than checking index >= start AND index < end.
  • Developer Productivity: Beyond speed, CompileMask simplifies life for engineers. In one production use case, it reduced the required code from 1,300 lines of hand-written CUDA kernels to just 80 lines of MaskSpec DSL, cutting development time from days to hours.

Key Results

  • Throughput: CompileMask achieved up to 3.8× speedup in training throughput compared to Flex-Triton and 1.33× over the highly optimized Flex-FA4 on variable-length workloads.
  • Scaling: At 32K sequence lengths, it outperformed Flex-FA4 by 1.44× to 2.87× across various mask types.
  • Generality: Unlike previous interval-based methods, CompileMask successfully handled the CLEAR multimodal mask, which requires more intervals than standard formats allow.
  • Low Overhead: For standard causal attention, CompileMask retains 85-89% of the performance of a native, hand-tuned Causal FlashAttention-4 kernel, showing that its generality comes with very little "tax."

Limitations and Future Work

  • Traversal Alignment: Currently, users must provide separate MaskSpecs for forward and backward passes because the kernels traverse the attention matrix in different directions (row-wise vs. column-wise).
  • Interval Bounds: The compiler must be able to determine an upper bound on the number of intervals that doesn't grow with sequence length. If a mask is truly "fractal" or random, the system falls back to slower predicate-based evaluation.
  • Future Directions: The authors suggest that future work could involve automatically deriving the backward mask specification from the forward one to further simplify the user experience.

Summary updated

BibTeX
@misc{li2026compilemask,
  author = {Hongzheng Li and Jiacheng Wan and Zhaolong Xing and Zhida Jiang and Yuanhang Yang and Junhua Shu and Jiaxing Wang and Xupeng Miao and Yingxia Shao and Zhen Chen and Ke Zhang},
  title = {{CompileMask: Bridging the Gap Between Expressiveness and Efficiency in Attention Masking}},
  year = {2026},
  eprint = {2610.13474},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2610.13474},
}
Figure