Skip to content

Training, adaptation, compression, and systems techniques · Training, fine-tuning, adaptation, compression, and systems techniques · TRN032 · Algorithm

FlashAttention

Computes exact attention with an IO-aware tiled algorithm that avoids materializing the full attention matrix.

Explained here Component demo, not a trained model CodeSplash's run and the reference's record

In CodeSplash

Documented in the Academy only. There's no Set up button.

Availability
Explained here
Code example
Component demo, not a trained model
Who ran it
CodeSplash's run and the reference's record
CodeSplash's run
The reference's example ran in Python and R here, and they agree.
The reference's record
Python: Ran in the reference's review. The report doesn't record which version of the code ran. R: Reviewed but not run: R wasn't available to the reference's authors.

Purpose

Computes exact attention with an IO-aware tiled algorithm that avoids materializing the full attention matrix.

Question / hypotheses

Does this intervention improve the intended quality, adaptation, resource, or reliability objective under a controlled comparison? It defines a method, not an intrinsic null-hypothesis test.

Architecture / computational structure

Blocks of queries/keys/values use online softmax statistics and fused computation; hardware kernels manage memory traffic.

Assumptions

Hardware, dtype, head dimension, masks, and dropout support must match the kernel. Exact means the same mathematical attention, subject to floating-point ordering differences.

Formula / computational definition

O=softmax(QKT/d)V;m′=max⁡(m,max⁡jsj),l′=em−m′l+∑jesj−m′.O=softmax(QK^T/\sqrt d)V;\quad m^\prime=\max(m,\max_j s_j),\quad l^\prime=e^{m-m^\prime}l+\sum_j e^{s_j-m^\prime}.

Parameters and notation

No additional learned model parameters. Hyperparameters/configuration: block/kernel selection, dtype, causal mask, head layout. The online normalizer l and max m are transient numerical state.

Fitting, fine-tuning, and adaptation

Use the supported framework backend, verify output/gradient agreement within tolerance, and profile realistic sequence lengths. Fine-tuning objectives and trainable modules remain unchanged.

How to use and calibrate

Measure peak memory and end-to-end speed. Example: fit longer text or video token sequences within a device budget.

Example application

fit longer text or video token sequences within a device budget.

Interpretation / pitfalls

Watch out

FlashAttention does not by itself change dense attention’s quadratic arithmetic complexity. The toy verifies the online softmax normalizer, not a GPU implementation.

Python and R code examples

Scope: numerical mechanism demonstration. Validation: Python executed successfully on the supplied synthetic inputs; R reviewed but not executed (Rscript unavailable).

Python and R

The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.

Python

Component demo, not a trained model — numerical mechanism demonstration. · needs numpy

import numpy as np
a=np.array([1.,2.]); b=np.array([3.,.5])
m=a.max(); l=np.exp(a-m).sum()
m_new=max(m,b.max()); l=np.exp(m-m_new)*l+np.exp(b-m_new).sum(); m=m_new
log_z=m+np.log(l); reference=np.logaddexp.reduce(np.r_[a,b])
assert np.isclose(log_z,reference); print(log_z)
R

Component demo, not a trained model — numerical mechanism demonstration.

a <- c(1,2); b <- c(3,.5)
m <- max(a); l <- sum(exp(a-m))
m.new <- max(m,b); l <- exp(m-m.new)*l+sum(exp(b-m.new)); m <- m.new
log.z <- m+log(l); all.scores <- c(a,b); ref <- max(all.scores)+log(sum(exp(all.scores-max(all.scores))))
stopifnot(isTRUE(all.equal(log.z,ref))); print(log.z)

Sources

From the CodeSplash reference, entry TRN032.