Benchmarking Local LLMs: Memory Bandwidth, the KV Cache, and Token Generation

Dec 3, 2026 · Article

In my previous post, Benchmarking Local LLMs: Where VRAM Limits, MoE, and PLE Architecture Collide, I ran a series of local models—Google Gemma 4 and Mistral Small 3.2—across an NVIDIA RTX 4060 Ti (16GB VRAM) and an Intel Core i5-13420H.

The benchmarks produced several counterintuitive numbers:

  1. Mistral Small 3.2 24B ingested prompts at a blazing 571 tok/s, but its token generation collapsed to a sluggish 4 tok/s.
  2. Gemma 4 12B (dense, 100% in VRAM) and Gemma 4 26B (MoE, split 56% CPU / 44% GPU) generated text at the exact same output speed: 32 tok/s.
  3. Loading a model with a default 262k context window quietly reserved gigabytes of VRAM before a single token was generated, pushing model layers out of VRAM and onto system RAM.

Recently, I read through Han Yu’s deep dives on transformer architectures, particularly The KV Cache, and Why Decode Is Memory-Bound. His breakdown of arithmetic intensity and memory traffic connected the dots. The bottlenecks I saw on consumer hardware weren’t random quirks of Ollama; they are governed by fundamental computer architecture.

Here is what is actually happening under the hood during local inference, and why it dictates what you can deploy.


1. Prefill vs. Decode: The Core Asymmetry

Running an LLM consists of two completely different computational workloads:

  • Prefill (Prompt Evaluation): The model ingests all prompt tokens simultaneously.
  • Decode (Token Generation): The model predicts tokens one by one, feeding each newly generated token back into the input for the next cycle.

The hardware demands of these two phases are night and day.

Prefill is Compute-Bound

When evaluating a 1,000-token prompt, the model multiplies large weight matrices by a matrix of 1,000 token vectors at once (GEMM — General Matrix Multiply). Because the weights loaded from memory are reused across all 1,000 tokens, the ratio of math operations to bytes read (arithmetic intensity) is very high:

Arithmetic Intensity = (Operations across all prompt tokens) / (Bytes Transferred)

In prefill, this ratio is typically well over 100 FLOPs per byte. Modern GPU Tensor Cores thrive on this—the arithmetic units stay fully saturated, which is why Mistral Small achieved 571 tok/s during prompt evaluation even when partially offloaded.

Decode is Memory-Bandwidth Bound

During token generation, the model predicts exactly one token at a time (batch size = 1). To produce that single token, every active parameter matrix in the model must be swept out of memory, across the bus, into registers, multiplied by a single vector (GEMV — General Matrix-Vector Multiply), and discarded.

The arithmetic required is roughly 2 FLOPs per parameter. For a 4-bit quantized weight (~0.55 bytes), the arithmetic intensity is:

Arithmetic Intensity ≈ (2 FLOPs) / (0.55 bytes) ≈ 3.6 FLOPs/byte

To tell whether a workload is compute-bound or memory-bound on a given GPU, you compare its arithmetic intensity against the chip’s machine balance—the break-even threshold of math operations the GPU must perform for every byte fetched from VRAM just to keep its compute cores 100% busy.

An NVIDIA RTX 4060 Ti 16GB has 22 TFLOPS (Tera FLOPS) of Shader Core (FP32) compute—a conservative floor since its Tensor Cores are even faster—and 288 GB/s of VRAM bandwidth. Converting both to base units gives its machine balance:

Machine Balance = (22 × 10^12 FLOPs/s) / (288 × 10^9 bytes/s) ≈ 76 FLOPs/byte
  • In prefill (arithmetic intensity over 100 vs. the 76 machine balance): The workload gives the GPU more math per byte than the break-even point, keeping compute cores saturated (compute-bound).
  • In decode (arithmetic intensity ~3.6 vs. the 76 machine balance): The workload reaches less than 5% of that break-even threshold (3.6 / 76 ≈ 4.7%), so the compute cores spend ~95% of their clock cycles idle, stalled waiting for weights to arrive across the memory bus (memory-bandwidth bound).

When compute sits idle waiting on data, throughput is governed by the Roofline equation (named after the Roofline model, which caps performance at whichever ceiling—compute or memory bandwidth—you hit first):

Generation Rate (tok/s) ≈ Memory Bandwidth (GB/s) / [Active Parameters (GB) + KV Cache Read (GB)]

