Code Pretraining Improves Entity Tracking Abilities of Language Models

Najoung Kim, Sebastian Schuster, Shubham ToshniwalarXiv 2024 · DOI

Introduction

Entity tracking—the ability to maintain the state of objects or characters as they undergo changes throughout a narrative—is a fundamental component of natural language understanding. While modern Large Language Models (LLMs) demonstrate varying degrees of proficiency in this area, the specific training factors that drive this capability have remained unclear. This study provides systematic evidence that pretraining LLMs on computer code significantly enhances their ability to track the state of entities in natural language. While code is a "clear winner" for this capability, other structured data types like mathematics or alignment tuning do not provide the same consistent benefits.

Experimental Methodology

To isolate the drivers of entity tracking, the researchers employed a "minimal pair" comparison strategy. They evaluated base models (such as Llama 2, DeepSeek, and Gemma) against versions of the same models that had undergone additional training on specific data types: code, mathematics, or alignment (instruction following).

Table 1: Summary of model families and additional training types compared in the study.

The primary evaluation tool was the "boxes task." In this setup, a model is given a description of items in several boxes followed by a series of 1–12 state-changing operations (e.g., "Move the ball from Box 1 to Box 2"). The model must then state the final contents of all boxes. To ensure that performance reflected reasoning rather than formatting difficulties, the study used constrained decoding to force models to output valid descriptions.

Results: The Primacy of Code

The results indicate that code pretraining is a uniquely effective driver of entity tracking performance. Models specialized for code consistently outperformed their base counterparts, particularly as the complexity of the task (the number of operations affecting box state) increased.

Figure 1: Accuracy on the boxes task for base models (red) versus code-trained versions (blue). Code training provides a consistent performance boost across different model families.

The authors hypothesize that code is effective because it is inherently procedural, requiring the tracking of variable states through logical steps—a direct parallel to tracking entities in a narrative. Notably, this benefit scales with model size; larger models, such as Llama 2 70B, showed more pronounced improvements from code training than smaller variants, suggesting a capacity threshold is required to fully leverage procedural data.

Analysis of Math and Alignment Tuning

Surprisingly, additional training on mathematical datasets yielded marginal to no consistent benefit. Despite being highly structured, mathematics lacks the explicit procedural state-tracking inherent in programming. This suggests that general logical structure is insufficient; the specific procedural nature of the training data is the critical factor.

Alignment tuning (instruction following) presented a mixed picture. While it helped base models understand the task format, it did not consistently improve—and sometimes even degraded—the performance of models already trained on code. This indicates that while alignment helps with task adherence, it does not necessarily enhance the underlying reasoning required for entity tracking.

Conclusion

This research demonstrates that code pretraining provides a vital training signal for developing complex reasoning and state-tracking capabilities in language models. While mathematics and alignment tuning offer specific advantages in other domains, they do not replicate the consistent gains observed with code. Future work aims to further isolate the variables within procedural data to understand exactly which elements of code training most effectively bolster natural language reasoning.

Summary updated

BibTeX
@misc{kim2024code,
  author = {Najoung Kim and Sebastian Schuster and Shubham Toshniwal},
  title = {{Code Pretraining Improves Entity Tracking Abilities of Language Models}},
  year = {2024},
  eprint = {2405.21068},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2405.21068},
}
Figure