Moving Attention Memory Off the GPU Without Wrecking Speed

APIFreeNVIDIA · Updated 2026-11-22 · 8 min read

Small desk and large filing cabinet, a metaphor for GPU and system memory
Offloading keeps the desk clear by filing inactive work nearby.

Long contexts and busy servers have one thing in common: they fill GPU memory with the KV cache, the stored attention state of every token the model has seen. When that memory runs out, requests queue, cached prefixes get thrown away, or the engine refuses to start. KV cache offloading promises a way out by moving part of that state to system RAM or even fast storage.

In practice, many people find it frustrating. A common report is that every configuration they try for a particular model family ends in errors or crashes, even though an old issue suggested support existed. Others manage to squeeze a model that seemed too big onto a single box by combining offloading with aggressive quantization and careful limits.

This article explains what is being offloaded, the two very different reasons to do it, what it costs in speed, and why it fails so often. It stays at the level of concepts and checks, because exact flags change between engine versions.

Frequently asked questions

What is the KV cache?

During attention, each token produces key and value vectors that later tokens need to look back at. The engine stores them so it does not have to recompute them for every new token. That store is the KV cache, and it grows with context length and the number of concurrent requests.

How big does the KV cache get?

It depends on the number of layers, the number of key-value heads, the head size, the precision and the number of tokens. For mid-sized models with long contexts it can reach several gigabytes per request, which is why long-context serving is so memory-hungry.

What does offloading mean here?

It means storing some KV cache blocks outside GPU memory, usually in CPU RAM, and moving them back when needed. Some setups add a further tier on fast SSDs or a shared cache server.

How does KV offloading differ from weight offloading?

Weight offloading keeps part of the model itself in system memory and is common in llama.cpp-based tools. KV offloading leaves the weights on the GPU and moves only the attention cache. The two solve different problems and have different speed penalties.

Does offloading make generation faster?

Usually not directly. Its main benefit is avoiding recomputation: a reused prefix fetched from RAM can be faster than processing it again. For a single short chat, offloading typically adds overhead without benefit.

Why does KV offloading crash on some models?

Offloading code must understand each model's attention layout. Architectures with unusual attention, such as hybrid or sliding-window designs, may not be supported by a given engine version, or only in specific configurations. Version mismatches between the engine and offload plug-ins are another common cause.

Which is better: offloading or quantizing the KV cache?

They combine well. Quantizing the cache to lower precision shrinks it on the GPU at a small quality cost, while offloading moves cold blocks away. Try cache quantization first because it is simpler.

Can offloading replace buying more VRAM?

Only partly. It helps keep long prefixes available and survive bursts of users, but active tokens still need GPU memory. If the model weights alone barely fit, offloading the cache will not create room for comfortable long-context work.

Where the memory goes on an inference server

GPU memory on an inference server holds three main things: the model weights, the KV cache, and working buffers for activations. Weights are fixed once the model is loaded. Buffers are modest. The KV cache is the part that grows with demand, and engines such as vLLM reserve a large pool for it up front, controlled by a memory utilisation setting.

When the pool is full, the engine has to choose: pause new requests, evict cached prefixes, or preempt a running request and recompute it later. Each option hurts either latency or throughput.

Context length multiplies everything

Doubling the maximum context roughly doubles the worst-case cache for each request. Setting a very long context limit when most requests are short wastes the reserved pool and reduces how many users fit.

Grouped attention makes caches smaller

Many modern models share key-value heads across several query heads, which shrinks the cache considerably. Comparing models by parameter count alone hides this, so check the architecture before assuming how much context fits.

Two different reasons to offload

Offloading serves two goals that are easy to confuse. The first is capacity: keeping more tokens alive than the GPU can hold, so long sessions or many users do not cause evictions. The second is reuse: remembering prefixes from earlier requests, so later ones skip prefill on the shared part.

For capacity, the cost of moving blocks back and forth over PCIe may outweigh the benefit if those blocks are needed constantly. For reuse, offloading often pays off, because fetching a cached prefix from RAM is cheaper than recomputing thousands of tokens.

A good habit is to read the startup log of your engine. Most serving engines print how many tokens of KV cache they managed to reserve after loading the weights, often expressed as a maximum number of concurrent sequences at full length. That single line tells you more than any calculation, because it reflects the real memory left after the model, the CUDA context and other buffers. If the number is lower than you expected, offloading may help, but first check whether a shorter maximum context or a lower memory utilisation for other processes would solve it more simply.

Agents are the classic reuse case

An agent that repeats the same instructions, tool definitions and file contents in every turn benefits greatly when that prefix survives between calls. The explainer on prefill and prefix caching shows why time to first token drops.

Many idle chats are the classic capacity case

A chat front-end with many users who pause between messages keeps many conversations alive but inactive. Moving their cache to RAM frees GPU memory for active users and brings it back when they reply.

