Skip to content

vllm.distributed.kv_transfer.kv_connector.v1.offloading.state

Classes:

Functions:

RequestOffloadState dataclass

Methods:

  • storable_chunks –

    Number of allocated and keyed leading chunks eligible for store.

Source code in vllm/distributed/kv_transfer/kv_connector/v1/offloading/state.py
@dataclass(slots=True)
class RequestOffloadState:
    config: SchedulerOffloadConfig
    req: Request
    req_context: ReqContext
    offloading_context: RequestOffloadingContext
    group_states: tuple[RequestGroupState, ...] = field(init=False)
    # upper bound on tokens to load for this request; None means no cap
    max_load_tokens: int | None = None
    # upper bound on tokens to offload for this request; None means no cap
    max_offload_tokens: int | None = None
    # number of hits in the GPU cache
    num_locally_computed_tokens: int = 0
    # In-flight job IDs. Per the connector's invariant, at any given time
    # this contains either a single load job, or one or more store jobs.
    transfer_jobs: set[int] = field(default_factory=set)
    # time.monotonic() of this request's first deferred offload lookup;
    # None once consumed (observed) or while no lookup is pending.
    deferred_lookup_start_time: float | None = None
    # Fine-grained token boundary selected beyond the last complete offload
    # chunk. It is consumed when the corresponding load is scheduled.
    partial_tail_boundary: int | None = None
    # Loaded attention keys cover their token positions. Recurrent state
    # supports the whole reused external prefix.
    load_key_ranges: list[tuple[int, int, OffloadKey]] = field(default_factory=list)
    # True once on_request_finished has been signaled to the manager.
    finished_signaled: bool = False
    # A load for this request failed; all its later lookups, including after
    # preemption, skip the offloaded cache so a faulting load is not retried.
    load_failed: bool = False

    def __post_init__(self) -> None:
        self.group_states = tuple(
            RequestGroupState() for _ in self.config.kv_group_configs
        )
        params = self.req.kv_transfer_params

        # NOTE: This field is experimental and subject to change in the future.
        raw = params.get("max_load_tokens") if params else None
        if type(raw) is int and raw >= 0:
            self.max_load_tokens = raw
            logger.debug(
                "Request %s: max_load_tokens set to %d",
                self.req.request_id,
                raw,
            )
        elif raw is not None:
            logger.warning(
                "max_load_tokens must be a non-negative int, got %r; ignoring", raw
            )

        # NOTE: This field is experimental and subject to change in the future.
        max_offload_tokens = get_max_offload_tokens(params)
        if max_offload_tokens is not None:
            self.max_offload_tokens = max_offload_tokens
            logger.debug(
                "Request %s: max_offload_tokens set to %d",
                self.req.request_id,
                self.max_offload_tokens,
            )

    def update_offload_keys(self) -> None:
        for group_config, group_state in zip(
            self.config.kv_group_configs, self.group_states
        ):
            stride = group_config.hashes_per_chunk
            start = stride * (len(group_state.offload_keys) + 1) - 1
            for req_block_hash in self.req.block_hashes[start::stride]:
                key = make_offload_key(req_block_hash, group_config.group_idx)
                group_state.offload_keys.append(key)
                self.req_context.set_offload_key_position(
                    key,
                    len(group_state.offload_keys) * group_config.tokens_per_chunk,
                )

    def update_block_id_groups(
        self, new_block_id_groups: tuple[list[int], ...] | None
    ) -> None:
        if new_block_id_groups is None:
            return

        for group_config, group_state in zip(
            self.config.kv_group_configs, self.group_states
        ):
            group_state.block_ids.extend(new_block_id_groups[group_config.group_idx])

    def storable_chunks(
        self,
        group_config: "GroupOffloadConfig",
        group_state: RequestGroupState,
        num_offloadable_tokens: int,
    ) -> int:
        """Number of allocated and keyed leading chunks eligible for store."""
        num_chunks = num_offloadable_tokens // group_config.tokens_per_chunk
        num_allocated_chunks = (
            len(group_state.block_ids) // self.config.blocks_per_chunk
        )
        num_keyed_chunks = len(group_state.offload_keys)
        return min(num_chunks, num_allocated_chunks, num_keyed_chunks)

    def num_offloadable_tokens(self, num_computed_tokens: int) -> int:
        # Only hashed tokens can be keyed; lookahead hashes lag the tokens.
        num = min(
            num_computed_tokens,
            self.req.num_tokens,
            len(self.req.block_hashes) * self.config.tokens_per_hash,
        )
        if self.max_offload_tokens is not None:
            num = min(num, self.max_offload_tokens)
        if self.config.offload_prompt_only:
            num = min(num, self.req.num_prompt_tokens)
        return num

    def register_boundary_key(self, group_idx: int, boundary_tokens: int) -> OffloadKey:
        hash_idx = boundary_tokens // self.config.tokens_per_hash - 1
        key = make_offload_key(self.req.block_hashes[hash_idx], group_idx)
        self.req_context.set_offload_key_position(key, boundary_tokens)
        return key

    def update_num_hit_chunks(self, num_cached_tokens: int) -> None:
        for group_config, group_state in zip(
            self.config.kv_group_configs, self.group_states
        ):
            group_state.num_hit_chunks = (
                num_cached_tokens // group_config.tokens_per_chunk
            )

