Masked Hard-Attention Transformers Recognize Exactly the Star-Free Languages

Andy Yang, David Chiang, Dana Angluin

TL;DR Researchers have discovered that a specific flavor of Transformers—those using "hard attention" and strict masking—is mathematically equivalent to Linear Temporal Logic (LTL). This means these models recognize exactly the "star-free" regular languages, providing a rare exact characterization of Transformer expressivity and proving that architectural choices like masking and depth directly dictate the "logic" the model can learn.

The Quest for Exact Characterization

While Transformers have taken over AI, defining their fundamental computational limits remains a challenge. How "smart" is a Transformer, really? In theoretical computer science, we measure this by seeing which "formal languages" (sets of strings following specific rules) a model can recognize.

Previous research usually provided "bounds"—saying a Transformer is at least as powerful as X or no more than Y. This paper aims for an exact characterization. The authors focus on a specific, simplified version of the architecture: masked hard-attention transformers. By stripping away the complexity of "soft" probabilities and focusing on models that attend to exactly one position at a time, they can finally pin down the exact computational class these models belong to.

Bridging Neural Networks and Formal Logic

The authors bridge the gap between neural networks and formal logic using a three-way equivalence:

  1. The Architecture: They define a Transformer that uses unique hard attention (it picks the single best-scoring position) and strict masking (a position can look at its neighbors, but never at itself).
  2. The Intermediate Language (B-RASP): They introduce Boolean RASP, a simplified version of the RASP programming language. B-RASP acts as a "compiler" target; it’s easy to turn a B-RASP program into a Transformer and vice versa.
  3. The Logic (LTL): They prove that B-RASP is equivalent to Linear Temporal Logic, a formalism used in software verification to describe sequences of events over time (e.g., "if A happens, B must eventually happen").
Equivalence hierarchy of Transformers, B-RASP, and LTL

By proving these three are equivalent, they show that what a Transformer "does" is essentially execute temporal logic.

Theoretical Evaluation Framework

As a theoretical paper, this work does not use standard training datasets like ImageNet or WikiText. Instead, it evaluates the model's power against formal language classes:

  • Star-Free Languages: Languages that can be built using basic operations (union, concatenation, complement) but without the Kleene star (which allows for arbitrary repetitions).
  • Dyck-1 (Depth 2): A language of balanced parentheses, used as a benchmark for hierarchical reasoning.
  • AC⁰: A complexity class representing languages recognizable by shallow, polynomial-sized circuits.

Key Insights and Discoveries

The "Star-Free" Connection

The most significant revelation is that these Transformers recognize exactly the star-free languages. In the world of automata theory, this is a major milestone because star-free languages are exactly those that can be described by First-Order Logic.

The Paradox of Self-Attention

Counter-intuitively, strict masking (forbidding a position from attending to itself) makes the model more expressive. If a model can attend to itself, it becomes "stutter-invariant"—it loses the ability to distinguish between a single "a" and a repeated "aa," which actually limits the types of patterns it can recognize.

Position Embeddings (PEs) Matter

The paper shows that adding different types of PEs changes the model's computational ceiling. For example, adding rational sinusoidal PEs (like those in the original Vaswani paper) elevates the model's power to the $AC^0$ complexity class, allowing it to recognize patterns like "is the number of 'a's even?" (which LTL alone cannot do).

Depth Hierarchy

The authors prove a "depth hierarchy" theorem: a Transformer with $k+1$ layers is strictly more powerful than one with $k$ layers. There is no "shortcut" where a shallow model can do everything a deep one can.

Summary of Equivalences

The researchers successfully mapped Transformer variants to well-known complexity classes:

  • No PEs: Equivalent to Star-Free Languages / LTL.
  • Finite-image PEs: Equivalent to LTL with monadic predicates.
  • Rational sinusoidal PEs: Equivalent to Regular languages in $AC^0$.

Furthermore, adding "past masking" to a "future masked" model doesn't actually increase the class of languages it can recognize—it just makes it more efficient.

Limitations and Future Work

  • Hard vs. Soft Attention: The biggest limitation is the use of "hard" attention. Real-world Transformers use "soft" attention (softmax), which allows them to aggregate information from multiple positions. While hard attention is a useful theoretical proxy, the "soft" case remains an open frontier.
  • Finite Image Assumption: The results for position embeddings rely on them having a "finite image" (a limited set of possible values). In practice, PEs are often continuous, and the implications of infinite-precision PEs aren't fully explored here.
  • Unmasked Models: This paper focuses heavily on masked (decoder-style) models. Characterizing unmasked (encoder-style) models with the same level of precision is a suggested next step.

Summary updated

BibTeX
@inproceedings{yang2024masked,
  author = {Andy Yang and David Chiang and Dana Angluin},
  title = {{Masked Hard-Attention Transformers Recognize Exactly the Star-Free Languages}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2024},
  url = {https://openreview.net/forum?id=FBMsBdH0yz},
}
Figure