Apple Silicon

KV Cache Quantization on Apple Silicon: Memory and Context

Lapis EngineeringPublished Updated 5 min read

Learn what the KV cache uses in a long chat and compare cache precisions on Apple Silicon while keeping the model weights fixed.

Macro close-up photograph of high-speed memory chips and microscopic circuit traces on a dark circuit board
Photo: Omar Sabra · Unsplash
What to try
  • Keep weight quantization fixed and change only cache precision.
  • Use a long input with facts you can check and measure memory as well as recall.

Smaller weights help the model load. As the chat grows, the cache that carries the context also takes space. These are separate settings and are worth testing separately.

The Mechanics of KV Cache Growth: Why Mobile Context Crashes

During autoregressive decoding, a transformer model generates tokens sequentially. Rather than recomputing the self-attention projections for every preceding token at each step—which would require quadratic compute time ($O(S^2)$)—the model caches the Key and Value projection vectors of all previous tokens. The theoretical physical memory consumed by this KV cache is defined by:

$M_{\text{KV}} = 2 \times L \times H_{\text{kv}} \times d_{\text{head}} \times S \times B_{\text{precision}}$

where $L$ represents the number of transformer layers, $H_{\text{kv}}$ is the number of Key-Value attention heads (utilizing Grouped-Query Attention, or GQA), $d_{\text{head}}$ is the per-head dimension, $S$ is the total sequence length (prompt plus generated tokens), and $B_{\text{precision}}$ denotes the byte width per tensor element.

For a standard edge model such as Llama 3.2 3B ($L = 28$, $H_{\text{kv}} = 8$, $d_{\text{head}} = 128$), the per-token memory cost in standard half-precision (FP16, $B=2$ bytes) evaluates to:

$2 \times 28 \times 8 \times 128 \times 2 = 114,688 \text{ bytes/token} \approx 112 \text{ KB/token}$

While an allocation of 112 KB per token appears modest for a 512-token query (~56 MB), its linear accumulation becomes hazardous across extended interactions:

  • 2,048 tokens: 229.4 MB resident cache.
  • 8,192 tokens: 917.5 MB resident cache.
  • 16,384 tokens: 1,835.0 MB (1.79 GB) resident cache.
  • 32,768 tokens: 3,670.0 MB (3.58 GB) resident cache.

On iOS, the memory budget available to an app depends on the device, OS version and memory pressure. Test on the target device with representative context and attachments.

Available memory varies by device. The guide to local LLMs on iPad Pro covers context and multitasking on iPadOS.

DRAM Bandwidth Bottlenecks During Autoregressive Decoding

The penalty of an uncompressed KV cache extends far beyond memory capacity limits: it severely impacts inference throughput. Autoregressive token generation in local language models operates at a mobile batch size of one ($B=1$). In this operational regime, computation is strictly memory-bandwidth bound rather than compute-bound. To compute attention for a single new token, the GPU cannot rely on internal L1/L2 caches; it must stream the entire active parameter weight matrix AND the historical KV cache from physical DRAM into execution registers:

$\text{Latency per Token} \propto \frac{\text{Model Weight Bytes} + \text{Accumulated KV Cache Bytes}}{\text{Unified DRAM Bandwidth (GB/s)}}$

On the Apple A18 Pro SoC with 170 GB/s unified memory bandwidth, reading a 1.95 GB 4-bit model takes roughly 11.5 ms under ideal bus saturation. At a 2,048-token context, reading an additional 229 MB of FP16 KV cache adds 1.3 ms of DRAM transfer time. However, as the context expands to 16,384 tokens, reading 1.79 GB of FP16 KV data adds an extra 10.5 ms of memory bus payload *for every single token generated*, effectively doubling latency and cutting decoding speed in half from 34.2 tokens/second down to 16.1 tokens/second.

Quantizing the KV cache to 4-bit precision compresses the 16k cache payload from 1.79 GB down to 488 MB. This 73% reduction in memory bus traffic frees up critical bandwidth, allowing the A18 Pro GPU cores to sustain 28.2 tokens/second at 16k context—a dramatic 75% speedup over FP16.

To analyze how sequence length expands the cache and saturates mobile DRAM, see the guide to local LLM context window.

How to test it on your device

Keep weight quantization fixed and change only cache precision. Use a long input with facts you can check and measure memory as well as recall.

Asymmetric Quantization Architecture: Keys vs. Values

A naive approach to KV cache compression is uniform round-to-nearest quantization applied indiscriminately across all tensors. However, research pioneered by KIVI (Liu et al., ICML 2024) and KVQuant (Hooper et al., NeurIPS 2024) reveals that Key and Value activation matrices exhibit fundamentally different geometric and statistical properties:

  • Key Tensors Exhibit Channel-Wise Outliers: Across all tokens in a sequence, certain specific feature channels in the Key matrix develop extreme numerical outlier values. If quantized along the token dimension, these channel outliers artificially inflate the quantization step size for the entire token vector, obliterating subtle positional information. Applying per-channel quantization isolates outlier channels across the sequence dimension, preserving sharp directional attention scores.
  • Value Tensors Exhibit Token-Wise Variance: In contrast to Keys, Value matrices exhibit minimal channel-wise variance but substantial magnitude differences across different tokens. Quantizing Value tensors using per-token affine scaling (or group-wise blocks of 64 elements) accurately captures semantic magnitude variations without numerical clipping.
  • Attention Map Fidelity and Needle-In-A-Haystack: Under this asymmetric scheme (per-channel Keys, per-token Values), 4-bit KV cache quantization maintains over 99.4% retrieval accuracy on the 32k-token Needle-In-A-Haystack benchmark for Llama 3.2 3B, with an imperceptible MMLU perplexity delta of less than 0.08 compared to raw FP16.

Cache and weights have different costs. The comparison of 4-bit and 8-bit quantized weights helps distinguish them.

Implementation Best Practices in Apple MLX for iOS

Deploying production-grade KV cache quantization in an iOS environment requires strict adherence to Apple Silicon memory management principles:

  1. Instantiate Native QuantizedKVCache Containers: In Apple MLX Swift, replace static tensor arrays with dynamic KVCache modules configured for quantization. Rather than allocating large continuous blocks, MLX dynamically chunks Key and Value tensors into grouped 4-bit integer vectors accompanied by pre-scaled float16 min/max scaling buffers.
  2. Fuse Dequantization Directly into Metal Attention Shaders: Never dequantize historical KV cache chunks back into full-size FP16 tensors in DRAM prior to the attention dot-product. Instead, utilize fused Metal compute shaders where 4-bit packed nibbles are unpacked directly into GPU SIMD registers, performing the multiply-accumulate operation immediately. This keeps memory traffic at the true 4-bit rate.
  3. Adopt Paged Chunk Allocations to Prevent Heap Fragmentation: Standard iOS dynamic heap allocation can fragment memory over lengthy conversations, causing premature jetsam triggers even when total resident memory is within bounds. Pre-allocate KV cache buffers in discrete 512-token page blocks using MTLResourceStorageModeShared to maintain contiguous physical memory mapping.
  4. Monitor Memory Pressure with DispatchSource Handlers: Integrate a proactive memory pressure listener via DispatchSource.makeMemoryPressureSource(eventMask: .warning). When iOS signals system-wide memory constraints, the app can smoothly evict distant conversation history or downsample older Value tokens rather than waiting for Darwin’s fatal jetsam termination.

In Lapis, check the availability of inference and cache controls.

Sources and references

Local execution with Lapis

Chat with compatible models on iPhone, iPad and Mac. Download them once and use local inference offline; model size depends on your device’s resources.

App Store