Skip to content

vllm.distributed.kv_transfer.kv_connector.v1.flexkv_connector

Classes:

FlexKVConnectorV1

Bases: KVConnectorBase_V1, SupportsHMA

KV Connector that offloads KV cache to FlexKV.

FlexKV is a distributed KV Store and multi-level cache management system designed for ultra-large-scale LLM inference. It supports offloading KV cache to CPU memory, SSD, and remote storage.

Installation

See https://github.com/taco-project/FlexKV for installation instructions. Quick start::

git clone git@github.com:taco-project/FlexKV.git
cd FlexKV && bash build.sh
Configuration

Pass kv_connector="FlexKVConnectorV1" via --kv-transfer-config::

--kv-transfer-config \
'{"kv_connector":"FlexKVConnectorV1","kv_role":"kv_both"}'

Methods:

build_connector_meta(scheduler_output)

Build the connector metadata for this step.

This function should NOT modify fields in the scheduler_output. Also, calling this function will reset the state of the connector.

Parameters:

build_kv_connector_stats(data=None) classmethod

FlexKV's stats, rebuilt in the process that logs them.

get_block_ids_with_load_errors()

Get the block ids that have failed to load.

get_finished(finished_req_ids)

Notify worker-side connector of requests that have finished generating tokens.

Returns:

  • set[str] | None –

    Tuple of (sending/saving ids, recving/loading ids) for requests

  • set[str] | None –

    that have finished asynchronous transfer. The finished saves/sends

  • tuple[set[str] | None, set[str] | None] –

    req ids must belong to a set provided in a call to this method

  • tuple[set[str] | None, set[str] | None] –

    (this call or a prior one).

get_kv_connector_stats()

Get the KV connector stats collected during the last interval.

get_loaded_kv_cache_group_ids(request)

The KV cache group a FlexKV load restores: the one FlexKV stores.

get_num_new_matched_tokens(request, num_computed_tokens)

Get the number of new tokens that can be loaded from the external KV cache beyond num_computed_tokens.

Parameters:

  • request

    (Request) –

    the request object.

  • num_computed_tokens

    (int) –

    the number of locally computed tokens for this request.

Returns:

  • int | None –

    Tuple of (num_external_tokens, is_ready) where

  • bool –

    num_external_tokens is the number of additional tokens that

  • tuple[int | None, bool] –

    can be loaded from the external KV cache, or None while FlexKV

  • tuple[int | None, bool] –

    still prefetches the request's prefix (ask again later).

get_transfer_results(finished_req_ids)

Report the loads FlexKV found failed, as received and failed.

FlexKV sees a load fail on the scheduler side and passes it to the workers in the connector metadata, as only a worker reports one.

has_pending_block_frees()

Whether a FlexKV PUT will soon free a finished request's blocks, which vLLM can wait for instead of preempting a request.

has_pending_push_work()

Whether FlexKV transfers still complete only while the engine keeps stepping (e.g. an SSD write after the request finished).

on_new_request(request)

Start FlexKV's prefetch of the request while it waits.

register_kv_caches(kv_caches)

Initialize with the KV caches. Useful for pre-registering the KV caches in the KVConnector (e.g. for NIXL).

Parameters:

  • kv_caches

    (dict[str, Tensor]) –

    dictionary of layer names to kv cache tensors.

request_finished(request, block_ids)

Called when a request has finished, before its blocks are freed.

Returns:

  • bool –

    Tuple of (async_save, kv_transfer_params) where async_save is

  • dict[str, Any] | None –

    True if the request is being saved/sent asynchronously and blocks

  • tuple[bool, dict[str, Any] | None] –

    should not be freed until the request_id is returned from

  • tuple[bool, dict[str, Any] | None] –

    get_finished. kv_transfer_params is an optional dict of

  • tuple[bool, dict[str, Any] | None] –

    KVTransferParams to be included in the request outputs.

request_finished_all_groups(request, block_ids)

request_finished with the hybrid KV cache manager: one block id list per KV cache group. FlexKV stores the prefix-cacheable group.

reset_cache()

Drop every tier of the FlexKV cache.

Returns False while transfers are still in flight; retry once they have drained.

restore_kv_caches()

Register the KV caches with FlexKV again after wake-up.

save_kv_layer(layer_name, kv_layer, attn_metadata, **kwargs)

No-op for FlexKV (currently).

FlexKV offloads KV cache asynchronously from the scheduler side after a request finishes (see request_finished). It does not intercept individual layer tensors during the forward pass.

This hook is retained to satisfy KVConnectorBase_V1 and as an extension point for future per-layer async offload support.

Parameters:

  • layer_name

    (str) –

    the name of the layer (unused).

  • kv_layer

    (Tensor) –

    the paged KV buffer (unused).

  • attn_metadata

    (AttentionMetadata) –

    the attention metadata (unused).

  • **kwargs

    (Any, default: {} ) –

    additional arguments (unused).

start_load_kv(forward_context, **kwargs)

Bind this step's layer-wise loads, if any.

FlexKV manages all KV transfers on the scheduler side via build_connector_meta (which calls launch_tasks) and update_connector_output (which polls query_finished_task). KV blocks are transferred directly between the FlexKV server and vLLM's GPU memory without worker-side intervention during the forward pass — similar to how NIXL operates. With use_layerwise this binds the per-layer completion events of the loads that wait_for_layer_load waits on.

Parameters:

  • forward_context

    (ForwardContext) –

    the forward context.

  • **kwargs

    (Any, default: {} ) –

    additional arguments (unused).

take_events()

Collect buffered KV cache events.

Returns:

unregister_kv_caches()

Release FlexKV's mappings of the KV caches before sleep.

update_connector_output(connector_output)

Update KVConnector state from worker-side connectors output.

Parameters:

  • connector_output

    (KVConnectorOutput) –

    the worker-side connectors output.

update_state_after_alloc(request, blocks, num_external_tokens)

Update KVConnector state after block allocation.

wait_for_layer_load(layer_name)

Block until the layer's KV is loaded (layer-wise loading only).

Parameters:

  • layer_name

    (str) –

    the name of the layer.

wait_for_save()

No-op for FlexKV (currently).

KV offload tasks are tracked asynchronously by the scheduler connector via request_finished / query_finished_task. There is no pending worker-side save to wait for at forward-context exit.

Retained to satisfy KVConnectorBase_V1 and as an extension point for future worker-side save-completion signalling.

_flexkv_impl_cls()

FlexKV's connector implementation class, or None without FlexKV.