Abstract
The expressive power of transformers over inputs of unbounded size can be studied through their ability to recognize classes of formal languages. In this paper, we establish exact characterizations of transformers with hard attention (in which all attention is focused on exactly one position) and attention masking (in which each position only attends to positions on one side). With strict masking (each position cannot attend to itself) and without position embeddings, these transformers are expressively equivalent to linear temporal logic (LTL), which defines exactly the star-free languages. A key technique is the use of Boolean RASP as a convenient intermediate language between transformers and LTL. We then take numerous results known for LTL and apply them to transformers, showing how position embeddings, strict masking, and depth all increase expressive power.
Introduction
While transformers dominate modern artificial intelligence, their theoretical limits—specifically, what kinds of patterns they can and cannot recognize over sequences of unbounded length—are still being mapped out. Most theoretical work on transformers provides either upper bounds (computational limits) or lower bounds (minimum capabilities). Finding an exact characterization—identifying a class of languages that transformers compute exactly—is a significant challenge in the field.
This paper addresses this challenge by analyzing a simplified theoretical model: the masked hard-attention transformer. In this model, "hard attention" implies that the model focuses 100% of its attention on a single token, and "masking" restricts tokens to attending to only one side (e.g., past tokens in autoregressive models). The authors determine exactly which class of formal languages this specific architecture can recognize.
Theoretical Framework: The B-RASP Bridge
The core methodology relies on formal language theory and logic. The authors introduce an intermediate programming language called B-RASP (Boolean Restricted Access Sequence Processing), derived from RASP, which is designed to mimic transformer operations. B-RASP restricts data to Boolean values, facilitating a three-way mathematical equivalence.
As shown in Figure 1, the authors prove that:
- Masked hard-attention transformers can be compiled into B-RASP.
- B-RASP programs can be decompiled into masked hard-attention transformers.
- B-RASP is expressively equivalent to Linear Temporal Logic (LTL), a formal logic system used to reason about sequences.
By establishing the equivalence Transformers $\leftrightarrow$ B-RASP $\leftrightarrow$ LTL, the authors can apply decades of established LTL theorems directly to transformer architectures.
Expressive Power and Logic Upgrades
The equivalence to LTL provides an immediate characterization: masked hard-attention transformers (without position embeddings and with strict masking) recognize exactly the star-free regular languages.
A critical finding is the role of strict masking. In standard transformers, "non-strict" masking allows a token to attend to itself. However, in a hard-attention setting, this can lead to "stutter-invariance," where the model cannot distinguish between sequences like "A-B" and "A-A-B." Strict masking—preventing a token from attending to itself—actually increases expressive power by forcing the model to look at other positions.
Furthermore, the authors demonstrate that position embeddings (PEs) act as logic upgrades. While the baseline model corresponds to LTL, adding specific PEs increases the class of recognizable languages:
- Rational sinusoidal embeddings allow the model to recognize regular languages in $\text{AC}^0$.
- Arbitrary finite-image embeddings upgrade the model to LTL with monadic predicates (LTL[Mon]).
The Depth Hierarchy and Asymmetric Attention
Unlike standard feed-forward networks, where a 2-layer network can often act as a universal approximator, adding layers to a hard-attention transformer consistently increases its expressive power. The authors prove a depth hierarchy: for any depth $k$, there exists a language requiring a depth $k+1$ transformer for recognition.
Additionally, the research shows that asymmetric attention is sufficient for full expressive power. A transformer only needs "future-masked, rightmost-hard" attention to recognize the full suite of star-free languages. Adding bidirectional capabilities or different tie-breaking rules (like leftmost-hard) does not expand the class of recognizable languages.
Limitations and Conclusion
While this work provides a rigorous foundation, it focuses on hard-attention models. Real-world transformers utilize "soft" (softmax) attention. Although hard attention is a valuable theoretical abstraction, these exact bounds may not apply directly to standard soft-attention models. Additionally, the mathematical proofs require position embeddings to have a "finite image," which differs slightly from the continuous floating-point implementations used in practice.
Despite these limitations, the bridge between transformers and LTL opens new avenues for understanding neural architectures through the lens of formal logic. Future work may explore how these findings translate to soft-attention mechanisms and the specific role of "Beginning of Sequence" (BOS) tokens in distinguishing positions.
Summary updated