HackerPapers

arXiv:2305.13673cs.CL

Physics of Language Models: Part 1, Learning Hierarchical Language Structures

Zeyuan Allen-Zhu, Yuanzhi Li

Abstract

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.

arXiv comments
V2 polishes writing and adds Appendix G; V3 polishes writing and changes the title; V4 improves writing and adds Appendix H (more uniform attention results)
Journal reference
DOI
Subjects
cs.CLComputation and Language cs.AIArtificial Intelligence cs.LGMachine Learning

On Hacker News

Linked by 2 identifiable commenters in 2 comments across 2 threads, from Nov 2024 to Mar 2026.

Comments linking this paper, per year.

Discussed in comments2 threads

    • This is very interesting since there is another notable paper which shows LLMs can recognize and generate CFGs https://arxiv.org/abs/2305.13673 and of course a^n b^n is also classic CFG, so it's not clear why one paper had positive results while the other hand negative.

      krackersView on HNParent context

Submitted to Hacker News

Connected through shared HN threads, not inferred similarity or a recommendation.