Skip to content

vllm.v1.core.block_pool

Classes:

  • BlockHashToBlockMap –

    Cache of blocks that are used for prefix caching, from block hash to

  • BlockPool –

    BlockPool that manages KVCacheBlocks.

BlockHashToBlockMap

Cache of blocks that are used for prefix caching, from block hash to one or more cached blocks of the pool.

A cached block is a full block with a block hash that can be used for prefix caching. The cached block may be used by running requests or in the free_block_queue that could potentially be evicted.

NOTE #1: We currently don't de-duplicate the blocks in the cache, meaning that if a block becomes full and is cached, we don't check if there is already an identical block in the cache. This is because we want to make sure the allocated block IDs won't change so that block tables are append-only. NOTE #2: Entries hold block ids, not blocks: the cyclic GC does not track a dict of only bytes and ints, so cached blocks add nothing to each collection.

Methods:

  • contain –

    Checks whether the key maps to the given block ID.

  • get_one_block –

    Gets any block with the given block hash key.

  • insert –

    Inserts the KVCacheBlock to the cache

  • pop –

    Pops block_id from the blocks cached under key, if it is there.

contain(key, block_id)

Checks whether the key maps to the given block ID.

get_one_block(key)

Gets any block with the given block hash key.

insert(key, block)

Inserts the KVCacheBlock to the cache

pop(key, block_id)

Pops block_id from the blocks cached under key, if it is there.

BlockPool

BlockPool that manages KVCacheBlocks. It provides methods to allocate, free and cache the kv cache blocks. The free_block_queue stores the free blocks in eviction order to enable allocation, free, and cache eviction. The cached_block_hash_to_block maps between block hash and cached block to support finding cached blocks by their block hash.

Parameters:

  • num_gpu_blocks

    (int) –

    The number of blocks in the pool.

  • enable_caching

    (bool) –

    Whether to enable prefix caching.

  • hash_block_size

    (int) –

    The block size of which the block hashes are computed. The actual block size usually equals hash_block_size, but in cases where different KV cache groups have different block sizes, the actual block size can be a multiple of hash_block_size.

  • enable_kv_cache_events

    (bool, default: False ) –

    Whether to enable kv cache events.

  • metrics_collector

    (KVCacheMetricsCollector | None, default: None ) –

    Optional metrics collector for tracking block residency.

  • num_hash_lookahead_tokens

    (int, default: 0 ) –

    Tokens after each block covered by its hash.

  • medium

    (str, default: MEDIUM_GPU ) –

    Storage medium reported in KV cache events.

Methods:

_build_block_stored_event(request, block_hashes, parent_block_hash, start_token_idx, end_token_idx, block_size, kv_cache_group_id, extra_keys_list, lookahead_token_ids)

Build a BlockStored KV event for request.

Shared by cache_full_blocks (newly cached blocks) and emit_cached_block_events (prefix-cache-reused blocks) so both emit identical event shapes for downstream consumers.

_maybe_evict_cached_block(block)

If a block is cached in cached_block_hash_to_block, we reset its hash metadata and evict it from the cache.

Parameters:

Returns:

  • bool –

    True if the block is evicted, False otherwise.

cache_full_blocks(request, blocks, num_cached_blocks, num_full_blocks, block_size, kv_cache_group_id, block_mask=None)

Cache a list of full blocks for prefix caching. This function takes a list of blocks that will have their block hash metadata to be updated and cached. Given a request, it updates the metadata for each block and caching it in the cached_block_hash_to_block. The block hashes values are computed by the Request object immediately when it is created and when new tokens are appended.

Parameters:

  • request

    (Request) –

    The request to cache the blocks.

  • blocks

    (list[KVCacheBlock]) –

    All blocks in the request.

  • num_cached_blocks

    (int) –

    The number of blocks that are already cached.

  • num_full_blocks

    (int) –

    The number of blocks that are full and should be cached after this function.

  • block_size

    (int) –

    Number of tokens in each block.

  • kv_cache_group_id

    (int) –

    The id of the KV cache group.

  • block_mask

    (list[bool] | None, default: None ) –

    Optional mask aligned with blocks[num_cached_blocks:num_full_blocks]. When provided, blocks where the mask is False are skipped (treated like null blocks). Used by groups whose find_longest_cache_hit only consults a subset of blocks (e.g. SWA tail-window), so blocks that can never serve a hit stay out of the prefix-cache hash map.

cache_partial_block(request, block, num_tokens, kv_cache_group_id, block_size, replace_existing_hashes=False)

Register a partial prefix-cache entry for an existing block.

Prefix-cache keys normally identify full cache blocks. A partial entry makes an existing cache block reachable from a fine-grained prefix boundary inside that block without allocating or copying a new KVCacheBlock.

