[ModelRunnerV2] Support prompt embeds (#42963)
Signed-off-by: gcanlin <canlinguosdu@gmail.com> Signed-off-by: Canlin Guo <961750412@qq.com> Signed-off-by: Nick Hill <nickhill123@gmail.com> Co-authored-by: Nick Hill <nickhill123@gmail.com>
This commit is contained in:
@@ -926,6 +926,12 @@ class ModelConfig:
|
||||
f"got {type(self.max_model_len).__name__}: {self.max_model_len!r}. "
|
||||
"Example: max_model_len=2048"
|
||||
)
|
||||
if self.enable_prompt_embeds and self.is_encoder_decoder:
|
||||
# No encoder-decoder model accepts `inputs_embeds`; their decoders
|
||||
# embed `input_ids` internally.
|
||||
raise ValueError(
|
||||
"--enable-prompt-embeds is not supported with encoder-decoder models."
|
||||
)
|
||||
return self
|
||||
|
||||
def _resolve_mm_device_do_normalize(
|
||||
|
||||
@@ -2449,9 +2449,6 @@ class VllmConfig:
|
||||
):
|
||||
unsupported.append("custom logits processors")
|
||||
|
||||
if model_config is not None and model_config.enable_prompt_embeds:
|
||||
unsupported.append("prompt embeds")
|
||||
|
||||
if self.cache_config.kv_sharing_fast_prefill:
|
||||
# Will be added by https://github.com/vllm-project/vllm/pull/35045
|
||||
unsupported.append("KV sharing fast prefill")
|
||||
|
||||
@@ -776,7 +776,7 @@ class DiffusionGemmaModelState(ModelState):
|
||||
) -> None:
|
||||
super().__init__(vllm_config, model, encoder_cache, device)
|
||||
|
||||
# Per-step MM data produced by get_mm_embeddings and consumed by
|
||||
# Per-step MM data produced by prepare_inputs_embeds and consumed by
|
||||
# prepare_inputs. Stored as raw (mm_embeds, is_mm_embed) so that
|
||||
# prepare_inputs can call embed_input_ids directly into the
|
||||
# persistent _inputs_embeds_buf, avoiding the intermediate copy
|
||||
@@ -874,7 +874,7 @@ class DiffusionGemmaModelState(ModelState):
|
||||
if idx is not None:
|
||||
self.diffusion_states.remove_request(idx)
|
||||
|
||||
def get_mm_embeddings(
|
||||
def prepare_inputs_embeds(
|
||||
self,
|
||||
scheduled_encoder_inputs: dict[str, list[int]],
|
||||
input_batch: InputBatch,
|
||||
|
||||
@@ -273,6 +273,9 @@ class LongcatFlashNgramForCausalLM(nn.Module, SupportsLoRA, SupportsPP):
|
||||
|
||||
|
||||
class LongcatNgramModelState(DefaultModelState):
|
||||
# prepare_inputs builds its own inputs_embeds from n-gram token embeddings.
|
||||
supports_prompt_embeds = False
|
||||
|
||||
"""Per-request n-gram token history for LongCat-Flash-Lite.
|
||||
|
||||
Maintains a small CPU-side per-slot context (last ``n-1`` processed tokens)
|
||||
|
||||
@@ -68,6 +68,14 @@ class NewRequestData:
|
||||
prefill_token_ids=prefill_token_ids,
|
||||
)
|
||||
|
||||
@property
|
||||
def prompt_len(self) -> int:
|
||||
if self.prompt_token_ids is not None:
|
||||
return len(self.prompt_token_ids)
|
||||
if self.prompt_embeds is not None:
|
||||
return self.prompt_embeds.shape[0]
|
||||
return 0
|
||||
|
||||
def __repr__(self) -> str:
|
||||
prompt_embeds_shape = (
|
||||
self.prompt_embeds.shape if self.prompt_embeds is not None else None
|
||||
|
||||
+13
-5
@@ -142,11 +142,19 @@ class Request:
|
||||
prompt_token_ids, prompt_embeds
|
||||
)
|
||||
self._output_token_ids: list[int] = []
|
||||
self._all_token_ids: list[int] = (
|
||||
self.prompt_token_ids.copy()
|
||||
if self.prompt_token_ids is not None
|
||||
else [0] * self.num_prompt_tokens
|
||||
)
|
||||
if self.prompt_token_ids is None:
|
||||
self._all_token_ids: list[int] = [0] * self.num_prompt_tokens
|
||||
elif self.prompt_is_token_ids is None:
|
||||
self._all_token_ids = self.prompt_token_ids.copy()
|
||||
else:
|
||||
# Mixed-mode prompt: positions covered by prompt_embeds hold a sentinel
|
||||
# special token id that may lie outside the embedding. Zero them, matching
|
||||
# the no-token-ids case above, so embedding gathers over these placeholder
|
||||
# ids stay in bounds; the actual inputs come from prompt_embeds.
|
||||
self._all_token_ids = [
|
||||
t if is_tok else 0
|
||||
for t, is_tok in zip(self.prompt_token_ids, self.prompt_is_token_ids)
|
||||
]
|
||||
|
||||
# Used in async scheduling.
|
||||
self.num_output_placeholders = 0
|
||||
|
||||
@@ -18,7 +18,7 @@ from vllm.multimodal.utils import (
|
||||
group_and_batch_mm_kwargs,
|
||||
set_mm_embedding_modality,
|
||||
)
|
||||
from vllm.utils.torch_utils import PIN_MEMORY
|
||||
from vllm.utils.torch_utils import PIN_MEMORY, async_tensor_h2d
|
||||
from vllm.v1.worker.gpu.mm.encoder_cache import EncoderCache
|
||||
from vllm.v1.worker.utils import (
|
||||
EncoderTimingStats,
|
||||
@@ -91,6 +91,17 @@ class EncoderRunner:
|
||||
continue
|
||||
if mm_feature.identifier in self.encoder_cache.encoder_outputs:
|
||||
continue
|
||||
if mm_feature.modality == "prompt_embeds":
|
||||
# Passthrough modality: the tensor is already in the
|
||||
# model's embedding space, so no encoder runs. Cache it
|
||||
# directly so gather_mm_embeddings splices it via the
|
||||
# standard is_mm_embed path.
|
||||
embeds = mm_feature.data["embedding"].data
|
||||
assert isinstance(embeds, torch.Tensor)
|
||||
self.encoder_cache.encoder_outputs[mm_feature.identifier] = (
|
||||
async_tensor_h2d(embeds, device=self.device)
|
||||
)
|
||||
continue
|
||||
mm_hashes.append(mm_feature.identifier)
|
||||
mm_kwargs.append((mm_feature.modality, mm_feature.data))
|
||||
|
||||
|
||||
@@ -46,6 +46,7 @@ from vllm.model_executor.layers.mamba.ops.ssu_dispatch import (
|
||||
initialize_mamba_ssu_backend,
|
||||
)
|
||||
from vllm.model_executor.model_loader import get_model_loader
|
||||
from vllm.model_executor.models.interfaces import requires_raw_input_tokens
|
||||
from vllm.model_executor.offloader import (
|
||||
create_offloader,
|
||||
get_offloader,
|
||||
@@ -231,6 +232,9 @@ class GPUModelRunner(LoRAModelRunnerMixin):
|
||||
self.supports_mm_inputs = self.mm_registry.supports_multimodal_inputs(
|
||||
self.model_config
|
||||
)
|
||||
self.uses_inputs_embeds = (
|
||||
self.supports_mm_inputs or self.model_config.enable_prompt_embeds
|
||||
)
|
||||
self.encoder_cache = None
|
||||
if self.supports_mm_inputs and self.is_first_pp_rank:
|
||||
self.encoder_cache = EncoderCache()
|
||||
@@ -956,7 +960,6 @@ class GPUModelRunner(LoRAModelRunnerMixin):
|
||||
|
||||
def add_requests(self, scheduler_output: SchedulerOutput) -> None:
|
||||
for new_req_data in scheduler_output.scheduled_new_reqs:
|
||||
assert new_req_data.prompt_token_ids is not None
|
||||
assert new_req_data.prefill_token_ids is not None
|
||||
req_id = new_req_data.req_id
|
||||
|
||||
@@ -965,7 +968,7 @@ class GPUModelRunner(LoRAModelRunnerMixin):
|
||||
# with the updated prompt_token_ids and mm_features.
|
||||
self._remove_request(req_id)
|
||||
|
||||
prompt_len = len(new_req_data.prompt_token_ids)
|
||||
prompt_len = new_req_data.prompt_len
|
||||
sampling_params = new_req_data.sampling_params
|
||||
self.req_states.add_request(
|
||||
req_id=req_id,
|
||||
@@ -980,6 +983,7 @@ class GPUModelRunner(LoRAModelRunnerMixin):
|
||||
|
||||
if self.pooling_runner is not None:
|
||||
assert new_req_data.pooling_params is not None
|
||||
assert new_req_data.prompt_token_ids is not None
|
||||
self.pooling_runner.add_request(
|
||||
req_id,
|
||||
req_index,
|
||||
@@ -1550,17 +1554,17 @@ class GPUModelRunner(LoRAModelRunnerMixin):
|
||||
input_ids = input_batch.input_ids
|
||||
inputs_embeds = None
|
||||
ec_connector_output = None
|
||||
if self.supports_mm_inputs and self.is_first_pp_rank:
|
||||
# Run MM encoder (if needed) and get multimodal embeddings.
|
||||
# Only first PP rank prepares multimodal embeddings.
|
||||
if self.uses_inputs_embeds and self.is_first_pp_rank:
|
||||
# Prepare inputs_embeds (MM encoder outputs and/or prompt_embeds
|
||||
# overlay). Only first PP rank prepares them.
|
||||
if dummy_run:
|
||||
# Obtain mm embeddings of correct shape for compiled model.
|
||||
# Obtain embeddings of correct shape for compiled model.
|
||||
inputs_embeds = self.model_state.dummy_inputs_embeds(
|
||||
input_batch.num_tokens_after_padding
|
||||
)
|
||||
else:
|
||||
scheduled_encoder_inputs = scheduler_output.scheduled_encoder_inputs
|
||||
if self.lora_config is not None:
|
||||
if self.supports_mm_inputs and self.lora_config is not None:
|
||||
set_active_mm_loras(
|
||||
model=self.model,
|
||||
lora_manager=self.lora_manager,
|
||||
@@ -1574,16 +1578,16 @@ class GPUModelRunner(LoRAModelRunnerMixin):
|
||||
) as ec_connector_output:
|
||||
if self.is_encoder_only:
|
||||
# Encode and publish, nothing else: this instance runs no
|
||||
# language model, so the gather inside get_mm_embeddings
|
||||
# language model, so the gather inside prepare_inputs_embeds
|
||||
# would build an inputs_embeds nobody reads -- and it
|
||||
# raises "Encoder cache miss" for any scheduled item this
|
||||
# instance did not encode, taking the engine down with it.
|
||||
self.model_state.execute_mm_encoder(scheduled_encoder_inputs)
|
||||
else:
|
||||
inputs_embeds = self.model_state.get_mm_embeddings(
|
||||
inputs_embeds = self.model_state.prepare_inputs_embeds(
|
||||
scheduled_encoder_inputs, input_batch, self.req_states
|
||||
)
|
||||
if inputs_embeds is not None and not self.model.requires_raw_input_tokens:
|
||||
if inputs_embeds is not None and not requires_raw_input_tokens(self.model):
|
||||
input_ids = None
|
||||
|
||||
if self.is_encoder_only:
|
||||
|
||||
@@ -7,6 +7,7 @@ from vllm.config import VllmConfig, get_layers_from_vllm_config
|
||||
from vllm.model_executor.layers.attention import Attention, CrossAttention
|
||||
from vllm.v1.attention.backend import AttentionType
|
||||
from vllm.v1.worker.gpu.mm.encoder_cache import EncoderCache
|
||||
from vllm.v1.worker.gpu.model_states.interface import ModelState
|
||||
|
||||
|
||||
def init_model_state(
|
||||
@@ -14,11 +15,22 @@ def init_model_state(
|
||||
model: nn.Module,
|
||||
encoder_cache: EncoderCache | None,
|
||||
device: torch.device,
|
||||
):
|
||||
) -> ModelState:
|
||||
cls = resolve_model_state_cls(vllm_config, model)
|
||||
|
||||
# Reject enable_prompt_embeds for states that would silently ignore it.
|
||||
if vllm_config.model_config.enable_prompt_embeds and not cls.supports_prompt_embeds:
|
||||
raise ValueError(f"--enable-prompt-embeds not supported with {cls.__name__}.")
|
||||
|
||||
return cls(vllm_config, model, encoder_cache, device)
|
||||
|
||||
|
||||
def resolve_model_state_cls(
|
||||
vllm_config: VllmConfig, model: nn.Module
|
||||
) -> type[ModelState]:
|
||||
# Let the model provide its own ModelState if it defines one.
|
||||
if hasattr(model, "get_model_state_cls"):
|
||||
cls = model.get_model_state_cls()
|
||||
return cls(vllm_config, model, encoder_cache, device)
|
||||
return model.get_model_state_cls()
|
||||
|
||||
# Cross-attention encoder-decoder models (Whisper, CohereASR, NemotronParse, ...)
|
||||
if any(isinstance(m, CrossAttention) for m in model.modules()):
|
||||
@@ -26,7 +38,7 @@ def init_model_state(
|
||||
EncoderDecoderModelState,
|
||||
)
|
||||
|
||||
return EncoderDecoderModelState(vllm_config, model, encoder_cache, device)
|
||||
return EncoderDecoderModelState
|
||||
|
||||
# Encoder-only attention is non-causal and needs no KV cache.
|
||||
if any(
|
||||
@@ -35,13 +47,13 @@ def init_model_state(
|
||||
):
|
||||
from vllm.v1.worker.gpu.model_states.encoder_only import EncoderOnlyModelState
|
||||
|
||||
return EncoderOnlyModelState(vllm_config, model, encoder_cache, device)
|
||||
return EncoderOnlyModelState
|
||||
|
||||
if vllm_config.model_config.is_hybrid or vllm_config.model_config.is_attention_free:
|
||||
from vllm.v1.worker.gpu.model_states.mamba_hybrid import MambaHybridModelState
|
||||
|
||||
return MambaHybridModelState(vllm_config, model, encoder_cache, device)
|
||||
return MambaHybridModelState
|
||||
|
||||
from vllm.v1.worker.gpu.model_states.default import DefaultModelState
|
||||
|
||||
return DefaultModelState(vllm_config, model, encoder_cache, device)
|
||||
return DefaultModelState
|
||||
|
||||
@@ -18,11 +18,14 @@ from vllm.v1.worker.gpu.mm.encoder_cache import EncoderCache
|
||||
from vllm.v1.worker.gpu.mm.rope import get_rope_state
|
||||
from vllm.v1.worker.gpu.model_states.interface import ModelState
|
||||
from vllm.v1.worker.gpu.model_states.mm_pruning import maybe_create_mm_pruner
|
||||
from vllm.v1.worker.gpu.model_states.prompt_embeds import PromptEmbedsState
|
||||
from vllm.v1.worker.gpu.states import RequestState
|
||||
from vllm.v1.worker.utils import AttentionGroup
|
||||
|
||||
|
||||
class DefaultModelState(ModelState):
|
||||
supports_prompt_embeds = True
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
vllm_config: VllmConfig,
|
||||
@@ -32,6 +35,18 @@ class DefaultModelState(ModelState):
|
||||
):
|
||||
super().__init__(vllm_config, model, encoder_cache, device)
|
||||
|
||||
self.prompt_embeds_state: PromptEmbedsState | None = None
|
||||
if self.model_config.enable_prompt_embeds:
|
||||
self.prompt_embeds_state = PromptEmbedsState(
|
||||
self.max_num_reqs, self.inputs_embeds_size, self.dtype, self.device
|
||||
)
|
||||
if not self.supports_mm_inputs:
|
||||
# Persistent buffer analogous to encoder_runner.inputs_embeds.
|
||||
embeds_buffer_size = (self.max_num_tokens, self.inputs_embeds_size)
|
||||
self.inputs_embeds = torch.zeros(
|
||||
embeds_buffer_size, dtype=self.dtype, device=self.device
|
||||
)
|
||||
|
||||
self.rope_state = get_rope_state(
|
||||
self.model_config,
|
||||
model,
|
||||
@@ -49,42 +64,69 @@ class DefaultModelState(ModelState):
|
||||
def add_request(self, req_index: int, new_req_data: NewRequestData) -> None:
|
||||
if self.rope_state is not None:
|
||||
assert new_req_data.prefill_token_ids is not None
|
||||
# `prompt_embeds` is a passthrough modality with no grid info, but
|
||||
# M-RoPE assumes per-feature grids. Filter it out.
|
||||
mm_features = [
|
||||
f for f in new_req_data.mm_features if f.modality != "prompt_embeds"
|
||||
]
|
||||
self.rope_state.init_prefill_positions(
|
||||
req_index,
|
||||
self.model,
|
||||
new_req_data.prefill_token_ids,
|
||||
mm_features=new_req_data.mm_features,
|
||||
mm_features=mm_features,
|
||||
)
|
||||
if self.prompt_embeds_state is not None:
|
||||
self.prompt_embeds_state.add_request(req_index, new_req_data)
|
||||
|
||||
def remove_request(self, req_id: str) -> None:
|
||||
if self.prompt_embeds_state is not None:
|
||||
self.prompt_embeds_state.remove_request(req_id)
|
||||
|
||||
def apply_staged_writes(self) -> None:
|
||||
if self.rope_state is not None:
|
||||
self.rope_state.apply_staged_writes()
|
||||
if self.prompt_embeds_state is not None:
|
||||
self.prompt_embeds_state.apply_staged_writes()
|
||||
|
||||
def dummy_inputs_embeds(self, num_tokens: int) -> torch.Tensor:
|
||||
"""Pre-allocated inputs_embeds buffer for dummy runs (contents unused)."""
|
||||
return self.encoder_runner.inputs_embeds[:num_tokens]
|
||||
if self.supports_mm_inputs:
|
||||
return self.encoder_runner.inputs_embeds[:num_tokens]
|
||||
return self.inputs_embeds[:num_tokens]
|
||||
|
||||
def get_mm_embeddings(
|
||||
def prepare_inputs_embeds(
|
||||
self,
|
||||
scheduled_encoder_inputs: dict[str, list[int]],
|
||||
input_batch: InputBatch,
|
||||
req_states: RequestState,
|
||||
) -> torch.Tensor:
|
||||
self.execute_mm_encoder(scheduled_encoder_inputs)
|
||||
|
||||
mm_embeds, is_mm_embed = super().gather_mm_embeddings(input_batch)
|
||||
if self.mm_pruner is not None and mm_embeds:
|
||||
# EVS: recompute mrope positions for pruned media.
|
||||
mm_embeds = self.mm_pruner.recompute(mm_embeds, input_batch, req_states)
|
||||
# We must flush the staged rope updates for prepare_inputs() to pick up.
|
||||
self.apply_staged_writes()
|
||||
|
||||
# Use unpadded input_ids to match is_mm_embed size (num_tokens).
|
||||
# input_batch.input_ids may be padded for CUDA graphs.
|
||||
input_ids_unpadded = input_batch.input_ids[: input_batch.num_tokens]
|
||||
inputs_embeds = self.encoder_runner.get_inputs_embeds(
|
||||
input_ids_unpadded, mm_embeds, is_mm_embed
|
||||
)
|
||||
|
||||
if self.supports_mm_inputs:
|
||||
self.execute_mm_encoder(scheduled_encoder_inputs)
|
||||
|
||||
mm_embeds, is_mm_embed = super().gather_mm_embeddings(input_batch)
|
||||
if self.mm_pruner is not None and mm_embeds:
|
||||
# EVS: recompute mrope positions for pruned media.
|
||||
mm_embeds = self.mm_pruner.recompute(mm_embeds, input_batch, req_states)
|
||||
# We must flush the staged rope updates for prepare_inputs() to pick up.
|
||||
self.apply_staged_writes()
|
||||
|
||||
inputs_embeds = self.encoder_runner.get_inputs_embeds(
|
||||
input_ids_unpadded, mm_embeds, is_mm_embed
|
||||
)
|
||||
else:
|
||||
input_embeddings = self.model.embed_input_ids(input_ids_unpadded)
|
||||
self.inputs_embeds[: input_embeddings.shape[0]] = input_embeddings
|
||||
inputs_embeds = self.inputs_embeds
|
||||
|
||||
if self.prompt_embeds_state is not None:
|
||||
self.prompt_embeds_state.apply(
|
||||
input_batch, req_states.num_computed_tokens.gpu, inputs_embeds
|
||||
)
|
||||
|
||||
return inputs_embeds[: input_batch.num_tokens_after_padding]
|
||||
|
||||
def gather_mm_embeddings(
|
||||
@@ -115,9 +157,8 @@ class DefaultModelState(ModelState):
|
||||
|
||||
def prepare_dummy_inputs(self, num_reqs: int, num_tokens: int) -> dict[str, Any]:
|
||||
model_inputs = {}
|
||||
if self.supports_mm_inputs:
|
||||
inputs_embeds = self.encoder_runner.inputs_embeds[:num_tokens]
|
||||
model_inputs["inputs_embeds"] = inputs_embeds
|
||||
if self.supports_mm_inputs or self.prompt_embeds_state is not None:
|
||||
model_inputs["inputs_embeds"] = self.dummy_inputs_embeds(num_tokens)
|
||||
if self.rope_state is not None:
|
||||
model_inputs["positions"] = self.rope_state.get_positions(num_tokens)
|
||||
return model_inputs
|
||||
|
||||
@@ -54,6 +54,10 @@ class EncoderDecoderModelState(ModelState):
|
||||
device: torch.device,
|
||||
) -> None:
|
||||
assert encoder_cache is not None
|
||||
if vllm_config.model_config.enable_prompt_embeds:
|
||||
raise ValueError(
|
||||
"--enable-prompt-embeds is not supported with encoder-decoder models."
|
||||
)
|
||||
super().__init__(vllm_config, model, encoder_cache, device)
|
||||
|
||||
self.max_encoder_len = getattr(
|
||||
@@ -67,7 +71,7 @@ class EncoderDecoderModelState(ModelState):
|
||||
|
||||
self.encoder_outputs: list[torch.Tensor] = []
|
||||
|
||||
def get_mm_embeddings(
|
||||
def prepare_inputs_embeds(
|
||||
self,
|
||||
scheduled_encoder_inputs: dict[str, list[int]],
|
||||
input_batch: InputBatch,
|
||||
|
||||
@@ -34,6 +34,9 @@ class EncoderOnlyModelState(DefaultModelState):
|
||||
the normal KV-backed path untouched.
|
||||
"""
|
||||
|
||||
# The V2 pooling path is not wired for prompt embeds.
|
||||
supports_prompt_embeds = False
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
vllm_config: VllmConfig,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Any, cast
|
||||
from typing import Any, ClassVar, cast
|
||||
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
@@ -43,6 +43,9 @@ class ModelSpecificAttnMetadata:
|
||||
|
||||
|
||||
class ModelState(ABC):
|
||||
supports_prompt_embeds: ClassVar[bool] = False
|
||||
"""Whether this state implements user-provided prompt embeddings."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
vllm_config: VllmConfig,
|
||||
@@ -153,12 +156,13 @@ class ModelState(ABC):
|
||||
return None
|
||||
|
||||
@abstractmethod
|
||||
def get_mm_embeddings(
|
||||
def prepare_inputs_embeds(
|
||||
self,
|
||||
scheduled_encoder_inputs: dict[str, list[int]],
|
||||
input_batch: InputBatch,
|
||||
req_states: RequestState,
|
||||
) -> 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:
|
||||
|
||||
@@ -0,0 +1,152 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
import torch
|
||||
|
||||
from vllm.triton_utils import tl, triton
|
||||
from vllm.utils.torch_utils import async_tensor_h2d
|
||||
from vllm.v1.core.sched.output import NewRequestData
|
||||
from vllm.v1.worker.gpu.buffer_utils import UvaBackedTensor
|
||||
from vllm.v1.worker.gpu.input_batch import InputBatch
|
||||
|
||||
TOKEN_BLOCK = 16
|
||||
|
||||
|
||||
class PromptEmbedsState:
|
||||
"""GPU-side state for user-provided prompt embeddings.
|
||||
|
||||
Each request's embeddings are copied to the GPU once at `add_request`,
|
||||
off the per-step hot path. A per-request pointer table (UVA) then lets a
|
||||
single triton kernel overlay all scheduled prompt-embeds rows onto
|
||||
`inputs_embeds` each step, with no python loops or per-request H2D copies.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
max_num_reqs: int,
|
||||
hidden_size: int,
|
||||
dtype: torch.dtype,
|
||||
device: torch.device,
|
||||
):
|
||||
self.hidden_size = hidden_size
|
||||
self.dtype = dtype
|
||||
self.device = device
|
||||
|
||||
# req_id -> (embeds, is_token_ids mask or None). Holds the references
|
||||
# that keep the pointer table below valid.
|
||||
self.gpu_tensors: dict[str, tuple[torch.Tensor, torch.Tensor | None]] = {}
|
||||
|
||||
# Indexed by req_state index. Stale entries after removal are
|
||||
# harmless: add_request rewrites all fields for every index it claims.
|
||||
self.embeds_ptrs = UvaBackedTensor(max_num_reqs, dtype=torch.int64)
|
||||
self.mask_ptrs = UvaBackedTensor(max_num_reqs, dtype=torch.int64)
|
||||
self.embeds_lens = UvaBackedTensor(max_num_reqs, dtype=torch.int32)
|
||||
|
||||
def add_request(self, req_index: int, new_req_data: NewRequestData) -> None:
|
||||
prompt_embeds = new_req_data.prompt_embeds
|
||||
if prompt_embeds is None:
|
||||
self.gpu_tensors.pop(new_req_data.req_id, None)
|
||||
self.embeds_lens.np[req_index] = 0
|
||||
return
|
||||
|
||||
embeds = async_tensor_h2d(prompt_embeds, device=self.device, dtype=self.dtype)
|
||||
embeds = embeds.contiguous()
|
||||
is_token_ids = new_req_data.prompt_is_token_ids
|
||||
mask = None
|
||||
if is_token_ids is not None:
|
||||
mask = async_tensor_h2d(is_token_ids, device=self.device, dtype=torch.uint8)
|
||||
self.gpu_tensors[new_req_data.req_id] = (embeds, mask)
|
||||
self.embeds_ptrs.np[req_index] = embeds.data_ptr()
|
||||
self.mask_ptrs.np[req_index] = 0 if mask is None else mask.data_ptr()
|
||||
self.embeds_lens.np[req_index] = embeds.shape[0]
|
||||
|
||||
def remove_request(self, req_id: str) -> None:
|
||||
self.gpu_tensors.pop(req_id, None)
|
||||
|
||||
def apply_staged_writes(self) -> None:
|
||||
self.embeds_ptrs.copy_to_uva()
|
||||
self.mask_ptrs.copy_to_uva()
|
||||
self.embeds_lens.copy_to_uva()
|
||||
|
||||
def apply(
|
||||
self,
|
||||
input_batch: InputBatch,
|
||||
num_computed_tokens: torch.Tensor,
|
||||
inputs_embeds: torch.Tensor,
|
||||
) -> None:
|
||||
"""Overlay prompt embeddings onto `inputs_embeds` for the batch."""
|
||||
if not self.gpu_tensors:
|
||||
return
|
||||
# The kernel reinterprets raw source pointers as inputs_embeds' dtype.
|
||||
assert inputs_embeds.dtype == self.dtype
|
||||
num_reqs = input_batch.num_reqs
|
||||
max_query_len = int(input_batch.num_scheduled_tokens.max())
|
||||
grid = (num_reqs, triton.cdiv(max_query_len, TOKEN_BLOCK))
|
||||
_apply_prompt_embeds_kernel[grid](
|
||||
inputs_embeds,
|
||||
inputs_embeds.stride(0),
|
||||
self.embeds_ptrs.gpu,
|
||||
self.mask_ptrs.gpu,
|
||||
self.embeds_lens.gpu,
|
||||
input_batch.idx_mapping,
|
||||
input_batch.query_start_loc,
|
||||
num_computed_tokens,
|
||||
self.hidden_size,
|
||||
TOKEN_BLOCK=TOKEN_BLOCK,
|
||||
BLOCK_SIZE=1024,
|
||||
)
|
||||
|
||||
|
||||
@triton.jit
|
||||
def _apply_prompt_embeds_kernel(
|
||||
inputs_embeds_ptr,
|
||||
inputs_embeds_stride,
|
||||
embeds_ptrs_ptr, # int64 [max_num_reqs], device pointers (0-len = unused)
|
||||
mask_ptrs_ptr, # int64 [max_num_reqs], 0 = no is-token-ids mask
|
||||
embeds_lens_ptr, # int32 [max_num_reqs]
|
||||
idx_mapping_ptr,
|
||||
query_start_loc_ptr,
|
||||
num_computed_tokens_ptr,
|
||||
hidden_size: tl.constexpr,
|
||||
TOKEN_BLOCK: tl.constexpr,
|
||||
BLOCK_SIZE: tl.constexpr,
|
||||
):
|
||||
batch_idx = tl.program_id(0)
|
||||
req_state_idx = tl.load(idx_mapping_ptr + batch_idx)
|
||||
embeds_len = tl.load(embeds_lens_ptr + req_state_idx)
|
||||
num_computed = tl.load(num_computed_tokens_ptr + req_state_idx)
|
||||
if num_computed >= embeds_len:
|
||||
# No prompt embeds for this request, or they are fully consumed.
|
||||
return
|
||||
|
||||
query_start = tl.load(query_start_loc_ptr + batch_idx)
|
||||
query_end = tl.load(query_start_loc_ptr + batch_idx + 1)
|
||||
num_rows = tl.minimum(query_end - query_start, embeds_len - num_computed)
|
||||
|
||||
t_start = tl.program_id(1) * TOKEN_BLOCK
|
||||
if t_start >= num_rows:
|
||||
return
|
||||
|
||||
src_ptr = tl.load(embeds_ptrs_ptr + req_state_idx).to(
|
||||
tl.pointer_type(inputs_embeds_ptr.dtype.element_ty)
|
||||
)
|
||||
mask_int = tl.load(mask_ptrs_ptr + req_state_idx)
|
||||
mask_ptr = mask_int.to(tl.pointer_type(tl.int8))
|
||||
|
||||
for t_offset in tl.static_range(TOKEN_BLOCK):
|
||||
t = t_start + t_offset
|
||||
if t < num_rows:
|
||||
src_row = (num_computed + t).to(tl.int64)
|
||||
is_token_id = 0
|
||||
if mask_int != 0:
|
||||
is_token_id = tl.load(mask_ptr + src_row).to(tl.int32)
|
||||
if is_token_id == 0:
|
||||
dst_row = (query_start + t).to(tl.int64)
|
||||
for h in tl.range(0, hidden_size, BLOCK_SIZE):
|
||||
offs = h + tl.arange(0, BLOCK_SIZE)
|
||||
h_mask = offs < hidden_size
|
||||
row = tl.load(src_ptr + src_row * hidden_size + offs, mask=h_mask)
|
||||
tl.store(
|
||||
inputs_embeds_ptr + dst_row * inputs_embeds_stride + offs,
|
||||
row,
|
||||
mask=h_mask,
|
||||
)
|
||||
Reference in New Issue
Block a user