[Model][MiniMax-H3] Multi-GPU serving fixes and layer-wise offload component selection (#7047)
Signed-off-by: heyuanliu-intel <heyuan.liu@intel.com>
This commit is contained in:
1 parent
2f1a18e69f
commit
40f8e94400
21 files changed
+1857
-60
No files matched your search
@@ -1,7 +1,8 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
|
||||
|
||||
from contextlib import contextmanager
|
||||
import weakref
|
||||
from contextlib import contextmanager, nullcontext
|
||||
from types import SimpleNamespace
|
||||
from typing import Any
|
||||
from unittest.mock import Mock
|
||||
@@ -76,6 +77,247 @@ def test_requested_model_offload_without_backend_uses_resident_components():
|
||||
pipeline.text_encoder.encode_ids.assert_called_once()
|
||||
|
||||
|
||||
def test_decoded_output_is_unchanged_without_active_model_offload():
|
||||
from vllm_omni.diffusion.models.minimax_h3 import MiniMaxH3Pipeline
|
||||
|
||||
pipeline = object.__new__(MiniMaxH3Pipeline)
|
||||
torch.nn.Module.__init__(pipeline)
|
||||
pipeline._model_cpu_offload_modules = []
|
||||
output = Mock()
|
||||
|
||||
assert pipeline._offload_model_cpu_stage_output(output) is output
|
||||
output.cpu.assert_not_called()
|
||||
|
||||
|
||||
def test_reply_rank_copies_decoded_output_to_host(monkeypatch):
|
||||
from vllm_omni.diffusion.models.minimax_h3 import MiniMaxH3Pipeline
|
||||
|
||||
pipeline = object.__new__(MiniMaxH3Pipeline)
|
||||
torch.nn.Module.__init__(pipeline)
|
||||
pipeline._model_cpu_offload_modules = [Mock()]
|
||||
monkeypatch.setattr(pipeline, "_is_output_owner_rank", lambda: True)
|
||||
output = Mock()
|
||||
host_output = Mock()
|
||||
output.cpu.return_value = host_output
|
||||
|
||||
assert pipeline._offload_model_cpu_stage_output(output) is host_output
|
||||
output.cpu.assert_called_once_with()
|
||||
|
||||
|
||||
def test_non_reply_rank_drops_decoded_output_storage(monkeypatch):
|
||||
from vllm_omni.diffusion.models.minimax_h3 import MiniMaxH3Pipeline
|
||||
|
||||
pipeline = object.__new__(MiniMaxH3Pipeline)
|
||||
torch.nn.Module.__init__(pipeline)
|
||||
pipeline._model_cpu_offload_modules = [Mock()]
|
||||
monkeypatch.setattr(pipeline, "_is_output_owner_rank", lambda: False)
|
||||
output = torch.ones((2, 3), dtype=torch.float16)
|
||||
|
||||
placeholder = pipeline._offload_model_cpu_stage_output(output)
|
||||
|
||||
assert placeholder.device.type == "meta"
|
||||
assert placeholder.shape == output.shape
|
||||
assert placeholder.dtype == output.dtype
|
||||
|
||||
|
||||
def test_decode_releases_only_audio_before_return(monkeypatch):
|
||||
"""Video is released by the callers of ``decode`` -- after quantization."""
|
||||
from vllm_omni.diffusion.models.minimax_h3 import MiniMaxH3Pipeline
|
||||
from vllm_omni.diffusion.models.minimax_h3 import pipeline_minimax_h3 as module
|
||||
|
||||
video = torch.ones((1, 1, 4, 4))
|
||||
audio = torch.ones((1, 8))
|
||||
pipeline = SimpleNamespace(
|
||||
device=torch.device("cpu"),
|
||||
video_vae=Mock(decode_latent=Mock(return_value=video)),
|
||||
audio_vae=Mock(decode_latent=Mock(return_value=audio)),
|
||||
_component_on_device=lambda component: nullcontext(),
|
||||
_offload_model_cpu_stage_output=Mock(side_effect=lambda value: value),
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
module.current_omni_platform,
|
||||
"create_autocast_context",
|
||||
lambda **kwargs: nullcontext(),
|
||||
)
|
||||
|
||||
actual_video, actual_audio = MiniMaxH3Pipeline.decode(
|
||||
pipeline,
|
||||
torch.zeros(1),
|
||||
torch.zeros(1),
|
||||
height=2,
|
||||
width=3,
|
||||
)
|
||||
|
||||
assert actual_video.shape[-2:] == (2, 3)
|
||||
assert actual_audio is audio
|
||||
calls = pipeline._offload_model_cpu_stage_output.call_args_list
|
||||
assert len(calls) == 1
|
||||
assert calls[0].args[0] is audio
|
||||
|
||||
|
||||
def _recording_release(events, releases):
|
||||
"""Stand-in for ``_offload_model_cpu_stage_output`` that records what it got."""
|
||||
|
||||
def release(value):
|
||||
events.append("release")
|
||||
releases.append(value)
|
||||
return value
|
||||
|
||||
return release
|
||||
|
||||
|
||||
def test_forward_releases_video_after_quantization_and_before_next_seed(monkeypatch):
|
||||
from vllm_omni.diffusion.models.minimax_h3 import MiniMaxH3Pipeline
|
||||
|
||||
# MiniMaxH3VideoVAE.decode_latent ends in frames.float(), so this is what
|
||||
# the release hook would see if it ran before the quantizer.
|
||||
decoded_dtype = torch.float32
|
||||
events: list[str] = []
|
||||
releases: list[torch.Tensor] = []
|
||||
|
||||
def diffuse(**kwargs):
|
||||
events.append(f"diffuse:{kwargs['seed']}")
|
||||
return torch.zeros(1), torch.zeros(1)
|
||||
|
||||
def decode(video_latent, audio_latent, *, height, width):
|
||||
events.append("decode")
|
||||
return (
|
||||
torch.zeros((1, 3, 2, height, width), dtype=decoded_dtype),
|
||||
torch.zeros((1, 8)),
|
||||
)
|
||||
|
||||
# ``forward`` indexes ``preencode_mp4`` (#7018): this test drives the
|
||||
# tensor-decode branch, which is the one the release hook wraps.
|
||||
context = {"num_outputs": 2, "seed": 41, "height": 4, "width": 4, "preencode_mp4": False}
|
||||
pipeline = SimpleNamespace(
|
||||
od_config=SimpleNamespace(),
|
||||
_extract_prompt=lambda raw: ("a prompt", {}),
|
||||
_extract_text_conditioning=lambda raw: None,
|
||||
_extract_prepared_reference_videos=lambda raw: None,
|
||||
_prepare_request_inputs=lambda **kwargs: context,
|
||||
_denoise_kwargs=lambda ctx: {},
|
||||
diffuse=diffuse,
|
||||
decode=decode,
|
||||
_offload_model_cpu_stage_output=_recording_release(events, releases),
|
||||
)
|
||||
request = SimpleNamespace(prompts=["a prompt"], sampling_params=SimpleNamespace())
|
||||
|
||||
output = MiniMaxH3Pipeline.forward(pipeline, request)
|
||||
|
||||
# The release must see the quantized frames, not the decoded floats.
|
||||
assert len(releases) == 2
|
||||
assert [tensor.dtype for tensor in releases] == [torch.uint8, torch.uint8]
|
||||
assert decoded_dtype is not torch.uint8
|
||||
# ... and it must still happen before the next seed reloads the DiT.
|
||||
assert events == ["diffuse:41", "decode", "release", "diffuse:42", "decode", "release"]
|
||||
assert output.output[0].dtype is torch.uint8
|
||||
|
||||
|
||||
def test_forward_drops_the_decoded_video_before_the_next_seed_diffuses(monkeypatch):
|
||||
"""Releasing the copy is not enough if a local still pins the original.
|
||||
|
||||
``_offload_model_cpu_stage_output`` hands back a *new* tensor, so the
|
||||
decoded frames stay alive for as long as anything still references them.
|
||||
The whole point of this branch is that they are gone before the next seed
|
||||
reloads the DiT, and ``diffuse`` is where that reload happens.
|
||||
"""
|
||||
from vllm_omni.diffusion.models.minimax_h3 import MiniMaxH3Pipeline
|
||||
|
||||
decoded_refs: list[weakref.ref] = []
|
||||
alive_at_diffuse: list[int] = []
|
||||
|
||||
def diffuse(**kwargs):
|
||||
alive_at_diffuse.append(sum(1 for ref in decoded_refs if ref() is not None))
|
||||
return torch.zeros(1), torch.zeros(1)
|
||||
|
||||
def decode(video_latent, audio_latent, *, height, width):
|
||||
video = torch.zeros((1, 3, 2, height, width), dtype=torch.float32)
|
||||
decoded_refs.append(weakref.ref(video))
|
||||
return video, torch.zeros((1, 8))
|
||||
|
||||
# ``forward`` indexes ``preencode_mp4`` (#7018): this test drives the
|
||||
# tensor-decode branch, which is the one the release hook wraps.
|
||||
context = {"num_outputs": 2, "seed": 41, "height": 4, "width": 4, "preencode_mp4": False}
|
||||
pipeline = SimpleNamespace(
|
||||
od_config=SimpleNamespace(),
|
||||
_extract_prompt=lambda raw: ("a prompt", {}),
|
||||
_extract_text_conditioning=lambda raw: None,
|
||||
_extract_prepared_reference_videos=lambda raw: None,
|
||||
_prepare_request_inputs=lambda **kwargs: context,
|
||||
_denoise_kwargs=lambda ctx: {},
|
||||
diffuse=diffuse,
|
||||
decode=decode,
|
||||
_offload_model_cpu_stage_output=lambda value: value.clone(),
|
||||
)
|
||||
request = SimpleNamespace(prompts=["a prompt"], sampling_params=SimpleNamespace())
|
||||
|
||||
MiniMaxH3Pipeline.forward(pipeline, request)
|
||||
|
||||
assert len(decoded_refs) == 2
|
||||
# Seed 1's diffuse sees nothing decoded yet; seed 2's must not see seed 1's
|
||||
# decoded frames still resident.
|
||||
assert alive_at_diffuse == [0, 0]
|
||||
|
||||
|
||||
def test_post_decode_releases_video_after_quantization(monkeypatch):
|
||||
from vllm_omni.diffusion.models.minimax_h3 import MiniMaxH3Pipeline
|
||||
from vllm_omni.diffusion.models.minimax_h3 import pipeline_minimax_h3 as module
|
||||
|
||||
events: list[str] = []
|
||||
releases: list[torch.Tensor] = []
|
||||
|
||||
def decode(video_latent, audio_latent, *, height, width):
|
||||
events.append("decode")
|
||||
return (
|
||||
torch.zeros((1, 3, 2, height, width), dtype=torch.float16),
|
||||
torch.zeros((1, 8)),
|
||||
)
|
||||
|
||||
pipeline = SimpleNamespace(
|
||||
od_config=SimpleNamespace(),
|
||||
_unpack_denoised_rows=lambda *args, **kwargs: (torch.zeros(1), torch.zeros(1)),
|
||||
decode=decode,
|
||||
_offload_model_cpu_stage_output=_recording_release(events, releases),
|
||||
)
|
||||
state = SimpleNamespace(
|
||||
latents=torch.zeros(1),
|
||||
extra={
|
||||
module._STEP_BRANCH: object(),
|
||||
module._STEP_AUDIO_ROWS: torch.zeros(1),
|
||||
module._STEP_SHAPE: {
|
||||
"latent_t": 1,
|
||||
"latent_h": 1,
|
||||
"latent_w": 1,
|
||||
"audio_t": 1,
|
||||
"height": 4,
|
||||
"width": 4,
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
output = MiniMaxH3Pipeline.post_decode(pipeline, state)
|
||||
|
||||
assert events == ["decode", "release"]
|
||||
assert len(releases) == 1
|
||||
assert releases[0].dtype is torch.uint8
|
||||
assert output.output[0] is releases[0]
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("available", "initialized", "rank", "expected"),
|
||||
[(False, False, 7, True), (True, False, 7, True), (True, True, 0, True), (True, True, 1, False)],
|
||||
)
|
||||
def test_output_owner_rank_matches_executor_contract(monkeypatch, available, initialized, rank, expected):
|
||||
from vllm_omni.diffusion.models.minimax_h3 import MiniMaxH3Pipeline
|
||||
from vllm_omni.diffusion.models.minimax_h3 import pipeline_minimax_h3 as module
|
||||
|
||||
monkeypatch.setattr(module.dist, "is_available", lambda: available)
|
||||
monkeypatch.setattr(module.dist, "is_initialized", lambda: initialized)
|
||||
monkeypatch.setattr(module.dist, "get_rank", lambda: rank)
|
||||
|
||||
assert MiniMaxH3Pipeline._is_output_owner_rank() is expected
|
||||
|
||||
|
||||
def test_h3_model_cpu_offload_registers_direct_vae_stages(monkeypatch):
|
||||
from vllm_omni.diffusion.models.minimax_h3 import MiniMaxH3Pipeline
|
||||
from vllm_omni.diffusion.models.minimax_h3 import pipeline_minimax_h3 as module
|
||||
|
||||
Reference in new issue
Block a user