Now the Mistral Small benchmark makes sense:

  • Mistral Small 3.2 24B at Q4_K_M totals ~14–15 GB of weights, and Ollama placed 63% of those layers onto system RAM (~9–10 GB per token read from CPU memory, while the remaining 37% stayed in VRAM).
  • System RAM bandwidth on DDR4/DDR5 is roughly 30–50 GB/s (compared to 288 GB/s on VRAM).
  • Plugging that CPU-offloaded bottleneck into the Roofline equation (~35 GB/s ÷ ~9–10 GB) caps generation at ~3.5–4 tok/s (~0.25–0.3s per token)—matching the 4 ± 1 tok/s measured in the benchmark (meanwhile, the GPU finishes its VRAM slice in under 0.02s).

The compute cores were capable of hundreds of tokens per second, but the bus starved them.


2. The KV Cache and Why Generation is Quadratic Without It

In an autoregressive transformer, predicting token t requires calculating attention over all previous t - 1 tokens.

At each layer, the model projects the token into three vectors: Query (Q), Key (K), and Value (V). The token does its looking with Q, while earlier tokens are looked at via their K and V.

Crucially: once a past token is processed, its Key and Value representations never change.

If you discard past Keys and Values, generating token t would require recomputing the entire forward pass for all preceding tokens from scratch. Generation time would grow quadratically with prompt length: O(N^2).

The KV cache is simply an allocated buffer in memory that holds onto past Keys and Values so they don’t have to be recomputed. On each generation step, the model only computes Q, K, V for the single new token, appends the new K and V to the cache, and computes attention against the stored history.

Generation becomes linear in operations—but it shifts the cost entirely to memory footprint.


3. The 262K Context Tax

The memory footprint of the KV cache scales directly with context length:

KV Cache Size (Bytes) = 2 × Layers × KV Heads × Head Dimension × Context Length × Precision (Bytes)

In plain terms:

  • Heads & MHA: A transformer splits its attention across multiple parallel channels called heads (typically 32). In traditional Multi-Head Attention (MHA), each of those 32 heads stores its own dedicated Key and Value vectors for every past token.
  • Head Dimension: The vector size per head (typically 128 numbers, slicing a 4,096 embedding across 32 heads).
  • The factor of 2: One set of numbers for Keys, and one for Values.

Consider what happens under traditional MHA (32 KV heads) on a 40-layer model:

  • Model: 40 layers, 32 heads, head dimension 128, FP16 precision (2 bytes).
  • At 8k context: 2 × 40 × 32 × 128 × 8,192 × 2 ≈ 5.3 GB
  • At 32k context: ≈ 21.4 GB
  • At 131k context: ≈ 85.9 GB
  • At 262k context: ≈ 171.8 GB

Under standard MHA, the KV cache alone for long context would dwarf the weights of any model and overwhelm consumer hardware.

How Grouped-Query Attention (GQA) Saves Us

Modern architectures like Gemma 4 and Mistral use Grouped-Query Attention (GQA). Instead of assigning a unique KV head to every query head, multiple query heads share a single key/value head (for example, an 8:1 ratio where 32 query heads share only 4 KV heads).

GQA slashes the KV cache footprint by 4× to 8× without noticeable quality degradation.

Even with GQA, however:

  • A 262k context on a 40-layer model with 8 KV heads still demands ~10–14 GB of memory solely for the KV cache at FP16.
  • In 8-bit quantized KV cache, it still requires ~5–7 GB.

When Ollama initializes a model with a default context window of 131k or 262k, it must reserve headroom for this cache upfront in VRAM. On a 16GB GPU, reserving 5–8 GB for the KV cache leaves only 8–11 GB for model weights.

This is the hidden reason why gemma4:26b was split 56/44 onto CPU RAM: the model weights didn’t exceed VRAM; the model weights plus the 262k KV cache reservation exceeded VRAM.


4. The “Rent Bill vs. Compute Bill” in Mixture of Experts

In the benchmark, gemma4:26b (MoE) generated tokens at 32 tok/s, perfectly matching gemma4:12b (dense) despite having 26B total parameters and running partially on CPU RAM.

This illustrates the core trade-off of Mixture of Experts, described vividly by Han Yu:

“The hours billed depend on how many specialists you use; the rent depends on how many you employ.”

