Abstract
The Transformer architecture has emerged as a landmark advancement within the broad field of artificial intelligence, effectively catalyzing the advent of large language models (LLMs). However, despite its remarkable capabilities and the substantial progress it has facilitated, the Transformer architecture still has some limitations. One such intrinsic limitation is its inability to effectively capture the Chomsky hierarchy, such as regular expressions or deterministic context-free grammars. Drawing inspiration from pushdown automata, which efficiently resolve deterministic context-free grammars using stacks, we propose StackTrans to address the aforementioned issue within LLMs. Unlike previous approaches that modify the attention computation, StackTrans explicitly incorporates hidden state stacks between Transformer layers. This design maintains compatibility with existing frameworks like flash-attention. Specifically, our design features stack operations -- such as pushing and popping hidden states -- that are differentiable and can be learned in an end-to-end manner. Our comprehensive evaluation spans benchmarks for both Chomsky hierarchies and large-scale natural languages. Across these diverse tasks, StackTrans consistently outperforms standard Transformer models and other baselines. We have successfully scaled StackTrans up from 360M to 7B parameters. In particular, our from-scratch pretrained model StackTrans-360M outperforms several larger open-source LLMs with 2-3x more parameters, showcasing its superior efficiency and reasoning capability.
The Inductive Bias Gap in Transformers
Despite their dominance, standard Transformers have a known "blind spot": the Chomsky hierarchy. While they excel at pattern matching, they struggle with tasks involving nested dependencies, regular expressions (REs), and deterministic context-free grammars (DCFs). The core issue is a lack of inductive bias. A standard Transformer treats sequences as flat or semi-structured through attention, but it lacks a native mechanism to "store and retrieve" information in a Last-In, First-Out (LIFO) manner—the way a computer or a linguist might process a nested mathematical expression or a complex sentence. Consequently, they often fail to generalize to sequences longer than those seen during training.
StackTrans: Architecture and Differentiable Operations
To address this, we introduce StackTrans, which augments the Transformer backbone with a differentiable stack mechanism. Unlike previous research that tried to replace the attention mechanism itself, StackTrans places these stacks between existing Transformer layers. This modular design ensures compatibility with high-performance kernels like FlashAttention.
The architecture relies on several key innovations:
- Differentiable Stack Operations: To allow for end-to-end training via backpropagation, discrete "push" and "pop" actions are replaced with "soft" operations. The model learns a probability distribution over three actions: push (add current state), pop (remove top state), and no-op (do nothing).
- Global Reading (Query-over-Stack Attention): Traditional stacks only allow visibility of the top element, which can lead to training instability. StackTrans implements a "global read" where the model uses a learnable query to attend to the entire stack, improving gradient flow and expressiveness.
- Multi-Head Low-Rank Stacks: Borrowing from multi-head attention, StackTrans uses multiple independent stacks. To minimize computational overhead, these stacks operate on low-rank projections of the hidden states.
Experimental Results
We evaluated StackTrans across two distinct domains: formal languages (13 tasks from the Chomsky hierarchy) and large-scale natural language pre-training (a 1-trillion-token corpus).
Formal Logic Mastery
StackTrans achieved nearly 100% accuracy on most RE and DCF tasks, where standard Transformers often hovered around 50% or failed to generalize to longer strings. Visualizations of the stack operations reveal that the model naturally learns a "push-then-pop" strategy, using earlier layers to store information and later layers to retrieve it for task resolution.
Superior Scaling and Reasoning
On natural language tasks, StackTrans-360M reached an average score of 44.3 across nine benchmarks, significantly higher than SmolLM2-360M (40.3) and Qwen2.5-0.5B (40.1). Most strikingly, on the GSM8K math reasoning benchmark, StackTrans-360M achieved a score of 33.6, while the comparable SmolLM2-360M scored only 3.2. This suggests the stack provides the "working memory" necessary for multi-step algorithmic reasoning. Furthermore, StackTrans-360M consistently outperformed 1.1B parameter models like OLMo and TinyLLaMA, proving that architectural inductive biases can be more powerful than raw parameter count.
Conclusion and Future Work
StackTrans effectively transforms an LLM into a large-scale Pushdown Automaton, providing the necessary inductive bias for hierarchical reasoning with minimal overhead (approximately 16% increase in training time). While the model still faces challenges with complex context-sensitive tasks and extreme temporal dependencies due to truncation for parallelism, it represents a significant step toward more efficient and capable reasoning models. Future work will explore scaling these benefits to frontier-sized models (70B+) and achieving Turing-completeness through multi-stack systems.
Summary updated