[Feature][MiniMax-H3] Support diffusion continuous batching (#5810)
Signed-off-by: princepride <wangzhipeng628@gmail.com> Signed-off-by: lishunyang12 <lishunyang12@163.com> Co-authored-by: Claude Opus 5 <noreply@anthropic.com> Co-authored-by: lishunyang12 <lishunyang12@163.com> Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
15 files changed
+1834
-140
No files matched your search
@@ -15,7 +15,10 @@ from typing import Any
|
||||
import torch
|
||||
|
||||
from vllm_omni.diffusion.attention.backends.abstract import VideoTokenLayout
|
||||
from vllm_omni.diffusion.forward_context import set_forward_context_denoise_step_idx
|
||||
from vllm_omni.diffusion.forward_context import (
|
||||
set_forward_context_denoise_step_idx,
|
||||
set_forward_context_denoise_timestep,
|
||||
)
|
||||
|
||||
from .scheduling_minimax_h3_euler_ancestral import (
|
||||
minimax_h3_euler_eta0_step,
|
||||
@@ -31,6 +34,17 @@ MINIMAX_H3_VIDEO_ROW_WIDTH = 96
|
||||
MINIMAX_H3_AUDIO_ROW_WIDTH = 32
|
||||
|
||||
|
||||
def minimax_h3_publish_denoise_progress(step: int | None, sigma_video: float | None) -> None:
|
||||
"""Publish denoise progress for step-gated attention features.
|
||||
|
||||
Both execution modes must publish the same pair: the step index drives the
|
||||
dense warmup of RAINFUSION_ATTN, and the normalized descending timestep
|
||||
drives the TRTLLM_ATTN skip gate, which stays dense while it is unset.
|
||||
"""
|
||||
set_forward_context_denoise_step_idx(step)
|
||||
set_forward_context_denoise_timestep(sigma_video)
|
||||
|
||||
|
||||
class MiniMaxH3DenoiseBranch:
|
||||
"""Static per-branch state: packed layout + fixed forward kwargs.
|
||||
|
||||
@@ -64,26 +78,39 @@ class MiniMaxH3DenoiseBranch:
|
||||
if int(token_tags.view(-1).shape[0]) != seq_len:
|
||||
raise ValueError(f"token_tags length {int(token_tags.view(-1).shape[0])} != seq_len {seq_len}")
|
||||
cu = packed["cu_seqlens"].to(torch.int32)
|
||||
self.device = device
|
||||
# ``used_len`` is the real (non-padding) document length; step-mode
|
||||
# batching concatenates layouts and needs both bounds as Python ints so
|
||||
# it can rebuild ``cu_seqlens`` without a device sync per step.
|
||||
self.used_len = int(cu[1])
|
||||
self.text_len = text_len
|
||||
self.img_pos_dev = self.img_pos.to(device)
|
||||
self.audio_pos_dev = self.audio_pos.to(device)
|
||||
self.update_mask_dev = self.update_mask.to(device)
|
||||
self.audio_update_mask_dev = self.audio_update_mask.to(device)
|
||||
self.x_base = torch.zeros(1, seq_len, MINIMAX_H3_VIDEO_ROW_WIDTH, dtype=torch.float32, device=device)
|
||||
self.audio_x_base = torch.zeros(1, seq_len, MINIMAX_H3_AUDIO_ROW_WIDTH, dtype=torch.float32, device=device)
|
||||
text_pos_dev = packed["text_pos"].view(-1).to(torch.long).to(device)
|
||||
self.text_pos_dev = packed["text_pos"].view(-1).to(torch.long).to(device)
|
||||
self.token_tags_dev = token_tags.view(-1).to(torch.long).to(device)
|
||||
self.img_position_ids_dev = packed["img_position_ids"].to(device)
|
||||
self.text_embeddings_dev = text_embeddings.to(device)
|
||||
self.static_kwargs: dict[str, Any] = {
|
||||
"img_position_ids": packed["img_position_ids"][None].to(device),
|
||||
"img_position_ids": self.img_position_ids_dev[None],
|
||||
"update_mask": self.update_mask_dev,
|
||||
"token_tags": token_tags.view(-1).to(torch.long).to(device),
|
||||
"token_tags": self.token_tags_dev,
|
||||
"skip_mask_out_condition": False,
|
||||
"prompt_embeds": text_embeddings.to(device),
|
||||
"prompt_embeds": self.text_embeddings_dev,
|
||||
"img_pos_info": {"position_ids": self.img_pos_dev},
|
||||
"audio_pos_info": {"position_ids": self.audio_pos_dev},
|
||||
"text_pos_info": {"position_ids": text_pos_dev},
|
||||
"text_pos_info": {"position_ids": self.text_pos_dev},
|
||||
"img_pos_for_infer_output_info": {"position_ids": self.img_pos_dev},
|
||||
"packed_seq_params": {
|
||||
"cu_seqlens_q": cu.to(device),
|
||||
"max_seqlen_q": int(cu[1]),
|
||||
"max_seqlen_q": self.used_len,
|
||||
# One request: valid rows are a prefix, so attention may use a
|
||||
# KV prefix length or a 1-D pad mask. See batched_packing for the
|
||||
# co-batched layout, where neither can describe the valid rows.
|
||||
"num_requests": 1,
|
||||
},
|
||||
"refiner_packed_seq_params": {
|
||||
"cu_seqlens_q": torch.tensor([0, text_len, text_len], dtype=torch.int32, device=device),
|
||||
@@ -136,34 +163,21 @@ class MiniMaxH3DenoiseBranch:
|
||||
}
|
||||
|
||||
|
||||
def minimax_h3_denoise_loop(
|
||||
def minimax_h3_prepare_denoise_rows(
|
||||
*,
|
||||
model: Any,
|
||||
positive: MiniMaxH3DenoiseBranch,
|
||||
initial_video_rows: torch.Tensor,
|
||||
initial_audio_rows: torch.Tensor,
|
||||
keyframe_cond_rows: torch.Tensor | None,
|
||||
audio_ref_rows: torch.Tensor | None = None,
|
||||
sigmas_video: list[float],
|
||||
sigmas_audio: list[float],
|
||||
audio_ref_rows: torch.Tensor | None,
|
||||
device: torch.device,
|
||||
imgvid_cond_noise_aug_for_inference: float = MINIMAX_H3_IMGVID_COND_TIMESTEP,
|
||||
audio_cond_noise_aug_for_inference: float = MINIMAX_H3_AUDIO_REF_COND_TIMESTEP,
|
||||
on_step_end: Callable[[int, torch.Tensor, torch.Tensor], None] | None = None,
|
||||
on_step_start: Callable[[int, float, float], None] | None = None,
|
||||
step_profiler: Callable[[int], AbstractContextManager] | None = None,
|
||||
) -> tuple[torch.Tensor, torch.Tensor]:
|
||||
"""Run the full denoise loop; returns final (video_rows, audio_rows).
|
||||
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor | None, torch.Tensor | None]:
|
||||
"""Validate the initial rows against the layout and pin the condition rows.
|
||||
|
||||
``initial_video_rows`` covers all image rows of the positive layout. For a
|
||||
conditional task, pass ``keyframe_cond_rows`` and/or ``audio_ref_rows`` to
|
||||
pin those rows across every step. The model's raw positive velocity is the
|
||||
update signal; MiniMax H3 only supports cfg-distilled checkpoints.
|
||||
Returns ``(video_rows, audio_rows, cond_anchor, audio_anchor)`` with the
|
||||
anchors already written into their rows, which is the state both the
|
||||
request-mode loop and step-mode ``prepare_encode()`` start from.
|
||||
"""
|
||||
if len(sigmas_video) != len(sigmas_audio):
|
||||
raise ValueError("video/audio sigma schedules must have equal length")
|
||||
if len(sigmas_video) < 2:
|
||||
raise ValueError("sigma schedules need at least 2 entries")
|
||||
n_cond = int((~positive.update_mask).sum())
|
||||
if keyframe_cond_rows is None:
|
||||
if n_cond != 0:
|
||||
@@ -197,19 +211,58 @@ def minimax_h3_denoise_loop(
|
||||
video_rows[~update] = cond_anchor
|
||||
if audio_anchor is not None:
|
||||
audio_rows[~audio_update] = audio_anchor
|
||||
return video_rows, audio_rows, cond_anchor, audio_anchor
|
||||
|
||||
|
||||
def minimax_h3_denoise_loop(
|
||||
*,
|
||||
model: Any,
|
||||
positive: MiniMaxH3DenoiseBranch,
|
||||
initial_video_rows: torch.Tensor,
|
||||
initial_audio_rows: torch.Tensor,
|
||||
keyframe_cond_rows: torch.Tensor | None,
|
||||
audio_ref_rows: torch.Tensor | None = None,
|
||||
sigmas_video: list[float],
|
||||
sigmas_audio: list[float],
|
||||
device: torch.device,
|
||||
imgvid_cond_noise_aug_for_inference: float = MINIMAX_H3_IMGVID_COND_TIMESTEP,
|
||||
audio_cond_noise_aug_for_inference: float = MINIMAX_H3_AUDIO_REF_COND_TIMESTEP,
|
||||
on_step: Callable[[int, torch.Tensor, torch.Tensor], None] | None = None,
|
||||
step_profiler: Callable[[int], AbstractContextManager] | None = None,
|
||||
) -> tuple[torch.Tensor, torch.Tensor]:
|
||||
"""Run the full denoise loop; returns final (video_rows, audio_rows).
|
||||
|
||||
``initial_video_rows`` covers all image rows of the positive layout. For a
|
||||
conditional task, pass ``keyframe_cond_rows`` and/or ``audio_ref_rows`` to
|
||||
pin those rows across every step. The model's raw positive velocity is the
|
||||
update signal; MiniMax H3 only supports cfg-distilled checkpoints.
|
||||
"""
|
||||
if len(sigmas_video) != len(sigmas_audio):
|
||||
raise ValueError("video/audio sigma schedules must have equal length")
|
||||
if len(sigmas_video) < 2:
|
||||
raise ValueError("sigma schedules need at least 2 entries")
|
||||
video_rows, audio_rows, cond_anchor, audio_anchor = minimax_h3_prepare_denoise_rows(
|
||||
positive=positive,
|
||||
initial_video_rows=initial_video_rows,
|
||||
initial_audio_rows=initial_audio_rows,
|
||||
keyframe_cond_rows=keyframe_cond_rows,
|
||||
audio_ref_rows=audio_ref_rows,
|
||||
device=device,
|
||||
)
|
||||
update = positive.update_mask_dev
|
||||
audio_update = positive.audio_update_mask_dev
|
||||
|
||||
num_steps = len(sigmas_video) - 1
|
||||
for step in range(num_steps):
|
||||
step_cm = step_profiler(step) if step_profiler is not None else nullcontext()
|
||||
with step_cm:
|
||||
# Publish the step index so step-gated attention features (e.g. the
|
||||
# dense warmup of RAINFUSION_ATTN) can see where we are.
|
||||
set_forward_context_denoise_step_idx(step)
|
||||
s_v, s_v_next = sigmas_video[step], sigmas_video[step + 1]
|
||||
s_a, s_a_next = sigmas_audio[step], sigmas_audio[step + 1]
|
||||
if on_step_start is not None:
|
||||
# Attention gates use the scheduler-style descending timestep.
|
||||
on_step_start(step, s_v, s_a)
|
||||
# Publish where we are so step-gated attention features (the dense
|
||||
# warmup of RAINFUSION_ATTN, the timestep gate of TRTLLM_ATTN) can
|
||||
# see it. Gates use the scheduler-style descending timestep, which
|
||||
# for a rectified-flow schedule is the video sigma.
|
||||
minimax_h3_publish_denoise_progress(step, s_v)
|
||||
t_v, t_a = 1.0 - s_v, 1.0 - s_a
|
||||
imgvid_cond_t = max(t_v, float(imgvid_cond_noise_aug_for_inference))
|
||||
audio_ref_cond_t = max(t_a, float(audio_cond_noise_aug_for_inference))
|
||||
@@ -250,10 +303,10 @@ def minimax_h3_denoise_loop(
|
||||
audio_rows[audio_update] = new_audio
|
||||
if audio_anchor is not None:
|
||||
audio_rows[~audio_update] = audio_anchor # per-step audio ref reset
|
||||
if on_step_end is not None:
|
||||
on_step_end(step, video_rows, audio_rows)
|
||||
if on_step is not None:
|
||||
on_step(step, video_rows, audio_rows)
|
||||
|
||||
set_forward_context_denoise_step_idx(None)
|
||||
minimax_h3_publish_denoise_progress(None, None)
|
||||
return video_rows, audio_rows
|
||||
|
||||
|
||||
@@ -264,4 +317,6 @@ __all__ = [
|
||||
"MINIMAX_H3_VIDEO_ROW_WIDTH",
|
||||
"MiniMaxH3DenoiseBranch",
|
||||
"minimax_h3_denoise_loop",
|
||||
"minimax_h3_prepare_denoise_rows",
|
||||
"minimax_h3_publish_denoise_progress",
|
||||
]
|
||||
Reference in new issue
Block a user