NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Grokking of Hierarchical Structure in Vanilla Transformers

Shikhar Murty, Pratyusha Sharma, Jacob Andreas, Christopher D. Manning

Abstract

While Transformers have revolutionized natural language processing, a persistent debate remains regarding their ability to internalize the hierarchical nature of human language. Previous research often suggested that these models rely on linear heuristics rather than structural rules. This paper demonstrates that "vanilla" Transformers can indeed learn hierarchical representations, but they do so through a process of "structural grokking"—a sudden jump in out-of-distribution generalization that occurs long after training accuracy has plateaued. Furthermore, we identify an inverted U-shaped scaling law regarding model depth, suggesting a "Goldilocks" zone for structural discovery.

Introduction

Human language is fundamentally organized into nested, tree-like structures where words form phrases and clauses. Whether standard Transformer architectures—which lack explicit structural biases—can recover these hierarchies is a central question in computational linguistics. Historically, many evaluations concluded that Transformers favor linear surface-level heuristics (e.g., identifying the first verb in a sentence) over hierarchical ones (e.g., identifying the main verb of the matrix clause).

This study re-evaluates these conclusions by investigating the temporal dynamics of learning. We hypothesize that hierarchical understanding is not absent, but rather delayed, emerging only after extensive training beyond the point of zero training error.

Methodology

To test for hierarchical discovery, we trained standard Transformer language models on tasks designed with a specific ambiguity: the training data can be explained by both a simple linear rule and a more complex hierarchical rule. Generalization is measured on a "structural" test set where only the hierarchical rule yields the correct answer.

Experimental Setup

  • Extended Training: Models were trained for up to 400,000 steps, significantly longer than the duration required to saturate in-domain validation accuracy.
  • Depth Variation: We analyzed models ranging from 2 to 10 layers to determine how architectural capacity influences structural learning.
  • Metrics: Beyond accuracy, we employed the tree-structuredness metric to quantify how closely internal representations approximate a tree structure, alongside monitoring weight norms and attention sparsity.

Datasets

  1. Dyck-LM: A bracket-matching task requiring the model to predict closing brackets in deeply nested sequences (depth of 10).
  2. Question-Formation: Converting declarative sentences into questions, a task that requires identifying the main verb regardless of its linear position.
  3. Tense-Inflection: Mapping sentences to correctly inflected forms based on the sentence's structural backbone.

Key Insights

The Phenomenon of Structural Grokking

Our primary finding is the emergence of "structural grokking." Similar to the grokking observed in algorithmic tasks like modular arithmetic, structural generalization in language tasks happens suddenly and late. A model may maintain 100% training accuracy for 100,000 steps while showing 0% generalization, only to experience a sharp "click" where generalization accuracy jumps to near-perfection by step 300,000.

The "Goldilocks" Depth

Generalization performance follows an inverted U-shaped curve relative to model depth.

  • Shallow models (2 layers): Often lack the representational capacity to encode complex hierarchies.
  • Deep models (10+ layers): Surprisingly, these models often fail to generalize, likely because they overfit to complex non-hierarchical patterns or fail to converge on the simpler hierarchical solution during the grokking phase.
  • Optimal models (4-6 layers): These models consistently achieve the highest generalization, representing a structural "Goldilocks" zone.

Tree-Structuredness as a Predictor

While metrics like weight norms and attention sparsity increase monotonically with training, they do not perfectly correlate with generalization. Instead, the "tree-structuredness" of internal computations peaks at the same depth where generalization is highest. This suggests that the emergence of tree-like internal representations is the functional driver of hierarchical success.

Results

  • Generalization Gains: In the Question-Formation task, average generalization accuracy improved from less than 40% at the point of training saturation to over 90% following extended training.
  • Task-Specific Optima: The optimal depth varied slightly by task—4 layers for Tense-Inflection and 6 layers for Question-Formation—but the inverted U-shaped trend remained consistent.
  • Internal Evolution: Even in models that failed to reach perfect generalization, internal analysis showed a gradual movement toward more tree-like computations over time, though only "optimal" models crossed the threshold required for successful out-of-distribution performance.

Discussion and Limitations

These findings suggest that previous claims regarding the inability of Transformers to learn hierarchy may have been artifacts of early stopping. If training is halted once validation loss stabilizes, the "aha moment" of structural discovery is missed.

However, several limitations remain. This study utilizes synthetic, context-free grammars and controlled English-like datasets. It remains to be seen if this grokking phenomenon occurs at the scale of Large Language Models (LLMs) trained on diverse web data. Additionally, the computational cost of inducing grokking is high, requiring training durations an order of magnitude longer than standard regimes. Future work should focus on methods to accelerate this structural awareness, potentially through architectural refinements or targeted data curriculum.

Summary updated

BibTeX
@inproceedings{murty2023grokking,
  author = {Shikhar Murty and Pratyusha Sharma and Jacob Andreas and Christopher D. Manning},
  title = {{Grokking of Hierarchical Structure in Vanilla Transformers}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2023},
  doi = {10.18653/v1/2023.acl-short.38},
}
Figure