Token Lab検索

← 論文ライブラリ
Attention & efficient inference / 2022

FlashAttention: optimize data movement before counting FLOPs

See why not storing an attention matrix can matter as much as arithmetic, and reconstruct softmax one tile at a time.

FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness · Dao et al. · arXiv:2205.14135

Original editorial analysis by Token Lab · Source checked 2026-10-25 · Paper claims and synthetic examples are separated.

Detailed technical articles are available in English and Chinese. This locale shows the English article with localized navigation.

01 · What the paper establishes

FlashAttention computes exact dense attention using tiling and online normalization to reduce HBM traffic. Its dense form is not a linear-time attention approximation. The paper reports hardware- and workload-specific speed improvements, which are not browser benchmark results.

Primary source: paper abstract ↗
Study guide & lesson outlineLEARNING CONTRACT

Understand → implement → investigate

Goal: explain the mechanism in this lesson, reproduce one example and test a failure case. Reading alone does not mark mastery.

  • Understand: state assumptions, shapes/units and a counterexample.
  • Implement: reproduce a numerical example and inspect boundaries.
  • Investigate: compare one alternative under a matched budget.

02 · The mechanism

Text / 原始公式
New tile scores s, values V:
m′=max(m,max(s))
l′=exp(m−m′)l + Σ exp(sⱼ−m′)
u′=exp(m−m′)u + Σ exp(sⱼ−m′)Vⱼ
Output=u′/l′

The row maximum m, denominator l and weighted numerator u are sufficient to merge another tile. When a larger maximum appears, rescale the old accumulators before adding the new tile. This is the same softmax algebra reorganized for memory locality. Backward computation can recompute intermediates instead of retaining the full probability matrix.

Notation and intuition are teaching explanations. Consult the original method for complete assumptions and implementation details.

Mechanism map & worked derivation

Read Q/K/V tiles from HBM → Compute a score tile in SRAM → Merge running m, l and u → Write normalized output
Original schematic; read the steps from top to bottom. SVG ↓
Symbols and assumptions
SymbolMeaning
mRunning maximum of one score row
lRunning exponential denominator
uRunning value-weighted numerator
  1. Partition one row [2,1,0] into [2,1] and [0], with values [1,3] and [5]. After the first tile, , and
  2. The next tile has a smaller maximum, so m stays 2. Add e⁻² to l and 5e⁻² to u. The final u/l matches dense attention: approximately 1.849579.
  3. Now reverse tile order. The running maximum rises from 0 to 2, so both old l and old u must be multiplied by e⁻² before adding the second tile. This is the essential merge invariant.
Experiment checkpoint

Test reversed tile order, extreme logits and partial final tiles. Exact algebra still produces floating-point tolerance differences.

LOCAL / SYNTHETIC / INTERACTIVE

Change one variable. Explain what moves.

Original teaching calculation, not a reproduction of the paper’s benchmark. Bars are relative within this panel; numeric values and units are authoritative.

04 · Work through an example

Original memory example: one dense FP16 score matrix at occupies N²×2 bytes =32 MiB per head, before other tensors. A 128×128 score tile occupies 32 KiB. That tile number is only score scratch space, not the full FlashAttention working set or total model memory. Double N and dense score storage quadruples; the chosen tile size need not change.

05 · Design your experiment

Implement dense and tiled softmax attention on a small CPU array. Compare outputs with tolerance, including large logits and causal boundaries. For a real GPU benchmark, warm up kernels, synchronize timing, fix dtype and head dimensions, record peak allocated memory and separate forward from backward. Report distributions of runtime, not a single cold-start measurement.

Research deliverable

State one hypothesis; save the configuration and raw results; identify a competing explanation; report the narrowest supported conclusion. Use the linked lab as a local starting point, not a full paper reproduction.

Open related lesson / lab →

06 · Limits & reviewer questions

Editorial caution: numerical equivalence is tolerance-based because floating-point reduction order changes. Exact attention still has quadratic arithmetic in sequence length. A score-tile diagram cannot predict speedup without accounting for SRAM capacity, bandwidth, occupancy and kernel launch overhead.

Why must the old numerator be rescaled when the running maximum increases?

Its terms were expressed relative to the old maximum. Multiplying by exp(m−m′) puts old and new contributions on the same exponential scale; otherwise the merged average is wrong.

07 · Read the original

Algorithm 1 and IO analysis; compare the exact and block-sparse variants explicitly.

Year is the first arXiv submission year. Unversioned links follow revisions; record the exact version in your reproduction report. DeepSeek-R1 here is explicitly pinned to v1.

Continue this topic

MATHEMATICAL REVIEW · 2026-10-26

Assumptions, implementation & acceptance

Initialize running max to −∞ and normalizer/accumulator to zero. Skip empty or fully masked tiles; an entirely masked row has no probability distribution. Tiling reduces materialized memory, not quadratic dense arithmetic.

Displayed formula, bilingual explanation and declared slider range. This code reproduces the scalar teaching calculation, not the paper training system.

Before running: predict a boundary case

Implement dense and tiled softmax attention on a small CPU array. Compare outputs with tolerance, including large logits and causal boundaries. For a real GPU benchmark, warm up kernels, synchronize timing, fix dtype and head dimensions, record peak allocated memory and separate forward from backward. Report distributions of runtime, not a single cold-start measurement.

Hint: isolate the assumption

Keep the baseline input fixed, change one parameter or boundary, and compare both the numerical result and the claimed units. A passing example verifies this fixture only.

Minimal executable example

Save the example and linked modules in one folder. Use Node.js 22 or newer, then run node flashattention.mjs. No third-party runtime dependencies.

import {paperCalc} from './paper-core.mjs';
const result=paperCalc("flash",4096);
console.log(result);
Reference output — reveal after predicting
{
  "labels": [
    "Dense score matrix",
    "128×128 score tile only"
  ],
  "values": [
    32,
    0.03125
  ],
  "unit": "MiB"
}

Test and transfer

Implement dense and tiled softmax attention on a small CPU array. Compare outputs with tolerance, including large logits and causal boundaries. For a real GPU benchmark, warm up kernels, synchronize timing, fix dtype and head dimensions, record peak allocated memory and separate forward from backward. Report distributions of runtime, not a single cold-start measurement.

Record input, code version, expected result and observed result in the notebook below. Then construct a counterexample that violates the assumption above and explain why the original conclusion no longer follows.

Download complete kit (.tar.gz) · Complete reference kit and regression checks

Predict, test and keep evidence

Before revealing an answer, predict one result. After the exercise, record the discrepancy and a transfer test. Saved locally across language editions.

Open notebook / export / clear →

The mathematical review scope is recorded per lesson. It does not establish empirical paper reproduction; report suspected errors in your notes. Review scope