Transformers Pretrained on Procedural Data Contain Modular Structures for Algorithmic Reasoning

Zachary Shinnick, Liangze Jiang, Hemanth Saratchandran, Anton van den Hengel, Damien Teney

TL;DR

Pretraining transformers on simple, semantic-free procedural data (like bracket matching or cellular automata) instills precise, modular inductive biases that significantly boost downstream algorithmic reasoning. Crucially, these learned structures localize within specific architectural components—such as attention layers or MLP blocks—and can be mixed and matched from different models to combine distinct reasoning capabilities.

What Problem Does This Paper Address?

While large language models (LLMs) excel at factual recall, their ability to perform robust algorithmic reasoning remains a major bottleneck. Recent work shows that pretraining models on structured, synthetic data (like computer code or formal languages) helps bridge this gap. However, we still don't fully understand why or where these reasoning mechanisms are encoded in a model's weights.

Is procedural pretraining just a fancy way to optimize weight magnitudes (a trivial initialization effect), or is it building genuine, transferable circuits? This paper investigates how semantic-free procedural data shapes transformer architectures to facilitate downstream reasoning tasks.

Methods

The authors train small GPT-2-style transformers on various procedural tasks and then fine-tune them on diagnostic reasoning benchmarks. To pinpoint exactly where the learned skills reside, they introduce a technique called selective weight transfer. Instead of moving the entire model, they selectively transfer only the attention layers ($T_{attn}$) or only the MLP blocks ($T_{mlp}$) to the downstream model.

To prove that the models are learning precise algorithmic circuits rather than just beneficial weight distributions, the authors apply weight perturbations—specifically, adding varying levels of Gaussian noise and randomly shuffling weights within layers. Finally, they explore modular composition by stitching together the attention layers of one pretrained model with the MLPs of another to create a hybrid initialization.

Figure 1: (a) We pretrain small transformers on various forms of procedural data, then fine-tune them on a series of diagnostic tasks. The data is generated from formal languages (b) or simple algorithms such as elementary cellular automata (c). In k-DYCK examples, matching brackets are color-coded. For STACK, ‘P’ is the pop operation. For STACK, IDENTITY, and SET, ‘|’ acts as a separator between the input and the expected output, on which the loss is computed (bold tokens).

Datasets

The paper utilizes two distinct sets of tasks:

  • Pretraining Tasks (Procedural Data):
    • $k$-DYCK / $k$-DYCK SHUFFLE: Formal languages consisting of well-nested (or shuffled) brackets.
  • STACK: Sequences simulating push/pop operations on a stack memory.
  • IDENTITY / SET: Tasks requiring the model to either repeat the input or remove duplicate tokens.
  • ECA RULE 110: Binary sequences generated by a complex, Turing-complete Elementary Cellular Automaton.
  • Downstream Diagnostic Tasks:
    • HAYSTACK: Long-context memory recall and retrieval.
  • Arithmetic: ADDITION (forward notation), REVERSED ADDITION, and MULTIPLICATION.
  • SORTING: Algorithmic sorting of integer sequences.
  • LANGUAGE MODELLING: Evaluated using the TinyStories dataset to test natural language completion.

Key Insights and Ideas

  • Localization of Bias: Inductive biases do not diffuse evenly across the network. For instance, bracket-matching ($k$-DYCK) and identity tasks build useful structures primarily within the attention layers. Conversely, cellular automata (ECA) impart structural utility almost exclusively to the MLP blocks.
  • "Frankenstein" Initializations Work: Because these structures are remarkably modular, components can be harvested from entirely different models. An initialization combining the attention layers of a SET-trained model and the MLPs of an ECA-trained model yields a network capable of succeeding at multiple distinct downstream tasks.
  • Structure Over Magnitude: Shuffling weights destroys the precise circuit structures while preserving the overall distribution and magnitude of the weights. Doing this severely tanks performance on tasks like HAYSTACK and ADDITION, proving that procedural pretraining creates non-trivial, highly precise structural circuits.

Key Results

  • Beating Natural Language: For the MULTIPLICATION task, pretraining on simple procedural data actually outperformed pretraining on a massive natural language corpus (OpenWebText).
  • Less is More in Transfer: Selective transfer frequently blows full-model transfer out of the water. When transferring an IDENTITY-pretrained model to the HAYSTACK task, transferring only the attention layers yielded 99.0% accuracy, compared to a dismal 18.8% when transferring the full model. Transferring task-specific MLPs can actively interfere with downstream learning.
  • Successful Composition: The modularly composed model (SET Attention + ECA MLPs) achieved strong performance across four distinct tasks simultaneously (94.4% on HAYSTACK, 80.3% on ADDITION, 82.9% on REVERSED ADDITION, and 99.4% on SORTING), whereas individual pretraining tasks failed on at least one.

Limitations and Future Work

While the empirical results are compelling, the paper lacks a first-principles mathematical explanation for why specific procedural tasks map to specific downstream capabilities (e.g., why standard $k$-DYCK helps HAYSTACK retrieval, but $k$-DYCK SHUFFLE does not).

Additionally, these experiments were conducted on small, controlled transformer architectures. Scaling these insights to multi-billion parameter models and optimizing data mixtures for generalist LLMs remain open challenges. Finally, the authors pose an exciting question: if we can precisely characterize these emergent procedural structures, can we skip the computationally expensive pretraining phase entirely and initialize weights using a closed-form mathematical recipe?

Summary updated

BibTeX
@inproceedings{shinnick2025transformers,
  author = {Zachary Shinnick and Liangze Jiang and Hemanth Saratchandran and Anton van den Hengel and Damien Teney},
  title = {{Transformers Pretrained on Procedural Data Contain Modular Structures for Algorithmic Reasoning}},
  booktitle = {MOSS @ ICML},
  year = {2025},
  url = {https://openreview.net/forum?id=mMfVdDyd4h},
}
Figure