[Paper Review] Physics of Language Models: Part 1, Learning Hierarchical Language Structures
This paper investigates how autoregressive transformers learn and represent context-free grammars (CFGs) through controlled synthetic experiments. It demonstrates that GPT-2 can achieve near-perfect accuracy in generating complex, long, and ambiguous CFGs, with hidden states precisely encoding non-terminal structure and attention mechanisms mimicking dynamic programming through boundary-to-boundary attention patterns.
Transformer-based language models are effective but complex, and understanding their inner workings and reasoning mechanisms is a significant challenge. Previous research has primarily explored how these models handle simple tasks like name copying or selection, and we extend this by investigating how these models perform recursive language structure reasoning defined by context-free grammars (CFGs). We introduce a family of synthetic CFGs that produce hierarchical rules, capable of generating lengthy sentences (e.g., hundreds of tokens) that are locally ambiguous and require dynamic programming to parse. Despite this complexity, we demonstrate that generative models like GPT can accurately learn and reason over CFG-defined hierarchies and generate sentences based on it. We explore the model's internals, revealing that its hidden states precisely capture the structure of CFGs, and its attention patterns resemble the information passing in a dynamic programming algorithm. This paper also presents several corollaries, including showing why absolute positional embeddings is inferior to relative and rotary embeddings; uniform attention alone is surprisingly effective (motivating our follow-up work on Canon layers); encoder-only models (e.g., BERT, DeBERTa) struggle with deep structure reasoning on CFGs compared to autoregressive models (e.g., GPT); and injecting structural or syntactic noise into pretraining data markedly improves robustness to corrupted language prompts.
Motivation & Objective
- Understand the physical mechanisms by which transformers learn complex, hierarchical language structures like context-free grammars (CFGs).
- Investigate how transformers handle long-range dependencies and ambiguity in CFGs, especially when dynamic programming is required for validation.
- Develop and validate probing techniques to analyze whether hidden states encode non-terminal (NT) structure and boundary information.
- Examine the robustness of transformers to grammar errors and the role of positional and relative positional embeddings in CFG learning.
- Demonstrate that attention patterns in trained models resemble dynamic programming by forming 'boundary-to-boundary' attention flows.
Proposed method
- Construct synthetic, non-trivial CFGs with varying difficulty levels, including long, ambiguous, and globally ambiguous grammars (e.g., cfg8, cfg9, cfg0 families).
- Pre-train GPT-2-small on large-scale synthetic datasets sampled from these CFGs using a language modeling objective.
- Use linear probing to evaluate whether hidden states encode non-terminal (NT) ancestor information with high accuracy.
- Visualize and quantify attention patterns to detect 'boundary-to-boundary' attention, indicating dynamic programming-like behavior.
- Apply rejection sampling to filter CFGs with unique dynamic programming solutions, enabling reliable probing on the cfg9 family.
- Compare model performance across different attention mechanisms (e.g., rotary, relative positional embeddings) to assess their impact on CFG learning.
Experimental results
Research questions
- RQ1Can transformers learn to generate sentences from complex, long, and ambiguous context-free grammars with high accuracy and diversity?
- RQ2How do hidden states in trained transformers encode non-terminal (NT) structure and hierarchical boundaries in CFGs?
- RQ3Do attention mechanisms in transformers form patterns resembling dynamic programming, such as boundary-to-boundary attention flows?
- RQ4To what extent do positional and relative positional embeddings influence the model’s ability to learn CFGs?
- RQ5How robust are transformers to grammar errors, and can they recover from local inconsistencies in ambiguous CFGs?
Key findings
- GPT-2-small achieves near-perfect generation accuracy (close to 100%) on all tested CFG families, including long and ambiguous grammars.
- Hidden states after pre-training linearly encode non-terminal (NT) ancestor information with high accuracy, especially at subtree boundaries.
- The model learns to form 'boundary-to-boundary' attention patterns that mirror the recursive structure of dynamic programming, enabling efficient parsing.
- The cfg9 family, which ensures unique dynamic programming solutions, enables reliable probing and confirms that NT structure is encoded precisely in hidden states.
- Relative and rotary positional embeddings significantly improve CFG learning performance, especially in long-sequence settings.
- Even in globally ambiguous CFGs (e.g., cfg8, cfg0), the model generates diverse, valid strings, indicating robustness to structural ambiguity.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.