Papers › Self-attention Does Not Need O(n²) Memory
Self-attention Does Not Need O(n²) Memory
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.
In Syntology View this paper on Syntology, its page in Syntology's graph. That page adds which of its functions are also present in other papers' code Syntology holds, with dates, and its dated run record as JSON and a README badge. It also lists the repositories linked to the paper, the abstract and the calls for agents.
Open this paper in Syntology's Atlas, the map of the papers in Syntology's graph and their citations.
For agents, on Syntology's MCP service (how to connect):
get_harvested_code_for_paper(arxiv_id="2112.05682")get_code(code_sha256="…")with a sample'scode_sha256from the list belowget_citation_path(paper_1="2112.05682", paper_2="…")with another paper's arXiv id or titleget_concepts_for_paper(arxiv_id="2112.05682")
Code
No sample from the archive's official repository (google-research/google-research) is linked to this paper; the 35 samples below come from 8 repositories the archive does not mark official, and 1 more is identical to code Syntology harvested from another paper's repository. Syntology harvested code from google-research/google-research for 24 other papers.
google-research/google-research: Syntology's graph links these samples to Efficient Neural Architecture Search via Parameter Sharing (arXiv:1802.03268; that paper's own run record: 18 ran (of which 10 constructed an object rather than computing a result; 12 with no instrument failure: 1 honoured, 0 violated, 11 with no contract checked; 6 where Syntology's instrument failed) · 20 unverified), Milking CowMask for Semi-Supervised Image Classification (arXiv:2003.12022; that paper's own run record: 1 ran (of which 1 constructed an object rather than computing a result; 1 with no instrument failure: 0 honoured, 0 violated, 1 with no contract checked; 0 where Syntology's instrument failed) · 1 unverified; the one sample that ran constructed an object rather than computing a result), Neural Additive Models: Interpretable Machine Learning with Neural Nets (arXiv:2004.13912; that paper's own run record: 26 ran (of which 14 constructed an object rather than computing a result; 26 with no instrument failure: 5 honoured, 0 violated, 21 with no contract checked; 0 where Syntology's instrument failed) · 7 unverified) and 21 more (every one in this page's JSON twin); nothing checks that they implement this paper's method.
Syntology 36 code samples: 35 from 8 repositories linked to this paper, 1 identical to code harvested from another paper's repository. Across all 36: 33 ran (of which 0 constructed an object rather than computing a result; 21 with no instrument failure: 3 honoured, 6 violated, 12 with no contract checked; 12 where Syntology's instrument failed) · 3 unverified. Where Syntology's instrument failed: 10 where our contract draft was wrong; 2 where the contract check could not run; failures of Syntology's instrument, not of the code.
By repository: named in the paper: 7 samples from 1 repository, 7 ran (4 with no instrument failure; 3 where Syntology's instrument failed); community (archive-listed): 28 samples from 7 repositories, 25 ran (16 with no instrument failure; 9 where Syntology's instrument failed); 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.
No sample is linked to this paper from 8 of its repositories that Syntology harvested for other papers:
- google-research/google-research, official repository: harvested for 24 other papers, listed under the repository
- BirgerMoell/SwedishLLMBenchmark, community (archive-listed): harvested for another paper, listed under the repository
- Peter-Devine/multilingual_mt_bench, community (archive-listed): harvested for another paper, listed under the repository
- SforAiDl/vformer, community (archive-listed): harvested for 2 other papers, listed under the repository
- aqlaboratory/openfold, community (archive-listed): harvested for another paper, listed under the repository
- baaivision/judgelm, community (archive-listed): harvested for another paper, listed under the repository
- beckschen/llavolta, community (archive-listed): harvested for another paper, listed under the repository
- lm-sys/fastchat, community (archive-listed): harvested for 2 other papers, listed under the repository
Levels written between 2026-09-22 and 2026-09-26 (UTC): the dates Syntology's graph wrote each sample's verification level, not when the code executed. 7 of the 33 samples that ran have no recorded date.
17 repositories listed; official and paper-mentioned ones first.
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.
A paper named beside a repository with no sample linked to this paper is shown with that paper's own run record, not this paper's: “ran” means executed on a synthesized input, not that the code is correct, and “where Syntology's instrument failed” counts failures of Syntology's instrument, not of the code. Papers are listed in arXiv-id order, at most three per repository.
Code Syntology ran Syntology
36 samples harvested: 33 ran (of which 0 constructed an object rather than computing a result; 21 with no instrument failure: 3 honoured, 6 violated, 12 with no contract checked; 12 where Syntology's instrument failed) · 3 unverified. “Where Syntology's instrument failed” counts failures of Syntology's instrument, not of the code. Syntology's record for this page last changed ; when this build read Syntology's graph is in the build record. That date is not when the samples ran.
Licence: 8 of the 36 samples are pointer only, meaning Syntology does not serve that copy's text. For 6 of them, this paper's copy is under a permissive licence and cleared, but the stored copy of the same code that Syntology serves is not; the licence label's tooltip gives Syntology's recorded reason. 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; the list below is grouped by repository, each group headed by its name and what it is to the paper. 1 sample is listed last, in a group without a repository. “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. A line beginning “fixture:” under a sample that ran gives the arguments Syntology's fixture called the sample with, each as name:type/rank/element kind (? where none was recorded), and the type of what the fixture's run returned, exactly as the harness recorded them; the arguments and the returned type were recorded by two separate runs of the same fixture. No output value is shown, and a returned type says nothing about whether the output was right. “Level recorded” gives the UTC date when Syntology's graph wrote the sample's verification level, not when the code executed; where the level was written before Syntology's graph kept that date, the row says “level recorded, date not recorded”. “Honoured” and “violated” refer to a contract Syntology drafted from the code itself. “Our draft was wrong” and “the contract check could not run” are failures of Syntology's instrument, not of the code: the sample ran on Syntology's synthesized input, and then the drafted contract did not describe it, or the check itself raised an error (RAISES), lacked a dependency (DEP_MISSING) or ran out of time (TIMEOUT); hover the label for which. A metamorphic tier, where shown, is evidence of the output's shape, not that the sample implements the method.
Each sample ends with its code_sha256. A sample's code_sha256 is Syntology's identity for that exact code. It is printed in full (16 hexadecimal characters for every sample in this build), and get_code(code_sha256="…") takes it exactly as printed. An agent calls it on Syntology's MCP service (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 are grouped by repository: a repository marked official first, then by how directly the repository is tied to the paper, then in the archive's order; never by how many ran. 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 six 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; the licence is outside the permissive list Syntology serves text under (MIT, Apache-2.0, BSD and similar); this paper's copy was not located, so its licence is not recorded; or this paper's copy is cleared, but the stored copy of the same code that Syntology serves is not. 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.
A function or class name links to its page when the name occurs in the harvested code of at least 5 papers; the name is a string, not an identity. A census tag appears when Syntology's census ran implementations with that name on one shared input per bucket: it gives how many were compared, from how many papers, and how many distinct outputs they produced, and names this sample's bucket when this sample was one of them.
census: 11 implementations from 10 papers, 7 distinct outputs across 5 buckets, one shared input per bucket memory_efficient_attention_pytorch/memory_efficient_attention.py ran MIT (permissive) ·
640155bcba1f2f92 · report
fixture: signature q:Tensor/4/float, k:Tensor/4/float, v:Tensor/4/float, mask:Tensor/2/bool, causal:bool/0/bool, attn_bias:Tensor/4/float · returned Tensor · level recorded 2026-09-24 60fff7c3c400d7ff · report
fixture: signature val:int/0/int, d:int/0/int · returned int · level recorded, date not recorded c16aee9490eb8729 · report
fixture: signature fn:callable/0/? · returned function · level recorded, date not recorded census: 5 implementations from 103 papers, 1 distinct output on one shared input this sample: bucket 1 of 1, arg 1: rank 1, kind int, dtype int64 memory_efficient_attention_pytorch/autoregressive_wrapper.py ran · violated contract MIT (permissive) here; the stored copy is pointer only ·
aa5486a3650902d8 · report
fixture: signature val:Tensor/1/int · returned bool · level recorded, date not recorded census: 12 implementations from 20 papers, 3 distinct outputs on one shared input this sample: bucket 1 of 1, arg 1: rank 2, kind float, dtype float32 memory_efficient_attention_pytorch/cosine_sim_flash_attention.py ran · our draft was wrong fingerprinted MIT (permissive) here; the stored copy is pointer only ·
3eb58dae620046d3 · report
fixture: signature t:Tensor/2/float · returned Tensor · level recorded, date not recorded 1f7d4befc2e41402 · report
fixture: signature router:dict/0/?, args:dict/0/?, depth:int/0/int · returned list · level recorded 2026-09-24 census: 7 implementations from 9 papers, 5 distinct outputs on one shared input this sample: bucket 1 of 1, arg 1: rank 2, kind float, dtype float32 memory_efficient_attention_pytorch/autoregressive_wrapper.py ran · our draft was wrong fingerprinted MIT (permissive) ·
3b65d5d77510c964 · report
fixture: signature logits:Tensor/2/float, thres:float/0/float · returned Tensor · level recorded 2026-09-22 6b1a95182607abd6 · report
fixture: signature query:ArrayImpl/4/float, key:ArrayImpl/4/float, value:ArrayImpl/4/float, mask:ArrayImpl/4/bool, bias:ArrayImpl/4/float, precision:Precision/0/?, query_chunk_size:int/0/int, key_chunk_size:int/0/int, bias_calc_fn:callable/0/?, mask_calc_fn:callable/0/?, weights_calc_fn:callable/0/?, calc_fn_data:dict/0/? · returned ArrayImpl · level recorded 2026-09-22 bdb24836d5607016 · report
36090e75c451149f · report
fixture: signature prediction_text:str/0/str, reference_text:str/0/str, epsilon:float/0/float · returned dict · level recorded 2026-09-24 46c06fec5aedf4a4 · report
fixture: signature text:str/0/str · returned str · level recorded 2026-09-24 cb00b798f297e5f3 · report
fixture: signature text:str/0/str · returned str · level recorded 2026-09-24 1fe910f8046380d4 · report
fixture: signature pil_img:Image/0/?, background_color:tuple/1/int · returned Image · level recorded 2026-09-24 c17fb3de0d47f94a · report
fixture: signature text:str/0/str, keywords:set/0/? · returned set · level recorded 2026-09-24 fa3bb9e402f7460c · report
fixture: signature data:list/1/dict · returned list · level recorded 2026-09-24 42a46570620cd9fa · report
fixture: signature lst:list/1/int, n:int/0/int, k:int/0/int · returned list · level recorded, date not recorded c3ee9d07c900dd55 · report
fixture: signature image:str/0/str · returned PngImageFile · level recorded 2026-09-24 a3698e4aaee89be7 · report
fixture: signature path:str/0/str · returned list · level recorded 2026-09-24 012fae59ee4131ba · report
fixture: signature images:list/1/Image, image_processor:callable/0/?, model_cfg:MockModelConfig/0/? · returned Tensor · level recorded 2026-09-24 076c252c52cbb161 · report
fixture: signature lst:list/1/int, n:int/0/int · returned list · level recorded, date not recorded 592b3c1a88f93d7c · report
fixture: signature pil_img:Image/0/?, background_color:tuple/1/int · returned Image · level recorded 2026-09-22 bae18947b56f2be1 · report
fixture: signature value:NoneType/0/? · returned bool · level recorded 2026-09-22 9b3c1cb391672ccb · report
fixture: signature image_file:str/0/str · returned Image · level recorded 2026-09-22 344dff4791fd1381 · report
fixture: signature images:list/1/Image, image_processor:callable/0/?, model_cfg:MockModelConfig/0/? · returned Tensor · level recorded 2026-09-26 44fab12a908ab057 · report
fixture: signature img_path:str/0/str, mclient:MockMemcacheClient/0/? · returned Image · level recorded 2026-09-24 census: 2 implementations from 6 papers, 1 distinct output on one shared input this sample: bucket 1 of 1, arg 1: rank 2, kind float, dtype float32 en_transformer/en_transformer.py ran · our draft was wrong fingerprinted MIT (permissive) ·
f2fe51a4a5646c6f · report
fixture: signature t:Tensor/2/float · returned float · level recorded 2026-09-22 915fbeb88e13d4a3 · report
fixture: signature val:str/0/str, d:callable/0/? · returned str · level recorded 2026-09-22 census: 1 implementation from 5 papers, 1 distinct output on one shared input fastchat/model/compression.py ran · our draft was wrong Apache-2.0 (permissive) here; the stored copy is pointer only ·
80dde39da1ba3708 · report
fixture: signature tensor:Tensor/2/float, config:QuantizationConfig/0/? · returned tuple · level recorded 2026-09-25 f545ae5663fedf88 · report
fixture: signature model:callable/0/?, tokenizer:callable/0/?, params:dict/0/?, device:str/0/str, context_len:int/0/int, stream_interval:int/0/int, judge_sent_end:bool/0/bool · returned generator · level recorded 2026-09-24 c5824da57107fe8c · report
fixture: signature module:callable/0/?, prefix:str/0/str · returned list · level recorded 2026-09-25 dc21db7cf25bfe72 · report
fixture: signature response:str/0/str · returned str · level recorded 2026-09-25 0bf414a2b44babe6 · report
fixture: signature device:str/0/str, load_8bit:bool/0/bool, cpu_offloading:bool/0/bool · returned bool · level recorded 2026-09-22 2613e53cccc8a4b4 · report
8dacf011c6f243dc · report
68b3637c9b987a61 · report
fixture: returned tuple · level recorded 2026-09-22 1 sample is identical to code Syntology harvested from another paper's repository; this paper's own copy was not located, so no repository is named and no licence is recorded.
856d86b39c6005b4 · report
fixture: signature numer:int/0/int, denom:int/0/int · returned bool · level recorded, date not recorded Results from the paper archive 2025-07-28
No leaderboard rows for this paper in the archive.
Methods
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