The cost of moving data #

An accelerator has several levels of storage. Fast on-chip memory holds relatively little data. Larger device memory supplies weights and activations at a different cost. A calculation that repeatedly retrieves the same value from the larger store can spend a substantial share of its time transferring bytes. Reusing a value near the arithmetic units changes that balance.

Section sources [1]

Putting an upper bound on performance #

The Roofline model relates performance to operational intensity, the number of operations performed per byte transferred across a specified memory boundary. Multiply intensity by the available bandwidth and compare the result with peak arithmetic throughput. The lower of the two values bounds performance in this simplified model.

For example, a device sustaining 100 gigabytes per second across a boundary can feed at most 200 billion operations per second to a workload performing two operations per transferred byte. The application may achieve less because of implementation overhead, irregular accesses or limited parallelism. Reordering its work to reuse data can raise its intensity.

Section sources [1]

An example in language models #

FlashAttention applies this idea to transformer attention. The algorithm breaks the calculation into tiles that fit in fast memory and avoids writing a complete matrix of intermediate attention scores to larger memory. It still computes exact attention up to ordinary floating-point effects. Faster execution here comes from changing how data travels through the machine, illustrating why a hardware forecast benefits from an accompanying account of software.

Section sources [2]

Finding the crossover point #

Suppose a processor sustains one trillion arithmetic operations per second and moves 100 billion bytes per second from a particular memory level. The two Roofline limits meet at ten operations per byte. A workload performing two operations per byte would reach its bandwidth limit well before the arithmetic ceiling. At twenty operations per byte the arithmetic ceiling would become the tighter upper bound. These figures illustrate a hypothetical machine, and each boundary in a real memory hierarchy can have a different crossover point.

FlashAttention illustrates a counterintuitive consequence. Recomputing some intermediate values can be cheaper than saving them to slower memory and reading them back. The total count of arithmetic operations can therefore give the wrong ranking of two implementations. A useful measurement pairs elapsed time with bytes transferred and records the sequence lengths, hardware and numerical precision used. Otherwise an improvement in data reuse can look mysterious or disappear when a workload changes.

Section sources [1] [2]

Sources

  1. Samuel Williams, Andrew Waterman and David Patterson · Roofline (2008) www2.eecs.berkeley.edu
  2. Tri Dao and colleagues · FlashAttention (2022) arxiv.org
  3. Larry McVoy and Carl Staelin · lmbench: Portable Tools for Performance Analysis (1996) usenix.org