2026-01-30 17:35:09 +08:00
|
|
|
import sys
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
import torch
|
|
|
|
|
from vllm.distributed.parallel_state import cleanup_dist_env_and_memory
|
|
|
|
|
|
2026-03-10 23:19:36 -05:00
|
|
|
from tests.utils import DeviceMemoryMonitor
|
2026-01-30 17:35:09 +08:00
|
|
|
from vllm_omni.inputs.data import OmniDiffusionSamplingParams
|
2026-02-27 00:52:15 -06:00
|
|
|
from vllm_omni.platforms import current_omni_platform
|
2026-01-30 17:35:09 +08:00
|
|
|
|
|
|
|
|
# ruff: noqa: E402
|
|
|
|
|
REPO_ROOT = Path(__file__).resolve().parents[2]
|
|
|
|
|
if str(REPO_ROOT) not in sys.path:
|
|
|
|
|
sys.path.insert(0, str(REPO_ROOT))
|
|
|
|
|
|
|
|
|
|
from vllm_omni import Omni
|
|
|
|
|
|
|
|
|
|
# Models to test and expected saved memory in MB, correspondingly
|
2026-02-12 17:29:55 +08:00
|
|
|
MODELS_SAVED_MEMORY_MB = {
|
|
|
|
|
"riverclouds/qwen_image_random": 4500,
|
|
|
|
|
# "Wan-AI/Wan2.2-T2V-A14B-Diffusers": 45000,
|
|
|
|
|
}
|
2026-01-30 17:35:09 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def run_inference(
|
|
|
|
|
model_name: str,
|
|
|
|
|
layerwise_offload: bool = False,
|
|
|
|
|
num_inference_steps: int = 3,
|
|
|
|
|
) -> float:
|
2026-02-27 00:52:15 -06:00
|
|
|
current_omni_platform.empty_cache()
|
2026-03-10 23:19:36 -05:00
|
|
|
device_index = current_omni_platform.current_device()
|
|
|
|
|
monitor = DeviceMemoryMonitor(device_index=device_index, interval=0.02)
|
2026-01-30 17:35:09 +08:00
|
|
|
monitor.start()
|
|
|
|
|
|
|
|
|
|
m = Omni(
|
|
|
|
|
model=model_name,
|
|
|
|
|
enable_layerwise_offload=layerwise_offload,
|
2026-03-09 22:25:41 +08:00
|
|
|
# TODO: we might want to add overlapped feature e2e tests
|
|
|
|
|
# cache_backend="cache_dit",
|
2026-01-30 17:35:09 +08:00
|
|
|
boundary_ratio=0.875,
|
|
|
|
|
flow_shift=5.0,
|
|
|
|
|
)
|
|
|
|
|
|
2026-03-10 23:19:36 -05:00
|
|
|
current_omni_platform.reset_peak_memory_stats()
|
2026-01-30 17:35:09 +08:00
|
|
|
|
|
|
|
|
# Refer to tests/e2e/offline_inference/test_t2v_model.py
|
|
|
|
|
# Use minimal settings for testing
|
|
|
|
|
height = 480
|
|
|
|
|
width = 640
|
|
|
|
|
num_frames = 5
|
|
|
|
|
|
|
|
|
|
m.generate(
|
|
|
|
|
"A cat sitting on a table",
|
|
|
|
|
OmniDiffusionSamplingParams(
|
|
|
|
|
height=height,
|
|
|
|
|
width=width,
|
2026-03-10 23:19:36 -05:00
|
|
|
generator=torch.Generator(device=current_omni_platform.device_type).manual_seed(42),
|
2026-01-30 17:35:09 +08:00
|
|
|
guidance_scale=1.0,
|
|
|
|
|
num_inference_steps=num_inference_steps,
|
|
|
|
|
num_frames=num_frames,
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
peak = monitor.peak_used_mb
|
|
|
|
|
monitor.stop()
|
|
|
|
|
|
|
|
|
|
return peak
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("model_name", MODELS_SAVED_MEMORY_MB.keys())
|
|
|
|
|
def test_layerwise_offload_diffusion_model(model_name: str):
|
|
|
|
|
"""Test that layerwise offloading reduces GPU memory usage.
|
|
|
|
|
|
|
|
|
|
This test verifies that layerwise offloading significantly reduces peak
|
|
|
|
|
GPU memory usage compared to loading the entire model on GPU. The layerwise
|
|
|
|
|
offloader keeps only a single transformer block on GPU at a time, with
|
|
|
|
|
prefetching for compute-memory overlap.
|
|
|
|
|
"""
|
|
|
|
|
try:
|
|
|
|
|
# Run without layerwise offloading (baseline)
|
|
|
|
|
no_offload_peak_memory = run_inference(model_name, layerwise_offload=False)
|
|
|
|
|
cleanup_dist_env_and_memory()
|
|
|
|
|
|
|
|
|
|
# Run with layerwise offloading (1 layer on device)
|
2026-02-12 17:29:55 +08:00
|
|
|
layerwise_offload_peak_memory = run_inference(model_name, layerwise_offload=True)
|
2026-01-30 17:35:09 +08:00
|
|
|
cleanup_dist_env_and_memory()
|
|
|
|
|
except Exception:
|
|
|
|
|
pytest.fail("Inference failed")
|
|
|
|
|
|
|
|
|
|
print(f"Layerwise offload peak memory (1 GPU layer): {layerwise_offload_peak_memory} MB")
|
|
|
|
|
print(f"No offload peak memory: {no_offload_peak_memory} MB")
|
|
|
|
|
|
|
|
|
|
# Verify that layerwise offloading significantly reduces memory usage
|
|
|
|
|
# Passes only if the actual savings exceeds the expected savings
|
|
|
|
|
assert layerwise_offload_peak_memory + MODELS_SAVED_MEMORY_MB[model_name] < no_offload_peak_memory, (
|
|
|
|
|
f"Layerwise offload peak memory {layerwise_offload_peak_memory} MB "
|
|
|
|
|
f"should be significantly less than no offload peak memory {no_offload_peak_memory} MB"
|
|
|
|
|
)
|