TPU Roofline Analysis for Performance Optimization
Introduction to Roofline Analysis
What Limits Your Code's Speed?
When a program runs slowly, we often blame the processor. But the processor's speed is only part of the story. A program's performance is usually limited by one of two things: how fast it can perform calculations, or how fast it can access data from memory.
If a program spends most of its time number-crunching without needing much data, it's compute-bound. Its speed is dictated by the processor's raw computational power. On the other hand, if a program constantly needs to fetch new data from memory to perform simple operations, it's memory-bound. It's limited by the system's memory bandwidth, no matter how fast the processor is.
The Roofline model is a simple visual tool that helps us understand this relationship. It plots a program's characteristics against the hardware's limits, showing us exactly what's holding back performance.
The Roofline Plot
The model gets its name from the shape of the graph it creates. It shows the maximum achievable performance for any given program on a specific piece of hardware.
This graph has two main parts:
-
The Slanted Roof: This line represents the limit imposed by memory bandwidth. When a program doesn't perform many calculations for each byte of data it reads, it falls into this region. Performance is directly proportional to how much data it can get, and how quickly.
-
The Flat Roof: This horizontal line represents the processor's peak performance. Once a program performs enough calculations per byte of data, it's no longer waiting on memory. It's now limited only by how fast the CPU can execute instructions. The performance flattens out at this maximum theoretical speed.
Three Key Ingredients
To understand the plot, we need to define its core components.
FLOPS
noun
Floating-Point Operations Per Second. A measure of computer performance, especially in fields that require a lot of mathematical calculations. We often measure it in GigaFLOPS (GFLOPS), or billions of operations per second.
Performance, measured in GFLOPS, is what we plot on the vertical axis. It's the goal we want to maximize.
Memory Bandwidth is the rate at which data can be transferred between the processor and memory, usually measured in Gigabytes per second (GB/s). This value determines the slope of the slanted part of the roof. A higher bandwidth results in a steeper slope, meaning better performance for memory-bound tasks.
The final ingredient, and the most important for positioning a program on the graph, is Arithmetic Intensity.
Arithmetic Intensity
noun
The ratio of floating-point operations performed to the number of bytes of data moved from memory to perform those operations.
Arithmetic Intensity (AI) is the value on the horizontal axis. It's the key characteristic of your specific program. You can think of it as a measure of data reuse.
Imagine a chef making a salad. If the chef brings one lettuce leaf from the fridge, cuts it once, and puts it in the bowl, the arithmetic intensity is low. If the chef brings the whole head of lettuce, then chops, dices, and slices it many times before getting another ingredient, the arithmetic intensity is high.
Reading the Plot
To use the model, you first measure the performance (GFLOPS) and arithmetic intensity (FLOPs/Byte) of your program. Then you plot this point on the graph for your specific hardware.
The location of this point tells you everything. The distance from your point to the "roof" above it represents the optimization potential. The closer you are to the roof, the more efficiently your program is using the hardware.
If your program's point lies under the slanted part of the roof, it is memory-bound. The primary way to improve its speed is to reduce memory traffic or increase its arithmetic intensity. This might involve restructuring algorithms to reuse data already in the processor's cache.
If the point is under the flat part of the roof, it is compute-bound. The program is getting data fast enough, but the processor isn't reaching its peak speed. This could be due to inefficient instructions, pipeline stalls, or other issues related to the computation itself.
By identifying which part of the roof is the limit, the Roofline model provides clear guidance on where to focus your optimization efforts.