[Paper Review] Transformers meet Neural Algorithmic Reasoners
This paper proposes TransNAR, a hybrid model that integrates a pre-trained graph neural network-based Neural Algorithmic Reasoner (NAR) with a Transformer to enhance algorithmic reasoning in natural language tasks. By enabling cross-attention between the Transformer’s tokens and the NAR’s node embeddings through a two-phase training procedure, TransNAR achieves significant improvements—over 20% absolute gain—in out-of-distribution generalization on the CLRS-Text benchmark, particularly in shape and CLRS scores, outperforming pure Transformer models.
Transformers have revolutionized machine learning with their simple yet effective architecture. Pre-training Transformers on massive text datasets from the Internet has led to unmatched generalization for natural language understanding (NLU) tasks. However, such language models remain fragile when tasked with algorithmic forms of reasoning, where computations must be precise and robust. To address this limitation, we propose a novel approach that combines the Transformer's language understanding with the robustness of graph neural network (GNN)-based neural algorithmic reasoners (NARs). Such NARs proved effective as generic solvers for algorithmic tasks, when specified in graph form. To make their embeddings accessible to a Transformer, we propose a hybrid architecture with a two-phase training procedure, allowing the tokens in the language model to cross-attend to the node embeddings from the NAR. We evaluate our resulting TransNAR model on CLRS-Text, the text-based version of the CLRS-30 benchmark, and demonstrate significant gains over Transformer-only models for algorithmic reasoning, both in and out of distribution.
Motivation & Objective
- To address the brittleness of pure Transformer models in algorithmic reasoning tasks, especially under out-of-distribution generalization.
- To bridge the gap between natural language understanding and robust algorithmic computation by integrating a pre-trained Neural Algorithmic Reasoner (NAR) into a Transformer architecture.
- To enable Transformers to generalize effectively to unseen input sizes and problem structures by grounding their reasoning in structured, GNN-based embeddings.
- To demonstrate that hybrid architectures combining language models with algorithmic reasoners can achieve superior and more reliable performance on complex, symbolic reasoning tasks in text.
Proposed method
- The TransNAR model uses a two-phase training procedure: first pre-training the NAR on graph-structured algorithmic tasks, then fine-tuning the hybrid Transformer-NAR system with cross-attention.
- The Transformer attends to node and edge embeddings from the NAR via learnable cross-attention layers, allowing it to dynamically access structured algorithmic knowledge during decoding.
- The NAR is a pre-trained GNN-based model (e.g., Triplet-GMPNN) capable of executing multiple algorithms on graph inputs, providing robust, size-generalizable reasoning.
- Cross-attention is applied at multiple layers, enabling the Transformer to modulate its token representations using high-dimensional NAR embeddings throughout the sequence.
- The model is evaluated on CLRS-Text, a text-based version of the CLRS-30 benchmark, using multi-faceted evaluation metrics including shape, parsing, and CLRS scores.
- A hybrid architecture with gated cross-attention layers interleaved with standard Transformer layers enables bidirectional information flow between the language model and the NAR.
![Figure 1 : Our TransNAR architecture, with its direct synergy of Transformers and Neural Algorithmic Reasoners, yields clear improvements in out-of-distribution reasoning across wide categories of algorithmic tasks in CLRS-Text [ 20 ] , a textual version of the CLRS-30 benchmark [ 35 ] . Here, the $](https://ar5iv.labs.arxiv.org/html/2406.09308/assets/icml2024/imgs/block-plot.png)
Experimental results
Research questions
- RQ1Can a pre-trained Neural Algorithmic Reasoner (NAR) be effectively integrated into a Transformer to improve algorithmic reasoning in natural language inputs?
- RQ2Does the fusion of a language model with a GNN-based NAR lead to improved out-of-distribution generalization on algorithmic tasks compared to pure Transformers?
- RQ3What role do NAR embeddings play in mitigating common failure modes of LLMs, such as incorrect output shape or parsing errors, during algorithmic reasoning?
- RQ4Can cross-attention mechanisms between the Transformer and NAR embeddings enable more robust and accurate reasoning across diverse algorithmic families and input sizes?
- RQ5What are the limitations of the current hybrid approach, particularly in handling tasks involving index-based search or novel boundary conditions?
Key findings
- TransNAR achieves over 20% absolute improvement in CLRS score on several algorithmic families in out-of-distribution settings compared to baseline Transformers.
- The model significantly improves shape accuracy, indicating it alleviates a major failure mode where LLMs produce outputs of incorrect length or structure.
- On tasks like Binary Search, Find Maximum Subarray, Minimum, and Quickselect—where index-based search is central—TransNAR fails to outperform the baseline, suggesting a shared failure mode related to unseen index boundaries.
- The shape score is consistently higher for TransNAR, showing that grounding outputs in NAR embeddings increases the likelihood of generating structurally correct responses.
- Parsing accuracy remains high across both models, indicating that the primary failure mode lies not in parsing but in structural and semantic correctness of the output.
- The results demonstrate that NAR-augmented LLMs can achieve emergent out-of-distribution generalization where pure Transformers fail entirely, especially in extrapolation regimes.

Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.