[Core] Defer generation payload host waits to output consumption (#8540)

Signed-off-by: amy-why-3459 <wuhaiyan17@huawei.com>
This commit is contained in:
amy-why-3459 authored and GitHub committed 2026-10-07 17:20:54 +08:00
1 parent d5a3380103
commit de253412b0
8 files changed
+350 -11

No files matched your search

+103 -10
View File
@@ -13,7 +13,7 @@ import gc
import logging
from collections.abc import Mapping
from dataclasses import replace
from typing import cast
from typing import Any, cast
import numpy as np
import torch
@@ -42,7 +42,13 @@ from vllm.v1.worker.utils import sanity_check_mm_encoder_outputs
from vllm_omni.outputs import OmniModelRunnerOutput
from vllm_omni.utils.mm_outputs import partition_payload_list
from vllm_omni.worker.gpu_ar_model_runner import ExecuteModelState, _ensure_tensor_values
from vllm_omni.worker.gpu_ar_model_runner import (
ExecuteModelState,
_AsyncCPUPayloadSnapshot,
_copy_tensor_payload_to_cpu,
_ensure_tensor_values,
_snapshot_tensor_payload_to_cpu_async,
)
from vllm_omni.worker.gpu_model_runner import OmniGPUModelRunner
from vllm_omni.worker.mixins import maybe_unpad_input_ids
from vllm_omni.worker.omni_connector_model_runner_mixin import (
@@ -53,6 +59,57 @@ from vllm_omni.worker.omni_connector_model_runner_mixin import (
logger = logging.getLogger(__name__)
class _AsyncGenerationOutput(AsyncModelRunnerOutput):
"""Generation payloads become readable when their copy-stream event completes."""
def __init__(self, output: OmniModelRunnerOutput, snapshot: _AsyncCPUPayloadSnapshot) -> None:
self._output = output
self._snapshot = snapshot
def get_output(self) -> OmniModelRunnerOutput:
self._snapshot.wait()
return self._output
def _copy_owned_generation_payload_to_cpu_async(
payload: Any, *, copy_stream: torch.cuda.Stream, pin_memory: bool
) -> _AsyncCPUPayloadSnapshot:
"""Copy immutable model-owned outputs, retaining their storage until D2H finishes.
The model must guarantee no subsequent forward or state update mutates any
returned CUDA payload storage. CPU values still need a synchronous snapshot.
record_stream also protects allocations if an unconsumed output is dropped.
"""
sources: list[torch.Tensor] = []
def retain(value: Any) -> Any:
if isinstance(value, torch.Tensor):
if value.device.type == "cuda":
tensor = value.detach()
sources.append(tensor)
return tensor
return value.detach().clone()
if isinstance(value, dict):
return {key: retain(item) for key, item in value.items()}
if isinstance(value, (list, tuple)):
items = [retain(item) for item in value]
return tuple(items) if isinstance(value, tuple) else items
return value
retained = retain(payload)
if not sources:
return _AsyncCPUPayloadSnapshot(retained, None, sources)
producer = torch.cuda.current_stream()
ready = torch.cuda.Event()
with torch.cuda.stream(copy_stream):
copy_stream.wait_stream(producer)
host_payload = _copy_tensor_payload_to_cpu(retained, pin_memory)
for source in sources:
source.record_stream(copy_stream)
ready.record(copy_stream)
return _AsyncCPUPayloadSnapshot(host_payload, ready, sources)
class _HostCopyBatch:
"""Device-to-host copies of one step's outputs behind a single host sync.
@@ -442,8 +499,15 @@ class GPUGenerationModelRunner(OmniGPUModelRunner, OmniConnectorModelRunnerMixin
# Build per-request multimodal_outputs list (dedicated channel).
# pooler_output is no longer used for multimodal data.
per_req_payloads: list[dict[str, object]] = []
pin_memory = is_pin_memory_available()
accumulate_full_payload = self._should_accumulate_full_payload_output()
use_async_output = (
self.use_async_scheduling and self.device.type == "cuda" and pin_memory and not accumulate_full_payload
)
# One host sync for the whole step instead of one per request tensor.
to_host = _HostCopyBatch(is_pin_memory_available())
to_host = _HostCopyBatch(pin_memory)
# Validate and split the payload before launching any asynchronous copies.
copy_tensor = torch.Tensor.detach if use_async_output else to_host.copy
if isinstance(multimodal_outputs_raw, torch.Tensor):
# One row per request. The old asserts (`shape[0] == 1` AND
# `shape[0] == num_reqs`) jointly forced num_reqs == 1, silently
@@ -456,7 +520,7 @@ class GPUGenerationModelRunner(OmniGPUModelRunner, OmniConnectorModelRunnerMixin
"to return multiple tensors per request, use a dict)."
)
for i in range(num_reqs):
per_req_payloads.append({"model_outputs": to_host.copy(multimodal_outputs_raw[i])})
per_req_payloads.append({"model_outputs": copy_tensor(multimodal_outputs_raw[i])})
elif isinstance(multimodal_outputs_raw, list):
# One entry per request. The old `len == 1` assert did not check
# num_reqs, so a batched step built a length-1 payload list that
@@ -469,7 +533,7 @@ class GPUGenerationModelRunner(OmniGPUModelRunner, OmniConnectorModelRunnerMixin
"to return multiple lists per request, use a dict)."
)
for out in multimodal_outputs_raw:
per_req_payloads.append({"model_outputs": to_host.copy(out) if out is not None else None})
per_req_payloads.append({"model_outputs": copy_tensor(out) if out is not None else None})
elif isinstance(multimodal_outputs_raw, Mapping):
num_reqs = self.input_batch.num_reqs
for i in range(num_reqs):
@@ -481,16 +545,42 @@ class GPUGenerationModelRunner(OmniGPUModelRunner, OmniConnectorModelRunnerMixin
f"Multimodal output list for key '{key}' has length {len(out)} "
f"but expected {num_reqs} (one entry per request)."
)
mm_payload[key] = to_host.copy(out[i])
mm_payload[key] = copy_tensor(out[i])
elif isinstance(out, torch.Tensor):
mm_payload[key] = to_host.copy(out)
mm_payload[key] = copy_tensor(out)
else:
logger.warning(f"Unsupported multimodal output type for key '{key}': {type(out)}")
per_req_payloads.append(_ensure_tensor_values(mm_payload))
else:
raise RuntimeError("Unsupported diffusion output type")
# Sync mode hands these payloads out as finished host tensors.
to_host.wait()
snapshot = None
if use_async_output:
# Models may opt in only when all CUDA payload storage is immutable
# across subsequent forwards. Outer model graph capture can still reuse
# these allocations, so require eager outer execution (internal graphs
# that return owned copies are safe). Otherwise keep the snapshot.
copy_payload = (
_copy_owned_generation_payload_to_cpu_async
if (
getattr(getattr(self, "model", None), "owns_generation_output_storage", False) is True
and getattr(getattr(self, "model_config", None), "enforce_eager", False) is True
)
else _snapshot_tensor_payload_to_cpu_async
)
# Dense waveforms can use one host allocation and one D2H transfer.
# Split only after copying; the CPU rows retain the batch storage.
if isinstance(multimodal_outputs_raw, torch.Tensor) and multimodal_outputs_raw.is_contiguous():
snapshot = copy_payload(
multimodal_outputs_raw, copy_stream=self.async_output_copy_stream, pin_memory=pin_memory
)
per_req_payloads = [{"model_outputs": row} for row in snapshot.payload.unbind(0)]
else:
snapshot = copy_payload(
per_req_payloads, copy_stream=self.async_output_copy_stream, pin_memory=pin_memory
)
per_req_payloads = snapshot.payload
else:
to_host.wait()
inter_stage_outputs: list[dict[str, object] | None] | None
multimodal_outputs: list[dict[str, object] | None] | None
@@ -505,7 +595,7 @@ class GPUGenerationModelRunner(OmniGPUModelRunner, OmniConnectorModelRunnerMixin
# [Omni] Copy req_id mappings to avoid async scheduling mutation.
req_ids_output_copy = self.input_batch.req_ids.copy()
req_id_to_index_output_copy = self.input_batch.req_id_to_index.copy()
if inter_stage_outputs and self._should_accumulate_full_payload_output():
if inter_stage_outputs and accumulate_full_payload:
for i, rid in enumerate(req_ids_output_copy):
req_state = self.requests.get(rid)
if req_state is not None and inter_stage_outputs[i]:
@@ -528,6 +618,9 @@ class GPUGenerationModelRunner(OmniGPUModelRunner, OmniConnectorModelRunnerMixin
)
output.omni_connector_output = self.get_omni_connector_output()
if snapshot is not None:
return _AsyncGenerationOutput(output, snapshot)
if not self.use_async_scheduling:
return output