Flash Attention
An algorithm that accelerates the calculation of attention and sharply reduces the consumption of video memory. The mathematical result is the same as with regular attention, but the method of calculation changes.
The Problem with Regular Attention
Classical self-attention constructs an N×N matrix for a sequence of N tokens, which does not fit into fast GPU memory and is constantly written to slow VRAM. This creates a bottleneck for memory and speed.
The Idea Behind Flash Attention
Instead of storing the entire attention matrix, the algorithm divides it into blocks and calculates them sequentially without writing intermediate results to external memory. This:
- reduces memory usage;
- increases speed;
- yields the same result.
Thanks to this, long contexts have become significantly cheaper, and models like modern LLM have become more practical.