storable_chunks(group_config, group_state, num_offloadable_tokens)

Number of allocated and keyed leading chunks eligible for store.

Source code in vllm/distributed/kv_transfer/kv_connector/v1/offloading/state.py
def storable_chunks(
    self,
    group_config: "GroupOffloadConfig",
    group_state: RequestGroupState,
    num_offloadable_tokens: int,
) -> int:
    """Number of allocated and keyed leading chunks eligible for store."""
    num_chunks = num_offloadable_tokens // group_config.tokens_per_chunk
    num_allocated_chunks = (
        len(group_state.block_ids) // self.config.blocks_per_chunk
    )
    num_keyed_chunks = len(group_state.offload_keys)
    return min(num_chunks, num_allocated_chunks, num_keyed_chunks)

SchedulerOffloadConfig

Bases: NamedTuple

Attributes:

Source code in vllm/distributed/kv_transfer/kv_connector/v1/offloading/state.py
class SchedulerOffloadConfig(NamedTuple):
    kv_group_configs: tuple[GroupOffloadConfig, ...]
    blocks_per_chunk: int
    tokens_per_hash: int
    num_workers: int
    offload_prompt_only: bool
    supports_partial_tail: bool
    alignment_tokens: int | None = None
    retention_interval: int | None = None
    dcp_world_size: int = 1
    sync_load_budget_bytes: int = 0
    # KV bytes per token on one worker, summed over the offloaded groups.
    kv_bytes_per_token: int = 0

    # Lookahead block hashes already cover positions past the computed tokens.
    num_hash_lookahead_tokens: int = 0
    cache_hit_alignment_tokens: int | None = None

    @property
    def supports_handoff(self) -> bool:
        """Whether the KV cache manager can hand off state blocks."""
        return any(cfg.requires_cow_source for cfg in self.kv_group_configs)

    @property
    def partial_tail_block_size(self) -> int:
        if not self.supports_partial_tail:
            return 0
        assert self.alignment_tokens is not None
        return self.alignment_tokens

    @classmethod
    def from_spec(
        cls,
        spec: OffloadingSpec,
        vllm_config: VllmConfig,
        kv_cache_config: KVCacheConfig,
    ) -> "SchedulerOffloadConfig":
        # Determine the alignment token count from the full-attention group(s).
        # This is the tokens_per_chunk of the full-attention group; load
        # hits are always aligned to this boundary, so SWA blocks earlier in
        # each segment can never serve a load hit. Relevant for hybrid
        # architectures like DeepSeek V4 (MLA + SWA groups).
        full_attn_tokens_per_chunk: set[int] = set()
        for group in spec.config.groups:
            if group.sliding_window_size_in_chunks is None:
                full_attn_tokens_per_chunk.add(
                    group.tokens_per_block * spec.blocks_per_chunk
                )

        # Only apply the optimization if there's a single consistent
        # full-attention alignment size.  For pure SWA/Mamba models (no
        # full-attention groups), fall back to the largest SWA/Mamba
        # tokens_per_chunk so retention_interval has a boundary
        # granularity to work with.
        alignment_tokens: int | None = None
        if len(full_attn_tokens_per_chunk) == 1:
            alignment_tokens = full_attn_tokens_per_chunk.pop()
        elif not full_attn_tokens_per_chunk:
            all_tokens_per_chunk = [
                tpb * spec.blocks_per_chunk for tpb in spec.tokens_per_block
            ]
            if all_tokens_per_chunk:
                alignment_tokens = max(all_tokens_per_chunk)

        retention_interval = vllm_config.cache_config.prefix_cache_retention_interval

        kv_group_configs_list: list[GroupOffloadConfig] = []
        for group in spec.config.groups:
            group_id = group.group_id
            tokens_per_block = group.tokens_per_block
            kv_cache_group = kv_cache_config.kv_cache_groups[group_id]
            kv_group_configs_list.append(
                GroupOffloadConfig(
                    group_idx=group_id,
                    tokens_per_block=tokens_per_block,
                    tokens_per_chunk=tokens_per_block * spec.blocks_per_chunk,
                    hashes_per_chunk=(
                        (tokens_per_block * spec.blocks_per_chunk)
                        // spec.tokens_per_hash
                    ),
                    sliding_window_size_in_chunks=group.sliding_window_size_in_chunks,
                    block_size=kv_cache_group.block_size,
                    manager_config=kv_cache_group.manager_config,
                    kv_event_group_spec=get_offloading_event_group_spec(kv_cache_group),
                    requires_cow_source=is_mamba_prefix_caching_group(kv_cache_group),
                )
            )
        kv_group_configs = tuple(kv_group_configs_list)
        has_partial_recurrent_group = any(
            config.requires_cow_source for config in kv_group_configs
        )
        # Store sub-block tails only when core can reuse them. Recurrent blocks
        # stay unsharded under DCP and must divide the full-attention chunk.
        dcp_world_size = vllm_config.parallel_config.decode_context_parallel_size
        cache_hit_alignment = kv_cache_config.cache_hit_alignment_tokens
        supports_partial_tail = (
            spec.blocks_per_chunk == 1
            and alignment_tokens is not None
            and cache_hit_alignment is not None
            and cache_hit_alignment < alignment_tokens
            and has_partial_recurrent_group
            and all(
                config.sliding_window_size_in_chunks is None
                or (
                    config.requires_cow_source
                    and alignment_tokens % config.tokens_per_block == 0
                    and config.tokens_per_block % spec.tokens_per_hash == 0
                )
                for config in kv_group_configs
            )
            and spec.tokens_per_hash % dcp_world_size == 0
        )

        if retention_interval is not None:
            if retention_interval < 0:
                raise ValueError(
                    f"prefix_cache_retention_interval "
                    f"({retention_interval}) must be non-negative."
                )
            for config in kv_group_configs:
                if (
                    config.sliding_window_size_in_chunks is not None
                    and retention_interval % config.tokens_per_chunk != 0
                ):
                    raise ValueError(
                        f"prefix_cache_retention_interval "
                        f"({retention_interval}) must be a multiple of "
                        f"tokens_per_chunk ({config.tokens_per_chunk})."
                    )

        return cls(
            num_workers=vllm_config.parallel_config.world_size,
            kv_group_configs=kv_group_configs,
            blocks_per_chunk=spec.blocks_per_chunk,
            tokens_per_hash=spec.tokens_per_hash,
            offload_prompt_only=spec.offload_prompt_only,
            supports_partial_tail=supports_partial_tail,
            alignment_tokens=alignment_tokens,
            retention_interval=retention_interval,
            dcp_world_size=dcp_world_size,
            # Per-step KV bytes (per worker) loaded within the step (0 disables).
            sync_load_budget_bytes=int(
                spec.extra_config.get("sync_load_budget_bytes", 0)
            ),
            kv_bytes_per_token=sum(
                kv_cache_config.kv_cache_groups[g.group_id].page_size_bytes
                // g.tokens_per_block
                for g in spec.config.groups
            ),
            num_hash_lookahead_tokens=vllm_config.num_hash_lookahead_tokens,
            cache_hit_alignment_tokens=cache_hit_alignment,
        )

