Skip to content

vllm.v1.worker.cpu.model_states.interface

Classes:

ModelSpecificAttnMetadata

Base class for model-specific attention metadata.

Source code in vllm/v1/worker/cpu/model_states/interface.py
class ModelSpecificAttnMetadata:
    """Base class for model-specific attention metadata."""

    def get_extra_common_attn_kwargs(
        self, kv_cache_group_id: int, num_reqs: int
    ) -> dict[str, Any]:
        return {}

    def get_extra_attn_kwargs(
        self, attn_metadata_builder: Any, num_reqs: int
    ) -> dict[str, Any]:
        return {}

ModelState

Bases: ABC

Methods:

Attributes:

Source code in vllm/v1/worker/cpu/model_states/interface.py
class ModelState(ABC):
    def __init__(
        self,
        vllm_config: VllmConfig,
        model: nn.Module,
        encoder_cache: EncoderCache | None,
        device: torch.device,
    ) -> None:
        self.vllm_config = vllm_config
        self.model_config = vllm_config.model_config
        self.scheduler_config = vllm_config.scheduler_config
        self.model = model
        self.device = device

        self.max_num_reqs = self.scheduler_config.max_num_seqs
        self.max_num_tokens = self.scheduler_config.max_num_batched_tokens
        self.inputs_embeds_size = self.model_config.get_inputs_embeds_size()
        self.dtype = self.model_config.dtype

        self.supports_mm_inputs = encoder_cache is not None
        if encoder_cache is not None:
            self.encoder_cache = encoder_cache
            observability_config = vllm_config.observability_config
            self.encoder_runner = EncoderRunner(
                model=self.model,
                max_num_tokens=self.max_num_tokens,
                hidden_size=self.inputs_embeds_size,
                encoder_cache=encoder_cache,
                dtype=self.dtype,
                device=self.device,
                enable_timing=bool(
                    observability_config
                    and observability_config.enable_mm_processor_stats
                ),
            )

    @property
    def max_model_len(self) -> int:
        # Auto-fit can reduce the limit after model state initialization.
        return self.model_config.max_model_len

    def get_supported_generation_tasks(self) -> tuple[GenerationTask, ...]:
        from vllm.model_executor.models.interfaces import (
            supports_realtime,
            supports_transcription,
        )
        from vllm.model_executor.models.interfaces_base import is_text_generation_model

        supported_tasks = list[GenerationTask]()
        if is_text_generation_model(self.model):
            supported_tasks.append("generate")
        if supports_transcription(self.model):
            if self.model.supports_transcription_only:
                return ("transcription",)
            supported_tasks.append("transcription")
        if supports_realtime(self.model):
            supported_tasks.append("realtime")
        return tuple(supported_tasks)

    def add_request(self, req_index: int, new_req_data: NewRequestData) -> None:
        return None

    def remove_request(self, req_id: str) -> None:
        return None

    def initialize_kv_cache(
        self, kv_cache_config: KVCacheConfig, block_tables: CPUBlockTables
    ) -> None:
        """Hook run after the KV cache tensors are allocated and bound."""
        return None

    def preprocess_state(
        self,
        input_batch: InputBatch,
        block_tables: tuple[torch.Tensor, ...],
        kv_cache_config: KVCacheConfig,
        num_computed_tokens: torch.Tensor,
    ) -> None:
        """Hook run on real batches before the forward pass (after block tables
        are gathered). Used by mamba "align" prefix caching to pre-copy state
        across block boundaries. No-op by default."""
        return None

    @abstractmethod
    def prepare_inputs_embeds(
        self,
        scheduled_encoder_inputs: dict[str, list[int]],
        input_batch: InputBatch,
        req_states: CPURequestState,
    ) -> torch.Tensor | None:
        """Prepare the ``inputs_embeds`` tensor for the current forward pass."""
        raise NotImplementedError

    def dummy_inputs_embeds(self, num_tokens: int) -> torch.Tensor | None:
        """Pre-allocated inputs_embeds buffer for dummy runs (contents unused)."""
        return None

    def execute_mm_encoder(
        self, scheduled_encoder_inputs: dict[str, list[int]]
    ) -> None:
        """Run the multi-modal encoder and cache its outputs by `mm_hash`.

        The encode half of `get_mm_embeddings`, without the gather, for callers
        that run no language model.
        """
        mm_hashes, mm_kwargs = self.encoder_runner.prepare_mm_inputs(
            scheduled_encoder_inputs
        )
        if mm_kwargs:
            with self.encoder_runner.timed_encoder_operation(
                scheduled_encoder_inputs.keys()
            ):
                encoder_outputs = self.encoder_runner.execute_mm_encoder(mm_kwargs)
            self.encoder_cache.encoder_outputs.update(zip(mm_hashes, encoder_outputs))

    def gather_mm_embeddings(
        self, input_batch: InputBatch
    ) -> tuple[list[torch.Tensor], torch.Tensor]:
        """Gather cached multimodal embeddings."""
        return self.encoder_runner.gather_mm_embeddings(
            input_batch.req_ids,
            input_batch.num_tokens,
            input_batch.num_scheduled_tokens,
            input_batch.query_start_loc_np,
            input_batch.prefill_len_np,
            input_batch.num_computed_tokens_np,
        )

    @abstractmethod
    def prepare_inputs(
        self, input_batch: InputBatch, req_states: CPURequestState
    ) -> dict[str, Any]:
        raise NotImplementedError

    @abstractmethod
    def prepare_dummy_inputs(self, num_reqs: int, num_tokens: int) -> dict[str, Any]:
        raise NotImplementedError

    @abstractmethod
    def prepare_attn(
        self,
        input_batch: InputBatch,
        block_tables: tuple[torch.Tensor, ...],
        slot_mappings: torch.Tensor,
        attn_groups: list[list[AttentionGroup]],
        kv_cache_config: KVCacheConfig,
    ) -> dict[str, Any]:
        raise NotImplementedError

    num_new_sampled_tokens_per_step: int = 1
    """New tokens sampled on each decode step
    (excluding accepted draft tokens, a.k.a num bonus tokens)."""