The partial entry is lookup metadata owned by block. If block has no primary hash, the key becomes its primary hash. If the block already has a primary hash, the partial entry is tracked in cached_block_hashes_by_block so eviction, reset, and promotion can remove every hash key that points to the block.

Parameters:

  • request

    (Request) –

    Request whose token IDs and block hashes define the partial entry.

  • block

    (KVCacheBlock) –

    Existing cache block to make reachable from the partial prefix boundary.

  • num_tokens

    (int) –

    Prefix length represented by the partial entry. It must be a positive multiple of self.hash_block_size and cannot exceed the request's computed block hashes.

  • kv_cache_group_id

    (int) –

    KV cache group that owns the partial entry.

  • block_size

    (int) –

    Cache block size for the owning group. The partial entry hash itself is always the prefix-chain hash at num_tokens; block_size must be a multiple of the hash block size.

  • replace_existing_hashes

    (bool, default: False ) –

    Whether the block contents were replaced and all existing cache entries must be removed before the new entry is registered.

Returns:

  • BlockHashWithGroupId | None –

    The hash key with group ID if a partial entry can be registered;

  • BlockHashWithGroupId | None –

    otherwise None for null blocks.

deprioritize_blocks(blocks)

Deprioritize free blocks so they are evicted after uncached blocks but before normal LRU-cached blocks, in FIFO order.

Blocks still referenced (ref_cnt > 0) or not in the free queue are left unchanged.

emit_cached_block_events(request, num_cached_blocks, block_size, kv_cache_group_id)

Generate BlockStored events for blocks reused from prefix cache.

Unlike cache_full_blocks(), this does NOT modify block state — the blocks are already cached. It only generates events so that external consumers (e.g. gateway) can learn about reused blocks.

Parameters:

  • request

    (Request) –

    The request whose prefix cache blocks were reused.

  • num_cached_blocks

    (int) –

    Number of blocks that were cache hits.

  • block_size

    (int) –

    Number of tokens per block.

  • kv_cache_group_id

    (int) –

    The KV cache group ID.

evict_blocks(block_ids)

Evict blocks from the prefix cache by their block IDs.

only evicts blocks that are currently cached (have a hash). blocks with ref_cnt > 0 are not freed from the block pool, only evicted from the prefix cache hash table.

Parameters:

  • block_ids

    (set[int]) –

    Set of block IDs to evict from cache.

free_blocks(ordered_blocks, evict_first=False)

Free a list of blocks. The blocks should be ordered by their eviction priority, where the first block will be evicted first.

Parameters:

  • ordered_blocks

    (Iterable[KVCacheBlock]) –

    A list of blocks to free ordered by their eviction priority.

  • evict_first

    (bool, default: False ) –

    Also put freed cached blocks at the front of the free queue, so they are evicted before other cached blocks. For blocks whose contents another tier already holds.

get_cached_block(block_hash, kv_cache_group_ids)

Get the cached block by the block hash for each group in kv_cache_group_ids, or None if cache miss for any group. If there are duplicated blocks, we return the first block in the cache.

Parameters:

  • block_hash

    (BlockHash) –

    The hash value of the block.

  • kv_cache_group_ids

    (list[int]) –

    The ids of the KV cache groups.

Returns:

get_new_blocks(num_blocks)

Get new blocks from the free block pool.

Note that we do not check block cache in this function.

Parameters:

  • num_blocks

    (int) –

    The number of blocks to allocate.

Returns:

get_num_free_blocks()

Get the number of free blocks in the pool.

Returns:

  • int –

    The number of free blocks.

get_num_uncached_free_blocks(limit)

Count, up to limit, the leading uncached free blocks that get_new_blocks hands out before any cached prefix block.

get_usage()

Get the KV cache usage.

Returns:

  • float –

    The KV cache usage (between 0.0 and 1.0).

is_block_writable(block)

Return whether a block can be mutated by its sole owner.

move_block_hashes(src_block, dst_block)

Re-point src_block's prefix-cache entries to dst_block.

Used when the request owning src_block keeps writing into it
the prefix cache holds a private copy (dst_block) under the same hashes instead. Entries stay live; no events emitted.

reset_prefix_cache()

Reset prefix cache. This function may be used in RLHF flows to invalidate prefix caching after the weights are updated, or used for resetting prefix caching status for benchmarking.

Returns:

  • bool ( bool ) –

    True if the prefix cache is successfully reset,

  • bool –

    False otherwise.

take_events()

Atomically takes all events and clears the queue.

Returns:

touch(blocks)

Touch a block increases its reference count by 1, and may remove the block from the free queue. This is used when a block is hit by another request with the same prefix.

Parameters:

unpin_blocks(blocks, on_reuse)

Release references to blocks that stay readable until reused.

The blocks become last-resort eviction candidates regardless of prefix caching: they join the tail of the free queue and count as free. on_reuse fires when get_new_blocks hands a block out (or the cache is reset), so the caller can stop reading it.