This document is relevant for: Trn2, Trn3
How to use CPU KV offloading#
Task overview#
This topic discusses how to add a host-memory tier to the prefix cache using vLLM on Neuron. vLLM stores full KV blocks from HBM to host memory. When a request reuses a prefix that HBM no longer holds, vLLM loads the blocks back instead of computing them again.
This is the engine side of a tiered prefix cache, and it uses the same
configuration as upstream vLLM. So a serving framework that configures vLLM’s
OffloadingConnector can use it, for example llm-d and its tiered prefix cache.
CPU KV offloading helps when requests reuse long prefixes that do not all fit in HBM. Examples are agentic sessions, multi-turn chat, and retrieval over the same long documents. It does not help when requests share no prefixes, or when the HBM prefix cache already holds them.
Prerequisites#
Prefix caching: Prefix caching is on (
--enable-prefix-caching).Segmented prefill:
--max-num-batched-tokensis less than--max-model-len. See Segmented prefill.Host memory: The host has free memory for the tier, more than the HBM KV cache. A host tier that is not larger than HBM adds few hits.
Lock limit (
asyncmode only): The container can lock the tier’s memory:ulimit -l unlimited, or--ulimit memlock=-1for a Docker container.No DCP: Decode-context parallelism is off. CPU KV offloading does not support DCP.
Instructions#
1. Choose how to enable CPU KV offloading#
Use one of these forms. The platform redirects each of them to
NeuronOffloadingConnector.
With --kv-transfer-config. This is the form that the llm-d guides use.
cpu_bytes_to_use is in bytes.
vllm serve meta-llama/Llama-3.1-8B-Instruct \
--tensor-parallel-size 8 \
--enable-prefix-caching \
--max-num-batched-tokens 512 \
--kv-transfer-config '{
"kv_connector": "OffloadingConnector",
"kv_role": "kv_both",
"kv_load_failure_policy": "recompute",
"kv_connector_extra_config": {"cpu_bytes_to_use": 68719476736}
}'
With --kv-offloading-size. The size is in GiB. The platform builds the
connector configuration, with kv_load_failure_policy set to recompute.
vllm serve meta-llama/Llama-3.1-8B-Instruct \
--tensor-parallel-size 8 \
--enable-prefix-caching \
--max-num-batched-tokens 512 \
--kv-offloading-size 64
Only vLLM’s own offloading backend (--kv-offloading-backend native, the
default) is redirected. With --kv-offloading-backend lmcache or
VLLM_USE_SIMPLE_KV_OFFLOAD=1, upstream vLLM handles the request on Model
Runner V1.
Next to NIXL on a disaggregated prefill server. List both connectors under a
MultiConnector. --kv-offloading-size refuses to replace another connector,
and its error shows this form.
{
"kv_connector": "MultiConnector",
"kv_role": "kv_producer",
"kv_load_failure_policy": "recompute",
"kv_connector_extra_config": {
"connectors": [
{"kv_connector": "NixlConnector", "kv_role": "kv_producer"},
{
"kv_connector": "OffloadingConnector",
"kv_role": "kv_both",
"kv_connector_extra_config": {"cpu_bytes_to_use": 68719476736}
}
]
}
}
2. Set the load failure policy to recompute#
Set "kv_load_failure_policy": "recompute" in --kv-transfer-config. Then a
failed CPU load is a late cache miss, and vLLM computes its tokens.
Note
With vLLM’s default, fail, the request of a failed CPU load fails. The server
logs a warning at startup. The policy applies to the whole engine, so it also
covers the NIXL receives of a MultiConnector.
3. Size the host tier#
cpu_bytes_to_use is all the host memory of the connector on the ranks of one
engine. It includes the staging memory of each rank:
Copy mode |
Staging per rank |
|---|---|
|
8 MiB ( |
|
512 MiB ( |
Each rank holds an equal share of the rest:
host tier per rank = (cpu_bytes_to_use - world_size x staging per rank) / world_size
Each data-parallel engine takes its own cpu_bytes_to_use. A server with
--data-parallel-size 4 on one host therefore needs 4 times the budget on that
host.
The server refuses a budget that the host cannot back. It checks the available
host memory before the engines start, before the ranks allocate the tier, and
after warmup. The available memory is the lower of the container’s headroom and
MemAvailable. The inactive page cache, for example of the checkpoint files,
counts as available. Each check allows at most host_memory_safety_fraction of
the available memory (default 0.8). Set the fraction to 0 to skip these checks.
The lock-limit check of the async mode still runs.
Servers that start together on one host also count each other. An example is
the prefill and decode servers of a single-node disaggregated deployment. While
a rank commits its tier, it holds a reservation in
/dev/shm/vllm_neuron_cpu_offload, and the checks of other ranks subtract it.
Servers in different containers count each other only when the containers share
/dev/shm (for example with --ipc=host) and run as the same user.
Important
The host tier counts toward the memory limit of the container. Set the
container or pod memory limit above cpu_bytes_to_use and the other host
memory of the server.
4. (Optional) Choose a copy mode#
The default, auto, picks async when all of these are true, and sync
otherwise:
async scheduling is on;
speculative decoding is off;
the pipeline-parallel size is 1;
the executor is
mp,rayorexternal_launcher;the libtorch-neuronx-lite transfer ops are present;
the lock limit covers the rank’s share of the tier and its arena.
Mode |
How it copies |
Cost |
|---|---|---|
|
|
Each step that copies waits for the copy and for the in-flight output |
|
libtorch-neuronx-lite async transfers on a transfer thread |
Page-locks a staging arena at startup, and the host tier after warmup |
To choose a mode, set "xfer_mode" to "sync" or "async" in
kv_connector_extra_config:
"kv_connector_extra_config": {"cpu_bytes_to_use": 68719476736, "xfer_mode": "sync"}
The server refuses an explicit async at startup when one of these is true:
async scheduling is off;
speculative decoding is on;
the executor is not
mp,rayorexternal_launcher;the lock limit is too low.
On several nodes, each node resolves auto on its own. Nodes with different
lock limits can pick different modes, and the server then refuses to start. Set
xfer_mode explicitly for a multi-node server, or give every node the same lock
limit.
5. Start the server#
Start the server as usual. In both modes, the host tier takes no memory during
the compile. After warmup, each rank commits its tier: sync mode populates it,
and async mode page-locks it. This adds to the startup time. The ranks commit
in parallel. In a test on a Trn3 instance with 8 ranks, each rank committed a
23.99 GiB tier in 6.1 s in sync mode.
Confirm your work#
To confirm that CPU KV offloading works, check the startup log and the metrics.
At startup, the server logs the copy mode and the reason:
Neuron platform: CPU KV offloading copies in async mode (every condition of the async readback gate holds).
Set "xfer_mode" to "sync" or "async" in kv_connector_extra_config to choose it
Each rank logs its share of the host tier:
Neuron CPU offloading capacity: <blocks> blocks x <size> MiB/block/rank = <tier> GiB per rank,
<total> GiB across <ranks> ranks; with staging <size> GiB per rank; <free> GiB available on this host
After warmup, each rank logs the commit of its tier, in both modes:
CPU offloading host tier resident: <size> GiB on this rank in <time> s
While the server serves traffic, check these metrics:
Metric |
Meaning |
|---|---|
|
Tokens that were looked up in the host tier |
|
Tokens that came from the host tier |
|
Bytes copied from HBM to host memory |
|
Bytes copied from host memory to HBM |
|
Failed loads. vLLM computes their tokens instead |
|
Failed stores. Their blocks are dropped |
To check the copies byte for byte on a test server, set "verify": true in
kv_connector_extra_config. Each worker then logs
offload verify: device=... totals ..., and offload verify mismatch for a
block that differs. The check is slow. Do not use it in production.
For a lighter check, set "verify_host_hash": true. Each worker records a
CRC-32 of each host block that a store writes, and checks it when a load reads
the block. The check reads no device memory and does not drain the in-flight
output, so the copies keep their production timing. Each worker logs
offload host hash: device=... totals ..., and offload host hash mismatch for
a block that changed after its store, or that no store wrote. This check does
not show a copy that moved the wrong device block.
Configuration reference#
Set these options in kv_connector_extra_config:
Option |
Default |
Description |
|---|---|---|
|
Required |
Host memory for one engine, in bytes, staging included |
|
|
|
|
|
Share of the available host memory that the checks allow. |
|
|
Copy buffer of each rank in |
|
|
Page-locked staging arena of each rank in |
|
|
A flush that sees no job finish for this long gives up on its jobs |
|
|
A load not finished this long after it was queued is computed instead |
|
|
A store not finished this long after it was queued is dropped |
|
|
Debug only. Compares every copied block with its host copy. Slow |
|
|
Debug only. Checks each loaded host block against the CRC-32 of its store. Reads host memory only |
Limitations#
Decode-context parallelism is not supported.
A chunk is one KV block. Leave out
blocks_per_chunkandblock_sizeinkv_connector_extra_config, or set only one of them:blocks_per_chunkto 1, orblock_sizeto the KV cache block size. vLLM refuses a configuration that sets both.canonical_layoutis not supported.MLA caches, key-only caches (such as a sparse-attention indexer), and Mamba or linear-attention state are not supported. The server refuses such a layer at startup.
Common issues#
The server refuses the budget at startup#
The message starts with cpu_bytes_to_use needs ... GiB of host memory on <host>, or with CPU KV offloading: cpu_bytes_to_use (...) applies to each data-parallel engine.
Possible solution: Lower
cpu_bytes_to_useto the value in the message. With data parallelism, the budget applies to each engine on the host. You can also run fewer engines on the host.
The server stops after warmup#
The message is the CPU offloading host tier takes ... GiB on <host> ..., but after warmup only ... GiB of host memory is available. Warmup or another
process took host memory after the first check.
Possible solution: Lower
cpu_bytes_to_useto the value in the message.
Another server on the host takes the memory#
The message is the CPU offloading host tier of this rank needs ... GiB, but only ... GiB of host memory is available. Other servers that are starting reserve ... GiB of it.
Possible solution: Start the servers one after another, or lower
cpu_bytes_to_use.
async mode cannot lock its memory#
The message starts with The async copy mode page-locks its host memory.
Possible solution: Raise the lock limit:
ulimit -l unlimited, or--ulimit memlock=-1for a Docker container. You can also setxfer_modetosync.
The server refuses an explicit async mode#
The message starts with xfer_mode=async needs async scheduling, or with
xfer_mode=async does not support speculative decoding.
Possible solution: Set
xfer_modetoautoorsync. You can also turn on async scheduling, or turn off speculative decoding.
The ranks do not agree on their settings#
The message starts with The ranks of this engine do not agree on their CPU offloading settings. It names the copy mode, the host block count and the
safety fraction of two ranks.
Possible solution: Set
xfer_modeinkv_connector_extra_config, so that no node resolvesautoon its own. You can also give every node the same lock limit (ulimit -l) and the samekv_connector_extra_config.
The budget does not cover the staging memory#
The message starts with cpu_bytes_to_use (...) does not cover the ... GiB of staging memory.
Possible solution: Raise
cpu_bytes_to_useabove the staging of all ranks. See Size the host tier.
The server refuses a chunk of several blocks#
The message starts with Neuron CPU offloading supports blocks_per_chunk == 1 only.
Possible solution: Remove
blocks_per_chunkandblock_sizefromkv_connector_extra_config. You can also setblock_sizeto the KV cache block size.
--kv-offloading-size refuses another connector#
The message starts with --kv-offloading-size would replace kv_connector.
Possible solution: Use the
MultiConnectorform that the message shows.
The server refuses CPU offloading with DCP#
The message is DCP requires kv_connector='NeuronNixlConnector', got 'NeuronOffloadingConnector'.
Possible solution: Turn off DCP, or turn off CPU KV offloading.
Host hits are rare#
The server logs CPU offloading host tier holds ..., no more than the HBM prefix cache.
Possible solution: Raise
cpu_bytes_to_use, so that the host tier is larger than the HBM KV cache.
A failed CPU load fails its request#
The server logs kv_load_failure_policy is 'fail' (vLLM's default) at startup.
Possible solution: Set
"kv_load_failure_policy": "recompute"in--kv-transfer-config.