Transformers meet Neural Algorithmic Reasoners

Wilfried Bounsi, Borja Ibarz, Andrew Dudzik, Jessica B. Hamrick, Larisa Markeeva, Alex Vitvitskyi, Razvan Pascanu, Petar Velickovic · DOI

Abstract

While Large Language Models (LLMs) demonstrate exceptional proficiency in natural language understanding, they frequently struggle with algorithmic reasoning tasks, particularly when required to generalize beyond the sequence lengths seen during training. This paper explores TransNAR, a hybrid architecture that integrates the linguistic capabilities of Transformers with the structural, algorithmic robustness of Graph Neural Networks (GNNs). By utilizing a cross-attention mechanism to allow a Transformer to consult a pre-trained Neural Algorithmic Reasoner (NAR), the model achieves significant performance gains in out-of-distribution (OOD) tasks, demonstrating a more robust approach to logical computation in neural models.

Introduction

Large Language Models have revolutionized the field of artificial intelligence, yet they remain notoriously "brittle" when faced with algorithmic logic. A standard Transformer may successfully sort a list of five numbers but fail catastrophically when presented with twenty. This failure in length generalization, or out-of-distribution extrapolation, suggests that Transformers often rely on pattern matching rather than an underlying understanding of algorithmic processes.

In contrast, Neural Algorithmic Reasoners (NARs)—typically implemented as GNNs—are designed to learn the underlying logic of algorithms. They exhibit high robustness, often generalizing perfectly from small graphs to much larger ones. However, NARs are specialized and "narrow," requiring inputs to be perfectly formatted as graphs and lacking the ability to process natural language. TransNAR bridges this gap, positioning the NAR as a high-dimensional "internal tool" for the Transformer.

The TransNAR Architecture

The TransNAR model introduces a hybrid framework that interleaves standard Transformer layers with gated cross-attention layers. The architecture consists of three primary components:

  1. The NAR Component: The researchers utilize a pre-trained Triplet-GMPNN, a GNN trained on the CLRS-30 benchmark, which encompasses 30 classic algorithms (e.g., Dijkstra’s, Quicksort). To maintain the algorithmic integrity learned during its initial training, the NAR remains frozen during the hybrid training phase.
  2. The Transformer Component: A 70-million parameter decoder-only Transformer serves as the primary interface for processing textual data.
  3. The Cross-Attention "Glue": Inspired by the Flamingo architecture, TransNAR allows text tokens in the Transformer to attend to the node and edge embeddings generated by the NAR. In this setup, text tokens act as queries, while the NAR’s structural embeddings provide the keys and values, allowing the Transformer to "read" the NAR's internal logic.

Training Strategy

The training process is conducted in two phases. First, the NAR is trained on graph-based tasks to master algorithmic logic. Second, the Transformer is trained on textual versions of these tasks, learning to extract and utilize the necessary logic from the NAR’s latent space to produce correct textual outputs.

Experimental Methodology

To evaluate the model, the study utilizes CLRS-Text, a textual adaptation of the CLRS-30 benchmark. This dataset converts graph-based problems into deterministic string representations.

The evaluation regime is designed to test both interpolation and extrapolation:

  • Training: Problem sizes of 4, 8, and 12.
  • Interpolation Testing: Problem size 10.
  • Extrapolation (OOD) Testing: Problem size 14.

This rigorous setup determines whether the model has truly internalized the algorithm or is simply memorizing patterns for specific input lengths.

Results and Discussion

Massive OOD Gains

TransNAR consistently outperformed baseline Transformers across nearly all 30 algorithms in the CLRS-30 suite. In categories such as Greedy algorithms and Sorting, the hybrid model showed over a 20% absolute improvement in execution accuracy for OOD tasks. In several instances where the baseline Transformer achieved near-zero accuracy, TransNAR enabled significant, non-zero performance.

Solving the "Shape" Failure

A critical insight from the study involves the "Shape Score." Standard Transformers often fail OOD tasks because they lose track of the required output structure (e.g., providing 10 outputs when 14 are required). TransNAR significantly improves this score, suggesting that the NAR provides a structural "skeleton" that guides the Transformer’s textual generation.

Synergy with Positional Encodings

The authors noted that the NAR provides the logic, but the Transformer still requires the correct spatial tools to organize its output. The use of randomized positional encodings was found to be essential; without them, the Transformer struggled to handle longer sequences even with the NAR's assistance.

Limitations and Future Work

Despite its success, TransNAR has limitations. It currently requires dual inputs—both the text description and a pre-processed graph representation—limiting its applicability to scenarios where graph data is readily available. Additionally, the model still faces challenges with tasks involving index-based searches, such as Binary Search, likely due to difficulties in generalizing unseen index boundaries.

Future research may focus on distillation, where a pure Transformer is trained to mimic the TransNAR hybrid. This could potentially allow the algorithmic robustness of a GNN to be baked directly into a unimodal language model, removing the need for dual inputs while retaining superior reasoning capabilities.

Conclusion

TransNAR represents a significant step toward more robust neural reasoning. By treating algorithmic reasoners as internal latent tools rather than external APIs, the architecture allows Transformers to leverage structural logic to overcome their inherent limitations in length generalization. This hybrid approach paves the way for more reliable AI systems capable of handling complex, algorithmic tasks in natural language contexts.

Summary updated

BibTeX
@misc{bounsi2024transformers,
  author = {Wilfried Bounsi and Borja Ibarz and Andrew Dudzik and Jessica B. Hamrick and Larisa Markeeva and Alex Vitvitskyi and Razvan Pascanu and Petar Velickovic},
  title = {{Transformers meet Neural Algorithmic Reasoners}},
  year = {2024},
  eprint = {2406.09308},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2406.09308},
}
Figure