arXiv:2610.13474cs.DCOct 2026

Manuscript Offprint

arXiv:2610.13474

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 Zhang

ABSTRACT

Modern attention workloads require diverse masking patterns whose boundaries depend on runtime metadata, yet efficient attention kernels natively support only a small set of patterns. Existing methods for supporting custom masks in these kernels face an expressiveness-efficiency trade-off. Predicate-based methods are expressive but incur substantial preprocessing and execution overhead, whereas interval-based methods execute efficiently but restrict expressiveness through a predefined interval count. We present COMPILEMASK, which addresses this trade-off by connecting expressive mask specifications with efficient attention execution. We design a compiler that analyzes visibility rules during the offline stage and generates a mask-specific preprocess kernel. We further propose MaskSpec, a region-based DSL that lets users express these rules. During the online stage, the generated kernel reads batch metadata and produces a compact Execution Descriptor that lets the attention kernel efficiently check visibility by comparing element indices against precomputed endpoints. Experiments show that COMPILEMASK achieves up to 3.8×3.8\times speedup over state-of-the-art methods.

1 INTRODUCTION

The attention mechanism is the central computational primitive of modern Transformers (Vaswani et al., 2017). Tasks such as preference optimization (Rafailov et al., 2023) with prefix sharing (Wang & Hegde, 2024), unified multimodal generation (Zhou et al., 2025), and generative recommendation (Zhai et al., 2024) each call for their own attention-masking pattern. The attention mask realizes these patterns by specifying which keys and values each query may attend to. This visibility often depends on runtime metadata such as sequence lengths and segment boundaries, so the mask's shape is not fixed ahead of time but instantiated per batch from these parameters.

Tile-based attention kernels (Dao et al., 2022) have become the de facto standard for their efficiency. However, these hardware-efficient kernels natively support only a small, fixed set of patterns such as causal or sliding-window. Realizing a mask outside this set forces a practitioner either to hand-modify the kernel or to materialize a dense O(N2)O(N^{2}) mask, leaving custom masking a second-class citizen.

Recently, two lines of work have been proposed to inject custom attention patterns into tile-based attention kernels. The predicate-based method (Dong et al., 2025) supports arbitrary masking rules through element-wise Boolean predicates. However, its efficiency is limited by expensive blockmask construction and per-element predicate evaluation during attention execution. The interval-based method (Wang et al., 2025), in contrast, enables efficient masking through compact interval representations. However, its expressiveness is limited because it cannot represent masks whose columns contain more intervals than its fixed format supports.

We find that the limitations of both approaches stem from their reliance on a single representation to satisfy two conflicting demands. The user defining the mask needs a representation that is expressive and easy to write, while the kernel executing it needs one that is compact and of fixed layout. These approaches prioritize either user-facing expressiveness or kernel efficiency within a single representation. In this paper, we call this the expressiveness-efficiency trade-off.

To bridge this gap, we present COMPILEMASK, which connects expressive user-facing mask specifications with an efficient kernel-facing representation through an offline compiler. COMPILEMASK works in two stages. During the offline stage, a user writes the masking logic once in a high-level, user-facing form, and the compiler analyzes it and emits a mask-specific preprocess kernel that encodes how the visible structure of every row is composed. During the online stage, for each batch, this preprocess kernel reads

∗^{*}Equal contribution. †^{\dagger}Work done during internship at JD.com. 1^{1}Beijing University of Posts and Telecommunications 2^{2}JD.com 3^{3}Peking University. Correspondence to: ‡^{\ddagger}Yingxia Shao shaoyx@bupt.edu.cn, Zhen Chen chenzhen48@jd.com.

the batch metadata and produces a compact Execution Descriptor that the attention kernel consumes directly. This descriptor is built once per batch and shared by attention layers that use the same mask. This separation allows users to express masking rules independently of the representation consumed by the attention kernel, while moving structural analysis offline and leaving only batch-dependent values to be computed online.

The question that follows is how to design the user-facing representation for offline compilation and the kernel-facing representation for online execution. On the user side, COMPILEMASK provides a MaskSpec composed of Regions, each of which specifies a range of query rows and a visibility predicate for those rows. These Regions let users flexibly specify visibility relationships between token segments. On the kernel side, COMPILEMASK uses an Execution Descriptor comprising an Interval Table and Tile-State Bitmaps. The Interval Table stores precomputed interval endpoints for fast element-wise visibility checks, while the Tile-State Bitmaps compactly encode tile scheduling information to reduce its memory footprint. The offline compiler connects these two representations to preserve both expressiveness and efficiency. For expressiveness, it derives the required interval count from each Region's visibility predicate rather than imposing a predefined count, allowing COMPILEMASK to support a wider range of masks compared with the existing interval-based method. For efficiency, the compiled interval representation lets the attention kernel check visibility by comparing element indices against precomputed endpoints, keeping online masking lightweight. Beyond these benefits, COMPILEMASK simplifies development by requiring users to write only a MaskSpec, from which the compiler generates the preprocess kernels. In our production workloads, this reduces user-written code from approximately 1,300 lines to 80 and development time from days to hours.

Our contributions are summarized as follows:

• We design a compiler that symbolically analyzes boolean visibility rules and generates a preprocess kernel that constructs the Execution Descriptor at runtime.

• We propose MaskSpec, a region-based DSL that lets users declare segment-structured attention masks without hand-writing kernels.

• We design an attention kernel that uses the Execution Descriptor’s Tile-State Bitmaps to schedule tiles and its Interval Table to determine element-wise visibility within partial tiles.

• We conduct comprehensive experiments, and the results show that COMPILEMASK achieves up to 3.80×3.80\times the single-operator training throughput of FlexAttention with the FA4 backend.

Figure

Figure 1. Segment-structured attention masks. (a) HSTU mask with context, history, and target segments. (b) Shared-prefix reward-model mask, where each response attends to the prompt and causally within itself. (c) CLEAR multimodal mask with global text interactions and local image-to-image attention.