Home · ASPLOS 2023 · Machine Learning Systems

FLAT: An Optimized Dataflow for Mitigating Attention Bottlenecks

Sheng-Chun Kao, Suvinay Subramanian, Gaurav Agrawal, Amir Yazdanbakhsh and Tushar Krishna

Conference
ASPLOS 2023
Published
2023
Pages
295-310
Citations
67

The attention score matrix grows with the square of sequence length and drowns accelerators in memory traffic — FLAT computes it in row tiles, runs softmax on-chip, and multiplies straight into the value matrix, so the quadratic tensor never leaves the chip and 64K-token transformers run on ordinary accelerators.

The argument in one glance

  1. Problem

    Attention wants long sequences, and long sequences explode

    Transformers compare every token with every other token, producing an N×N score matrix for a sequence of N tokens. Long inputs — protein chains, whole documents, audio — need N in the tens of thousands, and the score matrix grows with N². For BERT at 16K tokens it is 8.2 GB, hundreds of times larger than any accelerator’s on-chip memory.

  2. What people did

    Use the tricks that work for every other neural network layer

    Convolution and fully connected layers are fed by mature dataflow optimizations — tiling, loop reordering, and above all batching, which reuses the same weights across many inputs. Compilers also fuse matrix operators with cheap element-wise operators (like ReLU) to keep data on-chip.

  3. Why it fell short

    The two core attention operators are immune to all of it

    The score (Logit) and weighted-sum (Attend) multiplies take two activations as input — there are no weights to reuse, so batching does not raise their arithmetic-per-byte at all. And fusing them is blocked by softmax sitting between them, a many-to-many operator that needs a full row of N scores before it can normalize anything. Above 512 tokens, even exhaustively-optimized per-operator dataflows hit their roofline limit.

  4. This paper's idea

    Fuse Logit–softmax–Attend, tiled by rows, so the N×N matrix never exists in memory

    FLAT (Fused Logit Attention Tiling) computes a few rows of scores, softmaxes them on-chip (a full row is the minimum legal unit), and immediately multiplies them into the value matrix — then discards them. Memory footprint drops from quadratic to linear in N. On edge and cloud accelerators this yields 1.75×/1.65× speedups, 44–55% energy savings, and unlocks sequences up to 64K tokens.

Why this problem is worth solving

An attention layer measures how strongly each token in a sequence relates to every other token. For a sequence of N tokens it computes an N×N matrix of scores — that pairwise, global view is exactly what makes transformers good, and it is also the scaling disaster. The paper’s motivating applications need long sequences because meaning emerges only from full context: two proteins can look identical fragment-by-fragment and differ only over the whole chain, and a summary can change meaning when the full document is considered. Demand for 8K to 69K token inputs already existed when the paper was written; 64K tokens is roughly the length of a short novel.

The quadratic term turns concrete fast. The intermediate score tensor for BERT is about 10 MB at 512 tokens, 136 MB at 2K tokens, and 8.2 GB at 16K tokens. For comparison, a cloud accelerator in the paper’s study (modeled on the TPU-v3) has 32 MB of on-chip memory, and an edge accelerator has 512 KB. At long sequence lengths the score matrix is not merely too big to keep on-chip — it is 250× too big, so it must round-trip through off-chip DRAM, whose bandwidth is roughly 20× lower than on-chip memory in both accelerator classes studied (50 GB/s vs 1 TB/s on edge; 400 GB/s vs 8 TB/s on cloud).

Hardware designers’ previous answer was to change the math — sparsify, compress, or approximate attention — trading model accuracy for speed. FLAT’s claim is that the bottleneck can be removed without touching the functionality at all: same numbers, same accuracy, different movement of data.

What people did before, and where it broke

To see why standard tools fail, split an attention layer’s six operators into two families.

Activation-weight operators (Q, K, V, O). These multiply the input by learned weight matrices — ordinary fully connected (FC) computations. Their

arithmetic operations per byte moved from memory — the standard predictor of whether an operator is limited by compute or by memory bandwidth (the “roofline” model)
can always be raised by batching: the same weights serve every sequence in the batch, so more inputs mean more reuse per byte. Decades of dataflow research — tiling, loop reordering, weight/input/output stationary schedules — applies directly.

Activation-activation operators (L and A). The Logit operator (L) multiplies queries by keys to produce the N×N scores; the Attend operator (A) multiplies the normalized scores by values. Both inputs of each are freshly computed activations, not weights. Every new sequence brings entirely new operands, so batching adds zero reuse — the paper’s roofline analysis on a TPU-v3 shows L and A pinned deep in the memory-bound region at any batch size, while FC and convolution operators climb toward compute-bound as the batch grows. These two operators dominate attention latency and energy, and get worse as N grows.

