


Transformers power modern language models, but explanations often jump from a colorful attention diagram to dense matrix notation. This series takes the middle route: clear intuition, enough mathematics to verify the mechanism, and practical implications for model architecture and GPU inference. It assumes familiarity with programming, arrays and basic algebra, not previous experience building Transformers.
Reading path. Parts 1–4 build one attention calculation. Parts 5–8 assemble a Transformer block, including positional information and feed-forward networks (FFNs). Parts 9–13 connect the math to memory-efficient GPU execution and text generation. Parts 14–15 close with training, implementation and evidence-based performance analysis.
Editorial conventions. denotes token positions, the number of hidden features per token, Query/Key width, batch size and attention-head count. Dimensions are usually written as rows × columns. Approximate costs are analytic estimates, not measurements of a particular GPU.
Transformers rely heavily on matrix multiplication, which is good news for GPUs. But a common mistake is to predict attention speed from arithmetic counts alone. A GPU can have impressive compute throughput and still spend significant time moving data between high-bandwidth memory (HBM) and smaller on-chip working areas.
We need to distinguish three things: how many operations must be done, how much intermediate data is stored, and how often that data crosses memory boundaries.
Consider batch size , number of heads , sequence length , and head width (here means per-head width, not the full model width). The product compares every Query with every Key. There are Query-Key position pairs per head.
A dot product over features costs about floating-point operations when a multiply and an add count as two operations. Therefore the approximate work for one full-sequence score matrix across the batch is
The probability-by-Value product costs about the same if Value width also equals , giving roughly for those two matrix products together. These estimates exclude the projections, softmax, normalisation, FFN and other operations.
Doubling context length makes this dense pairwise work about four times larger. Increasing context length fourfold makes it about sixteen times larger. Tokenwise FFN projections, in contrast, scale linearly in token count when their widths are fixed.
One attention score matrix contains entries. If each stored entry uses bytes, its size is
For and two bytes per entry, a single dense intermediate matrix occupies bytes, or 1 GiB. If a naïve implementation materializes both full scores and full softmax probabilities in that representation, those two buffers alone approach 2 GiB.
This is not a claim that every modern GPU kernel stores both matrices or that 2 GiB is the entire model memory requirement. It is a controlled illustration of why naïvely materializing attention can become expensive.
HBM is large and high bandwidth, but it is farther from the arithmetic units than on-chip registers and shared memory. These on-chip resources are small, fast working areas. A straightforward attention implementation might write all scores to HBM, read them to compute softmax, write probabilities, and read them again to mix Values.
Even when the arithmetic is efficient, these round trips can dominate parts of the kernel. Arithmetic intensity measures useful arithmetic per byte moved at a given memory level. A kernel with limited data reuse can be bandwidth-bound; a kernel with ample reuse and enough work may be compute-bound.

For example, with and , the two dense attention matrix products together require roughly 275 billion arithmetic operations. That is an operation count, not 275 billion operations per second and not a prediction of milliseconds. Runtime depends on GPU architecture, data type, kernel selection, memory movement, batching and many other components.
Similarly, two models with the same theoretical FLOP count may differ in wall-clock latency because their tensor shapes map differently to GPU kernels or because they move different amounts of data between memory levels.
A conventional FFN of model width and intermediate width needs about dense-matmul FLOPs. That part scales linearly with token count. Yet its weight matrices may be substantial, and low-batch decoding may fetch large weight tensors for relatively little new-token work.
This explains why an “attention bottleneck” should not be assumed from the model name. In a real workload, attention, FFN matrices, KV-cache reads, normalization, kernel launch overhead or scheduling can each matter.
Takeaway. Count both operations and bytes. Dense attention has a quadratic token-pair term, but actual GPU performance depends heavily on data movement and reuse. The next post explains how FlashAttention changes the execution strategy without changing the dense attention formula.
Next: Part 10 makes the central FlashAttention trick understandable through streaming weighted averages.