[Core][Diffusion] Add a typed pre-D2H video media contract (#6615)
Signed-off-by: NancyFyong <NancyFyong@users.noreply.github.com> Signed-off-by: Samit <285365963@qq.com> Co-authored-by: NancyFyong <NancyFyong@users.noreply.github.com> Co-authored-by: Samit <285365963@qq.com>
This commit is contained in:
31 files changed
+3054
-60
No files matched your search
@@ -0,0 +1,152 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
|
||||
"""Device-side reduction of decoded video tensors to uint8 frames."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import replace
|
||||
|
||||
import torch
|
||||
from vllm.logger import init_logger
|
||||
|
||||
from vllm_omni.diffusion.data import OmniDiffusionConfig
|
||||
from vllm_omni.diffusion.media import (
|
||||
DiffusionMediaOutput,
|
||||
FloatVideoConsumer,
|
||||
VideoMediaOutput,
|
||||
VideoTensorEncoding,
|
||||
VideoTensorLayout,
|
||||
VideoTensorSpec,
|
||||
VideoTransportConstraints,
|
||||
VideoValueRange,
|
||||
ensure_request_owned_tensor,
|
||||
)
|
||||
from vllm_omni.inputs.data import OmniDiffusionSamplingParams
|
||||
from vllm_omni.platforms import current_omni_platform
|
||||
|
||||
# VaeImageProcessor.denormalize: (x * 0.5 + 0.5).clamp(0, 1)
|
||||
_DENORM_SCALE = 0.5
|
||||
_DENORM_SHIFT = 0.5
|
||||
|
||||
logger = init_logger(__name__)
|
||||
|
||||
|
||||
def _request_float_consumers(
|
||||
sampling_params: OmniDiffusionSamplingParams | None,
|
||||
) -> frozenset[FloatVideoConsumer]:
|
||||
if sampling_params is not None and sampling_params.enable_frame_interpolation:
|
||||
return frozenset({FloatVideoConsumer.FRAME_INTERPOLATION})
|
||||
return frozenset()
|
||||
|
||||
|
||||
def _request_supports_uint8_frames(sampling_params: OmniDiffusionSamplingParams | None) -> bool:
|
||||
return sampling_params is None or (sampling_params.output_type or "np") == "np"
|
||||
|
||||
|
||||
def _prepare_float_media_for_transport(
|
||||
media: DiffusionMediaOutput,
|
||||
video: VideoMediaOutput,
|
||||
) -> DiffusionMediaOutput:
|
||||
prepared = replace(
|
||||
media,
|
||||
video=video.with_tensor(ensure_request_owned_tensor(video.tensor)),
|
||||
prepared_for_transport=True,
|
||||
)
|
||||
prepared.validate()
|
||||
return prepared
|
||||
|
||||
|
||||
def prepare_diffusion_media_for_transport(
|
||||
media: DiffusionMediaOutput,
|
||||
*,
|
||||
od_config: OmniDiffusionConfig,
|
||||
sampling_params: OmniDiffusionSamplingParams | None = None,
|
||||
) -> DiffusionMediaOutput:
|
||||
"""Validate and prepare one request-local media output before D2H."""
|
||||
media.validate()
|
||||
if media.prepared_for_transport:
|
||||
# Only the model runner prepares media, exactly once, on media the
|
||||
# pipeline emits unprepared. Accepting pre-prepared media here would let
|
||||
# a pipeline forge runner-owned state and skip request policy (e.g. a
|
||||
# forged uint8 payload silently bypasses frame interpolation).
|
||||
raise ValueError(
|
||||
"Media reached transport preparation already prepared; pipelines must "
|
||||
"emit unprepared media so request policy is applied by the runner"
|
||||
)
|
||||
if media.video.spec.encoding is not VideoTensorEncoding.NORMALIZED_FLOAT:
|
||||
raise ValueError("Pipelines must emit unprepared media as NORMALIZED_FLOAT video")
|
||||
|
||||
video = media.video
|
||||
constraints = VideoTransportConstraints(
|
||||
pending_float_consumers=(video.constraints.pending_float_consumers | _request_float_consumers(sampling_params))
|
||||
)
|
||||
constrained_video = replace(video, constraints=constraints)
|
||||
|
||||
enabled = od_config.video_output_transport.enable_device_postprocess
|
||||
output_supported = _request_supports_uint8_frames(sampling_params)
|
||||
if not enabled or constraints.pending_float_consumers or not output_supported:
|
||||
if not enabled:
|
||||
reason = "disabled"
|
||||
elif constraints.pending_float_consumers:
|
||||
reason = ",".join(sorted(consumer.value for consumer in constraints.pending_float_consumers))
|
||||
else:
|
||||
reason = "unsupported_presentation"
|
||||
logger.debug("Device video preparation kept normalized float: reason=%s", reason)
|
||||
return _prepare_float_media_for_transport(media, constrained_video)
|
||||
|
||||
try:
|
||||
frames = reduce_video_to_uint8_frames(
|
||||
constrained_video.tensor,
|
||||
do_denormalize=constrained_video.spec.value_range is VideoValueRange.NEGATIVE_ONE_TO_ONE,
|
||||
)
|
||||
except torch.OutOfMemoryError:
|
||||
logger.warning("Device video preparation ran out of memory; using normalized float transport")
|
||||
else:
|
||||
prepared_video = replace(
|
||||
constrained_video,
|
||||
tensor=frames,
|
||||
spec=VideoTensorSpec(
|
||||
layout=VideoTensorLayout.BTHWC,
|
||||
encoding=VideoTensorEncoding.UINT8_FRAMES,
|
||||
value_range=VideoValueRange.ZERO_TO_255,
|
||||
),
|
||||
constraints=VideoTransportConstraints(),
|
||||
)
|
||||
prepared = replace(media, video=prepared_video, prepared_for_transport=True)
|
||||
prepared.validate()
|
||||
logger.debug(
|
||||
"Device video preparation converted normalized float to uint8: shape=%s",
|
||||
tuple(frames.shape),
|
||||
)
|
||||
return prepared
|
||||
|
||||
# Leave the exception handler before allocator cleanup and fallback
|
||||
# construction. A late conversion OOM otherwise keeps the helper traceback
|
||||
# (and its live float32 intermediates) reachable while the fallback may need
|
||||
# to clone a non-compact request view.
|
||||
current_omni_platform.empty_cache()
|
||||
current_omni_platform.synchronize()
|
||||
return _prepare_float_media_for_transport(media, constrained_video)
|
||||
|
||||
|
||||
def reduce_video_to_uint8_frames(video: torch.Tensor, *, do_denormalize: bool = True) -> torch.Tensor:
|
||||
"""Reduce a decoded ``[B, C, F, H, W]`` video to uint8 ``[B, F, H, W, C]`` frames.
|
||||
|
||||
Runs denormalize/clamp/permute/round on the input's device so the following
|
||||
D2H copy carries uint8 instead of float. The result matches
|
||||
``VideoProcessor.postprocess_video(output_type="np")`` then the ``*255``
|
||||
rounding done in the API server. Pass ``do_denormalize=False`` for VAEs that
|
||||
already emit ``[0, 1]``.
|
||||
"""
|
||||
if video.dim() != 5:
|
||||
raise ValueError(f"expected a [B, C, F, H, W] video tensor, got shape {tuple(video.shape)}")
|
||||
|
||||
# Match the numpy path, which promotes to float before scaling.
|
||||
frames = video.to(torch.float32)
|
||||
if do_denormalize:
|
||||
frames = frames.mul(_DENORM_SCALE).add(_DENORM_SHIFT).clamp_(0.0, 1.0)
|
||||
else:
|
||||
frames = frames.clamp(0.0, 1.0)
|
||||
frames = frames.permute(0, 2, 3, 4, 1)
|
||||
frames = frames.mul_(255.0).round_().clamp_(0.0, 255.0).to(torch.uint8)
|
||||
return frames.contiguous()
|
||||
@@ -0,0 +1,92 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import replace
|
||||
|
||||
from vllm_omni.diffusion.media import (
|
||||
DiffusionMediaOutput,
|
||||
FloatVideoConsumer,
|
||||
VideoTensorEncoding,
|
||||
VideoTensorLayout,
|
||||
VideoTransportConstraints,
|
||||
VideoValueRange,
|
||||
)
|
||||
from vllm_omni.diffusion.postprocess.rife_interpolator import interpolate_video_tensor
|
||||
from vllm_omni.inputs.data import OmniDiffusionSamplingParams
|
||||
|
||||
|
||||
def finalize_diffusion_media(
|
||||
media: DiffusionMediaOutput,
|
||||
*,
|
||||
sampling_params: OmniDiffusionSamplingParams,
|
||||
) -> dict[str, object]:
|
||||
"""Finalize typed video media without invoking a model postprocessor."""
|
||||
media.validate()
|
||||
if not media.prepared_for_transport:
|
||||
raise ValueError("Diffusion media reached the engine before transport preparation")
|
||||
|
||||
video = media.video
|
||||
metadata: dict[str, object] = {}
|
||||
consumers = video.constraints.pending_float_consumers
|
||||
if FloatVideoConsumer.FRAME_INTERPOLATION in consumers:
|
||||
if video.spec.encoding is not VideoTensorEncoding.NORMALIZED_FLOAT:
|
||||
raise ValueError("Frame interpolation requires NORMALIZED_FLOAT video")
|
||||
# RIFE guesses [0,1] vs [-1,1] from the tensor's min/max, so relying on
|
||||
# the sample content misclassifies both an all-nonnegative [-1,1] clip
|
||||
# (read as [0,1]) and a [0,1] clip whose single bf16 value overshoots 1
|
||||
# (read as [-1,1]). Always map to unit range per the declared spec and
|
||||
# clamp to [0,1] before RIFE, then restore the declared range so
|
||||
# downstream denormalization is correct regardless of the sample.
|
||||
to_negative_one = video.spec.value_range is VideoValueRange.NEGATIVE_ONE_TO_ONE
|
||||
if to_negative_one:
|
||||
interp_input = video.tensor.mul(0.5).add(0.5).clamp_(0.0, 1.0)
|
||||
else:
|
||||
interp_input = video.tensor.clamp(0.0, 1.0)
|
||||
interpolated, multiplier = interpolate_video_tensor(
|
||||
interp_input,
|
||||
exp=sampling_params.frame_interpolation_exp,
|
||||
scale=sampling_params.frame_interpolation_scale,
|
||||
model_path=sampling_params.frame_interpolation_model_path,
|
||||
)
|
||||
if to_negative_one:
|
||||
interpolated = interpolated.mul(2.0).sub(1.0)
|
||||
consumers = consumers - {FloatVideoConsumer.FRAME_INTERPOLATION}
|
||||
video = replace(
|
||||
video,
|
||||
tensor=interpolated,
|
||||
constraints=VideoTransportConstraints(pending_float_consumers=frozenset(consumers)),
|
||||
)
|
||||
metadata["video"] = {"video_fps_multiplier": multiplier}
|
||||
|
||||
if consumers:
|
||||
names = sorted(consumer.value for consumer in consumers)
|
||||
raise ValueError(f"Video has pending float consumers at finalization: {names}")
|
||||
|
||||
output_type = getattr(sampling_params, "output_type", None) or "np"
|
||||
if output_type == "latent":
|
||||
raise ValueError("Latent output cannot be finalized from VideoMediaOutput")
|
||||
|
||||
if video.spec.encoding is VideoTensorEncoding.UINT8_FRAMES:
|
||||
if video.spec.layout is not VideoTensorLayout.BTHWC:
|
||||
raise ValueError("UINT8_FRAMES video must use BTHWC layout")
|
||||
if output_type != "np":
|
||||
raise ValueError(f"UINT8_FRAMES finalization does not support output_type={output_type!r}")
|
||||
frames = video.tensor.detach().cpu().numpy()
|
||||
else:
|
||||
if video.spec.layout is not VideoTensorLayout.BCTHW:
|
||||
raise ValueError("NORMALIZED_FLOAT video must use BCTHW layout")
|
||||
from diffusers.video_processor import VideoProcessor
|
||||
|
||||
do_denormalize = video.spec.value_range is VideoValueRange.NEGATIVE_ONE_TO_ONE
|
||||
frames = VideoProcessor(vae_scale_factor=8).postprocess_video(
|
||||
video.tensor,
|
||||
output_type=output_type,
|
||||
do_denormalize=[do_denormalize] * video.tensor.shape[2],
|
||||
)
|
||||
|
||||
return {
|
||||
"payload": {"video": frames},
|
||||
"metadata": metadata,
|
||||
}
|
||||
Reference in new issue
Block a user