The attention mechanism in transformer models is, at its most basic level, a function that computes how much each element in a sequence should attend to every other element. For a sequence of length N, attention requires comparing every element to every other element — N² comparisons. For a 1,000-token context, that is 1 million comparisons. For a 100,000-token context (well within the range of modern models), that is 10 billion comparisons. For a 1-million-token context (possible with Gemini 1.5), that is 10^12 comparisons.
This quadratic scaling is not a bug. It is a direct consequence of how attention achieves its remarkable language understanding: by allowing every token in a sequence to be influenced by every other token. The flexibility and expressiveness of attention comes entirely from its all-to-all connectivity. Remove the all-to-all connectivity and you lose the flexibility. Keep it and you pay the quadratic cost.
The next generation of AI hardware is being shaped, more than any single factor, by the question of how to pay this quadratic cost more cheaply.
What the KV Cache Is and Why It Matters
During autoregressive inference — generating one token at a time — the attention computation would be catastrophically slow if recomputed from scratch at each step. If you are generating a 1,000-token response, you would recompute 1 million attention weights for the first token, then 2 million for the second, and so on, for a total of roughly 500 billion attention computations across the generation.
The key-value (KV) cache solves this by storing the intermediate attention computations from previous tokens so they do not need to be recomputed. When generating the 500th token, the keys and values for tokens 1 through 499 are cached; only the new token’s query needs to interact with all cached keys and values. This reduces computation from quadratic to linear in the output length, at the cost of memory.
The memory cost is substantial. For a 70B parameter model with a 32-layer, multi-head attention architecture, the KV cache for a 32,000-token context requires roughly 16GB of memory at FP16 precision — in addition to the 140GB of model weights. Total memory requirement for a 70B model serving a 32K context window: approximately 156GB. No single consumer GPU handles this; even the H100’s 80GB requires the model to be split across multiple chips.
As context windows grow — Anthropic’s Claude 3 supports 200K tokens, Google’s Gemini 1.5 supports 1M tokens — the KV cache grows proportionally. The memory and bandwidth requirements scale in ways that make long-context inference extraordinarily expensive with standard transformer architectures.
Flash Attention and the Memory Hierarchy Trick
Tri Dao, then a Stanford PhD student, published Flash Attention in 2022, introducing an optimization that reduced the memory requirement for attention computation without changing the mathematical result. The insight was about memory hierarchy: standard attention implementations moved data from DRAM to GPU on-chip SRAM and back repeatedly; Flash Attention restructured the computation to maximize reuse of data while it was in SRAM.
By loading tiles of the query, key, and value matrices into SRAM and computing partial attention results for those tiles before writing results back to DRAM, Flash Attention reduced the number of DRAM reads and writes by roughly 5-10x without changing the mathematical output. The theoretical computation remained quadratic, but the actual runtime improved by 2-4x because the bottleneck was DRAM bandwidth, not arithmetic.
Flash Attention 2, published in 2023, further improved performance through better parallelization across GPU threads and registers. Flash Attention 3, published in 2024, incorporated hardware-specific optimizations for H100’s asynchronous execution units. Each iteration demonstrated the same principle: the memory hierarchy, not the arithmetic, is the binding constraint for attention computation.
This observation has architectural implications. If attention is memory-bound, then hardware designed to minimize attention’s memory access cost — through in-package memory, large on-chip SRAM, or high-bandwidth interconnects — has a structural advantage over hardware that competes on arithmetic throughput.
Alternative Attention Architectures
The AI research community has not stood still while waiting for hardware to solve the attention scaling problem. Several architectural alternatives have been proposed and partially validated that modify or replace standard attention to reduce its quadratic cost.
Grouped Query Attention (GQA), used in Llama 2, Llama 3, and many other recent models, reduces the number of key-value heads relative to query heads — instead of computing separate K and V projections for every attention head, a smaller number of K-V heads is shared across groups of query heads. This reduces the KV cache size by 4-8x with minimal degradation in model quality, directly addressing the memory constraint at the cost of some model expressiveness.
Multi-Query Attention (MQA), a more aggressive version of GQA where all query heads share a single K-V projection, reduces KV cache size by up to 32x. Google’s PaLM model uses MQA. The quality tradeoff is larger but manageable for many inference tasks.
State Space Models (SSMs) like Mamba, published in December 2023, replace attention entirely with a recurrent structure that maintains a fixed-size hidden state. Unlike attention, SSMs have linear (not quadratic) computation cost in sequence length. Mamba achieved competitive quality with transformer models on language tasks while being substantially faster on long sequences.
Mamba’s linear scaling is not obviously better than transformers for all tasks — attention’s quadratic structure is not just a cost, it is also the source of its ability to perform exact recall of arbitrary information from long contexts. Mamba and SSM architectures have fixed-capacity state that cannot perfectly recall arbitrary long-range dependencies. Whether this matters depends on the application: for most chat applications, it does not matter; for tasks requiring perfect recall of long documents, it might.
What Speculative Decoding Actually Solves
Speculative decoding, proposed by multiple groups independently in 2022-2023, is the most practically impactful inference optimization for large language models that does not require changing the model architecture or hardware. The idea is disarmingly simple: use a small, fast “draft” model to generate a candidate sequence of multiple tokens, then verify all of them in parallel with the large “target” model.
Autoregressive generation is slow because each token must be generated sequentially — you cannot generate the second token until you have generated the first. But verification — checking whether a candidate sequence is consistent with the large model’s probability distribution — can be done in parallel for all candidate tokens simultaneously. If the draft model’s predictions are usually correct (as they tend to be for predictable text), the effective generation speed approaches the large model’s throughput divided by the typical acceptance length, which can be 3-5x faster.
Speculative decoding is now deployed in production by Google (for Gemini), Anthropic (for Claude), and most other major inference providers. The hardware implication is that it favors systems with low latency for the draft model and high throughput for the verification step — which suggests architectures where a small, local chip handles drafting and a larger, more powerful chip handles verification.
This maps naturally to hierarchical edge-cloud architectures: a device-side small model drafts tokens, and a cloud-side large model verifies them. The network latency for verification must be lower than the time saved by parallel verification. For high-bandwidth, low-latency connections (5G mmWave, Wi-Fi 7), this arithmetic often works. For high-latency connections, it does not. The inference architecture is not hardware-only; it is hardware plus network topology.
The Implications for Chip Design
The attention mechanism’s quadratic cost, and the various algorithmic responses to it, create a set of hardware requirements that are becoming clearer as models and inference volumes scale:
First, SRAM capacity on-chip matters more than previously appreciated. Flash Attention works by keeping attention tile computations in fast SRAM. More SRAM means larger tiles, less DRAM traffic, faster attention. Chips that prioritize SRAM over transistor-count-equivalent arithmetic units will win for attention-bound workloads.
Second, memory bandwidth per chip is the primary compute scaling lever. Each doubling of memory bandwidth roughly doubles attention throughput, because attention is memory-bound. HBM capacity improvements (HBM3 to HBM4) and bandwidth improvements directly translate to inference throughput.
Third, heterogeneous architectures — systems with different chips optimized for different parts of the computation — will become standard. The attention verification pass favors high-bandwidth memory; the feed-forward layers favor high compute throughput. A chip optimized for both is a chip that is optimal for neither. A system with two chip types, each optimized for its workload, can be substantially better than a uniform architecture.
The attention mechanism will not be replaced — it is too valuable for too many tasks. But the hardware stack it runs on is being redesigned from first principles around its specific computational demands. The chips that emerge from that redesign will look different from the GPUs that trained the first transformers, and different from each other as the workloads they optimize for diverge.
The quadratic trap is real, and the escape routes are being mapped.
One email a month: new articles, reviews and the upcoming live webinar + free recording. No spam, unsubscribe anytime.

