DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk7 min

How Transformer Attention Works—and Why Memory Becomes a Bottleneck

Transformer attention combines learned query, key, and value projections. Understand its quadratic arithmetic, FlashAttention’s memory-saving execution, and the KV cache’s compute-for-memory trade-off.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Transformer attention uses learned query, key, and value projections to decide how token representations mix. Dense attention still requires quadratic arithmetic in sequence length, but it does not have to store the full attention matrix in high-bandwidth GPU memory: FlashAttention changes the computation’s execution order to reduce data movement. During autoregressive generation, a separate trade-off appears: a KV cache avoids recalculating past keys and values, but consumes memory that grows with the cached context and workload.

What a Transformer layer does

A Transformer processes a sequence of token representations through learned operations. In a typical layer, attention mixes information across positions; a feed-forward network, often called an MLP, then transforms each position’s representation. Residual connections and normalization are also commonly part of the layer. Unlike recurrent networks, the original Transformer was built without recurrence or convolutions, using attention mechanisms instead, as described in Attention Is All You Need.

As an Amazon Associate I earn from qualifying purchases.

Attention begins with learned linear projections of the layer’s input. These produce three vectors for each token: a query (Q), a key (K), and a value (V). The names are useful labels, not evidence that the model is performing literal symbolic reasoning. Their learned interactions implement content-dependent mixing.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • Query: the projected representation used to score what information a position should draw from.
  • Key: the projected representation compared with queries to produce those scores.
  • Value: the projected representation that is combined according to the resulting weights.

In multi-head attention, the model performs this operation across multiple learned subspaces, then concatenates and projects the head outputs. The original design used smaller dimensions per head than the full model representation; NVIDIA’s inference optimization overview also describes the multi-head arrangement.

How the attention equation works

Scaled dot-product attention is commonly written as:

Attention(Q, K, V) = softmax(QKᵀ / √dk) V

  1. Compare queries and keys. The matrix product QKᵀ computes a score for each query-key pair. For a sequence of N positions, the resulting score matrix has N × N entries per head.
  2. Scale the scores. Dividing by the square root of the key dimension, √dk, moderates score magnitude before the softmax.
  3. Normalize with softmax. Softmax turns each query’s scores into weights that sum to one across the positions it can attend to. In autoregressive use, a causal mask prevents a position from attending to future tokens.
  4. Mix the values. Multiplying those weights by V forms a weighted combination of value vectors. The result is a context-aware representation for each query position.

So the short answer to “what are Q, K, and V?” is: Q and K determine the attention weights; V supplies the vectors those weights combine. The weights depend on the input representations, rather than being fixed connections between token positions.

Why dense attention is quadratic—and what that does not mean

With sequence length N and head dimension d, dense attention’s score-and-value work scales as O(N²d): every query is compared with keys across the sequence, then uses the resulting weights to combine values. Doubling N therefore makes the N × N score matrix four times as large, even though the exact runtime depends on hardware and implementation.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Arithmetic cost, intermediate storage, and memory traffic are different quantities:

  • Arithmetic: dense full attention retains O(N²d) work. Reducing the amount of data moved does not make this computation linear in sequence length.
  • Intermediate storage: a straightforward implementation may materialize the N × N scores and attention probabilities. Those intermediates can be large even though they are not model parameters.
  • Memory traffic: writing intermediate matrices to GPU high-bandwidth memory (HBM), then reading them back for softmax and value aggregation, can consume time. This is distinct from how many arithmetic operations the algorithm performs.

That distinction explains why an attention kernel can be limited by moving data rather than by arithmetic alone. A large temporary matrix is not the same thing as the model’s persistent weights or an inference KV cache.

How FlashAttention reduces memory movement

FlashAttention is an IO-aware exact attention algorithm. It divides queries, keys, and values into tiles, computes attention in blocks, and accumulates the output and normalization information without writing the entire N × N matrix of scores or probabilities to HBM. Its use of recomputation and tiling reduces HBM accesses; some intermediate work instead stays in faster on-chip storage.

The FlashAttention authors’ 2022 paper states that the algorithm returns O = softmax(QKᵀ)V with O(N²d) FLOPs and O(N) additional memory beyond the inputs and output. Here, N is sequence length and d is head dimension. The significance is not that attention’s dense arithmetic disappears: the algorithm preserves the exact attention operation while reducing the extra storage required by the computation and the data movement associated with a naïve implementation.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

