Logical Languages Accepted by Transformer Encoders with Hard Attention

Pablo Barceló, Alexander Kozachinskiy, Anthony Widjaja Lin, Vladimir V. Podolskii · DOI

TL;DR

This paper maps the theoretical boundaries of Transformer encoders that use "hard attention" (selecting specific positions rather than a weighted distribution). The authors prove that while these models are computationally restricted, they are surprisingly adept at recognizing complex logical languages, including those involving counting and position-based predicates that go beyond standard regular languages.

What Problem Does This Paper Address?

While Transformers have dominated natural language processing, our theoretical understanding of their "computational power"—specifically what kinds of formal languages they can and cannot recognize—is still evolving. Most existing research focuses on the full encoder-decoder architecture, which is Turing-complete (it can compute anything a standard computer can).

However, the Transformer encoder on its own is much more restricted. This paper specifically investigates encoders using hard attention mechanisms. In hard attention, a model doesn't look at a "blur" of the whole sentence; it picks one specific position (Unique Hard Attention, or UHAT) or an average of the best positions (Average Hard Attention, or AHAT). The authors seek to pinpoint exactly which logical "languages" (sets of strings defined by mathematical rules) these specific architectures can handle.

Methods

The authors use tools from circuit complexity and mathematical logic to define the limits of these models. Their approach involves:

  1. Defining the Architectures: They formalize UHAT (which breaks ties by picking the minimum index) and AHAT (which averages the vectors of all top-scoring positions).
  2. Logical Mapping: They attempt to "embed" specific logics into these architectures. Specifically, they look at:
  3. FO(Mon): First-order logic extended with unary numerical predicates (e.g., "is this position even?" or "is this the middle of the word?").
  4. LTL(Mon): Linear Temporal Logic with similar predicates.
  5. LTL(C, +): A more powerful logic that includes "counting terms" (e.g., "how many 'a's have appeared so far?").
  6. Constructive Proofs: To prove a Transformer can recognize a language, they show how to mathematically construct the specific weights and layers (using ReLU and affine transformations) required to "simulate" the logic's operations.

Datasets

Not discussed in the paper. As a theoretical study in formal language theory, this paper relies on mathematical proofs and logic rather than empirical training on datasets or benchmarks.

Key Insights and Ideas

  • UHAT is not all-powerful in $AC^0$: Previous work showed UHATs belong to the complexity class $AC^0$ (problems solvable by shallow, fast circuits). This paper provides a "negative result," proving there are $AC^0$ languages (like "approximate majority") that a UHAT simply cannot recognize.
  • The Power of Unary Predicates: Despite the limitation above, the authors show that UHATs can recognize any language definable in $FO(Mon)$. This is significant because $FO(Mon)$ can describe non-regular languages like $\{a^n b^n\}$, which standard finite automata cannot handle.
  • The "Average" Advantage: Moving from Unique (UHAT) to Average (AHAT) hard attention provides a massive boost in "counting" ability. By averaging positions, the model can effectively perform linear arithmetic on the number of occurrences of different letters.
  • Parikh Images: The authors show that Transformers are "Parikh-powerful." This means that if you only care about the count of letters in a string (ignoring their order), Transformers can recognize a much wider array of languages than standard regular or context-free grammars.

Key Results

  • Theorem 1: Every language definable in $FO(Mon)$ can be recognized by a UHAT encoder. This includes all regular languages that fall within the $AC^0$ complexity class.
  • Theorem 2: AHAT encoders are even more capable; they can recognize any language defined in $LTL(C, +)$, a logic that incorporates counting and linear inequalities.
  • Parikh Closure: AHATs can recognize the "permutation closure" of any regular language. For example, they can recognize a language consisting of all strings with an equal number of 'a's, 'b's, and 'c's—a task that is famously impossible for standard context-free grammars.
  • New Capabilities: The authors prove for the first time that AHATs can recognize the "parity" language (checking if the number of 'a's is even), which was previously an open question for this specific architecture.

Limitations and Future Work

  • Characterization Gaps: While the authors showed that UHATs can do $FO(Mon)$, they haven't yet proven if $FO(Mon)$ is the exact limit. There might be languages a UHAT can recognize that $FO(Mon)$ cannot describe.
  • The $TC^0$ Boundary: AHATs are known to sit inside the complexity class $TC^0$ (which includes majority gates). A major open question remains: Is there a language in $TC^0$ that an AHAT cannot recognize?
  • Logic vs. Architecture: The authors suggest looking for a language that an AHAT can accept but that cannot be defined in their proposed $LTL(C, +)$ logic, which would help further refine the boundaries of Transformer power.

Summary updated

BibTeX
@inproceedings{barcelo2024logical,
  author = {Pablo Barceló and Alexander Kozachinskiy and Anthony Widjaja Lin and Vladimir V. Podolskii},
  title = {{Logical Languages Accepted by Transformer Encoders with Hard Attention}},
  booktitle = {International Conference on Learning Representations},
  year = {2024},
  url = {https://openreview.net/forum?id=gbrHZq07mq},
}
Figure