[Model][Frontend] MiniMax-H3: Add latent-mask editing (#7465)
Signed-off-by: xiaoyu <1259084489@qq.com>
This commit is contained in:
1 parent
8e10c3ee07
commit
5a93ec1b4e
18 files changed
+2286
-122
No files matched your search
@@ -4,6 +4,7 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
from collections.abc import Mapping, Sequence
|
||||
from dataclasses import dataclass, fields
|
||||
from typing import Any
|
||||
@@ -123,7 +124,7 @@ class MiniMaxH3TextConditioning:
|
||||
|
||||
MINIMAX_H3_ENCODER_REQUEST_KEY = "minimax_h3_encoder_request"
|
||||
MINIMAX_H3_ENCODER_LAYOUT_KEY = "minimax_h3_encoder_layout"
|
||||
STAGE_SCHEMA_VERSION = 1
|
||||
STAGE_SCHEMA_VERSION = 2
|
||||
_ENCODER_WIRE_SCHEMA_ID = 1
|
||||
|
||||
_TASK_TO_CODE = {"t2va": 1, "fl2va": 2, "ref2va": 3}
|
||||
@@ -144,6 +145,104 @@ _AUDIO_CONDITION_WIDTH = 32
|
||||
_AUDIO_CONDITION_CHANNELS = 2
|
||||
|
||||
|
||||
def _validate_edit_mask_structure(
|
||||
mask: torch.Tensor,
|
||||
*,
|
||||
name: str,
|
||||
shape: tuple[int, ...],
|
||||
) -> None:
|
||||
"""Validate only the internal tensor contract after encoder-boundary parsing."""
|
||||
if mask.dtype != torch.float32 or tuple(mask.shape) != shape:
|
||||
raise ValueError(f"MiniMax H3 {name} edit mask must be FP32 with shape {shape}")
|
||||
_validate_contiguous_strided_layout(f"{name} edit mask", mask)
|
||||
|
||||
|
||||
def _validate_edit_tensors(
|
||||
clean_rows: torch.Tensor | None,
|
||||
raw_mask: torch.Tensor | None,
|
||||
*,
|
||||
name: str,
|
||||
width: int,
|
||||
mask_shape: tuple[int, ...],
|
||||
) -> int:
|
||||
values = (clean_rows, raw_mask)
|
||||
if all(value is None for value in values):
|
||||
return 0
|
||||
if clean_rows is None or raw_mask is None:
|
||||
raise ValueError(f"MiniMax H3 {name} edit requires clean rows and a raw mask tensor")
|
||||
if clean_rows.dtype != torch.float32 or clean_rows.ndim != 2 or clean_rows.shape[1] != width:
|
||||
raise ValueError(f"MiniMax H3 {name} edit rows must be FP32 with shape [rows, {width}]")
|
||||
row_count = int(clean_rows.shape[0])
|
||||
if row_count <= 0:
|
||||
raise ValueError(f"MiniMax H3 {name} edit rows must not be empty")
|
||||
_validate_edit_mask_structure(raw_mask, name=name, shape=mask_shape)
|
||||
if raw_mask.device != clean_rows.device:
|
||||
raise ValueError(f"MiniMax H3 {name} edit tensors must share one device")
|
||||
_validate_contiguous_strided_layout(f"{name} edit rows", clean_rows)
|
||||
return row_count
|
||||
|
||||
|
||||
def _packed_vector_rows(value: torch.Tensor, *, width: int) -> torch.Tensor:
|
||||
flat = value.reshape(-1)
|
||||
padding = (-int(flat.numel())) % width
|
||||
if padding:
|
||||
flat = torch.cat((flat, flat.new_zeros(padding)))
|
||||
return flat.reshape(-1, width)
|
||||
|
||||
|
||||
def _pack_condition_slot(
|
||||
condition: torch.Tensor | None,
|
||||
clean_rows: torch.Tensor | None,
|
||||
raw_mask: torch.Tensor | None,
|
||||
*,
|
||||
width: int,
|
||||
) -> torch.Tensor:
|
||||
if clean_rows is None:
|
||||
return condition if condition is not None else torch.empty((0,), dtype=torch.float32)
|
||||
if raw_mask is None:
|
||||
raise ValueError("MiniMax H3 edit rows require a raw mask tensor")
|
||||
values = [clean_rows, _packed_vector_rows(raw_mask, width=width)]
|
||||
if condition is not None:
|
||||
values.insert(0, condition)
|
||||
return torch.cat(values).contiguous()
|
||||
|
||||
|
||||
def _unpack_condition_slot(
|
||||
value: torch.Tensor,
|
||||
*,
|
||||
condition_rows: int,
|
||||
edit_rows: int,
|
||||
mask_shape: tuple[int, ...],
|
||||
width: int,
|
||||
name: str,
|
||||
) -> tuple[torch.Tensor | None, torch.Tensor | None, torch.Tensor | None]:
|
||||
mask_values = math.prod(mask_shape) if mask_shape else int(edit_rows > 0)
|
||||
mask_wire_rows = (mask_values + width - 1) // width
|
||||
expected_rows = condition_rows + edit_rows + mask_wire_rows
|
||||
if expected_rows == 0:
|
||||
if value.dtype != torch.float32 or tuple(value.shape) != (0,):
|
||||
raise ValueError(f"MiniMax H3 empty {name} condition slot must be FP32 with shape [0]")
|
||||
return None, None, None
|
||||
if value.dtype != torch.float32 or value.ndim != 2 or tuple(value.shape) != (expected_rows, width):
|
||||
raise ValueError(
|
||||
f"MiniMax H3 packed {name} condition must be FP32 with shape "
|
||||
f"[{expected_rows}, {width}], got {value.dtype} {tuple(value.shape)}"
|
||||
)
|
||||
cursor = 0
|
||||
condition = value[:condition_rows].contiguous() if condition_rows else None
|
||||
cursor += condition_rows
|
||||
if edit_rows == 0:
|
||||
return condition, None, None
|
||||
clean_rows = value[cursor : cursor + edit_rows].contiguous()
|
||||
cursor += edit_rows
|
||||
raw_mask = value[cursor : cursor + mask_wire_rows].reshape(-1)[:mask_values].clone()
|
||||
if mask_shape:
|
||||
raw_mask = raw_mask.reshape(mask_shape)
|
||||
else:
|
||||
raw_mask = raw_mask.reshape(())
|
||||
return condition, clean_rows, raw_mask
|
||||
|
||||
|
||||
def _wire_layout(payload: Mapping[str, Any]) -> torch.Tensor:
|
||||
private_metadata = payload.get("kv_metadata")
|
||||
if not isinstance(private_metadata, Mapping):
|
||||
@@ -259,6 +358,10 @@ class MiniMaxH3EncoderMediaInput:
|
||||
video_audios: tuple[tuple[torch.Tensor, int] | None, ...] = ()
|
||||
audios: tuple[tuple[torch.Tensor, int], ...] = ()
|
||||
keyframe_frame_indices: tuple[int, ...] = ()
|
||||
video_edit: torch.Tensor | None = None
|
||||
video_edit_mask: torch.Tensor | None = None
|
||||
audio_edit: tuple[torch.Tensor, int] | None = None
|
||||
audio_edit_mask: torch.Tensor | None = None
|
||||
|
||||
@classmethod
|
||||
def from_mm_tensors(
|
||||
@@ -331,6 +434,37 @@ class MiniMaxH3EncoderMediaInput:
|
||||
if waveform.ndim not in (1, 2):
|
||||
raise ValueError("MiniMax H3 audio must have shape [samples] or [channels, samples]")
|
||||
audios.append((waveform, sample_rate))
|
||||
|
||||
video_edit = None
|
||||
video_edit_mask = None
|
||||
if bool(metadata.get("has_video_edit", False)):
|
||||
if cursor + 2 > len(tensors):
|
||||
raise ValueError("MiniMax H3 encoder video edit input is truncated")
|
||||
video_edit, video_edit_mask = tensors[cursor : cursor + 2]
|
||||
cursor += 2
|
||||
if tuple(video_edit.shape) != (num_frames, height, width, 3):
|
||||
raise ValueError(
|
||||
"MiniMax H3 encoder video edit must have shape "
|
||||
f"[{num_frames}, {height}, {width}, 3], got {tuple(video_edit.shape)}"
|
||||
)
|
||||
_validate_edit_mask_structure(
|
||||
video_edit_mask,
|
||||
name="video",
|
||||
shape=(latent_t, height // 16, width // 16),
|
||||
)
|
||||
|
||||
audio_edit = None
|
||||
audio_edit_mask = None
|
||||
audio_edit_sample_rate = int(metadata.get("audio_edit_sample_rate", 0))
|
||||
if audio_edit_sample_rate:
|
||||
if cursor + 2 > len(tensors):
|
||||
raise ValueError("MiniMax H3 encoder audio edit input is truncated")
|
||||
waveform, audio_edit_mask = tensors[cursor : cursor + 2]
|
||||
cursor += 2
|
||||
if waveform.ndim not in (1, 2) or audio_edit_sample_rate <= 0:
|
||||
raise ValueError("MiniMax H3 encoder audio edit requires a waveform and positive sample rate")
|
||||
_validate_edit_mask_structure(audio_edit_mask, name="audio", shape=(2, audio_t))
|
||||
audio_edit = (waveform, audio_edit_sample_rate)
|
||||
if cursor != len(tensors):
|
||||
raise ValueError(f"MiniMax H3 encoder media input has {len(tensors) - cursor} trailing tensors")
|
||||
return cls(
|
||||
@@ -345,6 +479,10 @@ class MiniMaxH3EncoderMediaInput:
|
||||
video_audios=tuple(video_audios),
|
||||
audios=tuple(audios),
|
||||
keyframe_frame_indices=tuple(int(value) for value in metadata.get("keyframe_frame_indices", ())),
|
||||
video_edit=video_edit,
|
||||
video_edit_mask=video_edit_mask,
|
||||
audio_edit=audio_edit,
|
||||
audio_edit_mask=audio_edit_mask,
|
||||
)
|
||||
|
||||
def to_mm_tensors(self) -> list[torch.Tensor]:
|
||||
@@ -362,6 +500,28 @@ class MiniMaxH3EncoderMediaInput:
|
||||
audio_items = [item for item in self.video_audios if item is not None] + list(self.audios)
|
||||
if any(waveform.ndim not in (1, 2) or int(sample_rate) <= 0 for waveform, sample_rate in audio_items):
|
||||
raise ValueError("MiniMax H3 encoder audio must have a waveform and positive sample rate")
|
||||
video_edit_values = (self.video_edit, self.video_edit_mask)
|
||||
if any(value is not None for value in video_edit_values):
|
||||
if self.video_edit is None or self.video_edit_mask is None:
|
||||
raise ValueError("MiniMax H3 encoder video edit requires a source and mask")
|
||||
if tuple(self.video_edit.shape) != (self.num_frames, self.height, self.width, 3):
|
||||
raise ValueError(
|
||||
"MiniMax H3 encoder video edit must have shape "
|
||||
f"[{self.num_frames}, {self.height}, {self.width}, 3], got {tuple(self.video_edit.shape)}"
|
||||
)
|
||||
_validate_edit_mask_structure(
|
||||
self.video_edit_mask,
|
||||
name="video",
|
||||
shape=(self.latent_t, self.height // 16, self.width // 16),
|
||||
)
|
||||
audio_edit_values = (self.audio_edit, self.audio_edit_mask)
|
||||
if any(value is not None for value in audio_edit_values):
|
||||
if self.audio_edit is None or self.audio_edit_mask is None:
|
||||
raise ValueError("MiniMax H3 encoder audio edit requires a source and mask")
|
||||
waveform, sample_rate = self.audio_edit
|
||||
if waveform.ndim not in (1, 2) or int(sample_rate) <= 0:
|
||||
raise ValueError("MiniMax H3 encoder audio edit requires a waveform and positive sample rate")
|
||||
_validate_edit_mask_structure(self.audio_edit_mask, name="audio", shape=(2, self.audio_t))
|
||||
tensors = [*self.images, *self.videos]
|
||||
for item in self.video_audios:
|
||||
if item is None:
|
||||
@@ -369,10 +529,18 @@ class MiniMaxH3EncoderMediaInput:
|
||||
waveform, _sample_rate = item
|
||||
tensors.append(waveform)
|
||||
tensors.extend(waveform for waveform, _sample_rate in self.audios)
|
||||
if self.video_edit is not None:
|
||||
if self.video_edit_mask is None:
|
||||
raise ValueError("MiniMax H3 encoder video edit requires a mask")
|
||||
tensors.extend((self.video_edit, self.video_edit_mask))
|
||||
if self.audio_edit is not None:
|
||||
if self.audio_edit_mask is None:
|
||||
raise ValueError("MiniMax H3 encoder audio edit requires a mask")
|
||||
tensors.extend((self.audio_edit[0], self.audio_edit_mask))
|
||||
return tensors
|
||||
|
||||
def to_metadata(self) -> dict[str, Any]:
|
||||
return {
|
||||
metadata = {
|
||||
"task": self.task,
|
||||
"height": self.height,
|
||||
"width": self.width,
|
||||
@@ -387,6 +555,11 @@ class MiniMaxH3EncoderMediaInput:
|
||||
"audio_sample_rates": [sample_rate for _waveform, sample_rate in self.audios],
|
||||
"keyframe_frame_indices": list(self.keyframe_frame_indices),
|
||||
}
|
||||
if self.video_edit is not None:
|
||||
metadata["has_video_edit"] = True
|
||||
if self.audio_edit is not None:
|
||||
metadata["audio_edit_sample_rate"] = int(self.audio_edit[1])
|
||||
return metadata
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
@@ -403,6 +576,11 @@ class MiniMaxH3EncoderMediaConditioning:
|
||||
audio_condition_lengths: tuple[int, ...] = ()
|
||||
ref_blocks: tuple[dict[str, Any], ...] = ()
|
||||
keyframe_frame_indices: tuple[int, ...] = ()
|
||||
video_edit_clean_rows: torch.Tensor | None = None
|
||||
video_edit_mask: torch.Tensor | None = None
|
||||
audio_edit_clean_rows: torch.Tensor | None = None
|
||||
audio_edit_mask: torch.Tensor | None = None
|
||||
audio_edit_source_t: int = 0
|
||||
|
||||
def to_omni_components(
|
||||
self,
|
||||
@@ -418,7 +596,33 @@ class MiniMaxH3EncoderMediaConditioning:
|
||||
self.audio_condition,
|
||||
self.audio_condition_lengths,
|
||||
)
|
||||
empty = torch.empty((0,), dtype=torch.float32)
|
||||
video_mask_shape = (self.latent_t, self.height // 16, self.width // 16)
|
||||
audio_mask_shape = (2, self.audio_t)
|
||||
video_edit_rows = _validate_edit_tensors(
|
||||
self.video_edit_clean_rows,
|
||||
self.video_edit_mask,
|
||||
name="video",
|
||||
width=_VIDEO_CONDITION_WIDTH,
|
||||
mask_shape=video_mask_shape,
|
||||
)
|
||||
audio_edit_rows = _validate_edit_tensors(
|
||||
self.audio_edit_clean_rows,
|
||||
self.audio_edit_mask,
|
||||
name="audio",
|
||||
width=_AUDIO_CONDITION_WIDTH,
|
||||
mask_shape=audio_mask_shape,
|
||||
)
|
||||
expected_video_rows = self.latent_t * (self.height // 32) * (self.width // 32)
|
||||
if video_edit_rows not in (0, expected_video_rows):
|
||||
raise ValueError(f"MiniMax H3 video edit has {video_edit_rows} rows, expected {expected_video_rows}")
|
||||
expected_audio_rows = self.audio_t * _AUDIO_CONDITION_CHANNELS
|
||||
if audio_edit_rows not in (0, expected_audio_rows):
|
||||
raise ValueError(f"MiniMax H3 audio edit has {audio_edit_rows} rows, expected {expected_audio_rows}")
|
||||
if audio_edit_rows == 0:
|
||||
if self.audio_edit_source_t != 0:
|
||||
raise ValueError("MiniMax H3 audio_edit_source_t requires audio edit rows")
|
||||
elif not 0 < self.audio_edit_source_t <= self.audio_t:
|
||||
raise ValueError(f"MiniMax H3 audio_edit_source_t must be in [1, {self.audio_t}]")
|
||||
ref_rows = _ref_block_rows(self.ref_blocks)
|
||||
layout = [
|
||||
_ENCODER_WIRE_SCHEMA_ID,
|
||||
@@ -433,14 +637,27 @@ class MiniMaxH3EncoderMediaConditioning:
|
||||
len(self.audio_condition_lengths),
|
||||
len(ref_rows),
|
||||
len(self.keyframe_frame_indices),
|
||||
video_edit_rows,
|
||||
audio_edit_rows,
|
||||
self.audio_edit_source_t,
|
||||
]
|
||||
layout.extend(item for shape in self.visual_condition_shapes for item in shape)
|
||||
layout.extend(self.audio_condition_lengths)
|
||||
layout.extend(item for row in ref_rows for item in row)
|
||||
layout.extend(self.keyframe_frame_indices)
|
||||
return (
|
||||
self.visual_condition if self.visual_condition is not None else empty,
|
||||
self.audio_condition if self.audio_condition is not None else empty,
|
||||
_pack_condition_slot(
|
||||
self.visual_condition,
|
||||
self.video_edit_clean_rows,
|
||||
self.video_edit_mask,
|
||||
width=_VIDEO_CONDITION_WIDTH,
|
||||
),
|
||||
_pack_condition_slot(
|
||||
self.audio_condition,
|
||||
self.audio_edit_clean_rows,
|
||||
self.audio_edit_mask,
|
||||
width=_AUDIO_CONDITION_WIDTH,
|
||||
),
|
||||
torch.tensor(layout, dtype=torch.int64),
|
||||
)
|
||||
|
||||
@@ -461,6 +678,11 @@ class MiniMaxH3EncoderConditioning:
|
||||
audio_condition_lengths: tuple[int, ...] = ()
|
||||
ref_blocks: tuple[dict[str, Any], ...] = ()
|
||||
keyframe_frame_indices: tuple[int, ...] = ()
|
||||
video_edit_clean_rows: torch.Tensor | None = None
|
||||
video_edit_mask: torch.Tensor | None = None
|
||||
audio_edit_clean_rows: torch.Tensor | None = None
|
||||
audio_edit_mask: torch.Tensor | None = None
|
||||
audio_edit_source_t: int = 0
|
||||
|
||||
@classmethod
|
||||
def from_components(
|
||||
@@ -509,11 +731,16 @@ class MiniMaxH3EncoderConditioning:
|
||||
if layout.dtype not in _INTEGER_DTYPES or layout.ndim != 1:
|
||||
raise ValueError("MiniMax H3 encoder layout must be a one-dimensional integer tensor")
|
||||
values = [int(item) for item in layout.detach().cpu().tolist()]
|
||||
if len(values) < 12:
|
||||
if len(values) < 2:
|
||||
raise ValueError("MiniMax H3 encoder layout header is truncated")
|
||||
schema_id, version = values[:2]
|
||||
if schema_id != _ENCODER_WIRE_SCHEMA_ID or version != STAGE_SCHEMA_VERSION:
|
||||
raise ValueError(f"unsupported MiniMax H3 encoder wire schema {schema_id}:{version}")
|
||||
if len(values) < 15:
|
||||
raise ValueError("MiniMax H3 encoder layout header is truncated")
|
||||
(
|
||||
schema_id,
|
||||
version,
|
||||
_schema_id,
|
||||
_version,
|
||||
task_code,
|
||||
height,
|
||||
width,
|
||||
@@ -524,9 +751,10 @@ class MiniMaxH3EncoderConditioning:
|
||||
audio_count,
|
||||
ref_count,
|
||||
keyframe_count,
|
||||
) = values[:12]
|
||||
if schema_id != _ENCODER_WIRE_SCHEMA_ID or version != STAGE_SCHEMA_VERSION:
|
||||
raise ValueError(f"unsupported MiniMax H3 encoder wire schema {schema_id}:{version}")
|
||||
video_edit_rows,
|
||||
audio_edit_rows,
|
||||
audio_edit_source_t,
|
||||
) = values[:15]
|
||||
task = _CODE_TO_TASK.get(task_code)
|
||||
if task is None:
|
||||
raise ValueError(f"unsupported MiniMax H3 wire task code {task_code!r}")
|
||||
@@ -534,10 +762,12 @@ class MiniMaxH3EncoderConditioning:
|
||||
raise ValueError("MiniMax H3 encoder wire dimensions must be positive")
|
||||
if min(visual_count, audio_count, ref_count, keyframe_count) < 0:
|
||||
raise ValueError("MiniMax H3 encoder layout counts must be non-negative")
|
||||
expected_size = 12 + 3 * visual_count + audio_count + 5 * ref_count + keyframe_count
|
||||
if min(video_edit_rows, audio_edit_rows, audio_edit_source_t) < 0:
|
||||
raise ValueError("MiniMax H3 encoder edit layout values must be non-negative")
|
||||
expected_size = 15 + 3 * visual_count + audio_count + 5 * ref_count + keyframe_count
|
||||
if len(values) != expected_size:
|
||||
raise ValueError(f"MiniMax H3 encoder layout has {len(values)} integers, expected {expected_size}")
|
||||
cursor = 12
|
||||
cursor = 15
|
||||
visual_shapes = tuple(
|
||||
tuple(values[cursor + 3 * index : cursor + 3 * (index + 1)]) for index in range(visual_count)
|
||||
)
|
||||
@@ -548,14 +778,55 @@ class MiniMaxH3EncoderConditioning:
|
||||
cursor += 5 * ref_count
|
||||
ref_blocks_tensor = torch.tensor(ref_rows, dtype=torch.int64).reshape(-1, 5)
|
||||
keyframe_frame_indices = tuple(values[cursor : cursor + keyframe_count])
|
||||
visual_condition = visual if visual.numel() else None
|
||||
audio_condition = audio if audio.numel() else None
|
||||
video_mask_shape = (latent_t, height // 16, width // 16) if video_edit_rows else ()
|
||||
audio_mask_shape = (2, audio_t) if audio_edit_rows else ()
|
||||
visual_condition_rows = sum(t * (h // 2) * (w // 2) for t, h, w in visual_shapes)
|
||||
audio_condition_rows = _AUDIO_CONDITION_CHANNELS * sum(audio_lengths)
|
||||
visual_condition, video_edit_clean_rows, video_edit_mask = _unpack_condition_slot(
|
||||
visual,
|
||||
condition_rows=visual_condition_rows,
|
||||
edit_rows=video_edit_rows,
|
||||
mask_shape=video_mask_shape,
|
||||
width=_VIDEO_CONDITION_WIDTH,
|
||||
name="visual",
|
||||
)
|
||||
audio_condition, audio_edit_clean_rows, audio_edit_mask = _unpack_condition_slot(
|
||||
audio,
|
||||
condition_rows=audio_condition_rows,
|
||||
edit_rows=audio_edit_rows,
|
||||
mask_shape=audio_mask_shape,
|
||||
width=_AUDIO_CONDITION_WIDTH,
|
||||
name="audio",
|
||||
)
|
||||
_validate_condition_tensors(
|
||||
visual_condition,
|
||||
visual_shapes,
|
||||
audio_condition,
|
||||
audio_lengths,
|
||||
)
|
||||
_validate_edit_tensors(
|
||||
video_edit_clean_rows,
|
||||
video_edit_mask,
|
||||
name="video",
|
||||
width=_VIDEO_CONDITION_WIDTH,
|
||||
mask_shape=(latent_t, height // 16, width // 16),
|
||||
)
|
||||
_validate_edit_tensors(
|
||||
audio_edit_clean_rows,
|
||||
audio_edit_mask,
|
||||
name="audio",
|
||||
width=_AUDIO_CONDITION_WIDTH,
|
||||
mask_shape=(2, audio_t),
|
||||
)
|
||||
expected_video_edit_rows = latent_t * (height // 32) * (width // 32)
|
||||
if video_edit_rows not in (0, expected_video_edit_rows):
|
||||
raise ValueError(f"MiniMax H3 video edit has {video_edit_rows} rows, expected {expected_video_edit_rows}")
|
||||
if audio_edit_rows not in (0, audio_t * _AUDIO_CONDITION_CHANNELS):
|
||||
raise ValueError(f"MiniMax H3 audio edit has {audio_edit_rows} rows, expected {audio_t * 2}")
|
||||
if (audio_edit_rows == 0 and audio_edit_source_t != 0) or (
|
||||
audio_edit_rows and not 0 < audio_edit_source_t <= audio_t
|
||||
):
|
||||
raise ValueError("MiniMax H3 encoder audio edit source length is inconsistent")
|
||||
return cls(
|
||||
hidden_states=text.hidden_states,
|
||||
token_tags=text.token_tags,
|
||||
@@ -571,6 +842,11 @@ class MiniMaxH3EncoderConditioning:
|
||||
audio_condition_lengths=audio_lengths,
|
||||
ref_blocks=_decode_ref_blocks(ref_blocks_tensor),
|
||||
keyframe_frame_indices=keyframe_frame_indices,
|
||||
video_edit_clean_rows=video_edit_clean_rows,
|
||||
video_edit_mask=video_edit_mask,
|
||||
audio_edit_clean_rows=audio_edit_clean_rows,
|
||||
audio_edit_mask=audio_edit_mask,
|
||||
audio_edit_source_t=audio_edit_source_t,
|
||||
)
|
||||
|
||||
def to_omni_payload(self) -> dict[str, Any]:
|
||||
@@ -593,6 +869,11 @@ class MiniMaxH3EncoderConditioning:
|
||||
audio_condition_lengths=self.audio_condition_lengths,
|
||||
ref_blocks=self.ref_blocks,
|
||||
keyframe_frame_indices=self.keyframe_frame_indices,
|
||||
video_edit_clean_rows=self.video_edit_clean_rows,
|
||||
video_edit_mask=self.video_edit_mask,
|
||||
audio_edit_clean_rows=self.audio_edit_clean_rows,
|
||||
audio_edit_mask=self.audio_edit_mask,
|
||||
audio_edit_source_t=self.audio_edit_source_t,
|
||||
).to_omni_components()
|
||||
return {
|
||||
"hidden_states": {"output": text.hidden_states},
|
||||
|
||||
@@ -6,6 +6,7 @@ from __future__ import annotations
|
||||
|
||||
import math
|
||||
import os
|
||||
import subprocess
|
||||
import tempfile
|
||||
from collections.abc import Callable, Mapping, Sequence
|
||||
from contextlib import AbstractContextManager, nullcontext
|
||||
@@ -33,6 +34,7 @@ from vllm_omni.model_executor.models.minimax_h3.reference_video import (
|
||||
load_audio_file,
|
||||
load_video_audio,
|
||||
load_video_frames,
|
||||
prepare_edit_video,
|
||||
prepare_reference_videos,
|
||||
sample_reference_video_frames,
|
||||
validate_reference_audio_files,
|
||||
@@ -263,6 +265,101 @@ def _frames_to_tensor(frames: Sequence[Any]) -> torch.Tensor:
|
||||
)
|
||||
|
||||
|
||||
def _contains_bool(value: Any) -> bool:
|
||||
if isinstance(value, bool):
|
||||
return True
|
||||
if isinstance(value, torch.Tensor):
|
||||
return value.dtype == torch.bool
|
||||
return isinstance(value, (list, tuple)) and any(_contains_bool(item) for item in value)
|
||||
|
||||
|
||||
def _edit_mask(value: Any, *, name: str) -> torch.Tensor:
|
||||
if _contains_bool(value):
|
||||
raise OmniClientError(f"MiniMax H3 {name} must not contain booleans")
|
||||
try:
|
||||
mask = torch.as_tensor(value, dtype=torch.float32)
|
||||
except (TypeError, ValueError, RuntimeError, OverflowError) as exc:
|
||||
raise OmniClientError(f"MiniMax H3 {name} must be a finite numeric tensor") from exc
|
||||
if mask.numel() == 0 or not bool(torch.isfinite(mask).all().item()):
|
||||
raise OmniClientError(f"MiniMax H3 {name} must be non-empty and finite")
|
||||
if bool(((mask < 0.0) | (mask > 1.0)).any().item()):
|
||||
raise OmniClientError(f"MiniMax H3 {name} values must be in [0, 1]")
|
||||
return mask
|
||||
|
||||
|
||||
def _canonical_video_edit_mask(
|
||||
value: Any,
|
||||
*,
|
||||
latent_t: int,
|
||||
latent_h: int,
|
||||
latent_w: int,
|
||||
) -> torch.Tensor:
|
||||
"""Normalize every accepted request shape to one full latent grid."""
|
||||
mask = _edit_mask(value, name="video_noise_mask")
|
||||
token_shape = (latent_t, latent_h // 2, latent_w // 2)
|
||||
full_shape = (latent_t, latent_h, latent_w)
|
||||
row_count = math.prod(token_shape)
|
||||
candidate = mask
|
||||
while True:
|
||||
if candidate.ndim == 0:
|
||||
return candidate.expand(full_shape).contiguous()
|
||||
if candidate.ndim == 1 and candidate.numel() == row_count:
|
||||
candidate = candidate.reshape(token_shape)
|
||||
if tuple(candidate.shape) == token_shape:
|
||||
return candidate.repeat_interleave(2, dim=1).repeat_interleave(2, dim=2).contiguous()
|
||||
if tuple(candidate.shape) == full_shape:
|
||||
return candidate.contiguous()
|
||||
if not candidate.ndim or candidate.shape[0] != 1:
|
||||
break
|
||||
candidate = candidate.squeeze(0)
|
||||
raise OmniClientError(
|
||||
"MiniMax H3 video_noise_mask shape must be "
|
||||
f"scalar, ({row_count},), {token_shape}, or {full_shape}; got {tuple(mask.shape)}"
|
||||
)
|
||||
|
||||
|
||||
def _canonical_audio_edit_mask(value: Any, *, audio_t: int) -> torch.Tensor:
|
||||
"""Normalize every accepted request shape to a channel-major grid."""
|
||||
mask = _edit_mask(value, name="audio_noise_mask")
|
||||
row_count = 2 * audio_t
|
||||
candidate = mask
|
||||
while True:
|
||||
if candidate.ndim == 0:
|
||||
return candidate.expand(2, audio_t).contiguous()
|
||||
if candidate.ndim == 1 and candidate.numel() == audio_t:
|
||||
return candidate.expand(2, audio_t).contiguous()
|
||||
if candidate.ndim == 1 and candidate.numel() == row_count:
|
||||
return candidate.reshape(2, audio_t).contiguous()
|
||||
if tuple(candidate.shape) == (2, audio_t):
|
||||
return candidate.contiguous()
|
||||
if not candidate.ndim or candidate.shape[0] != 1:
|
||||
break
|
||||
candidate = candidate.squeeze(0)
|
||||
raise OmniClientError(
|
||||
"MiniMax H3 audio_noise_mask shape must be "
|
||||
f"scalar, ({audio_t},), (2, {audio_t}), or ({row_count},); got {tuple(mask.shape)}"
|
||||
)
|
||||
|
||||
|
||||
def _fit_audio_edit_rows(
|
||||
rows: torch.Tensor,
|
||||
source_audio_t: int,
|
||||
*,
|
||||
target_audio_t: int,
|
||||
) -> tuple[torch.Tensor, int]:
|
||||
"""Pad or trim channel-major clean source rows to the generated duration."""
|
||||
if rows.ndim != 2 or rows.shape[0] % 2 or rows.shape[1] != 32:
|
||||
raise ValueError("MiniMax H3 audio edit rows must have shape [2 * audio_t, 32]")
|
||||
encoded_audio_t = int(rows.shape[0]) // 2
|
||||
if not 0 < source_audio_t <= encoded_audio_t:
|
||||
raise ValueError(f"MiniMax H3 source audio length must be in [1, {encoded_audio_t}]")
|
||||
copied_t = min(source_audio_t, target_audio_t)
|
||||
source = rows.reshape(2, encoded_audio_t, 32)
|
||||
fitted = rows.new_zeros((2, target_audio_t, 32))
|
||||
fitted[:, :copied_t] = source[:, :copied_t]
|
||||
return fitted.reshape(2 * target_audio_t, 32).contiguous(), copied_t
|
||||
|
||||
|
||||
def _reuse_prepared_reference_videos(
|
||||
prepared: list[dict[str, Any]] | None,
|
||||
*,
|
||||
@@ -356,6 +453,37 @@ def prepare_encoder_inputs(
|
||||
diffusion_sampling_params,
|
||||
raw_images[0] if raw_images else None,
|
||||
)
|
||||
source_video = multi_modal_data.get("source_video")
|
||||
source_audio = multi_modal_data.get("source_audio")
|
||||
raw_video_edit_mask = multi_modal_data.get("video_noise_mask")
|
||||
raw_audio_edit_mask = multi_modal_data.get("audio_noise_mask")
|
||||
if (source_video is not None or source_audio is not None) and (
|
||||
raw_video_edit_mask is None and raw_audio_edit_mask is None
|
||||
):
|
||||
raise OmniClientError("MiniMax H3 edit sources require video_noise_mask or audio_noise_mask")
|
||||
video_edit_mask = (
|
||||
_canonical_video_edit_mask(
|
||||
raw_video_edit_mask,
|
||||
latent_t=latent_t,
|
||||
latent_h=height // 16,
|
||||
latent_w=width // 16,
|
||||
)
|
||||
if raw_video_edit_mask is not None
|
||||
else None
|
||||
)
|
||||
audio_edit_mask = (
|
||||
_canonical_audio_edit_mask(raw_audio_edit_mask, audio_t=audio_t) if raw_audio_edit_mask is not None else None
|
||||
)
|
||||
# An all-generate mask has no clean source dependency and remains a no-op.
|
||||
if video_edit_mask is not None and bool(torch.all(video_edit_mask == 1.0).item()):
|
||||
video_edit_mask = None
|
||||
if audio_edit_mask is not None and bool(torch.all(audio_edit_mask == 1.0).item()):
|
||||
audio_edit_mask = None
|
||||
if video_edit_mask is not None and source_video is None:
|
||||
raise OmniClientError("MiniMax H3 video_noise_mask requires source_video")
|
||||
if audio_edit_mask is not None and source_audio is None and source_video is None:
|
||||
raise OmniClientError("MiniMax H3 audio_noise_mask requires source_audio or source_video")
|
||||
|
||||
images = _prepare_encoder_images(
|
||||
task,
|
||||
raw_images,
|
||||
@@ -431,6 +559,48 @@ def prepare_encoder_inputs(
|
||||
audio_index += 1
|
||||
condition_labels.append(("audio", audio_index))
|
||||
|
||||
video_edit: torch.Tensor | None = None
|
||||
edit_video_metadata: dict[str, Any] | None = None
|
||||
if video_edit_mask is not None:
|
||||
with tempfile.TemporaryDirectory(prefix="minimax_h3_edit_") as workdir:
|
||||
try:
|
||||
edit_video_metadata = prepare_edit_video(
|
||||
source_video,
|
||||
target_width=width,
|
||||
target_height=height,
|
||||
target_frame_count=num_frames,
|
||||
workdir=workdir,
|
||||
)
|
||||
edit_video_frames = load_video_frames(edit_video_metadata["prepared_path"])
|
||||
except subprocess.CalledProcessError as exc:
|
||||
raise OmniClientError("MiniMax H3 could not decode source_video for editing") from exc
|
||||
video_edit = _frames_to_tensor(edit_video_frames)
|
||||
|
||||
audio_edit: tuple[torch.Tensor, int] | None = None
|
||||
if audio_edit_mask is not None:
|
||||
if source_audio is not None:
|
||||
try:
|
||||
waveform, sample_rate = _load_audio(source_audio)
|
||||
except subprocess.CalledProcessError as exc:
|
||||
raise OmniClientError("MiniMax H3 could not decode source_audio for editing") from exc
|
||||
else:
|
||||
if edit_video_metadata is not None and not edit_video_metadata["input_has_audio"]:
|
||||
raise OmniClientError("MiniMax H3 source_video has no audio track for audio editing")
|
||||
try:
|
||||
waveform, sample_rate = load_video_audio(
|
||||
str(source_video),
|
||||
duration_seconds=float(num_frames) / MINIMAX_H3_FPS,
|
||||
)
|
||||
except subprocess.CalledProcessError as exc:
|
||||
raise OmniClientError("MiniMax H3 could not decode source_video audio for editing") from exc
|
||||
if waveform.ndim not in (1, 2) or int(sample_rate) <= 0:
|
||||
raise OmniClientError("MiniMax H3 edit audio requires a waveform and positive sample rate")
|
||||
max_samples = max(1, int(round(float(num_frames) / MINIMAX_H3_FPS * int(sample_rate))))
|
||||
waveform = waveform[..., :max_samples].float().contiguous()
|
||||
if waveform.shape[-1] == 0:
|
||||
raise OmniClientError("MiniMax H3 edit audio must not be empty")
|
||||
audio_edit = (waveform, int(sample_rate))
|
||||
|
||||
if raw_audio is not None:
|
||||
validate_reference_audio_files(raw_audio)
|
||||
standalone_audios = _load_audios(raw_audio) if raw_audio is not None else []
|
||||
@@ -447,6 +617,10 @@ def prepare_encoder_inputs(
|
||||
video_audios=tuple(video_audio_inputs),
|
||||
audios=tuple((waveform.float().contiguous(), int(sample_rate)) for waveform, sample_rate in standalone_audios),
|
||||
keyframe_frame_indices=tuple(keyframe_indices),
|
||||
video_edit=video_edit,
|
||||
video_edit_mask=video_edit_mask,
|
||||
audio_edit=audio_edit,
|
||||
audio_edit_mask=audio_edit_mask,
|
||||
)
|
||||
audio_inputs = _effective_audio_inputs(
|
||||
media_input.video_audios,
|
||||
@@ -491,7 +665,8 @@ def encode_media(
|
||||
"""
|
||||
visual_rows: list[torch.Tensor] = []
|
||||
visual_shapes: list[tuple[int, int, int]] = []
|
||||
if media.images or media.videos:
|
||||
video_edit_clean_rows: torch.Tensor | None = None
|
||||
if media.images or media.videos or media.video_edit is not None:
|
||||
if video_vae is None:
|
||||
raise RuntimeError("MiniMax H3 video VAE is not resident on this rank")
|
||||
with component_scope(video_vae):
|
||||
@@ -504,19 +679,34 @@ def encode_media(
|
||||
rows, shape = video_vae.encode_video(frames)
|
||||
visual_rows.append(rows)
|
||||
visual_shapes.append(tuple(int(item) for item in shape))
|
||||
if media.video_edit is not None:
|
||||
frames = np.asarray(media.video_edit.detach().cpu().to(torch.uint8).contiguous().numpy())
|
||||
rows, shape = video_vae.encode_video(frames)
|
||||
shape = tuple(int(item) for item in shape)
|
||||
expected_shape = (media.latent_t, media.height // 16, media.width // 16)
|
||||
expected_rows = media.latent_t * (media.height // 32) * (media.width // 32)
|
||||
if shape != expected_shape or tuple(rows.shape) != (expected_rows, 96):
|
||||
raise ValueError(
|
||||
"MiniMax H3 video edit encoder shape mismatch: "
|
||||
f"got latent shape {shape} and rows {tuple(rows.shape)}, "
|
||||
f"expected {expected_shape} and ({expected_rows}, 96)"
|
||||
)
|
||||
video_edit_clean_rows = rows.to(dtype=torch.float32).contiguous()
|
||||
|
||||
if not emit_conditioning:
|
||||
return None
|
||||
|
||||
audio_rows: list[torch.Tensor] = []
|
||||
audio_lengths: list[int] = []
|
||||
audio_edit_clean_rows: torch.Tensor | None = None
|
||||
audio_edit_source_t = 0
|
||||
embedded_audio_count = sum(item is not None for item in media.video_audios)
|
||||
audio_inputs = _effective_audio_inputs(
|
||||
media.video_audios,
|
||||
media.audios,
|
||||
max_standalone_seconds=float(media.num_frames) / MINIMAX_H3_FPS,
|
||||
)
|
||||
if audio_inputs:
|
||||
if audio_inputs or media.audio_edit is not None:
|
||||
if audio_vae is None:
|
||||
raise RuntimeError("MiniMax H3 audio WVAE is not resident on the encoder leader")
|
||||
with component_scope(audio_vae):
|
||||
@@ -524,6 +714,15 @@ def encode_media(
|
||||
rows, length = audio_vae.encode_waveform(waveform, sample_rate)
|
||||
audio_rows.append(rows)
|
||||
audio_lengths.append(int(length))
|
||||
if media.audio_edit is not None:
|
||||
waveform, sample_rate = media.audio_edit
|
||||
rows, source_audio_t = audio_vae.encode_waveform(waveform, sample_rate)
|
||||
rows = rows.to(dtype=torch.float32).contiguous()
|
||||
audio_edit_clean_rows, audio_edit_source_t = _fit_audio_edit_rows(
|
||||
rows,
|
||||
int(source_audio_t),
|
||||
target_audio_t=media.audio_t,
|
||||
)
|
||||
if audio_lengths:
|
||||
if any(length < 80 or length > 600 for length in audio_lengths):
|
||||
raise ValueError("MiniMax H3 audio references must each be between 2 and 15 seconds")
|
||||
@@ -553,6 +752,22 @@ def encode_media(
|
||||
}
|
||||
for length in audio_lengths[embedded_audio_count:]
|
||||
)
|
||||
video_edit_mask = None
|
||||
if video_edit_clean_rows is not None:
|
||||
if media.video_edit_mask is None:
|
||||
raise ValueError("MiniMax H3 video edit rows require a mask")
|
||||
video_edit_mask = media.video_edit_mask.to(
|
||||
device=video_edit_clean_rows.device,
|
||||
dtype=torch.float32,
|
||||
).contiguous()
|
||||
audio_edit_mask = None
|
||||
if audio_edit_clean_rows is not None:
|
||||
if media.audio_edit_mask is None:
|
||||
raise ValueError("MiniMax H3 audio edit rows require a mask")
|
||||
audio_edit_mask = media.audio_edit_mask.to(
|
||||
device=audio_edit_clean_rows.device,
|
||||
dtype=torch.float32,
|
||||
).contiguous()
|
||||
return MiniMaxH3EncoderMediaConditioning(
|
||||
task=media.task,
|
||||
height=media.height,
|
||||
@@ -566,4 +781,9 @@ def encode_media(
|
||||
audio_condition_lengths=tuple(audio_lengths),
|
||||
ref_blocks=tuple(ref_blocks),
|
||||
keyframe_frame_indices=media.keyframe_frame_indices,
|
||||
video_edit_clean_rows=video_edit_clean_rows,
|
||||
video_edit_mask=video_edit_mask,
|
||||
audio_edit_clean_rows=audio_edit_clean_rows,
|
||||
audio_edit_mask=audio_edit_mask,
|
||||
audio_edit_source_t=audio_edit_source_t,
|
||||
)
|
||||
@@ -324,10 +324,21 @@ def _transcode_reference_video(
|
||||
workdir: str,
|
||||
start_time_seconds: float = 0.0,
|
||||
duration_seconds: float | None = None,
|
||||
pad_last_frame: bool = False,
|
||||
) -> str:
|
||||
output = str(Path(workdir) / "prepared.mp4")
|
||||
duration_args = ["-t", f"{float(duration_seconds):.6f}"] if duration_seconds is not None else []
|
||||
frame_count_args = ["-frames:v", str(int(target_frame_count))] if target_frame_count > 0 else []
|
||||
filters = [
|
||||
f"fps={MINIMAX_H3_FPS:g}",
|
||||
f"scale={target_width}:{target_height}:flags=lanczos",
|
||||
"setsar=1",
|
||||
]
|
||||
if pad_last_frame:
|
||||
# Keep cloning the final converted frame until -frames:v reaches the
|
||||
# requested target; longer inputs are deterministically trimmed by the
|
||||
# same -frames:v limit without enabling this padding filter.
|
||||
filters.append("tpad=stop_mode=clone:stop=-1")
|
||||
subprocess.run(
|
||||
[
|
||||
"ffmpeg",
|
||||
@@ -342,7 +353,7 @@ def _transcode_reference_video(
|
||||
"0:v:0",
|
||||
"-an",
|
||||
"-vf",
|
||||
(f"fps={MINIMAX_H3_FPS:g},scale={target_width}:{target_height}:flags=lanczos,setsar=1"),
|
||||
",".join(filters),
|
||||
*duration_args,
|
||||
*frame_count_args,
|
||||
"-metadata:s:v:0",
|
||||
@@ -366,6 +377,41 @@ def _transcode_reference_video(
|
||||
return output
|
||||
|
||||
|
||||
def prepare_edit_video(
|
||||
value: Any,
|
||||
target_width: int,
|
||||
target_height: int,
|
||||
target_frame_count: int,
|
||||
workdir: str,
|
||||
) -> dict[str, Any]:
|
||||
"""Resize an edit source and clone its last frame to the target length."""
|
||||
if not isinstance(value, str | os.PathLike):
|
||||
raise OmniClientError("MiniMax H3 edit video input must be a single file path")
|
||||
source = str(value)
|
||||
source_meta = _probe_video(source)
|
||||
if int(source_meta.get("frame_count", 0)) <= 0:
|
||||
raise OmniClientError(f"video has no frames: {source}")
|
||||
|
||||
Path(workdir).mkdir(parents=True, exist_ok=True)
|
||||
prepared_path = _transcode_reference_video(
|
||||
source,
|
||||
target_width=target_width,
|
||||
target_height=target_height,
|
||||
target_frame_count=target_frame_count,
|
||||
workdir=workdir,
|
||||
pad_last_frame=True,
|
||||
)
|
||||
|
||||
return {
|
||||
"original_path": source,
|
||||
"prepared_path": prepared_path,
|
||||
"input_has_audio": bool(source_meta.get("audio_codecs")),
|
||||
"width": target_width,
|
||||
"height": target_height,
|
||||
"frame_count": target_frame_count,
|
||||
}
|
||||
|
||||
|
||||
def prepare_reference_videos(
|
||||
values: Any,
|
||||
*,
|
||||
@@ -674,6 +720,7 @@ __all__ = [
|
||||
"load_audio_file",
|
||||
"load_video_audio",
|
||||
"load_video_frames",
|
||||
"prepare_edit_video",
|
||||
"prepare_reference_videos",
|
||||
"sample_reference_video_frames",
|
||||
"validate_reference_audio_files",
|
||||
|
||||
Reference in new issue
Block a user