---
title: Write the attention formula. Explain the time and memory complexity in sequence length, why scaling by √d matters, and…
url: https://gurkanozkan.ai/learn/questions/mle-self-attention-cost-and-the-kv-cache-15/
markdown: https://gurkanozkan.ai/learn/questions/mle-self-attention-cost-and-the-kv-cache-15.md
kind: question
role: ML engineering
stage: Technical
difficulty: Hard
json: https://gurkanozkan.ai/api/learn/questions/mle-self-attention-cost-and-the-kv-cache-15.json
robots: noindex
---

# Write the attention formula. Explain the time and memory complexity in sequence length, why scaling by √d matters, and how a KV cache changes decoding cost.

ML engineering · Technical · Hard

## Answer

*Model answer, written in a candidate's voice*

### In short

- Attention: softmax(QKᵀ/√d)V; naive compute O(T²·d), attention-score memory O(T²).
- √d scaling: dot-product logits have std ~√d, so dividing by √d keeps variance near 1 and avoids softmax saturation.
- Multi-head: h parallel subspaces of width d/h; same total order, concatenated and projected.
- KV cache: store past K and V; each new token costs O(T·d) attention instead of recomputing the prefix; cache grows with layers × heads × head_dim × T (×2 for K and V).
- Variants: FlashAttention tiles with online softmax to avoid materialising the T×T matrix; GQA/MQA share K/V across heads to cut cache size.

### In full

The core operation is softmax(QKᵀ/√d)V: queries, keys, and values are T×d, the score matrix is T×T, and the output is T×d. Naively this costs O(T²·d) compute and O(T²) memory for the attention probabilities, plus O(T·d) for Q,K,V. The √d divisor matters because a dot product of two d-dimensional vectors has std around √d, so unscaled logits grow with d and push softmax into a saturated, near one-hot regime; dividing by √d keeps the logits at roughly unit variance. Multi-head attention splits the model dimension into h parallel subspaces of width d/h, computes attention per head, then concatenates and projects, keeping the same total order while improving representation and parallelism. During decoding, a KV cache stores past K and V so a new token only needs to attend to the cached prefix, costing O(T·d) instead of recomputing the prefix; the cache grows with layers × heads × head_dim × T, times two for K and V. Efficient variants such as FlashAttention avoid materialising the full T×T matrix via tiling and online softmax, while GQA/MQA share K and V across query heads to reduce cache memory and bandwidth.

*Corrected by an independent AI reviewer.*

## What a strong answer covers

- softmax(QKᵀ/√d)V, with O(T²·d) compute and O(T²) attention memory.
- Scaling keeps logits' variance near 1 so the softmax does not saturate.
- Multi-head attention runs h subspaces in parallel.
- KV cache: store past K and V so each new token costs O(T·d) instead of recomputing; cache memory grows with layers × heads × head_dim × T.
- Mentions efficient variants (FlashAttention, GQA/MQA).

## Common mistakes

- forgetting the causal mask when decoding
- claiming the KV cache reduces the O(T²) cost of a full forward pass at training time.

## Topic guides

- [KV cache](https://gurkanozkan.ai/learn/topics/kv-cache.md)
- [LLM inference: decoding, batching, quantisation and speculative decoding](https://gurkanozkan.ai/learn/topics/inference.md)
- [Transformers, attention and scaling](https://gurkanozkan.ai/learn/topics/transformers.md)
---

*Study material compiled with AI: drafted by a local model (Inkling 2 Mini) from public sources, then checked against those sources by independent AI reviewers (Claude) in November 2026. Not reviewed line by line by a person.*