supports_handoff property

Whether the KV cache manager can hand off state blocks.

resolve_mamba_align_size(spec, kv_cache_config)

Scan all KV cache groups in spec and return the single mamba alignment size, or None if no group requires mamba alignment.

For Mamba groups with prefix caching the hit window must be rounded down to a multiple of the offloaded chunk size. Asserts that all such groups agree on the same value.

Source code in vllm/distributed/kv_transfer/kv_connector/v1/offloading/state.py
def resolve_mamba_align_size(
    spec: "OffloadingSpec", kv_cache_config: KVCacheConfig
) -> int | None:
    """Scan all KV cache groups in *spec* and return the single mamba alignment
    size, or None if no group requires mamba alignment.

    For Mamba groups with prefix caching the hit window must be rounded down
    to a multiple of the offloaded chunk size. Asserts that all such groups
    agree on the same value.
    """
    mamba_align_size: int | None = None
    for group in spec.config.groups:
        tokens_per_block = group.tokens_per_block
        if is_mamba_prefix_caching_group(
            kv_cache_config.kv_cache_groups[group.group_id]
        ):
            tokens_per_chunk = tokens_per_block * spec.blocks_per_chunk
            assert mamba_align_size is None or mamba_align_size == tokens_per_chunk
            mamba_align_size = tokens_per_chunk
    return mamba_align_size