Waiter walking a long corridor between kitchen and dining room
Each trip across PCIe takes time, so offloading pays off only for blocks reused later.

What offloading costs

Moving data between GPU and system memory goes through PCIe or a faster chip-to-chip link on some systems. A consumer PCIe slot is far slower than on-card memory, so blocks that must travel often slow things down. Unified-memory systems change this picture, because CPU and GPU share one memory pool, but they have their own bandwidth limits.

There is also CPU and RAM cost. Offloaded cache needs pinned host memory, and large pools can claim tens of gigabytes. Machines with little system RAM may end up swapping, which is far worse than recomputing.

Quantized cache as a first step

Before offloading, try storing the KV cache at lower precision, if your engine and model support it. That often frees a large share of cache memory with a small quality impact and no transfer overhead.

  • PCIe transfers add latency each time a cold block is needed again.
  • Host memory used for cache is unavailable to other processes.
  • Extra software layers add configuration complexity and new failure points.
  • Benefits appear only when reuse or idle sessions are common.

Why setups crash so often

Developers trying to enable offloading for newer model families commonly report that every configuration fails. The reasons are rarely mysterious once you know where to look.

Offload connectors must know the shape of each layer's cache. Models with mixed attention types, such as layers that alternate between full and linear or sliding-window attention, store state differently, and support for them tends to arrive later than for standard architectures. An old issue saying an architecture is supported may refer to an earlier model generation with a simpler layout.

Version alignment

If offloading comes from a separate library or plug-in, it must match the engine version exactly. Mixing a recent container image with an older plug-in, or the reverse, is a frequent source of import errors and crashes at the first request.

Read the first error, not the last

Long stack traces often end in a generic failure. The first error lines usually name the unsupported layer type, a missing kernel or a memory allocation that failed. Search for that specific message in the project's issue tracker.

Start from the documented example

Run the exact example configuration from the engine's documentation with a model it lists as supported. Once that works, change one variable at a time. Jumping straight to an unusual model with many custom flags makes failures impossible to diagnose.

Puzzle pieces from different sets that do not fit
Version mismatches and unsupported layer types cause most offloading crashes.

Squeezing a large model onto one box

Some builders report fitting a model that seemed too large for a single unified-memory machine by combining several techniques: a lower-bit quantization of the weights, a shorter maximum context, a quantized KV cache, and limits on concurrent sequences. Offloading is only one tool in that kit, and often not the decisive one.

These setups tend to be fragile. They work for one user with moderate prompts and fail when context or concurrency grows. That is fine for experiments, but plan for headroom if others depend on the server.

Trade-offs to write down

Record what you gave up for the fit: quantization level, context length, number of parallel requests. When answers feel worse, you will know which setting to revisit first.

A quick sizing estimate

You can estimate per-token cache size from a model's configuration file: layers times key-value heads times head dimension times two (keys and values) times bytes per element. Multiply by tokens to get a request's cache. The short script below does this for any configuration you plug in.

The numbers in the example are illustrative, not taken from a specific model. Read the real values from the model's configuration before deciding anything.

Hybrid models need a different formula

For models that mix attention types, only the full-attention layers grow with context. The others keep a fixed-size state. Count only the growing layers, or check the engine's reported cache capacity at startup.

def kv_cache_gib(layers, kv_heads, head_dim, tokens, bytes_per_elem=2):
    """Approximate KV cache size in GiB for one sequence."""
    per_token = layers * kv_heads * head_dim * 2 * bytes_per_elem
    return per_token * tokens / 1024**3

# illustrative values: 48 layers, 8 KV heads, head size 128, 16-bit cache
for ctx in (8_000, 32_000, 128_000):
    print(ctx, "tokens:", round(kv_cache_gib(48, 8, 128, ctx), 2), "GiB")
Tightly packed suitcase illustrating squeezing a large model
Fitting a large model usually takes several compromises, not one trick.

When to skip offloading entirely

If you are the only user and your prompts are short, offloading adds complexity for no gain. If your model weights barely fit, offloading the cache will not save you; a smaller model or quantization will. If your workload is many unrelated one-off prompts, there is little to reuse.

In those cases, a simpler engine configuration or even a hosted model for long-context jobs may serve you better. Large commercial APIs handle very long contexts with infrastructure no home server matches, and they are the honest choice when long context is the core of your product.

A hosted fallback for long prompts

The keyless API on this site accepts up to 32,000 tokens of context with free-ai, an independent open-weight model. It is not run by NVIDIA, and this site is not affiliated with NVIDIA. It can take occasional long jobs while your own server handles everyday traffic, as described in the local and hosted fallback guide.

Try the free API

Send a prompt from the chat box on the home page: no key, no account and no card, just a quick look at how free-ai answers.

Read also