A Compositional Theory of Causally Masked Transformers

Franz Nowak, Ryan Cotterell, Reda BoumasmoudarXiv 2026

TL;DR

This paper provides a rigorous algebraic framework to analyze the expressivity of causally masked transformers operating under realistic, finite-precision arithmetic. By mapping different attention mechanisms to specific algebraic semigroup varieties, the authors establish a tight hierarchy of formal languages these models can recognize, proving that hardware-level rounding errors fundamentally alter and expand a model's computational capacity.

What Problem Does This Paper Address?

Most theoretical analyses of transformer expressivity assume idealized, infinite-precision arithmetic. In reality, computers use finite-precision floating-point numbers where addition is non-associative, and tiny values can be completely absorbed or rounded away.

This paper bridges the gap between idealized mathematical theory and actual hardware implementation. The authors tackle a fundamental question: What formal languages can a causally masked, finite-precision transformer without positional embeddings (NoPE) actually compute over arbitrary input lengths?

Methods

The authors treat the attention head as a state-transition system, effectively modeling the transformer as an algebraic Recurrent Neural Network (RNN).

  1. Operational Memory as a Fold: Instead of tracking an ever-growing key-value cache, they define the state of an attention head as a finite, query-indexed collection of accumulators (the running numerator and denominator sums). Processing text from left to right acts as a mathematical "fold" over these finite accumulators.
  2. Semigroup Theory: To classify what these memory states can compute, the authors use finite semigroup theory and algebraic automata theory—specifically Krohn-Rhodes decomposition and Stiffler’s refinement.
  3. Compositional Analysis: They analyze how these state transformations compose in parallel (across multiple heads in a layer) and hierarchically (cascading across successive layers) to derive strict upper and lower bounds for the entire network.
Figure 1: Abstraction of a transformer to an algebraic object to a syntactic semigroup.

Datasets

Not discussed in the paper. This is a purely theoretical paper containing mathematical proofs; no empirical datasets or benchmarks were used.

Key Insights and Ideas

  • Finite Precision Implies Finite State: Because the arithmetic domain is finite, the set of possible accumulator values is also finite. This means that under finite precision, a transformer head's operational memory is fundamentally a finite-state machine.
  • Arithmetic Semantics Matter: The exact implementation of floating-point math changes the model's computational limits. For instance, full floating-point soft attention can absorb tiny increments due to rounding. This absorption prevents cycles and forces the transition semigroup to be aperiodic, expanding its language-recognition capabilities compared to idealized variants.
  • The Attention Expressivity Hierarchy: The authors discover that restricting the attention mechanism maps precisely onto classical algebraic language classes:
  • Width-one sliding-window attention maps to definite languages (it can only remember a fixed-size suffix window of the input).
  • Sharp soft attention (where scores too small to alter the normalizer are treated as zero) maps to $\mathcal{R}$-trivial languages (it acts like an irreversible checklist).
  • Full floating-point soft attention maps to star-free languages (it can retain ordering information from arbitrarily far back).
Figure 2: Illustration of the types of tasks that attention can perform depending on the variety of its transition semigroup.

Key Results

The paper establishes a strict hierarchy of expressivity based on the attention mechanism used, proving that under a "free-wiring" assumption (where layer connections can be arbitrary total maps), these bounds are completely tight:

Attention Type Semigroup Variety Language Class Recognized
Sliding-window (width 1) Definite ($\mathbf{Def}$) Definite languages
Sharp soft attention $\mathcal{R}$-trivial ($\mathbf{R}$) $\mathcal{R}$-trivial languages
Sliding + Sharp Cascade Locally $\mathcal{R}$-trivial ($\mathbf{LR}$) Locally $\mathcal{R}$-trivial languages
Full soft attention Aperiodic ($\mathbf{Ap}$) Star-free languages
Summary of attention types and their corresponding language classes

A striking qualitative finding is that ordinary soft attention is strictly more expressive than sharp soft attention. Floating-point underflow and absorption break the rigid constraints of $\mathcal{R}$-triviality, allowing the model to realize more complex memory operations. However, even with full soft attention, these models are fundamentally bounded by star-free languages, meaning they cannot perform any modular counting (such as determining the parity of a string), regardless of depth or width.

Limitations and Future Work

  • No Positional Embeddings: The framework is strictly limited to transformers without positional embeddings (NoPE). Introducing unbounded positional embeddings breaks the semigroup morphism because absolute position tags prevent text segments from being cleanly concatenated and analyzed algebraically.
  • State Space Explosion: While the theoretical analysis elegantly reduces questions to single-accumulator behaviors, the actual state space of a query-indexed family of accumulators ($A^Q$) is astronomically large, meaning this framework is a structural tool for proving bounds rather than a practical method for enumerating the states of a trained model.
  • Future Directions: The authors suggest extending this algebraic framework to accommodate finite or repeating positional embeddings, and using these insights to design novel attention variants with mathematically guaranteed expressivity characteristics.

Summary updated

BibTeX
@misc{nowak2026compositional,
  author = {Franz Nowak and Ryan Cotterell and Reda Boumasmoud},
  title = {{A Compositional Theory of Causally Masked Transformers}},
  year = {2026},
  eprint = {2607.26988},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2607.26988},
}
Figure