Why not just fuse them? Operator fusion — feeding a producer’s output straight into a consumer without materializing the tensor in memory — is the classic cure. But ML compilers only fuse matrix operators with element-wise operators (ReLU, add). Between L and A sits softmax, a many-to-many operator: each output row requires reading all N scores of that row, reducing them, then scaling each one. Arbitrary tiling breaks that dependency. DNNFusion, a state-of-the-art fusion study from 2021, reported tensor-tensor fusion as “too complicated or unprofitable.” Meanwhile attention accelerators like A³, ELSA, and Sanger (see also the DOTA explainer elsewhere on this site) sidestepped the problem by pruning or approximating scores — buying speed with accuracy.

Where FLAT changes the attention pipeline

  1. Q / K / V projections

    Ordinary activation-weight multiplies. Compute-bound with batching; FLAT leaves them as-is, handled by existing per-operator dataflow search.

  2. Logit (L) new

    Queries × keys → N×N scores. FLAT computes it in tiles of rows, streaming keys, keeping each partial result in the on-chip scratchpad.

  3. Softmax new

    Normalizes each row of scores. FLAT’s minimum tile is one complete row — the smallest unit that satisfies softmax’s reduction across all N scores — so it runs entirely on staged on-chip data.

  4. Attend (A) new

    Normalized rows × values, executed immediately per tile, interleaved with L inside one fused loop nest. The score tile is discarded after use; the full N×N tensor never reaches off-chip memory.

  5. Output projection (O)

    Ordinary activation-weight multiply, unchanged.

Hardware asks: a controller that can follow the fused fine-grained schedule, and a software-addressable on-chip buffer — features most recent industrial and academic accelerators already have. No change to the math, so accuracy is untouched.

The idea

FLAT stands for Fused Logit Attention Tiling, and the name is the design: fuse L–softmax–A into one loop nest, and tile it at a granularity chosen to fit the on-chip memory.

Fuse exactly these two, and nothing else. The paper measures the operational intensity of every plausible fusion pair. Fusing L with A lifts the combined operator’s intensity above the accelerator’s compute-bound threshold — the quadratic intermediate tensor becomes internal traffic instead of memory traffic. Fusing an FC into the pair (Q with L, or A with O) makes things worse: the FC side loses weight reuse and drops below its stand-alone performance. FCs are already compute-bound; they need no help. So FLAT draws the fusion boundary tightly around the sick operators.

Respect softmax by making the row the atom. Softmax reduces across a full row of N scores, so the smallest legal unit of fused work is one complete row — computed from one query vector against all N keys. FLAT calls this row granularity and only ever tiles in multiples of it. That single constraint preserves exact data dependencies — no approximation, no online correction, bit-identical results.

Scale the tile to the memory you have. Above the row, FLAT builds coarser tiles: multiple rows (R-Gran), whole heads (H-Gran), whole sequences (B-Gran), or everything at once (M-Gran — the naive keep-it-all-on-chip approach that dies beyond 2K tokens). Here is the subtle enabling fact: for FC-FC fusion, shrinking tiles destroys weight reuse, so fine granularity is self-defeating. But L and A have no algorithmic reuse to lose — every score is used once. So FLAT can shrink tiles almost freely to fit a 20 KB edge buffer or grow them to exploit a 32 MB cloud buffer, paying only a modest cost of re-streaming keys and values per tile.

Search, don’t guess. Fusion adds new choices — shared outer-loop order, per-operator inner loops, tile sizes in three dimensions — on top of classic per-operator mapping. FLAT ships a map-space exploration framework that exhaustively searches loop orders and tile sizes against an analytical cost model of the accelerator (validated within 1% of the established MAESTRO model), so each hardware target gets its own optimal mapping.

One tile of fused attention, on-chip from start to finish

N = sequence length; d = per-head hidden size; R = rows per FLAT-tile. Q/K/V tensors are already computed (by the unfused projection operators). 'Scratchpad' = software-addressable on-chip buffer. Baseline = each operator run separately with the score matrix written to off-chip DRAM. Sizes quoted for BERT-class models from the paper's Fig. 4.

Step 1 of 6

Baseline first: the quadratic round-trip

Without fusion, the Logit operator computes the entire N×N score matrix and writes it to off-chip DRAM (136 MB at 2K tokens; 8.2 GB at 16K). Softmax reads it all back, normalizes, writes it again. Attend reads it a third time. The largest tensor in the layer crosses the slowest link in the system three times — this is why L and A dominate attention latency.

