Sneaking Syntax into Transformer Language Models with Tree Regularization

Ananjan Nandi, Christopher D. Manning, Shikhar Murty

Abstract

Standard Transformer language models are fundamentally non-hierarchical, relying on massive datasets to implicitly learn the hierarchical nature of human language. We introduce TREEREG, a simple yet effective auxiliary loss function that encourages internal representations to align with tree structures via orthogonality constraints. By regularizing a subset of attention heads during training, TREEREG improves data efficiency and syntactic generalization. Crucially, TREEREG requires no architectural modifications and introduces zero overhead during inference, making it a scalable solution for enhancing the inductive biases of Large Language Models (LLMs).

Introduction: The Hierarchy Gap in Transformers

Human language is inherently hierarchical, where words form constituents like phrases and clauses. However, the Transformer architecture processes sequences through attention mechanisms that allow for arbitrary, non-local information routing without an explicit structural bias. Consequently, LLMs require astronomical amounts of data to learn linguistic structure. Previous attempts to incorporate syntax often involved complex architectural changes—such as restricted attention patterns or specialized stack-based modules—which frequently compromised scalability and inference speed. TREEREG addresses this by "sneaking" syntax into the model as a soft regularizer during training.

Methods: Orthogonality as a Structural Guide

The core of TREEREG is the Span Contextual Independence Score (SCIN). This metric is based on the linguistic intuition that a constituent (e.g., a noun phrase) should be relatively independent of its immediate context.

Overview of the TREEREG mechanism, illustrating the SCIN score and the use of orthogonality to represent linguistic independence.

The approach follows three main steps:

  1. Geometric Regularization: TREEREG enforces contextual independence by making the vector representation of a span maximally orthogonal to its surrounding context.
  2. Soft Supervision: During training, the model is provided with "silver parses" from automated tools. The loss function encourages high SCIN scores for valid constituents and low scores for random spans.
  3. Targeted Application: The regularizer is applied only to a small subset of attention heads in the middle layers, where syntactic information is most prominent.

Because TREEREG is an auxiliary loss used only during training, the resulting model remains a standard Transformer, preserving the efficiency of the original architecture at test time.

Key Insights

The "Middle Layer" Sweet Spot

Analysis reveals that syntactic structure is not uniformly distributed across a Transformer's depth. Syntactic information typically peaks in the middle layers (e.g., layer 12 of a 16-layer model) before "unraveling" in the final layers as the model transitions to next-token prediction. TREEREG is most effective when targeted at these specific layers.

Data Efficiency and Grokking

TREEREG significantly reduces the data requirements for learning syntax. Models trained with this regularizer can outperform standard LMs while using less than half the training data. Furthermore, on algorithmic linguistic tasks, TREEREG accelerates "grokking"—the sudden transition to perfect generalization—by up to 10 times compared to baselines.

Robustness Against Spurious Shortcuts

By forcing the model to respect hierarchical syntax, TREEREG discourages the learning of "spurious shortcuts" (e.g., relying on simple keyword presence for NLI tasks). This leads to more robust performance on adversarial benchmarks.

Key Results

TREEREG demonstrates consistent improvements across a variety of benchmarks, including BLiMP and SyntaxGym for syntactic generalization, and Penn TreeBank (PTB) for out-of-distribution (OOD) perplexity.

Comparison of TREEREG LM against Base LM and other syntax-aware models across syntactic generalization and perplexity metrics.
  • Syntactic Generalization: TREEREG achieved up to a 9.5-point improvement on the SyntaxGym benchmark.
  • OOD Performance: Pre-training with TREEREG resulted in up to 10% lower perplexity on OOD data.
  • Adversarial Robustness: When fine-tuning a Sheared Llama-1.3B model, TREEREG mitigated performance drops on adversarial benchmarks by 41.2 points.
  • Implicit Parsing: Probing the hidden states of TREEREG-trained models allows for the extraction of parse trees that match gold-standard parses with F1 scores exceeding 90.

Limitations and Future Work

While effective, TREEREG currently relies on "silver parses" generated by external tools, which may be unavailable for low-resource languages. Additionally, implementing the regularizer increases training time by approximately 25%. Future research will explore unsupervised versions of TREEREG that allow models to discover optimal hierarchical structures without external supervision.

Summary updated

BibTeX
@inproceedings{nandi2025sneaking,
  author = {Ananjan Nandi and Christopher D. Manning and Shikhar Murty},
  title = {{Sneaking Syntax into Transformer Language Models with Tree Regularization}},
  booktitle = {Conference of the North American Chapter of the Association for Computational Linguistics},
  year = {2025},
  doi = {10.18653/v1/2025.naacl-long.407},
}
Figure