In-Context Algorithm Emulation in Fixed-Weight Transformers

Jerry Yao-Chieh Hu, Hude Liu, Jennifer Yuntong Zhang, Han Liu
8/24/2025
cs.LGcs.AIstat.ML

No official code found. Best implementation: MAGICS-LAB/algo_emu (3 stars, Apache-2.0, last commit 2026-03-10).

Abstract

We prove that a minimal Transformer with frozen weights emulates a broad class of algorithms by in-context prompting. We formalize two modes of in-context algorithm emulation. In the task-specific mode, for any continuous function $f: \mathbb{R} \to \mathbb{R}$, we show the existence of a single-head softmax attention layer whose forward pass reproduces functions of the form $f(w^\top x - y)$ to arbitrary precision. This general template subsumes many popular machine learning algorithms (e.g., gradient descent, linear regression, ridge regression). In the prompt-programmable mode, we prove universality: a single fixed-weight two-layer softmax attention module emulates all algorithms from the task-specific class (i.e., each implementable by a single softmax attention) via only prompting. Our key idea is to construct prompts that encode an algorithm's parameters into token representations, creating sharp dot-product gaps that force the softmax attention to follow the intended computation. This construction requires no feed-forward layers and no parameter updates. All adaptation happens through the prompt alone. Numerical results corroborate our theory. These findings forge a direct link between in-context learning and algorithmic emulation, and offer a simple mechanism for large Transformers to serve as prompt-programmable libraries of algorithms. They illuminate how GPT-style foundation models may swap algorithms via prompts alone, and establish a form of algorithmic universality in modern Transformer models.

View on arXivView PDF

Code Implementations(1)

Apache-2.0

In‑Context Algorithm Emulation in Fixed‑Weight Transformers

31PythonAug 21, 20257 months agoApache-2.0
algorithm-emulationalgorithm-swappingattention-mechanismfoundation-modelin-context-algorithm-emulation+3 more

Similar Papers

Cite this paper

@article{hu2025incontext,
  title  = {In-Context Algorithm Emulation in Fixed-Weight Transformers},
  author = {Jerry Yao-Chieh Hu and Hude Liu and Jennifer Yuntong Zhang and Han Liu},
  year   = {2025},
  eprint = {2508.17550},
  archivePrefix = {arXiv},
  url    = {http://arxiv.org/abs/2508.17550v2}
}

Add a badge to your README

Shows whether official code is linked for this paper.

Code of Paper badge
[![Code of Paper](https://codeofpaper.com/badge/2508.17550.svg)](https://codeofpaper.com/paper/2508.17550)

Discussion