Prefix Caching¶
Overview¶
Prefix caching reuses cached key-value (KV) states for shared request prefixes, which can reduce prefill computation and time to first token.
Sub-block prefix caching¶
Upstream vLLM matches cached prefixes at full KV cache block boundaries. With RBLN’s large blocks, this leaves the shared tokens in the final, incomplete block to be recomputed. RBLN vLLM adds sub-block prefix caching to reuse those tokens in smaller units called sub-blocks.
For a 4,096-token KV cache block, a sub-block size of 128, and a cached 3,000-token prefix shared with a longer prompt:
| Shared prefix tokens | Without sub-block caching | With sub-block caching |
|---|---|---|
| Reused | 0 | 23 × 128 = 2,944 |
| Recomputed | 3,000 | 3,000 − 2,944 = 56 |
New tokens in the request require computation in both cases.
Configuration¶
sub_block_size sets the sub-block size in tokens. It defaults to the prefill chunk size, max_num_batched_tokens.
Set additional_config["sub_block_size"] in Python as shown below, or use --rbln-sub-block-size on the CLI.
vllm model implementation¶
Support
Prefix caching supports text and multimodal generation, with these exclusions for sub-block caching:
- Mamba state caches.
- Cross-attention.
Size requirements
sub_block_size <= max_num_batched_tokens.sub_block_size < block_size.block_size % sub_block_size == 0.
Caching controls
Sub-block caching is enabled automatically when these requirements are met and both enable_prefix_caching and enable_sub_block_cache are True (the default).
To disable it, use --no-rbln-enable-sub-block-cache and leave sub_block_size unset.
transformers (optimum) model implementation¶
Support
Prefix caching supports text and multimodal generation, with these exclusions:
- Sliding-window attention.
- Hybrid full/sliding-window attention.
- Mamba state caches.
- Hybrid models that use Mamba state caches.
- Quantized KV caches.
- Encoder-decoder and pooling models.
Size requirements
max_num_batched_tokens <= sub_block_size <= block_size.sub_block_size % max_num_batched_tokens == 0.block_size % sub_block_size == 0.
Caching controls
Sub-block caching follows enable_prefix_caching. The enable_sub_block_cache option does not apply to this implementation.
Limitation: KV cache allocation and copying¶
Upstream vLLM reuses full KV cache blocks directly, without copying. RBLN vLLM's vllm model implementation does the same for full-block matches.
For sub-block matches, however, both model implementations allocate separate blocks and copy cached KV data into them. This avoids recomputation but adds allocation and copy overhead.
Prefix cache-aware routing¶
Model implementation support
This section applies only to the vllm implementation.
A prefix cache-aware router sends each request to an engine that already caches its prefix.
With sub-block caching enabled, KV cache events are published at sub-block granularity. To configure routing:
- Enable event publishing with
--kv-events-configusing upstream vLLM'sKVEventsConfigoptions, and configure the router to receive the events. - Match the router's token-processing block size to the engine's sub-block size. If
--rbln-sub-block-sizeis not specified, the engine uses--max-num-batched-tokensas its sub-block size.
If sub-block caching is inactive, use --block-size to match the ordinary KV cache events.
Interaction with KV connectors¶
Model implementation support
This section applies only to the vllm implementation.
For each request, the scheduler uses either sub-block caching or a KV connector, without combining them. It chooses the one that provides more additional cached tokens. On a tie, it uses sub-block caching.
KV connectors are used for KV cache offloading and disaggregated prefill / decode.
For upstream prefix caching behavior, see the vLLM APC guide and prefix caching design.