[Feat]audio streaming input for async chunk (#3614)

Signed-off-by: CHEN <116010019@link.cuhk.edu.cn>
This commit is contained in:
ChenWenjing authored and GitHub committed 2026-05-31 17:39:25 +08:00
1 parent 6f4bd3e45c
commit 5dfdf584dd
20 files changed
+536 -184

No files matched your search

@@ -214,3 +214,34 @@ def compute_talker_prompt_ids_length(prompt_ids: list[int]) -> int:
pass
return sum_user_len + assistant_len
def construct_next_stage_streaming_input_prompt(payload_data: dict[str, Any], request: Any) -> None:
"""Update a downstream streaming request prompt from connector payload ids.
Async-chunk downstream stages are prewarmed before the real Talker prompt is
known. When a Thinker payload carries ``ids.prompt``, this helper:
* Preserves ``num_computed_tokens`` (the scheduler token watermark).
* Moves already-computed output tokens into ``prompt_token_ids``.
* Appends a new placeholder prompt slice sized from the upstream ids.
* Refreshes block hashes so the scheduler allocates KV slots for the
extended prompt without discarding prior computed state.
"""
ids = payload_data.get("ids", {})
prompt_token_ids = ids.get("prompt", None)
if not prompt_token_ids:
return
num_computed_tokens = request.num_computed_tokens
kept_output_tokens = request._all_token_ids[request.num_prompt_tokens : num_computed_tokens]
del request._all_token_ids[num_computed_tokens:]
request._output_token_ids.clear()
assert request.prompt_token_ids is not None
# Extend prompt with kept output tokens.
request.prompt_token_ids.extend(kept_output_tokens)
next_prompt_len = max(1, compute_talker_prompt_ids_length(prompt_token_ids))
new_prompt = [0] * next_prompt_len
request._all_token_ids.extend(new_prompt or ())
request.prompt_token_ids.extend(new_prompt or ())
request.update_block_hashes()
request.num_prompt_tokens = len(request.prompt_token_ids)
@@ -9,8 +9,9 @@ from typing import Any
import torch
from vllm.v1.request import Request, RequestStatus
from vllm_omni.data_entry_keys import OmniPayloadStruct, unflatten_payload
from vllm_omni.data_entry_keys import MetaStruct, OmniPayloadStruct, unflatten_payload
from ..adapter import construct_next_stage_streaming_input_prompt
from ..factory import OmniConnectorFactory
from ..utils.config import ConnectorSpec
from ..utils.logging import get_connector_logger
@@ -54,6 +55,7 @@ class OmniChunkTransferAdapter(OmniTransferAdapterBase):
self.put_req_chunk: dict[str, int] = defaultdict(int)
self.get_req_chunk: dict[str, int] = defaultdict(int)
self.finished_requests: set[str] = set()
self.segment_finished_requests: set[str] = set()
self.request_payload = {}
self.code_prompt_token_ids: dict[str, list[torch.Tensor]] = defaultdict(list)
self.request_ids_mapping: dict[str, str] = {}
@@ -63,6 +65,7 @@ class OmniChunkTransferAdapter(OmniTransferAdapterBase):
self.requests_with_ready_chunks = set()
self.requests_origin_status = {}
self.requests_num_chunks_sent: dict[str, int] = defaultdict(int)
self._pending_streaming_prefills: dict[str, dict] = {}
@staticmethod
def _is_truthy_scalar(value: Any) -> bool:
@@ -123,16 +126,26 @@ class OmniChunkTransferAdapter(OmniTransferAdapterBase):
self,
pooling_output: torch.Tensor | None = None,
request: Request | None = None,
is_segment_finished: bool = False,
):
"""Build and enqueue one chunk for asynchronous sending.
Payload extraction happens in ``_send_single_request`` on the
background save_loop thread.
For streaming input request ``is_segment_finished`` marks the end
of the current realtime input segment. It is intentionally separate
from ``request.is_finished()``: a resumable `/v1/realtime` session
can finish one audio segment and later continue with another segment
under the same external request id. For other requests, it is the same
as ``request.is_finished()``.
Args:
pooling_output: Partial pooling output dictionary
request: Request object
is_segment_finished: whether the segment of request is finished
"""
is_finished = request.is_finished() and not request.resumable
confirmed_num_computed_tokens = self._confirmed_num_computed_tokens(request)
@@ -150,7 +163,8 @@ class OmniChunkTransferAdapter(OmniTransferAdapterBase):
task = {
"pooling_output": pooling_output,
"request": request,
"is_finished": request.is_finished(),
"is_finished": is_finished,
"is_segment_finished": is_segment_finished,
}
self._pending_save_reqs.append(task)
with self._save_cond:
@@ -185,13 +199,24 @@ class OmniChunkTransferAdapter(OmniTransferAdapterBase):
meta = payload_data.get("meta", {})
payload_finished = self._is_truthy_scalar(meta.get("finished"))
payload_segment_finished = self._is_truthy_scalar(meta.get("is_segment_finished"))
if self.model_mode == "ar":
request.additional_information = payload_data
if chunk_id > 0 and request.resumable:
# For new streaming input segment, we should update prompt from payload
construct_next_stage_streaming_input_prompt(payload_data, request)
if payload_finished:
self.finished_requests.add(req_id)
request.resumable = False
if payload_segment_finished:
self.segment_finished_requests.add(req_id)
else:
if payload_finished:
self.finished_requests.add(req_id)
request.resumable = False
if payload_segment_finished:
self.segment_finished_requests.add(req_id)
new_ids = payload_data.get("codes", {}).get("audio")
has_tensor_codes = isinstance(new_ids, torch.Tensor)
@@ -249,6 +274,7 @@ class OmniChunkTransferAdapter(OmniTransferAdapterBase):
pooling_output = unflatten_payload(raw_po) if isinstance(raw_po, dict) else raw_po
request = task["request"]
is_finished = task["is_finished"]
is_segment_finished = task["is_segment_finished"]
stage_id = self.connector.stage_id
next_stage_id = stage_id + 1
external_req_id = request.external_req_id
@@ -262,14 +288,23 @@ class OmniChunkTransferAdapter(OmniTransferAdapterBase):
transfer_manager=self,
pooling_output=pooling_output,
request=request,
is_finished=is_finished,
# Existing processors use is_finished as a flush signal.
is_finished=is_segment_finished,
)
except Exception as e:
logger.error(f"Failed to use custom_process_input_func for payload extraction: {e}")
if payload_data is None:
return
if not (is_segment_finished or is_finished):
return
# Segment/request finish markers must still reach downstream even when
# the processor has no tensor payload.
payload_data = OmniPayloadStruct()
if payload_data.meta is None:
payload_data.meta = MetaStruct()
payload_data.meta.finished = torch.tensor(is_finished, dtype=torch.bool)
payload_data.meta.is_segment_finished = torch.tensor(is_segment_finished, dtype=torch.bool)
success, size, metadata = self.connector.put(
from_stage=str(stage_id),
@@ -298,13 +333,22 @@ class OmniChunkTransferAdapter(OmniTransferAdapterBase):
if is_payload_finished:
self.cleanup(request.request_id, external_req_id)
if is_finished:
if is_segment_finished:
self.code_prompt_token_ids.pop(external_req_id, None)
self.requests_num_chunks_sent.pop(external_req_id, None)
cached_ic = getattr(self, "_cached_ic", None)
if cached_ic is not None:
cached_ic.pop(external_req_id, None)
def is_done_receiving_chunks(self, request_id: str) -> bool:
"""Return True if the request should stop polling upstream chunks.
Covers both the whole-request finish marker (``finished_requests``) and
the per-segment finish marker (``segment_finished_requests``) used while
waiting for the next streaming input slice.
"""
return request_id in self.finished_requests or request_id in self.segment_finished_requests
########################################################################
# Cleanup
########################################################################
@@ -319,6 +363,7 @@ class OmniChunkTransferAdapter(OmniTransferAdapterBase):
Idempotent: calling with an already-cleaned or unknown id is safe.
"""
self.finished_requests.discard(request_id)
self.segment_finished_requests.discard(request_id)
self.get_req_chunk.pop(request_id, None)
self.requests_with_ready_chunks.discard(request_id)
self.request_ids_mapping.pop(request_id, None)
@@ -339,6 +384,7 @@ class OmniChunkTransferAdapter(OmniTransferAdapterBase):
self.request_payload.pop(external_req_id, None)
self.code_prompt_token_ids.pop(external_req_id, None)
self.requests_num_chunks_sent.pop(external_req_id, None)
self._pending_streaming_prefills.pop(external_req_id, None)
cached_ic = getattr(self, "_cached_ic", None)
if cached_ic is not None:
@@ -459,7 +505,8 @@ class OmniChunkTransferAdapter(OmniTransferAdapterBase):
# Requests that have loaded chunk from last round
# of schedule, but have not scheduled
continue
if request.request_id in self.finished_requests:
if self.is_done_receiving_chunks(request.request_id):
request.additional_information = None
continue
# Requests that waiting for chunk
self.load_async(request)