arXiv(opens in a new tab)cs.LGAug 2026Cited by 1 paper on PaperVantage

Manuscript Offprint

arXiv:2608.31067

Universal Transformers for Circuit Computations: Perfect Length Generalization in Tiny Transformers

Takuya Ito, Ruchir Puri, Murray Campbell, Parikshit Ram

ABSTRACT

Learning generalizable algorithmic computations remains a challenge for neural networks, as reflected in persistent failures on compositional and length generalization benchmarks. We present a provably correct, transformer parameterization (with only 280 learnable parameters for Boolean algebra tasks) capable of learning and evaluating problems of any depth or length. We assume inputs are fully parenthesized, well-formed expressions. Our approach conceptualizes algorithmic tasks as circuit models embedded in transformers, enabling depth-1 circuit reduction in a single forward pass. To achieve depth generalization, we introduce a positional encoding that tracks each gate's depth within the circuit, enabling the model to identify evaluable subexpressions at each iteration via masked hard attention, with O(n)O(n) per-iteration complexity via linear attention. Combined with an autonomous halting criterion, the model terminates after d iterations for problems of depth d, yielding O(n⋅d)O(n \cdot d) total complexity. We show that training on shallow problem instances (depth 1 and depth 2) effectively recovers interpretable parameters that snap into place, resulting in exact length generalization. Though we establish that our construction provably evaluates Boolean expressions – a universal symbolic computation – of arbitrary length perfectly, in other experiments we also demonstrate that our transformer variant can learn and generalize perfectly (100% accuracy) on other common length generalization benchmarks, including modular arithmetic and ListOps.

Keywords universal transformer · length generalization · circuits · mechanistic interpretability

1 Introduction

Understanding how transformers can execute exact symbolic computations remains a challenge in artificial intelligence. Standard transformer architectures have struggled with symbolic generalization [Lake and Baroni, 2018], particularly when generalizing to longer input sequences [Hupkes et al., 2020, Dziri et al., 2023, Zhou et al., 2024]. Although recent works have reported substantial progress on algorithmic benchmarks – such as arithmetic reasoning and formal language recognition – and in some cases have identified interpretable internal representations [He et al., 2024, Cho et al., 2025, Fan et al., 2025, Adler et al., 2025], these advances often rely on task-specific heuristics and lack guarantees of exact algorithmic correctness outside the training distribution. Thus, strong benchmark performance does not ensure that a transformer learns a generalizable algorithm.

We first address this by studying transformers on Boolean formulas – a canonical family of compositional computations that can be represented as circuits [Arora and Barak, 2009]. Circuits provide explicit algorithmic structure – gates, edges, and intermediate values – and naturally scale to arbitrary sizes (Fig. 1). Many string-based tasks (e.g., modular arithmetic and context-free grammars) can be mapped to circuits, yielding a unified notion of algorithmic generalization via a graph-based structure [Ito et al., 2025]. Here, we parameterize by hand a fixed, small transformer (280 parameters) that provably evaluates arbitrary non-uniform Boolean expressions via a circuit reduction procedure. When randomly initialized, the model can learn to generalize perfectly from depth-1 and depth-2 samples. This behavior is enabled by a novel positional encoding (PE) mechanism that infers circuit depth from a parenthesized string and identifies

∗^{*}takuya.ito@ibm.com evaluable subexpressions at each step. The model is also computationally efficient: attention scales linearly in memory, and the number of iterations scales approximately logarithmically in input length, suggesting that exact algorithmic computation in transformers can be realized without sacrificing efficiency or parameter compactness.

A

Boolean circuits

Ex: “((TRUE OR FALSE) AND (NOT FALSE))”

B

Modular arithmetic circuits

Ex (mod 10): “ (3+5)×(2+4)(3 + 5) \times (2 + 4)”

Figure

Figure 1: Example A) Boolean and B) modular arithmetic expressions, encoded as circuits. Circuits provide a structured algorithm to evaluate an expression by following the edges from the input gates to the output gate. We illustrate circuit evaluation through circuit reduction, whereby each operator gate is iteratively updated by the subexpression's computed value until the output is obtained.

To demonstrate generality beyond Boolean formulas, we show that the same architecture learns and perfectly generalizes to other common compositional tasks, such as modular arithmetic and ListOps [Nangia and Bowman, 2018] from shallow training data. Together, these results provide an example of a transformer that provably implements an exact circuit algorithm while remaining interpretable, efficient, and learnable. We present our findings first by introducing the exact model construction that can evaluate Boolean formulas without any training, followed by empirical learnability experiments for all tasks in randomly initialized models.

References (66)