Papers › Self-attention Does Not Need O(n²) Memory

Self-attention Does Not Need O(n²) Memory

10 Dec 2021arXiv:2112.05682archive 2025-07-28

Markus N. Rabe, Charles Staats

We present a very simple algorithm for attention that requires O(1) memory with respect to sequence length and an extension to self-attention that requires O(logn) memory. This is in contrast with the frequently stated belief that self-attention requires O(n²) memory. While the time complexity is still O(n²), device memory rather than compute capability is often the limiting factor on modern accelerators. Thus, reducing the memory requirements of attention allows processing of longer sequences than might otherwise be feasible. We provide a practical implementation for accelerators that requires O(√(n)) memory, is numerically stable, and is within a few percent of the runtime of the standard implementation of attention. We also demonstrate how to differentiate the function while remaining memory-efficient. For sequence length 16384, the memory overhead of self-attention is reduced by 59X for inference and by 32X for differentiation.

PaperPDFCodeCode Syntology ran

In Syntology Open this paper in Syntology's Atlas, the map of the papers in Syntology's graph and their citations.

For agents, Syntology's MCP tool lists every function and class Syntology harvested from this paper and whether it ran (how to connect): get_harvested_code_for_paper(arxiv_id="2112.05682")

Code

Syntology Ran 17 of 36 code samples harvested from 8 repositories linked to this paper; 19 have no recorded run. Of those that ran: 1 ran · honoured contract; 6 ran · violated contract; 7 ran · our draft was wrong; 2 ran · fixture could not drive it; 1 ran with no contract checked.

By repository: named in the paper: 7 samples from 1 repository, 5 ran; community (archive-listed): 28 samples from 7 repositories, 11 ran; 1 identical to code first harvested elsewhere. The run record, sample by sample. “Ran” means executed on a synthesized input, not that the code is correct or reproduces the paper.

17 repositories listed; official and paper-mentioned ones first.

lucidrains/memory-efficient-attention-pytorch mentioned in papermentioned on GitHubpytorchMIT report
BirgerMoell/SwedishLLMBenchmark mentioned on GitHubpytorchApache-2.0 report
Peter-Devine/multilingual_mt_bench mentioned on GitHubpytorchApache-2.0 report
SforAiDl/vformer mentioned on GitHubpytorch report
X-iZhang/Libra mentioned on GitHubpytorchApache-2.0 report
aqlaboratory/openfold mentioned on GitHubjax report
baaivision/judgelm mentioned on GitHubpytorch report
beckschen/llavolta mentioned on GitHubpytorchApache-2.0 report
jihaonew/mm-instruct mentioned on GitHubpytorchApache-2.0 report
lightblue-tech/multilingual-mt-bench mentioned on GitHubpytorchApache-2.0 report
lm-sys/fastchat mentioned on GitHubpytorch report
lucidrains/En-transformer mentioned on GitHubpytorch report
lucidrains/flash-cosine-sim-attention mentioned on GitHubpytorch report
stability-ai/fastchat mentioned on GitHubpytorchApache-2.0 report

Repository list and official/mentioned flags are the archive's, frozen 2025-07-28. Reachability, where shown, is from one Syntology probe window (2026-09-16 to 2026-09-18); repositories not probed show nothing. GitHub stars are not tracked.

Code Syntology ran Syntology

36 samples harvested; 17 ran; 1 honoured the contract we drafted; 19 have no recorded run. Read from Syntology's graph 2026-09-24; that is when this build read the record, not when the samples ran.

1ran · honoured contract
6ran · violated contract
7ran · our draft was wrong
2ran · fixture could not drive it
1ran
19unverified

Licence: 2 of the 36 samples are pointer only, meaning Syntology does not serve that copy's text. This page shows no code text for any sample; each one links to its file in the repository.

Harvested from 8 repositories linked to this paper, official or community; each sample names its own and says which. Some samples are identical code Syntology first harvested from another repository; for those, this paper's copy is not located and its licence is not recorded. “Ran” means the sample executed on a synthesized input. It does not mean the output is correct, and nothing here reproduces the paper's results. “Honoured” and “violated” refer to a contract Syntology drafted from the code itself; “our draft was wrong” and “fixture could not drive it” are failures of Syntology's instrument, not of the code.

