---
title: "Nyströmformer: Approximating self-attention in linear time and memory via the Nyström method"
description: "Nyströmformer replaces quadratic softmax self-attention with an O(n) approximation built on the Nyström method, which reconstructs a matrix from a sampled subset of its rows and columns via a Moore-Penrose pseudoinverse."
url: "https://swe.quest/articles/nystromformer-approximating-self-attention-in-linear-time-and-memory-via-the-fd6613fb9ac42d67"
canonical: "https://huggingface.co/blog/nystromformer"
source: "Hugging Face"
published: "2022-08-02"
difficulty: "hard"
kind: "Deep dive"
reading_minutes: 6
tags: ["llms","machine-learning","algorithms","python"]
---

# Nyströmformer: Approximating self-attention in linear time and memory via the Nyström method

A post on [Hugging Face](https://swe.quest/sources/hugging-face.md), published 2022-08-02, 6 min read, Deep dive. Original: [https://huggingface.co/blog/nystromformer](https://huggingface.co/blog/nystromformer)

## Summary

Nyströmformer replaces quadratic softmax self-attention with an O(n) approximation built on the Nyström method, which reconstructs a matrix from a sampled subset of its rows and columns via a Moore-Penrose pseudoinverse. Sampling columns of the softmax matrix directly is impossible because each entry needs its full row for normalization. The model therefore builds landmarks from segment means of queries and keys and composes three small softmax kernels (n×m, m×m pseudoinverse, m×n), so QKᵀ is never computed. The paper reports 32–64 landmarks staying competitive at sequence lengths of 4096–8192. A walkthrough of the Hugging Face PyTorch implementation follows, along with masked-language-modeling usage of the released checkpoints.

Written by swe.quest, not by the author.

**Difficulty:** Hard: written for specialists in the field

## Topics

- [LLMs](https://swe.quest/tags/llms.md) (AI & ML)
- [Machine learning](https://swe.quest/tags/machine-learning.md) (AI & ML)
- [Algorithms](https://swe.quest/tags/algorithms.md) (Architecture & design)
- [Python](https://swe.quest/tags/python.md) (Languages)

## Prerequisites

- Transformer self-attention
- linear algebra and matrix pseudoinverse
- PyTorch basics

## Keywords

Nyströmformer, Nyström method, Hugging Face Transformers, PyTorch, Moore-Penrose pseudoinverse

## Related posts

- [Perceiver IO: a scalable, fully-attentional model that works on any modality](https://swe.quest/articles/perceiver-io-a-scalable-fully-attentional-model-that-works-on-any-modality-f8a43e7ae2c87797.md): Hugging Face · 2021-12-15 · Hard · 21 min read · Machine learning, LLMs, Computer vision, Python
  Perceiver IO avoids the quadratic cost of self-attention by running attention over a small fixed set of latent variables (e.g. 256×1280).
- [Hugging Face Reads, Feb. 2021 - Long-range Transformers](https://swe.quest/articles/hugging-face-reads-feb-2021-long-range-transformers-59eb38f5969384a8.md): Hugging Face · 2021-03-09 · Hard · 8 min read · LLMs, Machine learning, Model training
  Long-range attention methods attack the quadratic memory and time cost of self-attention in sequence length, and four papers illustrate the main families.
- [Fine-tuning LLMs to 1.58bit: extreme quantization made easy](https://swe.quest/articles/fine-tuning-llms-to-1-58bit-extreme-quantization-made-easy-1f532a3df4c4d82f.md): Hugging Face · 2024-09-18 · Hard · 28 min read · Inference & serving, Model training, LLMs
  Existing LLMs can be fine-tuned into BitNet's 1.58-bit ternary format (-1, 0, 1) instead of pre-training from scratch, and a Llama3 8B fine-tuned this way on 100B tokens outperforms Llama 1 7B on MMLU.
- [A Gentle Introduction to 8-bit Matrix Multiplication for transformers at scale using transformers, accelerate and bitsandbytes](https://swe.quest/articles/a-gentle-introduction-to-8-bit-matrix-multiplication-for-transformers-at-scale-af5b5f40a9067e95.md): Hugging Face · 2022-08-17 · Medium · 21 min read · Inference & serving, LLMs, Machine learning, Hardware
  LLM.int8() makes 8-bit inference of 175B-class transformers possible with no measurable quality loss.
- [Quanto: a PyTorch quantization backend for Optimum](https://swe.quest/articles/quanto-a-pytorch-quantization-backend-for-optimum-8330ba5aea428b63.md): Hugging Face · 2024-03-18 · Medium · 5 min read · Inference & serving, Python, Machine learning, Open source
  Quanto is a PyTorch quantization backend for Optimum that works in eager mode on any device (CPU, CUDA, MPS), supports int2/int4/int8/float8 weights and int8/float8 activations, and accelerates int8-int8, fp16-int4, bf16-int8 and bf16-int4 matmuls on CUDA.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
