Emergent Stack Representations in Modeling Counter Languages Using Transformers

Utkarsh Tiwari, Aviral Gupta, Michael Hahn

TL;DR

Can standard Transformer architectures naturally learn structured memory representations without architectural modifications? By probing the intermediate representations of Transformers trained on counter languages like Dyck-1 and Shuffle-$k$, this paper shows that models implicitly construct internal, linear stack-like depth representations simply through autoregressive next-token prediction.

What Problem Does This Paper Address?

Despite the dominance of Transformers in natural language processing and algorithmic reasoning, interpreting their internal mechanics remains an open challenge. Tasks involving hierarchical or nested structures—such as evaluating nested arithmetic or parsing syntax—theoretically require structured memory mechanisms like counters or pushdown stacks.

While previous research often engineered explicit differentiable stacks into neural networks to enforce such memory, the authors ask a more fundamental mechanistic question: Do standard Transformers spontaneously form stack-like internal representations when trained purely on formal counter languages?

Methods

To isolate and inspect the representations:

  • Language Setup: The authors study 4 counter languages: Dyck-1 (well-formed single-parenthesis language, requiring 1 counter/stack) and Shuffle-$k$ for $k \in \{2, 4, 6\}$ (the arbitrary interleaving of $k$ independent Dyck-1 languages with disjoint bracket vocabularies, requiring $k$ independent counters).
  • Base Architecture: Encoder-only Transformers with linear heads trained autoregressively on next-token prediction following the protocol of Bhattamishra et al. (2020).
  • Probing Classifiers: For a sequence of length $T$, token embeddings of size $1 \times d_{\text{model}}$ are extracted from the final encoder layer. The authors train multi-class classifiers (ranging from simple linear probes to 6-layer MLPs with ReLU activations) to predict the exact stack depth/counter value at each token. For Shuffle-$k$, $k$ separate probes are evaluated (one per stack).
  • Controls & Sanity Checks:
  • Selectivity Baselines: Following Hewitt & Liang (2019), target stack depths were randomized to ensure probes were measuring genuine representation rather than memorizing data.
  • Tracr Verification: As a positive control, the probing methodology was first validated on a Transformer compiled directly from a known RASP program for Dyck-1 using Tracr (Lindner et al., 2023).

Datasets

The authors use synthetic formal language datasets:

  • Training Corpus: Synthetic strings generated from Dyck-1 and Shuffle-$k$ ($k \in \{2, 4, 6\}$).
  • Probing Dataset: 10,000 sequence samples per language, with sequence lengths spanning between 2 and 50 tokens (split into 8,000 training and 2,000 validation pairs of embedding and ground-truth stack depth).

Key Insights and Ideas

  • Emergent Stack Counters: Even standard linear probes easily recover stack depths from the final encoder representations, achieving accuracies far higher than random control baselines. This confirms that Transformers encode counter variables in their activation spaces.
  • Higher Fidelity in Interleaved Languages: Probing accuracy surprisingly improves as the number of interleaved stacks ($k$) grows (e.g., Shuffle-6 achieves higher probe accuracy than Dyck-1). The authors hypothesize that because individual stacks in Shuffle-6 update less frequently (roughly once every 6 tokens instead of every token), the model maintains cleaner, more stable representations per counter.
  • Linear Readability: Complex non-linear probing models only marginalize improvements over simple linear probes, implying that the counter state is largely arranged linearly in the model's residual representation space.

Key Results

Language Linear Probe Accuracy 6-Layer Probe Accuracy Control Baseline
Dyck-1 53.4% 64.9% ~4.6%
Shuffle-2 75.5% – 76.6% 88.1% ~6.7% – 6.8%
Shuffle-4 90.6% – 90.8% 97.0% – 97.1% ~7.2% – 7.3%
Shuffle-6 94.9% – 95.5% 98.3% – 99.1% ~9.1% – 9.2%

The near-chance performance on the randomized control task (~4% to 9%) confirms high selectivity: the probes read out structured representations rather than learning spurious correlations.

Limitations and Future Work

  • Correlation vs. Causality: Probing identifies the presence of stack depth representations but does not prove the model causally uses these counter representations to produce next-token predictions.
  • Algorithmic Execution: The paper focuses on representation recovery rather than discovering the exact attention and MLP circuits that update these counters.
  • Future Directions: The authors highlight causal intervention techniques (such as activation patching or interchange interventions), probing error/failure analysis, and testing architectural variations across larger context horizons.

Summary updated

BibTeX
@inproceedings{tiwari2025emergent,
  author = {Utkarsh Tiwari and Aviral Gupta and Michael Hahn},
  title = {{Emergent Stack Representations in Modeling Counter Languages Using Transformers}},
  booktitle = {World Models @ ICLR},
  year = {2025},
  url = {https://openreview.net/forum?id=o5xTyDilNs},
}
Figure