Each sample ends with its code_sha256, Syntology's identity for that exact code. An agent fetches the stored sample with Syntology's MCP tool get_code(code_sha256="…") (how to connect); click an identity to copy that call.

Repository labels, per sample. official repository: The archive marks this repository official for the paper. named in the paper: The archive records that the paper mentions this repository; it is not marked official. community (archive-listed): In the archive's code links for this paper, not marked official and not recorded as mentioned in the paper. found in paper text by Syntology: Syntology found this repository in the paper's own text; whether it is the authors' implementation is not asserted. community: Not in the archive's code links for this paper; a community repository Syntology harvested. Samples from a repository marked official are listed first. Licence labels name the repository's licence as recorded at harvest. “Pointer only” means Syntology does not serve that copy's text, for one of four reasons: no licence file was found; the licence was not identified; the licence is recorded as permissive but that copy's record is not marked cleared; or the licence is outside the permissive list Syntology serves text under (MIT, Apache-2.0, BSD and similar). Some licences outside that list permit redistribution, such as WTFPL, and GPL-3.0 under its conditions; they are simply not on the list. Hover a licence label for the reason. File links open the file on GitHub at the default branch, which may have changed since the harvest.

default lucidrains/memory-efficient-attention-pytorch/memory_efficient_attention_pytorch/cosine_sim_flash_attention.py named in the paper ran · violated contract fingerprinted MIT (permissive) · 60fff7c3c400d7ff · report
eval_decorator lucidrains/memory-efficient-attention-pytorch/memory_efficient_attention_pytorch/autoregressive_wrapper.py named in the paper ran · our draft was wrong MIT (permissive) · c16aee9490eb8729 · report
exists lucidrains/memory-efficient-attention-pytorch/memory_efficient_attention_pytorch/autoregressive_wrapper.py named in the paper ran · violated contract MIT (permissive) · aa5486a3650902d8 · report
l2norm lucidrains/memory-efficient-attention-pytorch/memory_efficient_attention_pytorch/cosine_sim_flash_attention.py named in the paper ran · our draft was wrong fingerprinted MIT (permissive) · 3eb58dae620046d3 · report
top_k lucidrains/memory-efficient-attention-pytorch/memory_efficient_attention_pytorch/autoregressive_wrapper.py named in the paper ran · our draft was wrong fingerprinted MIT (permissive) · 3b65d5d77510c964 · report
attention lucidrains/memory-efficient-attention-pytorch/memory_efficient_attention_pytorch/memory_efficient_attention.py named in the paper unverified MIT (permissive) · 640155bcba1f2f92 · report
route_args lucidrains/memory-efficient-attention-pytorch/memory_efficient_attention_pytorch/reversible.py named in the paper unverified MIT (permissive) · 1f7d4befc2e41402 · report
create_tensors facebookresearch/xformers/xformers/benchmarks/benchmark_mem_eff_attention.py community (archive-listed) ran · fixture could not drive it licence not identified · pointer only · 68b3637c9b987a61 · report
default lucidrains/flash-cosine-sim-attention/flash_cosine_sim_attention/flash_cosine_sim_attention.py community (archive-listed) ran · violated contract MIT (permissive) · 915fbeb88e13d4a3 · report
efficient_dot_product_attention AminRezaei0x443/memory-efficient-attention/memory_efficient_attention/attention_jax.py community (archive-listed) ran · fixture could not drive it MIT (permissive) · 6b1a95182607abd6 · report
expand2square jihaonew/mm-instruct/llava/mm_utils.py community (archive-listed) ran · our draft was wrong Apache-2.0 (permissive) · 592b3c1a88f93d7c · report
get_chunk X-iZhang/Libra/libra/eval/eval_vqa_libra.py community (archive-listed) ran · our draft was wrong fingerprinted Apache-2.0 (permissive) · 42a46570620cd9fa · report
is_none jihaonew/mm-instruct/llava/eval/model_vqa_mmbench.py community (archive-listed) ran · violated contract Apache-2.0 (permissive) · bae18947b56f2be1 · report
load_image jihaonew/mm-instruct/predict.py community (archive-listed) ran · honoured contract Apache-2.0 (permissive) · 9b3c1cb391672ccb · report
load_image_from_base64 X-iZhang/Libra/libra/mm_utils.py community (archive-listed) ran Apache-2.0 (permissive) · c3ee9d07c900dd55 · report
max_neg_value lucidrains/En-transformer/en_transformer/en_transformer.py community (archive-listed) ran · our draft was wrong fingerprinted MIT (permissive) · f2fe51a4a5646c6f · report
raise_warning_for_incompatible_cpu_offloading_configuration stability-ai/fastchat/fastchat/model/model_adapter.py community (archive-listed) ran · violated contract Apache-2.0 (permissive) · 0bf414a2b44babe6 · report
split_list X-iZhang/Libra/libra/eval/eval_vqa_libra.py community (archive-listed) ran · our draft was wrong fingerprinted Apache-2.0 (permissive) · 076c252c52cbb161 · report
calculate_tem_score X-iZhang/Libra/libra/eval/temporal_f1.py community (archive-listed) unverified Apache-2.0 (permissive) · 36090e75c451149f · report
clean_text X-iZhang/Libra/libra/eval/radiology_report.py community (archive-listed) unverified Apache-2.0 (permissive) · 46c06fec5aedf4a4 · report
clean_text X-iZhang/Libra/libra/eval/temporal_f1.py community (archive-listed) unverified Apache-2.0 (permissive) · cb00b798f297e5f3 · report
compress stability-ai/fastchat/fastchat/model/compression.py community (archive-listed) unverified Apache-2.0 (permissive) · 80dde39da1ba3708 · report
efficient_dot_product_attention AminRezaei0x443/memory-efficient-attention/memory_efficient_attention/attention_torch.py community (archive-listed) unverified MIT (permissive) · bdb24836d5607016 · report
expand2square X-iZhang/Libra/libra/mm_utils.py community (archive-listed) unverified Apache-2.0 (permissive) · 1fe910f8046380d4 · report
extract_entities X-iZhang/Libra/libra/eval/temporal_f1.py community (archive-listed) unverified Apache-2.0 (permissive) · c17fb3de0d47f94a · report
extract_sections X-iZhang/Libra/libra/eval/radiology_report.py community (archive-listed) unverified Apache-2.0 (permissive) · fa3bb9e402f7460c · report
generate_stream_codet5p stability-ai/fastchat/fastchat/model/model_codet5p.py community (archive-listed) unverified Apache-2.0 (permissive) · f545ae5663fedf88 · report
get_compressed_list stability-ai/fastchat/fastchat/model/compression.py community (archive-listed) unverified Apache-2.0 (permissive) · c5824da57107fe8c · report
get_conv_template stability-ai/fastchat/fastchat/conversation.py community (archive-listed) unverified Apache-2.0 (permissive) · 2613e53cccc8a4b4 · report
load_compress_model stability-ai/fastchat/fastchat/model/compression.py community (archive-listed) unverified Apache-2.0 (permissive) · 8dacf011c6f243dc · report
load_json X-iZhang/Libra/libra/eval/radiology_report.py community (archive-listed) unverified Apache-2.0 (permissive) · a3698e4aaee89be7 · report
process_images jihaonew/mm-instruct/llava/mm_utils.py community (archive-listed) unverified Apache-2.0 (permissive) · 344dff4791fd1381 · report
process_images X-iZhang/Libra/libra/mm_utils.py community (archive-listed) unverified Apache-2.0 (permissive) · 012fae59ee4131ba · report
process_response stability-ai/fastchat/fastchat/model/model_chatglm.py community (archive-listed) unverified Apache-2.0 (permissive) · dc21db7cf25bfe72 · report
read_img jihaonew/mm-instruct/llava/eval/model_vqa_loader.py community (archive-listed) unverified Apache-2.0 (permissive) · 44fab12a908ab057 · report
divisible_by identical code first harvested elsewhere ran · violated contract fingerprinted licence of this copy not recorded · 856d86b39c6005b4 · report

Results from the paper archive 2025-07-28

No leaderboard rows for this paper in the archive.

Methods

AttentionLinear LayerSoftmax

Report a problem or propose a change · a person checks every report against the paper or source before anything changes; decisions are listed on /corrections