In an MoE architecture:

  • Total Parameters (The Rent Bill): Gemma 4 26B has 25.8B parameters. All 25.8B weights must reside somewhere in memory (VRAM or system RAM), ready in case a token routes to them.
  • Active Parameters (The Compute Bill): For each token, the router selects only a small subset of experts, activating roughly 3.8B parameters.

At batch size 1 decode, the accelerator only needs to read the weights of the active experts for each token step.

Because gemma4:26b only transferred 3.8B parameters per step across the slower PCIe/RAM bus, while gemma4:12b transferred all 11.9B parameters across the faster VRAM bus, their throughput rates converged:

(Slower PCIe Bandwidth / 3.8B Active Parameters) ≈ (Faster VRAM Bandwidth / 11.9B Active Parameters) ≈ 32 tok/s

MoE allows you to achieve the generation speed of a compact 4B model while benefiting from the knowledge and reasoning capacity of a 26B model. The catch is that you still have to pay the rent bill in total memory capacity.


5. Why Quantization Directly Buys Decode Speed

Another direct implication of the memory-bandwidth ceiling is why quantization is such a superpower for local inference.

In compute-bound workloads (like prefill), quantizing weights can sometimes introduce runtime overhead because GPU Tensor Cores must dequantize values on the fly during matrix multiplication.

In decode, however, the compute cores are almost entirely idle waiting for data. Because generation speed is bounded by Memory Bandwidth / Bytes Read, every bit you shave off your weights or cache translates almost linearly into faster tokens per second:

FormatPrecisionBytes / WeightBandwidth Traffic vs. FP16Approximate Decode Speedup
FP16 / BF1616-bit2.00 bytes1.0× (Baseline)1.0×
Q8_0 / INT88-bit~1.05 bytes~0.5×~1.9× faster
Q6_K6-bit~0.82 bytes~0.4×~2.4× faster
Q4_K_M / Q4_0 / AWQ4-bit~0.55 bytes~0.28×~3.6× faster
Q3_K_M / Q2_K2–3-bit~0.35–0.45 bytes~0.2×~4.5×–5.5× faster

This linear speedup explains why running Q4_K_M feels snappy on a desktop GPU while FP16 crawls: you are physically moving almost 4 times less data across the memory bus for every token generated.

Don’t Forget KV Cache Quantization

Most people only quantize the model weights, leaving the KV cache running in uncompressed 16-bit float (FP16).

As we saw in the 262k context tax, the KV cache can easily swell to several gigabytes. Modern runtimes like llama.cpp and vLLM allow you to quantize the KV cache itself:

  • FP8 or Q8_0 KV cache: Halves the memory footprint. For short-to-medium context chat, degradation is negligible, though long-context needle retrieval and precise code generation can see minor attention distortion (since quantization noise in Key vectors directly skews the attention score calculation over past tokens).
  • Q4_0 KV cache: Reduces the cache footprint by ~4×. While it allows massive context windows to squeeze into tight VRAM budgets, it comes with a noticeable penalty in long-context recall and multi-step reasoning accuracy.

6. Actionable Takeaways for Local LLM Deployment

Understanding these constraints turns LLM deployment from guesswork into simple sizing math:

1. Clamp num_ctx Unless You Are Actually Ingesting Books

Never run a local model with its default 131k or 262k context unless your task requires it. If you are doing coding, chat, or agent workflows, 8k–16k context is usually plenty.

Restricting the context in your Modelfile:

FROM gemma4:26b
PARAMETER num_ctx 16384

Clamping from 262k down to 16k reclaims several gigabytes of VRAM, allowing Ollama to allocate more model layers directly onto the GPU. Shifting a split-offloaded model into 100% GPU offload can turn a 4 tok/s crawl into a 30+ tok/s experience.

2. Choose MoE When You Have Memory Capacity but Want Fluid Interaction

If you have a 32GB or 64GB machine with a mid-range GPU, MoE models (like Gemma 4 26B or Mixtral) are ideal. Even when partially offloaded, their low active parameter count keeps interactive token generation fluid.

3. Choose Dense When You Are Strictly VRAM-Constrained

If you have a strict 8GB or 12GB VRAM ceiling and want 100% GPU offload without PCIe bus penalties, a right-sized dense model (like Gemma 4 12B or 4B) will fit entirely into VRAM and maximize prompt evaluation throughput.


References & Further Reading