[Feature] add session based streaming input support to v1 (#28973)
Signed-off-by: Joshua Deng <joshuakdeng@gmail.com> Signed-off-by: Patrick von Platen <patrick.v.platen@gmail.com> Signed-off-by: Nick Hill <nickhill123@gmail.com> Signed-off-by: Roger Wang <hey@rogerw.io> Co-authored-by: Roger Wang <hey@rogerw.io> Co-authored-by: Patrick von Platen <patrick.v.platen@gmail.com> Co-authored-by: Nick Hill <nickhill123@gmail.com>
This commit is contained in:
co-authored by
Roger Wang
Patrick von Platen
Nick Hill
parent
d4dbb7af63
commit
91601ff478
+162
-11
@@ -7,11 +7,13 @@ import time
|
||||
import warnings
|
||||
from collections.abc import AsyncGenerator, Iterable, Mapping
|
||||
from copy import copy
|
||||
from typing import Any, cast
|
||||
from dataclasses import dataclass
|
||||
from typing import Any
|
||||
|
||||
import torch
|
||||
|
||||
import vllm.envs as envs
|
||||
from vllm import TokensPrompt
|
||||
from vllm.config import VllmConfig
|
||||
from vllm.engine.arg_utils import AsyncEngineArgs
|
||||
from vllm.engine.protocol import EngineClient
|
||||
@@ -20,11 +22,11 @@ from vllm.inputs import PromptType
|
||||
from vllm.logger import init_logger
|
||||
from vllm.lora.request import LoRARequest
|
||||
from vllm.multimodal import MULTIMODAL_REGISTRY, MultiModalRegistry
|
||||
from vllm.outputs import PoolingRequestOutput, RequestOutput
|
||||
from vllm.outputs import STREAM_FINISHED, PoolingRequestOutput, RequestOutput
|
||||
from vllm.plugins.io_processors import get_io_processor
|
||||
from vllm.pooling_params import PoolingParams
|
||||
from vllm.renderers import RendererLike
|
||||
from vllm.sampling_params import SamplingParams
|
||||
from vllm.sampling_params import RequestOutputKind, SamplingParams
|
||||
from vllm.tasks import SupportedTask
|
||||
from vllm.tokenizers import TokenizerLike
|
||||
from vllm.tracing import init_tracer
|
||||
@@ -38,6 +40,7 @@ from vllm.v1.engine.exceptions import EngineDeadError, EngineGenerateError
|
||||
from vllm.v1.engine.input_processor import InputProcessor
|
||||
from vllm.v1.engine.output_processor import OutputProcessor, RequestOutputCollector
|
||||
from vllm.v1.engine.parallel_sampling import ParentRequest
|
||||
from vllm.v1.engine.utils import get_prompt_text
|
||||
from vllm.v1.executor import Executor
|
||||
from vllm.v1.metrics.loggers import (
|
||||
StatLoggerFactory,
|
||||
@@ -50,6 +53,30 @@ from vllm.v1.metrics.stats import IterationStats
|
||||
logger = init_logger(__name__)
|
||||
|
||||
|
||||
@dataclass
|
||||
class StreamingInput:
|
||||
"""Input data for a streaming generation request.
|
||||
|
||||
This is used with generate() to support multi-turn streaming sessions
|
||||
where inputs are provided via an async generator.
|
||||
"""
|
||||
|
||||
prompt: PromptType
|
||||
sampling_params: SamplingParams | None = None
|
||||
|
||||
|
||||
class InputStreamError(Exception):
|
||||
"""Wrapper for errors from the input stream generator.
|
||||
|
||||
This is used to propagate errors from the user's input generator
|
||||
without wrapping them in EngineGenerateError.
|
||||
"""
|
||||
|
||||
def __init__(self, cause: Exception):
|
||||
self.cause = cause
|
||||
super().__init__(str(cause))
|
||||
|
||||
|
||||
class AsyncLLM(EngineClient):
|
||||
def __init__(
|
||||
self,
|
||||
@@ -261,7 +288,7 @@ class AsyncLLM(EngineClient):
|
||||
async def add_request(
|
||||
self,
|
||||
request_id: str,
|
||||
prompt: EngineCoreRequest | PromptType,
|
||||
prompt: EngineCoreRequest | PromptType | AsyncGenerator[StreamingInput, None],
|
||||
params: SamplingParams | PoolingParams,
|
||||
arrival_time: float | None = None,
|
||||
lora_request: LoRARequest | None = None,
|
||||
@@ -297,6 +324,20 @@ class AsyncLLM(EngineClient):
|
||||
tokenization_kwargs,
|
||||
)
|
||||
|
||||
if isinstance(prompt, AsyncGenerator):
|
||||
# Streaming input case.
|
||||
return await self._add_streaming_input_request(
|
||||
request_id,
|
||||
prompt,
|
||||
params,
|
||||
arrival_time,
|
||||
lora_request,
|
||||
tokenization_kwargs,
|
||||
trace_headers,
|
||||
priority,
|
||||
data_parallel_rank,
|
||||
)
|
||||
|
||||
# Convert Input --> Request.
|
||||
if isinstance(prompt, EngineCoreRequest):
|
||||
request = prompt
|
||||
@@ -322,10 +363,7 @@ class AsyncLLM(EngineClient):
|
||||
priority,
|
||||
data_parallel_rank,
|
||||
)
|
||||
if isinstance(prompt, str):
|
||||
prompt_text = prompt
|
||||
elif isinstance(prompt, Mapping):
|
||||
prompt_text = cast(str | None, prompt.get("prompt"))
|
||||
prompt_text = get_prompt_text(prompt)
|
||||
|
||||
self.input_processor.assign_request_id(request)
|
||||
|
||||
@@ -380,6 +418,104 @@ class AsyncLLM(EngineClient):
|
||||
if self.log_requests:
|
||||
logger.info("Added request %s.", request.request_id)
|
||||
|
||||
async def _add_streaming_input_request(
|
||||
self,
|
||||
request_id: str,
|
||||
input_stream: AsyncGenerator[StreamingInput, None],
|
||||
sampling_params: SamplingParams | PoolingParams,
|
||||
arrival_time: float | None = None,
|
||||
lora_request: LoRARequest | None = None,
|
||||
tokenization_kwargs: dict[str, Any] | None = None,
|
||||
trace_headers: Mapping[str, str] | None = None,
|
||||
priority: int = 0,
|
||||
data_parallel_rank: int | None = None,
|
||||
) -> RequestOutputCollector:
|
||||
self._validate_streaming_input_sampling_params(sampling_params)
|
||||
|
||||
inputs = dict(
|
||||
arrival_time=arrival_time,
|
||||
lora_request=lora_request,
|
||||
tokenization_kwargs=tokenization_kwargs,
|
||||
trace_headers=trace_headers,
|
||||
priority=priority,
|
||||
data_parallel_rank=data_parallel_rank,
|
||||
)
|
||||
|
||||
if not sampling_params.skip_clone:
|
||||
sampling_params = sampling_params.clone()
|
||||
sampling_params.skip_clone = True
|
||||
|
||||
# Create request for validation, also used as the finished signal
|
||||
# once the input stream is closed.
|
||||
final_req = self.input_processor.process_inputs(
|
||||
request_id=request_id,
|
||||
prompt=TokensPrompt(prompt_token_ids=[0]),
|
||||
params=sampling_params,
|
||||
**inputs, # type: ignore[arg-type]
|
||||
)
|
||||
self.input_processor.assign_request_id(final_req)
|
||||
internal_req_id = final_req.request_id
|
||||
|
||||
queue = RequestOutputCollector(sampling_params.output_kind, internal_req_id)
|
||||
|
||||
async def handle_inputs():
|
||||
cancelled = False
|
||||
try:
|
||||
async for input_chunk in input_stream:
|
||||
sp = input_chunk.sampling_params
|
||||
if sp:
|
||||
self._validate_streaming_input_sampling_params(sp)
|
||||
else:
|
||||
sp = sampling_params
|
||||
req = self.input_processor.process_inputs(
|
||||
request_id=internal_req_id,
|
||||
prompt=input_chunk.prompt,
|
||||
params=sp,
|
||||
resumable=True,
|
||||
**inputs, # type: ignore[arg-type]
|
||||
)
|
||||
req.external_req_id = request_id
|
||||
if req.prompt_embeds is not None:
|
||||
raise ValueError(
|
||||
"prompt_embeds not supported for streaming inputs"
|
||||
)
|
||||
prompt_text = get_prompt_text(input_chunk.prompt)
|
||||
await self._add_request(req, prompt_text, None, 0, queue)
|
||||
except (asyncio.CancelledError, GeneratorExit):
|
||||
cancelled = True
|
||||
except Exception as error:
|
||||
# Wrap in InputStreamError so generate() can propagate it
|
||||
# without wrapping in EngineGenerateError.
|
||||
queue.put(InputStreamError(error))
|
||||
finally:
|
||||
queue._input_stream_task = None
|
||||
if not cancelled:
|
||||
# Send empty final request to indicate that inputs have
|
||||
# finished. Don't send if cancelled (session was aborted).
|
||||
await self._add_request(final_req, None, None, 0, queue)
|
||||
|
||||
# Ensure output handler is running.
|
||||
self._run_output_handler()
|
||||
|
||||
queue._input_stream_task = asyncio.create_task(handle_inputs())
|
||||
return queue
|
||||
|
||||
@staticmethod
|
||||
def _validate_streaming_input_sampling_params(
|
||||
params: SamplingParams | PoolingParams,
|
||||
):
|
||||
if (
|
||||
not isinstance(params, SamplingParams)
|
||||
or params.n > 1
|
||||
or params.output_kind == RequestOutputKind.FINAL_ONLY
|
||||
or params.stop
|
||||
):
|
||||
raise ValueError(
|
||||
"Input streaming not currently supported "
|
||||
"for pooling models, n > 1, request_kind = FINAL_ONLY "
|
||||
"or with stop strings."
|
||||
)
|
||||
|
||||
# TODO: we should support multiple prompts in one call, as you
|
||||
# can do with LLM.generate. So that for multi-prompt completion
|
||||
# requests we don't need to send multiple messages to core proc,
|
||||
@@ -387,7 +523,7 @@ class AsyncLLM(EngineClient):
|
||||
# re-multiplexed in the API server anyhow.
|
||||
async def generate(
|
||||
self,
|
||||
prompt: EngineCoreRequest | PromptType,
|
||||
prompt: EngineCoreRequest | PromptType | AsyncGenerator[StreamingInput, None],
|
||||
sampling_params: SamplingParams,
|
||||
request_id: str,
|
||||
*,
|
||||
@@ -437,9 +573,10 @@ class AsyncLLM(EngineClient):
|
||||
|
||||
# Note: both OutputProcessor and EngineCore handle their
|
||||
# own request cleanup based on finished.
|
||||
finished = out.finished
|
||||
assert isinstance(out, RequestOutput)
|
||||
yield out
|
||||
finished = out.finished
|
||||
if out is not STREAM_FINISHED:
|
||||
yield out
|
||||
|
||||
# If the request is disconnected by the client, generate()
|
||||
# is cancelled or the generator is garbage collected. So,
|
||||
@@ -463,6 +600,14 @@ class AsyncLLM(EngineClient):
|
||||
logger.info("Request %s failed (bad request): %s.", request_id, e)
|
||||
raise
|
||||
|
||||
# Error from input stream generator - propagate directly.
|
||||
except InputStreamError as e:
|
||||
if q is not None:
|
||||
await self.abort(q.request_id, internal=True)
|
||||
if self.log_requests:
|
||||
logger.info("Request %s failed (input error): %s.", request_id, e)
|
||||
raise e.cause from e
|
||||
|
||||
# Unexpected error in the generate() task (possibly recoverable).
|
||||
except Exception as e:
|
||||
if q is not None:
|
||||
@@ -478,6 +623,9 @@ class AsyncLLM(EngineClient):
|
||||
)
|
||||
logger.info("Request %s failed due to %s.", request_id, s)
|
||||
raise EngineGenerateError() from e
|
||||
finally:
|
||||
if q is not None:
|
||||
q.close()
|
||||
|
||||
def _run_output_handler(self):
|
||||
"""Background loop: pulls from EngineCore and pushes to AsyncStreams."""
|
||||
@@ -703,6 +851,9 @@ class AsyncLLM(EngineClient):
|
||||
if self.log_requests:
|
||||
logger.info("Request %s failed.", request_id)
|
||||
raise EngineGenerateError() from e
|
||||
finally:
|
||||
if q is not None:
|
||||
q.close()
|
||||
|
||||
@property
|
||||
def tokenizer(self) -> TokenizerLike | None:
|
||||
|
||||
Reference in New Issue
Block a user