Skip to content

vllm.distributed.kv_transfer.kv_connector.v1.lmcache_integration

Modules:

Classes:

LMCacheMPSchedulerAdapter

Methods:

Attributes:

tp_size property

The tensor parallel size.

worker_id property

The worker id.

world_size property

The world size.

__init__(server_url, context, model_name, vllm_block_size, parallel_strategy)

Args: server_url: The server URL for the LMCache message queue context: The ZMQ context

model_name: The model name used for LMCache keys vllm_block_size: The block size used in vLLM parallel_strategy: The parallel strategy, which includes use_mla, world_size, worker_id and so on

_create_hash_key(chunk_hash, request_id=None)

Create a hash-mode IPC cache engine key.

_create_key(token_ids, start=0, end=0, request_id=None)

Convert token IDs to an IPC cache engine key.

check_lookup_result(request_id)

Check the result of a previously submitted lookup request.

Parameters:

  • request_id

    (str) –

    The ID of the lookup request submitted in maybe_submit_lookup_request

Returns:

  • int | None –

    An integer representing the total number of tokens matched

  • int | None –

    in LMCache (prefix matching), or

  • int | None –

    None if the lookup request is not finished yet.

cleanup_lookup_result(request_id)

Clean up lookup future for a finished request to prevent memory leak.

Parameters:

  • request_id

    (str) –

    The ID of the finished request.

end_session(request_id)

Notify LMCache server to remove the session for a finished request.

Parameters:

  • request_id

    (str) –

    The ID of the finished request.

maybe_submit_lookup_request(request_id, block_hashes=None, token_ids=None)

Submit a new lookup request to LMCache if there is no ongoing request.

Supports both token-based and hash-based vLLM: - token_ids: token IDs (token-based vLLM) -> single token-mode key - block_hashes: block hashes (hash-based vLLM) -> strided hash-mode keys

Exactly one of block_hashes or token_ids must be provided.

Parameters:

  • request_id

    (str) –

    The ID of the lookup request. The same ID indicates it's from the same request

  • block_hashes

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

    Block hashes to lookup from LMCache (hash mode)

  • token_ids

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

    Token IDs to lookup from LMCache (token mode)

Returns:

  • None –

    None

Notes

This function will have a side-effect: submitting a look up request to LMCache, which will essentially 'lock' the KV cache chunks in the LMCache for later retrieve operations. In the meantime, this function will record the lookup request, and the status of the look up request can be checked by check_lookup_result.

num_blocks_per_chunk()

Returns: The number of vllm blocks in a LMCache data chunk

LMCacheMPWorkerAdapter

Methods:

Attributes:

is_first_rank_of_pp_group property

Is the first rank of the pipeline parallel group.

use_mla property

Whether to use MLA.

worker_id property

The worker id.

world_size property

The world size.

_create_hash_key(chunk_hash, request_id=None)

Create a hash-mode IPC cache engine key.

_create_key(token_ids, start=0, end=0, request_id=None)

Convert token IDs to an IPC cache engine key.

_update_and_get_finished_store()

Converge the internal states about finished stores and returns the 'safe finished store request ids' back

batched_submit_retrieve_requests(request_ids, ops, event)

Submit a batched retrieve request to LMCache.

Parameters:

  • request_ids

    (list[str]) –

    The IDs of the requests

  • ops

    (list[LoadStoreOp]) –

    The LoadStoreOps describing the retrieve operations. Should have the same length as request_ids

  • event

    (Event) –

    The CUDA event that is recorded after the current model inference step

batched_submit_store_requests(request_ids, ops, event)

Submit a batched store request to LMCache.

Parameters:

  • request_ids

    (list[str]) –

    The IDs of the requests

  • ops

    (list[LoadStoreOp]) –

    The LoadStoreOps describing the store operations. Should have the same length as request_ids

  • event

    (Event) –

    The CUDA event that is recorded after the current model inference step

get_finished(finished_req_ids_from_engine)

Check and get the finished store and retrieve requests.

Parameters:

  • finished_req_ids_from_engine

    (set[str]) –

    the set of request ids that are reported as finished from the vLLM engine side.

Returns:

  • set[str] | None –

    A tuple of two sets:

  • set[str] | None –
    • The first set contains the finished store request ids. The returned store request ids MUST be seen before in the finished_req_ids_from_engine.
  • tuple[set[str] | None, set[str] | None] –
    • The second set contains the finished retrieve request ids.
Notes

When enabling async scheduling in vLLM, the same request ID may appear multiple times in finished_req_ids_from_engine. The adapter should take care of deduplicating the request IDs and only return the request IDs that have not been returned before.

num_blocks_per_chunk()

Returns: The number of vllm blocks in a LMCache data chunk

register_kv_caches(kv_caches)

Register the kv caches with LMCache server.

Parameters:

  • kv_caches

    (dict[str, Tensor]) –

    A dict of kv caches to register. The keys are the layer names and the values are the corresponding tensors.

shutdown()

Shutdown the LMCache MP worker adapter.

submit_retrieve_request(request_id, op, event)

Submit a KV cache retrieve request to LMCache.

Parameters:

  • request_id

    (str) –

    The ID of the request

  • op

    (LoadStoreOp) –

    The LoadStoreOp describing the retrieve operation.

  • event

    (Event) –

    The CUDA event that is recorded after the current model inference step

submit_store_request(request_id, op, event)

Submit a KV cache store request to LMCache.

Parameters:

  • request_id

    (str) –

    The ID of the request

  • op

    (LoadStoreOp) –

    The LoadStoreOp describing the store operation.

  • event

    (Event) –

    The CUDA event that is recorded after the current model inference step

LoadStoreOp dataclass

Attributes:

  • block_hashes (list[bytes] | None) –

    Block hashes for the load/store operation (hash mode)

  • block_ids (list[int]) –

    Block ids for the load/store operation

  • end (int) –

    End token index (token mode only)

  • start (int) –

    Start token index (token mode only)

  • token_ids (list[int] | None) –

    Token IDs for the load/store operation (token mode)

block_hashes = None class-attribute instance-attribute

Block hashes for the load/store operation (hash mode)

block_ids instance-attribute

Block ids for the load/store operation

end = 0 class-attribute instance-attribute

End token index (token mode only)

start = 0 class-attribute instance-attribute

Start token index (token mode only)

token_ids = None class-attribute instance-attribute

Token IDs for the load/store operation (token mode)

ParallelStrategy dataclass

Attributes:

actual_worker_id instance-attribute

The actual worker id of the sub-process.

actual_world_size instance-attribute

The actual world size.

kv_worker_id instance-attribute

The kv worker id of the sub-process, kv_worker_id may not be equal to the actual_worker_id, in the case of mla, it will 'exclude' the effect of TP, the value is calculated by extract_world_size_and_kv_rank in lmcache_mp_connector.py.

kv_world_size instance-attribute

The kv world size, kv_world_size may not be equal to the actual_world_size, in the case of mla, it will 'exclude' the effect of TP, the value is calculated by extract_world_size_and_kv_rank in lmcache_mp_connector.py.

pp_size instance-attribute

The pipeline parallel size.

tp_size instance-attribute

The tensor parallel size.

use_mla instance-attribute

Whether to use the MLA.