Rule Extrapolation in Language Modeling: A Study of Compositional Generalization on OOD Prompts

Anna Mészáros, Szilvia Ujváry, Wieland Brendel, Patrik Reizinger, Ferenc Huszár

TL;DR

This paper introduces "rule extrapolation," a novel framework to test how language models handle out-of-distribution (OOD) prompts by observing if they can still follow one grammatical rule when another is intentionally broken. By testing various architectures on formal languages, the authors reveal that Transformers are not a universal silver bullet—they struggle with simple regular languages compared to LSTMs and State Space Models (Mamba). The paper also proposes a theoretical framework based on algorithmic simplicity to explain how ideal models should generalize.

What Problem Does This Paper Address?

Large Language Models (LLMs) are surprisingly adept at handling out-of-distribution (OOD) prompts, a capability often observed in "in-context learning." However, we lack a systematic, theoretical understanding of why this happens and how different architectures influence this ability.

Studying this phenomenon in natural language is incredibly difficult because defining what is truly "out-of-distribution" in a massive, messy text corpus is nearly impossible. To solve this, the authors turn to formal languages (like mathematical or programming grammars). Formal languages are defined by strict intersections of rules (e.g., Rule A AND Rule B).

Conceptual diagram of rule extrapolation, where a model trained on in-distribution (ID) data must generalize to an out-of-distribution (OOD) prompt that violates a learned rule.

The authors ask a fundamental question: if a model is trained on data that perfectly obeys both rules, what happens if we prompt it with an OOD sequence that fundamentally violates Rule B? Will the model completely break down, or will it "extrapolate" and still try to satisfy Rule A?

Methods & Datasets

The authors tackle this problem through both empirical and theoretical lenses.

  1. Empirical Evaluation: They trained five different sequence modeling architectures—Linear models, LSTMs, Transformers, Mamba (a State Space Model), and the newly introduced xLSTM—on sequences generated from formal languages. They then evaluated these models on OOD prompts designed to violate one specific rule of the language, measuring whether the model's completion still adhered to the remaining rule.
  2. Normative Theory: To understand what a model should do in an ideal world, they propose a theoretical framework inspired by Algorithmic Information Theory and the Solomonoff prior. This non-parametric prior formalizes a "rational" approach to OOD completion, essentially stating that an ideal model should favor the simplest algorithmic explanation (Occam's razor) consistent with the training data.

To test this, the authors generated synthetic datasets based on six formal languages, categorized by the Chomsky hierarchy:

  • Regular Languages: $L_1$ (even number of 'a's, starts with 'b') and $L_2$ (even 'a's, all 'b's precede 'a's).
  • Context-Free Languages: $L_3$ ($a^n b^n$, meaning equal numbers of 'a's and 'b's, with 'a's first) and $L_4$ (Dyck language: nested and paired brackets/parentheses).
  • Context-Sensitive Languages: $L_5$ ($a^n b^n c^n$) and $L_6$ (Context-sensitive Dyck: paired but not necessarily nested brackets).

Key Insights and Results

The empirical results challenge the assumption that Transformers are the ultimate architecture for all sequence tasks. The "best" architecture depends heavily on the structural complexity of the task.

Performance comparison of different architectures across formal languages, showing completion rates for rules R1 and R2.
  • The Transformer's Parity Weakness (Regular Languages): Transformers struggle significantly with regular languages that require counting or calculating parity (e.g., ensuring an even number of 'a's). On $L_1$ and $L_2$, LSTMs and Mamba excel. The Transformer performs below chance level on $L_1$ even on in-distribution data, despite achieving a similar test loss to the other models.
  • Transformer Dominance on Complex Grammars: On Context-Free and Context-Sensitive Languages ($L_3, L_4, L_5$), the Transformer dominates. For example, on the OOD prompts for $L_3$, the Transformer successfully extrapolated the non-violated rule 66% of the time, compared to the LSTM (38%) and Mamba (30%).
  • Simplicity Bias in Training Dynamics: When watching a Transformer learn the $a^n b^n$ language, the authors noticed it doesn't learn the complex intersection of rules all at once. It first learns the simpler rule ("a's come before b's"), and only later learns the harder rule ("equal number of a's and b's") as a subset. This proves models have a bias toward lower algorithmic complexity.
  • A Mathematical "North Star": The proposed normative theory shows that rule extrapolation isn't just a lucky artifact of deep learning; it is the mathematically rational behavior expected from an ideal agent that favors simple theories over complex ones.

Interestingly, in a small pilot study, humans actually found these formal language extrapolation tasks quite difficult, sometimes performing worse than the best neural network for a given language.

Limitations and Future Work

The authors acknowledge that formal languages are a far cry from the nuances of natural language, so demonstrating rule extrapolation in real-world LLM deployments remains an open challenge. Additionally, their hyperparameter search was not exhaustive, meaning specific architectural tweaks might alter the exact performance numbers. Finally, the proposed normative algorithm (based on the Solomonoff prior) is mathematically uncomputable in practice.

For future work, the authors suggest that finding practical, computable approximations of their normative algorithm could lead to the development of new LLM architectures with vastly superior, mathematically grounded OOD generalization capabilities.

Summary updated

BibTeX
@inproceedings{meszaros2024rule,
  author = {Anna Mészáros and Szilvia Ujváry and Wieland Brendel and Patrik Reizinger and Ferenc Huszár},
  title = {{Rule Extrapolation in Language Modeling: A Study of Compositional Generalization on OOD Prompts}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2024},
  url = {https://openreview.net/forum?id=Li2rpRZWjy},
}
Figure