“Exact” describes the attention algorithm, not a promise that every hardware implementation produces bit-for-bit identical floating-point values: changing operation order can affect finite-precision arithmetic. Nor does the algorithm guarantee the same speedup in every workload. Performance depends on hardware, dimensions, sequence length, precision, batch size, and implementation. The original paper reports configuration-specific results, not a universal multiplier.

The living Hugging Face Transformers attention documentation describes the general distinction between implementations that perform the same attention computation and optimized implementations that rearrange it to reduce memory traffic; it describes FlashAttention 2 as using block tiling and fast on-chip memory. Available backends and installation details can change, so check the documentation for the version and hardware in use before selecting one.

Why inference uses a KV cache

Autoregressive generation produces one token at a time. At each step, the model needs to attend to the tokens already in the context. Without a cache, it would recalculate the earlier tokens’ key and value projections as generation proceeds. A KV cache stores those past keys and values; the next step computes the new token’s query and attends against the stored history. NVIDIA illustrates this reuse in its article on KV-cache optimization.

The cache exchanges memory for less repeated computation. A useful dimensional estimate for an uncompressed cache is:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

batch × layers × context_length × 2 × KV_heads × head_dimension × bytes_per_element

The factor of 2 counts keys and values. This is a formula for estimating the main stored tensor data, not a measured footprint for a particular model. Actual allocation can also include alignment, page or block allocation, quantization metadata, and other implementation overhead. Increasing batch size, number of layers, context length, KV-head count, head dimension, or bytes per element increases the estimate.

KV-cache capacity is therefore separate from attention’s temporary score matrix. It grows with the tokens being retained for active requests and the model architecture, whereas FlashAttention addresses how attention work is carried out. A method that reduces cache storage does not, by itself, remove dense prefill attention’s quadratic arithmetic.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

How MHA, GQA, and MQA affect cache size

The number of key/value heads stored per layer changes the cache estimate. Multi-head attention (MHA) uses multiple KV heads; multi-query attention (MQA) shares a single KV head across query heads; grouped-query attention (GQA) uses fewer KV heads than query heads. Holding other dimensions constant, fewer KV heads mean fewer stored key and value vectors.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Arrangement KV-head relationship Cache implication Trade-off to assess
MHA Multiple KV heads; the original multi-head arrangement uses separate head subspaces. More KV heads increase cache storage relative to an arrangement with fewer KV heads, all else equal. Measure the target model’s memory use, decode throughput, and task quality.
GQA Fewer KV heads than query heads, with groups of query heads sharing KV heads. Reduces KV-cache requirements compared with MHA when other factors are held constant. It is positioned as a balance between memory requirements and model quality; the outcome is architecture- and model-specific.
MQA One KV head shared across query heads. Reduces KV-cache requirements compared with MHA when other factors are held constant. Evaluate the quality and performance trade-off for the particular model and settings.

NVIDIA’s inference overview discusses MQA and GQA as ways to reduce cache demand through fewer KV heads, and characterizes GQA as a balance between memory requirements and model quality. The architecture alone does not establish a universal quality outcome. For a real deployment, compare cache bytes at the intended batch size, context length, and precision, alongside decode throughput and task quality on the target model.

Which bottleneck should you address?

Choose an optimization based on what is actually constrained; the terms “attention memory” and “GPU memory” can refer to different resources.

Observed constraint Relevant distinction What to examine
Attention intermediates are large or HBM traffic is costly. Temporary score/probability storage and memory movement during attention. Whether an optimized exact-attention implementation such as FlashAttention is supported for the model, hardware, dimensions, and precision.
Autoregressive decoding repeats work over prior tokens. Recomputation versus persistent storage of past keys and values. Whether a KV cache fits the intended active batch and context workload.
The KV cache consumes too much capacity. Persistent cache size, influenced by KV-head count and bytes per element. Model architecture, cache precision or quantization, allocation overhead, and measured quality and throughput for the chosen configuration.
Long-sequence attention still takes substantial compute time. Dense attention’s arithmetic complexity remains quadratic in sequence length. Whether the workload can use a different attention pattern or approximation, recognizing that it changes the computation and may change results.

Comparisons should also distinguish training forward/backward support from inference decoding support, and account for library compatibility and numerical precision. A kernel that reduces HBM traffic may help attention without shrinking a persistent KV cache; a cache-saving architecture may help decoding memory without changing the cost of dense attention over a long prefill.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.