num_new_sampled_tokens_per_step = 1 class-attribute instance-attribute

New tokens sampled on each decode step (excluding accepted draft tokens, a.k.a num bonus tokens).

dummy_inputs_embeds(num_tokens)

Pre-allocated inputs_embeds buffer for dummy runs (contents unused).

Source code in vllm/v1/worker/cpu/model_states/interface.py
def dummy_inputs_embeds(self, num_tokens: int) -> torch.Tensor | None:
    """Pre-allocated inputs_embeds buffer for dummy runs (contents unused)."""
    return None

execute_mm_encoder(scheduled_encoder_inputs)

Run the multi-modal encoder and cache its outputs by mm_hash.

The encode half of get_mm_embeddings, without the gather, for callers that run no language model.

Source code in vllm/v1/worker/cpu/model_states/interface.py
def execute_mm_encoder(
    self, scheduled_encoder_inputs: dict[str, list[int]]
) -> None:
    """Run the multi-modal encoder and cache its outputs by `mm_hash`.

    The encode half of `get_mm_embeddings`, without the gather, for callers
    that run no language model.
    """
    mm_hashes, mm_kwargs = self.encoder_runner.prepare_mm_inputs(
        scheduled_encoder_inputs
    )
    if mm_kwargs:
        with self.encoder_runner.timed_encoder_operation(
            scheduled_encoder_inputs.keys()
        ):
            encoder_outputs = self.encoder_runner.execute_mm_encoder(mm_kwargs)
        self.encoder_cache.encoder_outputs.update(zip(mm_hashes, encoder_outputs))

gather_mm_embeddings(input_batch)

Gather cached multimodal embeddings.

Source code in vllm/v1/worker/cpu/model_states/interface.py
def gather_mm_embeddings(
    self, input_batch: InputBatch
) -> tuple[list[torch.Tensor], torch.Tensor]:
    """Gather cached multimodal embeddings."""
    return self.encoder_runner.gather_mm_embeddings(
        input_batch.req_ids,
        input_batch.num_tokens,
        input_batch.num_scheduled_tokens,
        input_batch.query_start_loc_np,
        input_batch.prefill_len_np,
        input_batch.num_computed_tokens_np,
    )

initialize_kv_cache(kv_cache_config, block_tables)

Hook run after the KV cache tensors are allocated and bound.

Source code in vllm/v1/worker/cpu/model_states/interface.py
def initialize_kv_cache(
    self, kv_cache_config: KVCacheConfig, block_tables: CPUBlockTables
) -> None:
    """Hook run after the KV cache tensors are allocated and bound."""
    return None

prepare_inputs_embeds(scheduled_encoder_inputs, input_batch, req_states) abstractmethod

Prepare the inputs_embeds tensor for the current forward pass.

Source code in vllm/v1/worker/cpu/model_states/interface.py
@abstractmethod
def prepare_inputs_embeds(
    self,
    scheduled_encoder_inputs: dict[str, list[int]],
    input_batch: InputBatch,
    req_states: CPURequestState,
) -> torch.Tensor | None:
    """Prepare the ``inputs_embeds`` tensor for the current forward pass."""
    raise NotImplementedError

preprocess_state(input_batch, block_tables, kv_cache_config, num_computed_tokens)

Hook run on real batches before the forward pass (after block tables are gathered). Used by mamba "align" prefix caching to pre-copy state across block boundaries. No-op by default.

Source code in vllm/v1/worker/cpu/model_states/interface.py
def preprocess_state(
    self,
    input_batch: InputBatch,
    block_tables: tuple[torch.Tensor, ...],
    kv_cache_config: KVCacheConfig,
    num_computed_tokens: torch.Tensor,
) -> None:
    """Hook run on real batches before the forward pass (after block tables
    are gathered). Used by mamba "align" prefix caching to pre-copy state
    across block boundaries. No-op by default."""
    return None