AlgoFormer: An Efficient Transformer Framework with Algorithmic Structures

Yihang Gao, Chuanyang Zheng, Enze Xie, Han Shi, Tianyang Hu, Yu Li, Michael Ng, Zhenguo Li, Zhaoqiang Liu

Abstract

AlgoFormer is a novel transformer framework that explicitly mimics the structure of human-designed algorithms by organizing layers into pre-processing, iterative looping, and post-processing modules. By incorporating these "algorithmic priors," the model achieves superior efficiency and performance on both complex synthetic reasoning tasks and real-world language applications like translation and classification.

Introduction

Transformers have revolutionized artificial intelligence, yet their internal mechanisms remain a subject of intense study. Recent research suggests that Transformers do not merely approximate functions but learn to execute "in-context" algorithms, such as gradient descent, to solve problems. However, standard Transformers are typically structured as flat stacks of layers, which may not be the most efficient representation for multi-step algorithmic logic. Previous attempts to address this, such as "looped transformers" that reuse weights, often lack the flexibility required for complex data preparation or result refinement. This paper introduces AlgoFormer, a framework that bridges the gap between flexible neural networks and structured algorithmic logic.

The AlgoFormer Architecture

The core innovation of AlgoFormer is its tripartite functional organization, designed to mirror the flow of classical algorithms. By forcing the model into a pre-loop-post structure, the authors provide a "structural prior" that guides the model toward algorithmic solutions.

  1. Pre-transformer ($TF_{pre}$): This module handles task preprocessing, transforming raw input into a mathematical representation suitable for iterative optimization.
  2. Looped transformer ($TF_{loop}$): Serving as the model's "engine," this block consists of weight-shared layers applied iteratively. This structure mimics the "for-loop" or "while-loop" constructs found in optimization algorithms like Gradient Descent or Newton’s Method.
  3. Post-transformer ($TF_{post}$): This module performs post-processing, refining the output of the iterative solver to produce final results, such as category labels or translated tokens.
The AlgoFormer architecture maps specific algorithmic stages (preprocessing, looping, post-processing) to distinct transformer modules.

A key component of the framework is a training strategy that minimizes loss across a range of iteration counts. This ensures the model learns a stable, iterative procedure rather than memorizing a fixed sequence of steps. Remarkably, AlgoFormers trained on a specific number of loops often demonstrate improved performance when run for significantly more steps during inference, suggesting the learning of truly convergent algorithms.

Experimental Evaluation

The researchers evaluated AlgoFormer across synthetic and real-world domains. Synthetic tasks included regression with representation learning, Autoregressive (AR) time-series modeling, and "Chain-of-Thought" (CoT) reasoning using Multi-Layer Perceptrons (MLPs). These tasks specifically tested the model's ability to learn underlying mathematical rules.

Comparison of square error across in-context samples for Standard, Looped, and AlgoFormer models on synthetic tasks.

Real-world applications focused on Neural Machine Translation (NMT) using the IWSLT 2015 German-English dataset and text classification across five benchmarks: AG News, IMDB, DBPedia, Yelp Review, and Yahoo News.

Key Results and Insights

AlgoFormer consistently outperformed both standard and vanilla looped Transformers across all benchmarks:

  • Machine Translation: On the German-to-English task, AlgoFormer achieved a BLEU score of 14.72, significantly higher than the standard Transformer (9.30) and the vanilla looped version (10.56).
  • Text Classification: The model set new performance benchmarks, notably hitting 97.92% accuracy on AG News (vs. 92.07% for the standard model) and 81.25% on Yahoo News (vs. 73.96% for the standard model).
  • Weight Efficiency: Because the "looped" section reuses weights across many iterations, AlgoFormer achieves high expressive power with significantly fewer parameters than standard models of equivalent depth.
  • Robustness: In synthetic regression tasks, AlgoFormer proved more robust to noise than traditional human-designed algorithms like Newton's method, maintaining lower error rates in high-noise environments.

Conclusion and Future Work

AlgoFormer represents a significant step toward more interpretable and efficient AI architectures by providing a structural prior that facilitates algorithmic learning. While the current study demonstrates success on medium-scale tasks, future work is needed to scale these algorithmic structures to the size of Large Language Models (LLMs). Additionally, exploring more complex logic, such as nested loops or multi-processing within the transformer framework, remains a promising avenue for research.

Summary updated

BibTeX
@article{gao2025algoformer,
  author = {Yihang Gao and Chuanyang Zheng and Enze Xie and Han Shi and Tianyang Hu and Yu Li and Michael Ng and Zhenguo Li and Zhaoqiang Liu},
  title = {{AlgoFormer: An Efficient Transformer Framework with Algorithmic Structures}},
  journal = {Trans. Mach. Learn. Res.},
  year = {2025},
  url = {https://openreview.net/forum?id=zAxNPrBD2p},
}
Figure