[Bugfix][Diffusion] Fix MiniMax-H3 model-level CPU offload residency (#6072)

Signed-off-by: Xiaodong Ye <xiaodong.ye@mthreads.com>
This commit is contained in:
R0CKSTAR authored and GitHub committed 2026-08-21 10:30:53 +00:00
1 parent 4f7c08c0eb
commit 260db8bab5
7 files changed
+553 -102

No files matched your search

@@ -8,7 +8,7 @@ import math
import os
import tempfile
from collections.abc import Iterable, Mapping, Sequence
from contextlib import contextmanager
from contextlib import contextmanager, nullcontext
from itertools import groupby
from pathlib import Path
from typing import Any, ClassVar
@@ -44,7 +44,13 @@ from vllm_omni.diffusion.models.interface import (
SupportsComponentDiscovery,
)
from vllm_omni.diffusion.models.progress_bar import ProgressBarMixin
from vllm_omni.diffusion.offloader import OffloadPlan
from vllm_omni.diffusion.offloader import (
OffloadPlan,
apply_sequential_offload,
remove_sequential_offload,
sequential_offload_component,
)
from vllm_omni.diffusion.offloader.module_collector import ModuleDiscovery
from vllm_omni.diffusion.profiler.diffusion_pipeline_profiler import (
DiffusionPipelineProfilerMixin,
)
@@ -543,9 +549,8 @@ class MiniMaxH3Pipeline(
_PROFILER_TARGETS: ClassVar[list[str]] = [
"_prepare_reference_videos",
"encode_prompt",
"_encode_video_conditions",
"_encode_video_audio_conditions",
"_encode_audio_conditions",
"_encode_visual_conditions",
"_encode_reference_audio_conditions",
"diffuse",
"decode",
]
@@ -1129,9 +1134,7 @@ class MiniMaxH3Pipeline(
input_ids: torch.Tensor,
vision_kwargs: dict[str, torch.Tensor],
) -> torch.Tensor:
if self.od_config.enable_cpu_offload and not getattr(
self.od_config, "enable_distributed_layerwise_offload", False
):
if getattr(self, "_model_cpu_offload_modules", None):
# Invoke nn.Module.__call__ so the generic model-level offloader
# swaps the resident DiT and encoder.
return self.text_encoder(input_ids, **vision_kwargs)
@@ -1160,8 +1163,48 @@ class MiniMaxH3Pipeline(
or getattr(od_config, "enable_distributed_layerwise_offload", False)
)
def enable_omni_model_cpu_offload(
self,
*,
device: torch.device,
pin_memory: bool,
use_hsdp: bool,
) -> None:
if getattr(self, "_model_cpu_offload_modules", None):
return
components = ModuleDiscovery.discover(self)
dits = components.dits
stages = [*components.encoders, *components.vaes]
modules = [*dits, *stages]
apply_sequential_offload(
dit_modules=dits,
encoder_modules=stages,
device=device,
pin_memory=pin_memory,
use_hsdp=use_hsdp,
offload_initial_dits=True,
)
self._model_cpu_offload_modules = modules
logger.info(
"MiniMax-H3 model-level CPU offload enabled for %d DiT(s), text encoder, video VAE, and audio VAE",
len(dits),
)
def disable_omni_model_cpu_offload(self) -> None:
modules = getattr(self, "_model_cpu_offload_modules", None)
if not modules:
return
remove_sequential_offload(modules)
self._model_cpu_offload_modules = []
@contextmanager
def _component_on_device(self, component: nn.Module):
if getattr(self, "_model_cpu_offload_modules", None):
with sequential_offload_component(component):
yield
return
staged = self._uses_manual_component_offload()
if staged:
component.load_to_device()
@@ -1171,21 +1214,6 @@ class MiniMaxH3Pipeline(
if staged:
component.offload_to_cpu()
def _encode_visual_condition(
self,
image: Image.Image,
) -> torch.Tensor:
_, rank, _ = _dit_rank_world()
rows = None
if rank == 0:
with self._component_on_device(self.video_vae):
rows = self.video_vae.encode_image(image)
return _broadcast_tensor(
rows,
dtype=torch.float32,
device=self.device,
)
def _encode_visual_conditions(
self,
images: list[Image.Image],
@@ -1195,44 +1223,34 @@ class MiniMaxH3Pipeline(
) -> tuple[torch.Tensor | None, list[tuple[int, int, int]]]:
rows: list[torch.Tensor] = []
shapes: list[tuple[int, int, int]] = []
for image in images:
rows.append(self._encode_visual_condition(image))
shapes.append((1, image.height // 16, image.width // 16))
if video_count:
video_rows, video_shapes = self._encode_video_conditions(
prepared_videos,
count=video_count,
)
rows.append(video_rows)
shapes.extend(video_shapes)
_, rank, _ = _dit_rank_world()
# Keep image and video references in one residency window when both
# appear in a request; otherwise the video branch would reload the VAE.
needs_video_vae = video_count > 0 or (rank == 0 and bool(images))
video_vae_context = self._component_on_device(self.video_vae) if needs_video_vae else nullcontext()
with video_vae_context:
if images:
image_rows = None
if rank == 0:
image_rows = torch.cat([self.video_vae.encode_image(image) for image in images])
rows.append(
_broadcast_tensor(
image_rows,
dtype=torch.float32,
device=self.device,
)
)
shapes.extend((1, image.height // 16, image.width // 16) for image in images)
if video_count:
video_rows, video_shapes = self._encode_video_conditions_resident(
prepared_videos,
count=video_count,
)
rows.append(video_rows)
shapes.extend(video_shapes)
return (torch.cat(rows) if rows else None), shapes
def _encode_audio_condition(
self,
audio: tuple[torch.Tensor, int],
) -> tuple[torch.Tensor, int]:
_, rank, _ = _dit_rank_world()
rows = None
audio_t = 0
if rank == 0:
with self._component_on_device(self.audio_vae):
rows, audio_t = self.audio_vae.encode_waveform(*audio)
audio_t_tensor = torch.tensor(
[audio_t],
dtype=torch.long,
device=self.device,
)
group, _, world_size = _dit_rank_world()
if world_size > 1:
dist.broadcast(audio_t_tensor, src=0, group=group)
rows = _broadcast_tensor(
rows,
dtype=torch.float32,
device=self.device,
)
return rows, int(audio_t_tensor.item())
def _encode_audio_conditions(
def _encode_audio_conditions_resident(
self,
audios: list[tuple[torch.Tensor, int]],
*,
@@ -1269,15 +1287,6 @@ class MiniMaxH3Pipeline(
[int(value) for value in lengths.tolist()],
)
def _encode_video_conditions(
self,
prepared_videos: list[dict[str, Any]] | None,
*,
count: int,
) -> tuple[torch.Tensor, list[tuple[int, int, int]]]:
with self._component_on_device(self.video_vae):
return self._encode_video_conditions_resident(prepared_videos, count=count)
def _encode_video_conditions_resident(
self,
prepared_videos: list[dict[str, Any]] | None,
@@ -1325,15 +1334,6 @@ class MiniMaxH3Pipeline(
[tuple(int(value) for value in item) for item in shapes.tolist()],
)
def _encode_video_audio_conditions(
self,
prepared_videos: list[dict[str, Any]] | None,
*,
has_audio: list[bool],
) -> tuple[torch.Tensor | None, list[int]]:
with self._component_on_device(self.audio_vae):
return self._encode_video_audio_conditions_resident(prepared_videos, has_audio=has_audio)
def _encode_video_audio_conditions_resident(
self,
prepared_videos: list[dict[str, Any]] | None,
@@ -1377,6 +1377,34 @@ class MiniMaxH3Pipeline(
[int(value) for value in lengths.tolist()],
)
def _encode_reference_audio_conditions(
self,
prepared_videos: list[dict[str, Any]] | None,
*,
has_audio: list[bool],
standalone_audios: list[tuple[torch.Tensor, int]],
max_duration_seconds: float,
) -> tuple[torch.Tensor | None, list[int], torch.Tensor | None, list[int]]:
# Embedded and standalone audio are consecutive direct Audio-VAE
# calls. Keep the component resident across both paths.
needs_audio_vae = any(has_audio) or bool(standalone_audios)
audio_vae_context = self._component_on_device(self.audio_vae) if needs_audio_vae else nullcontext()
with audio_vae_context:
embedded_condition, embedded_lengths = self._encode_video_audio_conditions_resident(
prepared_videos,
has_audio=has_audio,
)
external_condition, external_lengths = self._encode_audio_conditions_resident(
standalone_audios,
max_duration_seconds=max_duration_seconds,
)
return (
embedded_condition,
embedded_lengths,
external_condition,
external_lengths,
)
def _initial_noise(
self,
*,
@@ -1755,12 +1783,15 @@ class MiniMaxH3Pipeline(
prepared_videos,
video_count=video_count,
)
embedded_audio_condition, embedded_audio_lengths = self._encode_video_audio_conditions(
(
embedded_audio_condition,
embedded_audio_lengths,
external_audio_condition,
external_audio_lengths,
) = self._encode_reference_audio_conditions(
prepared_videos,
has_audio=has_audio,
)
external_audio_condition, external_audio_lengths = self._encode_audio_conditions(
standalone_audios,
standalone_audios=standalone_audios,
max_duration_seconds=float(num_frames) / float(sampling.fps or MINIMAX_H3_FPS),
)
audio_parts = [