traffic: O(N²) written, read, written, read
at N=16K: 8.2 GB × multiple trips over ~50–400 GB/s

Why it works

  • Both operands of L and A are fresh activations → batching cannot add reuse → the only reuse left to create is temporal locality of the intermediate tensor → fusion is the uniquely-fitting lever.
  • Softmax needs whole rows → FLAT makes the row the smallest tile → the dependency is satisfied structurally → fusion becomes legal with bit-identical results, where compilers had declared it impractical.
  • Score tiles die on-chip → the N×N tensor generates no off-chip traffic → the fused operator’s arithmetic-per-byte rises above the compute-bound threshold → the processing array, previously starved, approaches full utilization (0.99 vs 0.55 for the best unfused dataflow at 20 KB in the paper’s sweep).
  • L/A have no weight reuse to lose → tiles can shrink to fit any buffer → the same dataflow serves a 20 KB edge chip and a 2 GB research configuration, and footprint linear in N unlocks 64K-token sequences.
  • Off-chip bandwidth demand falls 71–82% → the accelerator no longer competes with itself for the memory bus → performance survives on cheap, bandwidth-poor hardware.

What it costs

  • Re-streaming. Keys and values are fetched again for every tile; finer tiles mean more re-fetches. This is the price of fitting small buffers, and the search framework balances it against footprint.
  • Hardware assumptions. The accelerator needs a software-addressable scratchpad and a controller able to follow the fused schedule — common in modern designs, absent in fixed-function pipelines.
  • Offline search. The exhaustive map-space exploration must be run per accelerator and per model shape before deployment.
  • No help for the rest of the model. Q/K/V/O and feed-forward layers are already compute-bound; FLAT leaves their performance unchanged, so end-to-end gains (1.5× at 512 tokens) are smaller than L/A-only gains.
  • Simulation-first evidence. Headline numbers come from an analytical cost model (validated within 1% of MAESTRO on single operators), not silicon — though a real-GPU implementation on a Tesla T4 corroborates the memory-footprint claims.

The evaluation

FLAT is evaluated on five transformer models (BERT, TrXL, FlauBERT, T5, XLM) across two accelerator classes — an edge design (32×32 processing elements, 512 KB buffer, 50 GB/s off-chip) and a cloud design modeled on TPU-v3 (256×256 elements, 32 MB, 400 GB/s) — against the strongest possible software baseline: FLEX-Opt, an exhaustive search over all existing per-operator dataflows, so any win is attributable to fusion rather than to better tuning.

Three dataflow regimes on the same hardware

PropertyNaïve (fixed dataflow)FLEX-Opt (best per-operator mapping)FLAT-Opt (fused L–softmax–A + tiling)
What it optimizes
End-to-end model speedup and energy
Long sequences (64K tokens)
Off-chip bandwidth needed for 95% utilization
Real GPU check (Tesla T4, 16 GB, batch 1)
Accelerator results from the paper's analytical cost model (within 1% of MAESTRO on single-operator checks), energy via Accelergy; models BERT, TrXL, FlauBERT, T5, XLM; edge = 32×32 PEs / 512 KB / 50 GB/s, cloud = 256×256 PEs / 32 MB / 400 GB/s (TPU-v3-like). GPU rows measured on real hardware.Underlined values explain themselves when clicked.

A buffer-size sweep sharpens the story: with a luxurious 2 GB of on-chip memory, FLEX-Opt and FLAT-Opt both reach ~0.97–0.99 utilization — fusion is unnecessary when everything fits. At a realistic 200 KB, FLAT-Opt holds 0.98 while FLEX-Opt manages 0.58 (a 1.7× gap on L/A); at a scarce 20 KB, the gap is 0.55 vs 0.25. FLAT is precisely a technique for the memory-poor regime that real chips live in.

Check your understanding

  1. 1. Batching dramatically speeds up fully connected layers on accelerators. Predict what happens to the Logit (query×key) operator when you increase the batch size.

  2. 2. ML compilers routinely fuse matrix operators with ReLU, yet refused to fuse Logit with Attend. What exactly blocked tensor-tensor fusion for attention?

  3. 3. Micro-batching (shrinking tiles) hurts fused FC-FC pairs, yet FLAT shrinks L/A tiles down to a single row on small chips. Why is fine-grained tiling safe here but harmful there?

  4. 4. With FLAT, how many bytes of the N×N score matrix are written to off-chip memory during an attention layer?

Related papers on Machine Learning Systems

See all 212 →