Unique Hard Attention: A Tale of Two Sides

Selim Jerad, Anej Svete, Jiaoda Li, Ryan Cotterell

TL;DR

This paper reveals that a seemingly trivial design choice—how a transformer breaks ties when multiple tokens share the maximum attention score—fundamentally dictates its mathematical expressivity. Under strict future masking and finite precision, unique hard attention with leftmost tiebreaking is proven to be strictly less powerful than rightmost tiebreaking, collapsing instead to the exact same expressive power as standard soft attention.

What Problem Does This Paper Address?

To understand what transformers can and cannot compute, theoreticians often analyze idealized versions of the architecture. One popular variant is the Unique Hard Attention (UHA) transformer, where the attention mechanism selects exactly one token that maximizes the attention score. But what happens when there is a tie? Typically, the model defaults to picking either the leftmost or the rightmost token.

Prior foundational work suggested that UHA transformers are equivalent to Linear Temporal Logic (LTL), meaning they can recognize the entire class of "star-free" formal languages. However, those studies assumed the model could utilize both or rightmost tiebreaking. This paper addresses a critical overlooked gap: Does the choice of direction in tiebreaking actually matter? Specifically, if a causal (future-masked) transformer is restricted to leftmost tiebreaking, does it lose expressivity?

Methods

The authors tackle this question using formal language theory, logic, and automata theory. They analyze a finite-precision, future-masked transformer architecture without positional encodings.

To bridge the gap between neural networks and formal logic, they use an intermediary Boolean programming language called B-RASP. They introduce a restricted variant, $B\text{-}RASP^\text{F}_\blacktriangleleft$, which enforces strict future masking (a token cannot attend to itself or the future) and leftmost tiebreaking ($\blacktriangleleft$).

The authors then construct mathematical proofs to establish a chain of equivalences between:

  1. Leftmost UHA transformers ($T^\text{F}_\blacktriangleleft$)
  2. The $B\text{-}RASP^\text{F}_\blacktriangleleft$ programming language
  3. A restricted fragment of Linear Temporal Logic that only looks at the past ($LTL[\diamondsuit^-]$)
  4. Partially Ordered Finite-State Automata (POFA)
The mechanism of unique hard attention at a single layer, where the highest-scoring token $x_{t^*}^{(\ell)}$ is selected and combined with the current token via a residual connection.

Datasets

Not discussed in the paper. This is a purely theoretical paper and does not evaluate models on empirical datasets.

Key Insights and Ideas

  • The Tiebreaking Asymmetry: Rightmost tiebreaking ($\blacktriangleright$) can easily simulate leftmost tiebreaking ($\blacktriangleleft$) in two steps. However, the reverse is impossible. Causal leftmost attention cannot isolate the token immediately to its left ($t-1$) if older tokens also match the query criteria.
  • The Soft Attention Connection: Surprisingly, leftmost hard attention has the exact same expressive power as standard soft attention (softmax) under finite precision. This makes leftmost UHA a much better, simpler proxy for studying real-world soft-attention models than rightmost UHA.
  • Explaining "Attention Glitches": Soft-attention transformers famously struggle with the "flip-flop" language modeling task, where they must recall the symbol following the most recent (rightmost) write instruction. This paper provides the theoretical reason why: soft attention (and leftmost UHA) is fundamentally bounded by $LTL[\diamondsuit^-]$, meaning it lacks the mechanism to inherently locate the most recent occurrence of a token when older ones exist.
  • Why ALiBi Works: The fact that rightmost UHA is strictly more expressive helps explain the empirical success of positional biases like ALiBi, which penalize distant tokens. By biasing attention toward recent tokens, they help the network approximate the more powerful rightmost tiebreaking behavior.

Key Results

The paper refines the theoretical landscape of transformer expressivity by proving that directionality matters immensely.

A comprehensive taxonomy mapping different finite-precision transformer idealizations to their exact logical and automata-theoretic equivalents.

As summarized in the taxonomy above, the authors prove that:

  • Future-masked rightmost UHA ($T^\text{F}_\blacktriangleright$) is equivalent to full LTL and counter-free automata.
  • Future-masked leftmost UHA ($T^\text{F}_\blacktriangleleft$) is strictly weaker, mapping precisely to $LTL[\diamondsuit^-]$ and POFA.

Consequently, leftmost UHA transformers cannot model strictly local languages, $n$-gram models, or languages requiring the detection of contiguous substrings—limitations they share identically with soft-attention transformers.

Limitations and Future Work

  • Purely Theoretical Idealization: The results assume strict finite precision, no positional encodings, and a single-head attention mechanism. While the authors note that layer normalization and multi-head attention can be integrated into their proofs, real-world transformers with complex positional encodings might behave differently.
  • No Learning Dynamics: The paper focuses entirely on representational capacity (what the architecture is mathematically capable of representing) rather than learnability (what a model can actually learn via gradient descent). In practice, training hard-attention models directly is unfeasible due to non-differentiability.
  • Future Direction: The authors propose future empirical work to contrast how these theoretical boundaries manifest when training soft-attention transformers vs. transformers explicitly biased toward rightmost or leftmost attention on formal language benchmarks.

Summary updated

BibTeX
@inproceedings{jerad2025unique,
  author = {Selim Jerad and Anej Svete and Jiaoda Li and Ryan Cotterell},
  title = {{Unique Hard Attention: A Tale of Two Sides}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2025},
  doi = {10.18653/v1/2025.acl-short.76},
}
Figure