[Bugfix] Send a downstream terminal chunk when a parked stage ends (#6889)
Signed-off-by: psv666 <2693925048@qq.com>
This commit is contained in:
1 parent
68294066e3
commit
9ef9fc442a
3 files changed
+519
-13
No files matched your search
@@ -688,6 +688,68 @@ class OmniSchedulerMixin:
|
||||
engine_core_outputs[0] = output = EngineCoreOutputs()
|
||||
output.scheduler_stats = stats
|
||||
|
||||
def add_request(self, request: Request) -> None:
|
||||
"""Route a terminal streaming update that lands on a parked session.
|
||||
|
||||
Upstream ``Scheduler.add_request`` turns "final update (``resumable``
|
||||
is False) arrives while the session sits in
|
||||
``WAITING_FOR_STREAMING_REQ``" into a local
|
||||
``finish_requests(FINISHED_ABORTED)``. That is fine for a stage whose
|
||||
output the orchestrator forwards, but an async-chunk *sender* also
|
||||
owes its downstream stage a terminal chunk: the receiver learns that a
|
||||
stream ended only from a connector payload carrying ``finished``, and
|
||||
an abort emits none. The downstream stage then parks in
|
||||
``WAITING_FOR_CHUNK`` until ``VLLM_OMNI_INPUT_WAIT_TIMEOUT_S`` (600s by
|
||||
default), long after the client's own deadline -- the realtime
|
||||
async-chunk hang in vllm-project/vllm-omni#6670.
|
||||
|
||||
Whether this races is timing, not configuration: the stage parks only
|
||||
when its own stop beats both the upstream terminal chunk and the
|
||||
orchestrator's terminal update, a window of tens of milliseconds at
|
||||
the end of the last segment. Emit the terminal chunk here so the
|
||||
downstream stage terminates on the payload path either way.
|
||||
"""
|
||||
existing = self.requests.get(request.request_id)
|
||||
if existing is not None and existing.status == RequestStatus.WAITING_FOR_STREAMING_REQ:
|
||||
adapter = None if getattr(request, "resumable", False) else self._adapter_owing_terminal(existing)
|
||||
if adapter is not None:
|
||||
self._finish_parked_streaming_session(existing, adapter)
|
||||
return
|
||||
super().add_request(request)
|
||||
|
||||
def _adapter_owing_terminal(self, request: Request) -> OmniChunkTransferAdapter | None:
|
||||
"""The adapter that owes *request*'s downstream stage a terminal chunk.
|
||||
|
||||
Sender ownership starts when the first chunk is queued, before the
|
||||
background thread initializes its chunk counter. A prewarmed receiver
|
||||
needs a terminal even if that first chunk has not been sent yet.
|
||||
Final stages never queue sends, so they never match.
|
||||
"""
|
||||
adapter = getattr(self, "chunk_transfer_adapter", None)
|
||||
if adapter is None:
|
||||
return None
|
||||
external_req_id = getattr(request, "external_req_id", None) or request.request_id
|
||||
return adapter if adapter.has_active_sender(external_req_id) else None
|
||||
|
||||
def _finish_parked_streaming_session(self, request: Request, adapter: OmniChunkTransferAdapter) -> None:
|
||||
"""End a parked async-chunk session with a downstream terminal chunk."""
|
||||
request.resumable = False
|
||||
# A segment stop already advanced the adapter's dedup watermark to
|
||||
# ``generation + 1`` for the segment that never arrived. Claim that
|
||||
# generation, exactly as ``_update_request_as_session`` would have,
|
||||
# or ``save_async`` drops this chunk as a late duplicate.
|
||||
request._omni_segment_generation = int(getattr(request, "_omni_segment_generation", 0) or 0) + 1
|
||||
# ``force_request_finished``: the terminal must be enqueued *before*
|
||||
# ``finish_requests``, which skips requests that already report
|
||||
# ``is_finished()``.
|
||||
adapter.save_async(
|
||||
None,
|
||||
request,
|
||||
is_segment_finished=False,
|
||||
force_request_finished=True,
|
||||
)
|
||||
self.finish_requests(request.request_id, RequestStatus.FINISHED_STOPPED)
|
||||
|
||||
def finish_requests(
|
||||
self,
|
||||
request_ids: str | Iterable[str] | None,
|
||||
|
||||
@@ -32,6 +32,16 @@ class _SenderGeneration:
|
||||
def __init__(self) -> None:
|
||||
self.in_flight = False
|
||||
self.cancelled = False
|
||||
# A request-terminal chunk is queued but not yet on the wire. The
|
||||
# downstream stage has no other way to learn the stream ended -- it
|
||||
# polls the connector and parks in WAITING_FOR_CHUNK until a chunk
|
||||
# carrying ``finished`` arrives -- so cleanup must not discard it.
|
||||
# See vllm-project/vllm-omni#6670.
|
||||
self.terminal_pending = False
|
||||
# ``cleanup_sender`` asked to reclaim this generation while the fence
|
||||
# was up. The terminal task owes that cleanup once it is done with the
|
||||
# connector -- on every exit path, including a failed or raising put.
|
||||
self.cleanup_deferred = False
|
||||
|
||||
|
||||
class _LoadEntry:
|
||||
@@ -321,6 +331,17 @@ class OmniChunkTransferAdapter(OmniTransferAdapterBase):
|
||||
self._processor_accepts_step_tokens[processor] = accepts
|
||||
return accepts
|
||||
|
||||
def has_active_sender(self, external_req_id: str) -> bool:
|
||||
"""Whether this request owns a sender, including work still queued.
|
||||
|
||||
``save_async`` registers the generation before the background thread
|
||||
can send its first chunk. Chunk counters are initialized later and
|
||||
cannot determine whether a prewarmed receiver needs a terminal.
|
||||
"""
|
||||
with self._sender_state_lock:
|
||||
sender_token = self._sender_tokens.get(external_req_id)
|
||||
return sender_token is not None and not sender_token.cancelled
|
||||
|
||||
def save_async(
|
||||
self,
|
||||
multimodal_output: dict[str, Any] | None = None,
|
||||
@@ -329,6 +350,7 @@ class OmniChunkTransferAdapter(OmniTransferAdapterBase):
|
||||
new_token_ids: Iterable[int] | None = None,
|
||||
confirmed_num_computed_tokens: int | None = None,
|
||||
segment_generation: int | None = None,
|
||||
force_request_finished: bool = False,
|
||||
):
|
||||
"""Build and enqueue one chunk for asynchronous sending.
|
||||
|
||||
@@ -352,8 +374,14 @@ class OmniChunkTransferAdapter(OmniTransferAdapterBase):
|
||||
before a streaming transition can mutate ``request``
|
||||
segment_generation: generation captured before a resumable stop
|
||||
can apply a queued update to the mutable request
|
||||
force_request_finished: send this chunk as the request terminal
|
||||
even though ``request`` is not in a finished status yet. The
|
||||
scheduler needs this when it ends a *parked* streaming session:
|
||||
the terminal has to be enqueued before ``finish_requests``,
|
||||
because ``finish_requests`` skips requests that already report
|
||||
``is_finished()``. See ``_finish_parked_streaming_session``.
|
||||
"""
|
||||
is_finished = request.is_finished() and not request.resumable
|
||||
is_finished = force_request_finished or (request.is_finished() and not request.resumable)
|
||||
if not hasattr(self, "_segment_generation"):
|
||||
self._segment_generation = defaultdict(int)
|
||||
external_req_id = request.external_req_id
|
||||
@@ -414,6 +442,10 @@ class OmniChunkTransferAdapter(OmniTransferAdapterBase):
|
||||
self._sender_tokens[external_req_id] = sender_token
|
||||
task["sender_token"] = sender_token
|
||||
self._pending_save_reqs.append(task)
|
||||
if is_finished:
|
||||
# Fence this generation against cleanup until the terminal
|
||||
# chunk has been handed to the connector (#6670).
|
||||
sender_token.terminal_pending = True
|
||||
if is_segment_finished:
|
||||
# The queued FIFO item now owns the old segment. Start the next
|
||||
# segment's deduplication watermark before the worker sends it.
|
||||
@@ -624,32 +656,71 @@ class OmniChunkTransferAdapter(OmniTransferAdapterBase):
|
||||
if sender_token is None:
|
||||
self._send_single_request_for_generation(task)
|
||||
return
|
||||
is_terminal_task = bool(task.get("is_finished"))
|
||||
with self._sender_state_lock:
|
||||
is_current = self._sender_tokens.get(external_req_id) is sender_token
|
||||
if is_current and not sender_token.cancelled:
|
||||
# ``cancelled`` does not veto a terminal chunk. It is the
|
||||
# downstream stage's only end-of-stream signal, so dropping it
|
||||
# leaves that stage parked in WAITING_FOR_CHUNK until the input
|
||||
# deadline -- the hang this fence exists to prevent (#6670).
|
||||
# Losing ownership of the external id still vetoes: the state the
|
||||
# chunk describes then belongs to a different generation.
|
||||
if is_current and (is_terminal_task or not sender_token.cancelled):
|
||||
sender_token.in_flight = True
|
||||
else:
|
||||
is_current = False
|
||||
if not is_current:
|
||||
logger.debug("Discarding stale queued chunk for aborted request %s", external_req_id)
|
||||
if is_terminal_task:
|
||||
self._release_terminal_fence(external_req_id, sender_token)
|
||||
return
|
||||
try:
|
||||
self._send_single_request_for_generation(task, sender_token)
|
||||
finally:
|
||||
if is_terminal_task:
|
||||
# Drop the fence and run the cleanup it deferred, whether the
|
||||
# put succeeded, reported failure, or raised. Until this runs,
|
||||
# the block below must not reclaim the generation either.
|
||||
self._release_terminal_fence(external_req_id, sender_token)
|
||||
with self._sender_state_lock:
|
||||
if self._sender_tokens.get(external_req_id) is sender_token:
|
||||
sender_token.in_flight = False
|
||||
if sender_token.cancelled:
|
||||
if sender_token.cancelled and not sender_token.terminal_pending:
|
||||
self._sender_tokens.pop(external_req_id, None)
|
||||
self._clear_sender_state_locked(external_req_id)
|
||||
|
||||
def _release_terminal_fence(self, external_req_id: str, sender_token: _SenderGeneration) -> None:
|
||||
"""Drop the terminal fence and run the cleanup it deferred.
|
||||
|
||||
Called on every exit path of a terminal task. ``cleanup_sender``
|
||||
cannot reclaim a generation whose terminal chunk is still queued, so
|
||||
it records the request on the token instead; without this the only
|
||||
path that ever replayed it was a *successful* put, and a connector
|
||||
failure leaked the whole per-request sender state (#6670).
|
||||
"""
|
||||
with self._sender_state_lock:
|
||||
sender_token.terminal_pending = False
|
||||
deferred = sender_token.cleanup_deferred
|
||||
sender_token.cleanup_deferred = False
|
||||
# A generation that no longer owns the external id was already
|
||||
# reclaimed by whoever replaced it; cleaning now would clear the
|
||||
# successor's state.
|
||||
still_current = self._sender_tokens.get(external_req_id) is sender_token
|
||||
if deferred and still_current:
|
||||
self.cleanup_sender(external_req_id)
|
||||
|
||||
def _sender_generation_is_active(
|
||||
self,
|
||||
external_req_id: str,
|
||||
sender_token: _SenderGeneration,
|
||||
*,
|
||||
is_terminal: bool = False,
|
||||
) -> bool:
|
||||
with self._sender_state_lock:
|
||||
return self._sender_tokens.get(external_req_id) is sender_token and not sender_token.cancelled
|
||||
if self._sender_tokens.get(external_req_id) is not sender_token:
|
||||
return False
|
||||
# Terminal chunks outlive cancellation -- see _send_single_request.
|
||||
return is_terminal or not sender_token.cancelled
|
||||
|
||||
def _send_single_request_for_generation(
|
||||
self,
|
||||
@@ -712,7 +783,9 @@ class OmniChunkTransferAdapter(OmniTransferAdapterBase):
|
||||
if payload_data.meta.is_segment_finished is None:
|
||||
payload_data.meta.is_segment_finished = torch.tensor(is_segment_finished, dtype=torch.bool)
|
||||
|
||||
if sender_token is not None and not self._sender_generation_is_active(external_req_id, sender_token):
|
||||
if sender_token is not None and not self._sender_generation_is_active(
|
||||
external_req_id, sender_token, is_terminal=is_finished
|
||||
):
|
||||
logger.debug("Skipping cancelled chunk for request %s before connector put", external_req_id)
|
||||
return
|
||||
|
||||
@@ -723,16 +796,25 @@ class OmniChunkTransferAdapter(OmniTransferAdapterBase):
|
||||
data=payload_data,
|
||||
)
|
||||
|
||||
if sender_token is not None and not self._sender_generation_is_active(external_req_id, sender_token):
|
||||
# cleanup_sender() may cancel this generation while put() is
|
||||
# blocked. Do not let that stale completion recreate state for a
|
||||
# request whose cleanup is already in progress.
|
||||
logger.debug("Ignoring completed put for cancelled request %s", external_req_id)
|
||||
return
|
||||
with self._sender_state_lock:
|
||||
if sender_token is not None:
|
||||
still_current = self._sender_tokens.get(external_req_id) is sender_token
|
||||
retiring_without_terminal = sender_token.cancelled and not (
|
||||
is_finished or sender_token.terminal_pending
|
||||
)
|
||||
if not still_current or retiring_without_terminal:
|
||||
logger.debug("Ignoring completed put for retired request %s", external_req_id)
|
||||
return
|
||||
if success:
|
||||
# A receiver may already have consumed this key while put was
|
||||
# in flight. Even if cleanup retired this generation, its
|
||||
# pending terminal must use the next key. Check ownership and
|
||||
# advance counters under the same lock so a stale completion
|
||||
# cannot modify a successor's state.
|
||||
self.put_req_chunk[external_req_id] += 1
|
||||
self.ramp_chunk_count[external_req_id] += 1
|
||||
|
||||
if success:
|
||||
self.put_req_chunk[external_req_id] += 1
|
||||
self.ramp_chunk_count[external_req_id] += 1
|
||||
logger.debug(f"[Stage-{stage_id}] Sent {connector_put_key}")
|
||||
# Sender uses struct attr access here; the receive path in
|
||||
# `_load_one_request` / `_update_request_payload` reads dict keys.
|
||||
@@ -862,6 +944,18 @@ class OmniChunkTransferAdapter(OmniTransferAdapterBase):
|
||||
if sender_token is None:
|
||||
self._clear_sender_state_locked(external_req_id)
|
||||
return
|
||||
if sender_token.terminal_pending:
|
||||
# A request-terminal chunk is still queued. Reclaiming the
|
||||
# state now would drop it, and the downstream stage would sit
|
||||
# in WAITING_FOR_CHUNK until VLLM_OMNI_INPUT_WAIT_TIMEOUT_S
|
||||
# with no way to know the stream ended (#6670). Retire the
|
||||
# generation anyway -- ``save_async`` rejects new chunks on
|
||||
# ``cancelled``, which is what keeps a request reusing this
|
||||
# external id from joining a generation that is going away --
|
||||
# and hand the reclaim to the terminal task.
|
||||
sender_token.cancelled = True
|
||||
sender_token.cleanup_deferred = True
|
||||
return
|
||||
sender_token.cancelled = True
|
||||
if sender_token.in_flight:
|
||||
# The sender's finally block is the first point where no
|
||||
|
||||
Reference in new issue
Block a user