[Test] Cleanup for helpers (#6523)

Signed-off-by: wangyu <410167048@qq.com>
Signed-off-by: [Your Name] <your.email@example.com>
Co-authored-by: amy-why-3459 <wuhaiyan17@huawei.com>
This commit is contained in:
wangyuandamy-why-3459 authored and GitHub committed 2026-08-25 09:10:17 +08:00
1 parent 8360346a66
commit 4af0ddc1c2
140 files changed
+4121 -4194

No files matched your search

+4 -1
View File
@@ -1,6 +1,9 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
"""Shared, importable test helper utilities.
Submodules (``assertions``, ``env``, ``media``, ``runtime``, …) are imported
Submodules (``assertions``, ``clean``, ``client``, ``media``, ``runtime``, …) are imported
explicitly by callers. Unit tests for these helpers live under ``tests/``.
Avoid star-importing everything here: that ran before refactor only inside the
old monolithic ``conftest``; a greedy ``__init__`` changes import order and can
+23 -32
View File
@@ -1,5 +1,9 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
"""Assertion and response validation helpers for tests."""
import base64
import io
import json
import math
@@ -14,7 +18,7 @@ from pathlib import Path
from typing import TYPE_CHECKING, Any, Literal, TypeVar
if TYPE_CHECKING:
from tests.helpers.runtime import DiffusionResponse
from tests.helpers.client import DiffusionResponse
import av
import numpy as np
@@ -24,7 +28,6 @@ from PIL import Image
from tests.helpers.media import (
convert_audio_bytes_to_text,
cosine_similarity_text,
decode_b64_image,
preprocess_text,
)
@@ -171,7 +174,7 @@ def _short_transcript_contains_expected(transcript: str, expected: str) -> bool:
def assert_image_diffusion_response(
response: "DiffusionResponse",
request_config: dict[str, Any],
run_level: str = None,
run_level: str | None = None,
) -> None:
"""
Validate image diffusion response.
@@ -243,14 +246,15 @@ def assert_images_generations_response(
assert isinstance(item, dict), "Image generation data entries must be objects"
b64_json = item.get("b64_json")
assert isinstance(b64_json, str) and b64_json, "Image generation response is missing b64_json"
image = decode_b64_image(b64_json)
image = Image.open(io.BytesIO(base64.b64decode(b64_json)))
image.load()
assert_image_valid(image, width=width, height=height)
def assert_video_diffusion_response(
response: "DiffusionResponse",
request_config: dict[str, Any],
run_level: str = None,
run_level: str | None = None,
) -> None:
"""
Validate video diffusion response.
@@ -319,7 +323,7 @@ def assert_video_first_frame_matches(
def assert_audio_diffusion_response(
response: "DiffusionResponse",
request_config: dict[str, Any],
run_level: str = None,
run_level: str | None = None,
) -> None:
"""
Validate audio diffusion response.
@@ -634,7 +638,7 @@ def _response_has_audio_output(response: Any) -> bool:
return bool(audio_data)
def _omni_assertion_needs_audio_transcript(request_config: dict[str, Any], run_level: str) -> bool:
def _omni_assertion_needs_audio_transcript(request_config: dict[str, Any], run_level: str | None) -> bool:
if run_level not in {"advanced_model", "full_model"}:
return False
modalities = request_config.get("modalities", ["text", "audio"])
@@ -656,7 +660,7 @@ def _omni_assertion_needs_audio_transcript(request_config: dict[str, Any], run_l
return "text" in modalities
def _speech_assertion_needs_audio_transcript(request_config: dict[str, Any], run_level: str) -> bool:
def _speech_assertion_needs_audio_transcript(request_config: dict[str, Any], run_level: str | None) -> bool:
if run_level not in {"advanced_model", "full_model"}:
return False
if request_config.get("response_format") == "pcm":
@@ -667,7 +671,7 @@ def _speech_assertion_needs_audio_transcript(request_config: dict[str, Any], run
def _resolve_audio_transcript(
response: Any,
request_config: dict[str, Any],
run_level: str,
run_level: str | None,
*,
speech_api: bool,
) -> str | None:
@@ -842,28 +846,13 @@ def _assert_transcript_matches(
)
def assert_audio_speech_response(response: Any, request_config: dict[str, Any], run_level: str) -> None:
"""Validate speech API results from :class:`~tests.helpers.runtime.OmniResponse`.
def assert_audio_speech_response(response: Any, request_config: dict[str, Any], run_level: str | None = None) -> None:
"""Validate speech API results from :class:`~tests.helpers.client.OmniResponse`.
When ``request_config`` carries ``status_code`` and/or ``err_message``, the
request is expected to be rejected: assert it failed and that the HTTP status
/ error text match. Otherwise the normal success-path checks run.
Success-path checks only. Negative / contract cases belong on
:meth:`~tests.helpers.client.OnlineOmniClient.send_audio_speech_http_request`
with :func:`assert_http_error`.
"""
expected_status = request_config.get("status_code")
expected_err = request_config.get("err_message")
if expected_status is not None or expected_err is not None:
assert not response.success, "Expected an error response, but the request succeeded."
if expected_status is not None:
allowed = expected_status if isinstance(expected_status, (list, tuple)) else (expected_status,)
assert response.status_code in allowed, f"Expected HTTP status in {allowed}, got {response.status_code}"
if expected_err is not None:
alternatives = expected_err if isinstance(expected_err, (list, tuple)) else (expected_err,)
error_text = response.error_message or ""
assert any(alt in error_text for alt in alternatives), (
f"Expected one of {alternatives} in error text, got: {error_text!r}"
)
return
assert response.success, "The request failed."
# Optional floor on decoded audio size (models with very short clips may use a lower value).
@@ -910,7 +899,9 @@ def assert_audio_speech_response(response: Any, request_config: dict[str, Any],
)
def assert_diffusion_response(response: "DiffusionResponse", request_config: dict[str, Any], run_level: str = None):
def assert_diffusion_response(
response: "DiffusionResponse", request_config: dict[str, Any], run_level: str | None = None
):
assert response.success, "The request failed."
has_any_content = any(content is not None for content in (response.images, response.videos, response.audios))
assert has_any_content, "Response contains no images, videos, or audios"
@@ -976,9 +967,9 @@ def assert_http_error(
err_message: str | tuple[str, ...] | list[str] | None = None,
websocket_json_message: bool = False,
) -> dict[str, Any] | None:
"""Validate a raw-HTTP :class:`~tests.helpers.runtime.HttpResponse`-like object.
"""Validate a raw-HTTP :class:`~tests.helpers.client.HttpResponse`-like object.
Used by :class:`~tests.helpers.runtime.OpenAIClientHandler` ``send_*_http_request`` helpers when
Used by :class:`~tests.helpers.client.OnlineOmniClient` ``send_*_http_request`` helpers when
``request_config`` contains optional ``err_code`` and/or ``err_message``.
When ``websocket_json_message=True``, only ``json_body`` is checked (first JSON WebSocket text frame).
+252
View File
@@ -0,0 +1,252 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
"""Test cleanup helpers (device memory, distributed state, and memory monitoring).
``vllm_omni.platforms`` is imported only inside functions that need it so importing this module
at pytest plugin load does not run before session autouse fixtures.
"""
from __future__ import annotations
import gc
import logging
import os
import subprocess
import time
from vllm_omni.platforms import current_omni_platform
logger = logging.getLogger(__name__)
def get_physical_device_indices(devices):
"""Map logical device indices to physical IDs via the platform visibility env.
Uses ``current_omni_platform.device_control_env_var`` (e.g. ``CUDA_VISIBLE_DEVICES``,
``ASCEND_RT_VISIBLE_DEVICES``) rather than hard-coding CUDA.
"""
visible_devices = os.environ.get(current_omni_platform.device_control_env_var)
if visible_devices is None:
return devices
visible_indices = [int(x) for x in visible_devices.split(",") if x.strip() != ""]
index_mapping = {i: physical for i, physical in enumerate(visible_indices)}
return [index_mapping[i] for i in devices if i in index_mapping]
def pick_least_used_device_indices(num_devices: int) -> list[int]:
"""Return physical device indices for the least-used accelerators.
Args:
num_devices: Number of device indices to return.
Returns:
Physical device indices sorted from least to most used memory.
"""
if num_devices <= 0:
raise ValueError(f"num_devices must be positive, got {num_devices}.")
logical_count = current_omni_platform.get_device_count()
if logical_count < num_devices:
raise RuntimeError(f"Need {num_devices} devices, but only {logical_count} are available.")
device_usage: list[tuple[int, int]] = []
for device_index in range(logical_count):
with current_omni_platform.device(device_index):
free_bytes, total_bytes = current_omni_platform.get_device_memory()
device_usage.append((total_bytes - free_bytes, device_index))
device_usage.sort()
logical_indices = [device_index for _, device_index in device_usage[:num_devices]]
return get_physical_device_indices(logical_indices)
def wait_for_gpu_memory_to_clear(
*,
devices: list[int],
threshold_bytes: int | None = None,
threshold_ratio: float | None = None,
timeout_s: float = 120,
) -> None:
assert threshold_bytes is not None or threshold_ratio is not None
devices = get_physical_device_indices(devices)
start_time = time.time()
device_list = ", ".join(str(d) for d in devices)
if threshold_bytes is not None:
condition_str = f"Memory usage ≤ {threshold_bytes / 2**30:.2f} GiB"
def is_free(used, total):
return used <= threshold_bytes / 2**30
else:
ratio = threshold_ratio
assert ratio is not None
condition_str = f"Memory usage ratio ≤ {ratio * 100:.1f}%"
def is_free(used, total):
return used / total <= ratio
print(f"[Device Memory Monitor] Waiting for device(s) {device_list} to free memory, Condition: {condition_str}")
def get_mem_gib(device: int) -> tuple[float, float]:
with current_omni_platform.device(device):
free_bytes, total_bytes = current_omni_platform.mem_get_info()
return (total_bytes - free_bytes) / 2**30, total_bytes / 2**30
while True:
output_raw = {d: get_mem_gib(d) for d in devices}
output = {
d: f"{used:.1f}GiB/{total:.1f}GiB ({(used / total) * 100 if total > 0 else 0:.1f}%)"
for d, (used, total) in output_raw.items()
}
print("[Device Memory Status] Current usage:")
for device_id, mem_info in output.items():
print(f" Device {device_id}: {mem_info}")
dur_s = time.time() - start_time
if all(is_free(used, total) for used, total in output_raw.values()):
print(f"[Device Memory Freed] Device(s) {device_list} meet memory condition")
print(f" Condition: {condition_str}")
print(f" Wait time: {dur_s:.1f} seconds ({dur_s / 60:.1f} minutes)")
break
if dur_s >= timeout_s:
raise ValueError(
f"[Device Memory Timeout] Device(s) {device_list} still don't meet memory condition after {dur_s:.1f} seconds\n"
f"Condition: {condition_str}\n"
f"Current status:\n" + "\n".join(f" Device {d}: {output[d]}" for d in devices)
)
gc.collect()
current_omni_platform.empty_cache()
time.sleep(5)
def _run_smi(label: str, cmd: list[str], head_lines: int, timeout: float = 5) -> None:
print("\n" + "=" * 80)
print(label)
print("=" * 80)
try:
result = subprocess.run(cmd, capture_output=True, text=True, timeout=timeout)
if result.returncode == 0 and result.stdout.strip():
lines = result.stdout.strip().split("\n")
for line in lines[:head_lines]:
print(line)
if len(lines) > head_lines:
print(f"... (showing first {head_lines} of {len(lines)} lines)")
else:
print(f"{cmd[0]} command failed or produced no output")
except (subprocess.TimeoutExpired, FileNotFoundError):
print(f"{cmd[0]} not available or timed out")
except Exception as e:
print(f"Error running {' '.join(cmd)}: {e}")
def _print_device_processes() -> None:
"""Print device information via platform SMI tools."""
if current_omni_platform.is_cuda():
_run_smi("NVIDIA GPU Information (nvidia-smi)", ["nvidia-smi"], 20)
_run_smi("Detailed GPU Processes (nvidia-smi pmon)", ["nvidia-smi", "pmon", "-c", "1"], 100, timeout=3)
elif current_omni_platform.is_npu():
_run_smi("Ascend NPU Information (npu-smi info)", ["npu-smi", "info"], 40)
elif current_omni_platform.is_rocm():
_run_smi("AMD GPU Information (amd-smi)", ["amd-smi"], 30)
_run_smi("Detailed AMD GPU Processes (amd-smi process)", ["amd-smi", "process"], 100, timeout=3)
elif current_omni_platform.is_xpu():
_run_smi("Intel XPU Information (xpu-smi discovery)", ["xpu-smi", "discovery"], 40)
elif current_omni_platform.is_musa():
_run_smi("Moore Threads GPU Information (mthreads-gmi)", ["mthreads-gmi"], 30)
else:
print("\n" + "=" * 80)
print("WARNING: No supported device platform detected")
print("=" * 80)
def _cleanup_stale_device_locks() -> None:
"""Remove stale device-initialization lock files whose recorded PID is dead.
Lock files at ``/tmp/vllm_omni_device_*_init.lock`` may persist after a
crashed / killed test run and block subsequent orchestrator startups.
"""
import glob as _glob
for lock_file in _glob.glob("/tmp/vllm_omni_device_*_init.lock"):
try:
with open(lock_file) as fh:
content = fh.read().strip()
if not content:
continue
pid = int(content)
except (OSError, ValueError):
continue
try:
os.kill(pid, 0)
except ProcessLookupError:
print(f" Removing stale device lock {lock_file} (PID {pid} is dead)")
try:
os.unlink(lock_file)
except OSError:
pass
except PermissionError:
pass
def cleanup_test_environment(*, shutdown_ray: bool = False) -> None:
"""Tear down distributed state and reset device memory for tests."""
from vllm import envs
from vllm.distributed.parallel_state import (
destroy_distributed_environment,
destroy_model_parallel,
)
envs.disable_envs_cache()
if current_omni_platform.is_rocm():
from vllm._aiter_ops import rocm_aiter_ops
rocm_aiter_ops.refresh_env_variables()
gc.unfreeze()
destroy_model_parallel()
destroy_distributed_environment()
if shutdown_ray:
import ray
ray.shutdown()
print("Pre-test device status:")
_cleanup_stale_device_locks()
num_devices = current_omni_platform.device_count()
if num_devices > 0:
try:
wait_for_gpu_memory_to_clear(
devices=list(range(num_devices)),
threshold_ratio=0.05,
timeout_s=60,
)
except Exception as e:
print(f"Device cleanup note: {e}")
gc.collect()
if not current_omni_platform.is_cpu():
current_omni_platform.empty_cache()
try:
import torch
torch._C._host_emptyCache()
except AttributeError:
logger.warning("torch._C._host_emptyCache() only available in Pytorch >=2.5")
if current_omni_platform.is_available():
print("Post-test device status:")
_print_device_processes()
__all__ = [
"cleanup_test_environment",
"get_physical_device_indices",
"pick_least_used_device_indices",
"wait_for_gpu_memory_to_clear",
]
File diff suppressed because it is too large. Load diff
-297
View File
@@ -1,297 +0,0 @@
"""Test environment / lifecycle helpers (device cleanup hooks and memory monitoring for tests).
``vllm_omni.platforms`` is imported only inside functions that need it so importing this module
at pytest plugin load does not run before session autouse fixtures.
"""
from __future__ import annotations
import gc
import os
import subprocess
import threading
import time
from contextlib import contextmanager
import torch
from vllm_omni.platforms import current_omni_platform
def get_physical_device_indices(devices):
visible_devices = os.environ.get("CUDA_VISIBLE_DEVICES")
if visible_devices is None:
return devices
visible_indices = [int(x) for x in visible_devices.split(",")]
index_mapping = {i: physical for i, physical in enumerate(visible_indices)}
return [index_mapping[i] for i in devices if i in index_mapping]
def pick_least_used_device_indices(num_devices: int) -> list[int]:
"""Return physical device indices for the least-used accelerators.
Args:
num_devices: Number of device indices to return.
Returns:
Physical device indices sorted from least to most used memory.
"""
if num_devices <= 0:
raise ValueError(f"num_devices must be positive, got {num_devices}.")
logical_count = current_omni_platform.get_device_count()
if logical_count < num_devices:
raise RuntimeError(f"Need {num_devices} devices, but only {logical_count} are available.")
device_usage: list[tuple[int, int]] = []
for device_index in range(logical_count):
with current_omni_platform.device(device_index):
free_bytes, total_bytes = current_omni_platform.get_device_memory()
device_usage.append((total_bytes - free_bytes, device_index))
device_usage.sort()
logical_indices = [device_index for _, device_index in device_usage[:num_devices]]
return get_physical_device_indices(logical_indices)
def wait_for_gpu_memory_to_clear(
*,
devices: list[int],
threshold_bytes: int | None = None,
threshold_ratio: float | None = None,
timeout_s: float = 120,
) -> None:
assert threshold_bytes is not None or threshold_ratio is not None
devices = get_physical_device_indices(devices)
start_time = time.time()
device_list = ", ".join(str(d) for d in devices)
if threshold_bytes is not None:
condition_str = f"Memory usage ≤ {threshold_bytes / 2**30:.2f} GiB"
def is_free(used, total):
return used <= threshold_bytes / 2**30
else:
condition_str = f"Memory usage ratio ≤ {threshold_ratio * 100:.1f}%"
def is_free(used, total):
return used / total <= threshold_ratio
print(f"[GPU Memory Monitor] Waiting for GPU {device_list} to free memory, Condition: {condition_str}")
@contextmanager
def smi_scope():
if current_omni_platform.is_rocm():
from amdsmi import amdsmi_init, amdsmi_shut_down
amdsmi_init()
try:
yield
finally:
amdsmi_shut_down()
elif current_omni_platform.is_cuda():
from vllm.third_party.pynvml import nvmlInit, nvmlShutdown
nvmlInit()
try:
yield
finally:
nvmlShutdown()
else:
yield
def get_mem_gib(device: int) -> tuple[float, float]:
if current_omni_platform.is_rocm():
from amdsmi import amdsmi_get_gpu_vram_usage, amdsmi_get_processor_handles
info = amdsmi_get_gpu_vram_usage(amdsmi_get_processor_handles()[device])
return info["vram_used"] / 2**10, info["vram_total"] / 2**10
if current_omni_platform.is_npu():
free_bytes, total_bytes = torch.npu.mem_get_info(device)
return (total_bytes - free_bytes) / 2**30, total_bytes / 2**30
from vllm.third_party.pynvml import nvmlDeviceGetHandleByIndex, nvmlDeviceGetMemoryInfo
info = nvmlDeviceGetMemoryInfo(nvmlDeviceGetHandleByIndex(device))
return info.used / 2**30, info.total / 2**30
with smi_scope():
while True:
output_raw = {d: get_mem_gib(d) for d in devices}
output = {
d: f"{used:.1f}GiB/{total:.1f}GiB ({(used / total) * 100 if total > 0 else 0:.1f}%)"
for d, (used, total) in output_raw.items()
}
print("[GPU Memory Status] Current usage:")
for device_id, mem_info in output.items():
print(f" GPU {device_id}: {mem_info}")
dur_s = time.time() - start_time
if all(is_free(used, total) for used, total in output_raw.values()):
print(f"[GPU Memory Freed] Devices {device_list} meet memory condition")
print(f" Condition: {condition_str}")
print(f" Wait time: {dur_s:.1f} seconds ({dur_s / 60:.1f} minutes)")
break
if dur_s >= timeout_s:
raise ValueError(
f"[GPU Memory Timeout] Devices {device_list} still don't meet memory condition after {dur_s:.1f} seconds\n"
f"Condition: {condition_str}\n"
f"Current status:\n" + "\n".join(f" GPU {d}: {output[d]}" for d in devices)
)
gc.collect()
current_omni_platform.empty_cache()
time.sleep(5)
def _run_smi(label: str, cmd: list[str], head_lines: int, timeout: float = 5) -> None:
print("\n" + "=" * 80)
print(label)
print("=" * 80)
try:
result = subprocess.run(cmd, capture_output=True, text=True, timeout=timeout)
if result.returncode == 0 and result.stdout.strip():
lines = result.stdout.strip().split("\n")
for line in lines[:head_lines]:
print(line)
if len(lines) > head_lines:
print(f"... (showing first {head_lines} of {len(lines)} lines)")
else:
print(f"{cmd[0]} command failed or produced no output")
except (subprocess.TimeoutExpired, FileNotFoundError):
print(f"{cmd[0]} not available or timed out")
except Exception as e:
print(f"Error running {' '.join(cmd)}: {e}")
def _print_gpu_processes() -> None:
"""Print device information via nvidia-smi/npu-smi/amd-smi."""
from vllm_omni.platforms import current_omni_platform
if current_omni_platform.is_cuda():
_run_smi("NVIDIA GPU Information (nvidia-smi)", ["nvidia-smi"], 20)
_run_smi("Detailed GPU Processes (nvidia-smi pmon)", ["nvidia-smi", "pmon", "-c", "1"], 100, timeout=3)
elif current_omni_platform.is_npu():
_run_smi("Ascend NPU Information (npu-smi info)", ["npu-smi", "info"], 40)
elif current_omni_platform.is_rocm():
_run_smi("AMD GPU Information (amd-smi)", ["amd-smi"], 30)
_run_smi("Detailed AMD GPU Processes (amd-smi process)", ["amd-smi", "process"], 100, timeout=3)
else:
print("\n" + "=" * 80)
print("WARNING: No supported device platform detected")
print("=" * 80)
print("\n" + "=" * 80)
print("System Processes with GPU keywords")
print("=" * 80)
def _cleanup_stale_device_locks() -> None:
"""Remove stale device-initialization lock files whose recorded PID is dead.
Lock files at ``/tmp/vllm_omni_device_*_init.lock`` may persist after a
crashed / killed test run and block subsequent orchestrator startups.
"""
import glob as _glob
for lock_file in _glob.glob("/tmp/vllm_omni_device_*_init.lock"):
try:
with open(lock_file) as fh:
content = fh.read().strip()
if not content:
continue
pid = int(content)
except (OSError, ValueError):
continue
try:
os.kill(pid, 0)
except ProcessLookupError:
print(f" Removing stale device lock {lock_file} (PID {pid} is dead)")
try:
os.unlink(lock_file)
except OSError:
pass
except PermissionError:
pass
def run_pre_test_cleanup() -> None:
print("Pre-test GPU status:")
_cleanup_stale_device_locks()
num_gpus = current_omni_platform.device_count()
if num_gpus > 0:
try:
wait_for_gpu_memory_to_clear(
devices=list(range(num_gpus)),
threshold_ratio=0.05,
timeout_s=60,
)
except Exception as e:
print(f"Pre-test cleanup note: {e}")
def run_post_test_cleanup() -> None:
if current_omni_platform.is_available():
gc.collect()
current_omni_platform.empty_cache()
print("Post-test GPU status:")
_print_gpu_processes()
class DeviceMemoryMonitor:
"""Poll global device memory usage."""
def __init__(self, device_index: int, interval: float = 0.05):
self.device_index = device_index
self.interval = interval
self._peak_used_mb = 0.0
self._stop_event = threading.Event()
self._thread: threading.Thread | None = None
def start(self) -> None:
from vllm_omni.platforms import current_omni_platform
def monitor_loop() -> None:
while not self._stop_event.is_set():
try:
with current_omni_platform.device(self.device_index):
free_bytes, total_bytes = current_omni_platform.mem_get_info()
used_mb = (total_bytes - free_bytes) / (1024**2)
self._peak_used_mb = max(self._peak_used_mb, used_mb)
except Exception:
pass
time.sleep(self.interval)
self._thread = threading.Thread(target=monitor_loop, daemon=False)
self._thread.start()
def stop(self) -> None:
if self._thread is None:
return
self._stop_event.set()
self._thread.join(timeout=2.0)
@property
def peak_used_mb(self) -> float:
from vllm_omni.platforms import current_omni_platform
fallback_alloc = current_omni_platform.max_memory_allocated(device=self.device_index) / (1024**2)
fallback_reserved = current_omni_platform.max_memory_reserved(device=self.device_index) / (1024**2)
return max(self._peak_used_mb, fallback_alloc, fallback_reserved)
def __del__(self):
self.stop()
__all__ = [
"DeviceMemoryMonitor",
"get_physical_device_indices",
"pick_least_used_device_indices",
"run_post_test_cleanup",
"run_pre_test_cleanup",
"wait_for_gpu_memory_to_clear",
]
+49
View File
@@ -0,0 +1,49 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
"""Opt-in cleanup fixtures for GPU memory, pipeline registry, and speaker cache."""
from __future__ import annotations
import pytest
from vllm_omni.config.pipeline_registry import OMNI_PIPELINES
@pytest.fixture
def clean_gpu_memory_between_tests():
"""Opt-in GPU pre/post hooks for a test (no environment-variable gate).
Use as a test parameter or ``@pytest.mark.usefixtures("clean_gpu_memory_between_tests")``.
"""
from tests.helpers.clean import cleanup_test_environment
print("\n=== PRE-TEST DEVICE CLEANUP ===")
cleanup_test_environment()
yield
cleanup_test_environment()
@pytest.fixture
def clean_pipeline_registry():
"""Ensure the OMNI_PIPELINES are in a clean state for a test that mutates it."""
snapshot = dict(OMNI_PIPELINES)
yield
OMNI_PIPELINES.clear()
OMNI_PIPELINES.update(snapshot)
@pytest.fixture
def clean_speaker_cache():
"""Reset the process-wide speaker cache singleton before and after the test."""
import vllm_omni.utils.speaker_cache as _sc
def _reset():
with _sc._SINGLETON_LOCK:
if _sc._SINGLETON is not None:
_sc._SINGLETON.clear()
_sc._SINGLETON = None
_reset()
yield
_reset()
+42 -9
View File
@@ -1,14 +1,47 @@
"""Fixtures config creation & out of tree registry management."""
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
"""Session-scoped config and environment fixtures."""
from __future__ import annotations
import os
import pytest
from vllm_omni.config.pipeline_registry import OMNI_PIPELINES
import torch
from vllm.config import DeviceConfig, VllmConfig, set_current_vllm_config
@pytest.fixture
def clean_pipeline_registry():
"""Ensure the OMNI_PIPELINES are in a clean state for a test that mutates it."""
snapshot = dict(OMNI_PIPELINES)
@pytest.fixture(scope="session", autouse=True)
def default_env():
# Keep behavior but avoid import-time side effects (RFC #2299).
keys = ("VLLM_WORKER_MULTIPROC_METHOD", "VLLM_TARGET_DEVICE")
previous = {key: os.environ.get(key) for key in keys}
os.environ["VLLM_WORKER_MULTIPROC_METHOD"] = previous["VLLM_WORKER_MULTIPROC_METHOD"] or "spawn"
os.environ["VLLM_TARGET_DEVICE"] = previous["VLLM_TARGET_DEVICE"] or (
"cuda" if torch.cuda.is_available() and torch.accelerator.device_count() > 0 else "cpu"
)
yield
OMNI_PIPELINES.clear()
OMNI_PIPELINES.update(snapshot)
for key, value in previous.items():
if value is None:
os.environ.pop(key, None)
else:
os.environ[key] = value
@pytest.fixture(scope="session", autouse=True)
def default_vllm_config():
"""Set a default VllmConfig for the whole test session.
Session scope ensures module-scoped fixtures (e.g. ``omni_runner``) and
deferred imports of ``tests.helpers.runtime`` both see the same context.
Function-scoped autouse ran too late for ``OmniRunner`` setup and could
desynchronize vLLM init vs request preprocessing (e.g. renderer state).
"""
# Use CPU device if no GPU is available (e.g., in CI environments)
has_gpu = torch.cuda.is_available() and torch.accelerator.device_count() > 0
device = "cuda" if has_gpu else "cpu"
device_config = DeviceConfig(device=device)
with set_current_vllm_config(VllmConfig(device_config=device_config)):
yield
-74
View File
@@ -1,74 +0,0 @@
import os
import pytest
import torch
from vllm.distributed import parallel_state
@pytest.fixture(scope="session", autouse=True)
def default_env():
# Keep behavior but avoid import-time side effects (RFC #2299).
keys = ("VLLM_WORKER_MULTIPROC_METHOD", "VLLM_TARGET_DEVICE")
previous = {key: os.environ.get(key) for key in keys}
os.environ["VLLM_WORKER_MULTIPROC_METHOD"] = previous["VLLM_WORKER_MULTIPROC_METHOD"] or "spawn"
os.environ["VLLM_TARGET_DEVICE"] = previous["VLLM_TARGET_DEVICE"] or (
"cuda" if torch.cuda.is_available() and torch.accelerator.device_count() > 0 else "cpu"
)
yield
for key, value in previous.items():
if value is None:
os.environ.pop(key, None)
else:
os.environ[key] = value
@pytest.fixture(scope="session")
def model_prefix() -> str:
prefix = os.environ.get("MODEL_PREFIX", "")
return f"{prefix.rstrip('/')}/" if prefix else ""
@pytest.fixture
def clean_gpu_memory_between_tests():
"""Opt-in GPU pre/post hooks for a test (no environment-variable gate).
Use as a test parameter or ``@pytest.mark.usefixtures("clean_gpu_memory_between_tests")``.
"""
from tests.helpers.env import run_post_test_cleanup, run_pre_test_cleanup
print("\n=== PRE-TEST GPU CLEANUP ===")
run_pre_test_cleanup()
yield
run_post_test_cleanup()
@pytest.fixture(scope="session", autouse=True)
def default_vllm_config():
"""Set a default VllmConfig for the whole test session.
Session scope ensures module-scoped fixtures (e.g. ``omni_runner``) and
deferred imports of ``tests.helpers.runtime`` both see the same context.
Function-scoped autouse ran too late for ``OmniRunner`` setup and could
desynchronize vLLM init vs request preprocessing (e.g. renderer state).
"""
from vllm.config import DeviceConfig, VllmConfig, set_current_vllm_config
# Use CPU device if no GPU is available (e.g., in CI environments)
has_gpu = torch.cuda.is_available() and torch.accelerator.device_count() > 0
device = "cuda" if has_gpu else "cpu"
device_config = DeviceConfig(device=device)
with set_current_vllm_config(VllmConfig(device_config=device_config)):
yield
@pytest.fixture()
def init_fake_tp_group(mocker):
"""Provide a fake TP group so vllm linear layers can be instantiated."""
mock_tp = mocker.MagicMock()
mock_tp.world_size = 1
mock_tp.rank_in_group = 0
old = parallel_state._TP
parallel_state._TP = mock_tp
yield
parallel_state._TP = old
+49 -24
View File
@@ -1,10 +1,13 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
"""Runtime fixtures (OmniRunner / OmniServer). Imports are deferred to fixture time.
Loading ``tests.helpers.runtime`` at plugin import time (before session fixtures)
pulls in vLLM/vllm_omni too early and breaks initialization order vs the legacy
monolithic conftest. Defer imports until fixtures run so ``default_env`` /
``default_vllm_config`` run first. Implementation helpers live in
``tests.helpers.runtime`` (``iter_omni_server`` / ``iter_omni_runner``).
monolithic conftest. Defer imports until fixtures run so session fixtures in
``tests.helpers.fixtures.config`` run first. Server/runner helpers live in
``tests.helpers.runtime``; request clients live in ``tests.helpers.client``.
"""
from __future__ import annotations
@@ -25,15 +28,14 @@ omni_fixture_lock = threading.Lock()
def omni_server_function(
request: pytest.FixtureRequest,
run_level: str,
model_prefix: str,
) -> Generator[OmniServer, Any, None]:
from tests.helpers.runtime import iter_omni_server
yield from iter_omni_server(request, run_level, model_prefix, omni_fixture_lock)
yield from iter_omni_server(request, run_level, omni_fixture_lock)
@pytest.fixture(scope="module")
def omni_server(request: pytest.FixtureRequest, run_level: str, model_prefix: str) -> Generator[OmniServer, Any, None]:
def omni_server(request: pytest.FixtureRequest, run_level: str) -> Generator[OmniServer, Any, None]:
"""Start vLLM-Omni through the standard or stage-CLI launcher.
The fixture stays module-scoped because multi-stage initialization is costly.
@@ -42,16 +44,16 @@ def omni_server(request: pytest.FixtureRequest, run_level: str, model_prefix: st
"""
from tests.helpers.runtime import iter_omni_server
yield from iter_omni_server(request, run_level, model_prefix, omni_fixture_lock)
yield from iter_omni_server(request, run_level, omni_fixture_lock)
@pytest.fixture
def openai_client(request: pytest.FixtureRequest, run_level: str):
def online_client(request: pytest.FixtureRequest, run_level: str):
"""Resolve ``omni_server`` lazily so parametrized server fixtures work like upstream."""
from tests.helpers.runtime import OpenAIClientHandler
from tests.helpers.client import OnlineOmniClient
server = request.getfixturevalue("omni_server")
return OpenAIClientHandler(
return OnlineOmniClient(
host=server.host,
port=server.port,
api_key="EMPTY",
@@ -61,12 +63,18 @@ def openai_client(request: pytest.FixtureRequest, run_level: str):
@pytest.fixture
def openai_client_function(request: pytest.FixtureRequest, run_level: str):
def openai_client(online_client):
"""Historical alias for :func:`online_client`."""
return online_client
@pytest.fixture
def online_client_function(request: pytest.FixtureRequest, run_level: str):
"""Resolve ``omni_server_function`` lazily for function-scoped reliability tests."""
from tests.helpers.runtime import OpenAIClientHandler
from tests.helpers.client import OnlineOmniClient
server = request.getfixturevalue("omni_server_function")
return OpenAIClientHandler(
return OnlineOmniClient(
host=server.host,
port=server.port,
api_key="EMPTY",
@@ -75,10 +83,15 @@ def openai_client_function(request: pytest.FixtureRequest, run_level: str):
)
@pytest.fixture
def openai_client_function(online_client_function):
"""Historical alias for :func:`online_client_function`."""
return online_client_function
@pytest.fixture(scope="function")
def omni_runner_function(
request: pytest.FixtureRequest,
model_prefix: str,
run_level: str,
) -> Generator[OmniRunner, Any, None]:
"""Function-scoped :class:`~tests.helpers.runtime.OmniRunner` (cf. :func:`omni_server_function`).
@@ -88,30 +101,42 @@ def omni_runner_function(
"""
from tests.helpers.runtime import iter_omni_runner
yield from iter_omni_runner(request, model_prefix, run_level, omni_fixture_lock)
yield from iter_omni_runner(request, run_level, omni_fixture_lock)
@pytest.fixture(scope="module")
def omni_runner(request: pytest.FixtureRequest, model_prefix: str, run_level: str) -> Generator[OmniRunner, Any, None]:
def omni_runner(request: pytest.FixtureRequest, run_level: str) -> Generator[OmniRunner, Any, None]:
"""Module-scoped :class:`~tests.helpers.runtime.OmniRunner` (cf. :func:`omni_server`).
Reuses one runner for the whole module to amortize multi-stage init cost.
"""
from tests.helpers.runtime import iter_omni_runner
yield from iter_omni_runner(request, model_prefix, run_level, omni_fixture_lock)
yield from iter_omni_runner(request, run_level, omni_fixture_lock)
@pytest.fixture
def omni_runner_handler_function(omni_runner_function: OmniRunner):
"""Resolve :class:`~tests.helpers.runtime.OmniRunnerHandler` for :func:`omni_runner_function`."""
from tests.helpers.runtime import OmniRunnerHandler
def offline_client_function(omni_runner_function: OmniRunner):
"""Resolve :class:`~tests.helpers.client.OfflineOmniClient` for :func:`omni_runner_function`."""
from tests.helpers.client import OfflineOmniClient
return OmniRunnerHandler(omni_runner_function)
return OfflineOmniClient(omni_runner_function)
@pytest.fixture
def omni_runner_handler(omni_runner: OmniRunner):
from tests.helpers.runtime import OmniRunnerHandler
def omni_runner_handler_function(offline_client_function):
"""Historical alias for :func:`offline_client_function`."""
return offline_client_function
return OmniRunnerHandler(omni_runner)
@pytest.fixture
def offline_client(omni_runner: OmniRunner):
from tests.helpers.client import OfflineOmniClient
return OfflineOmniClient(omni_runner)
@pytest.fixture
def omni_runner_handler(offline_client):
"""Historical alias for :func:`offline_client`."""
return offline_client
-21
View File
@@ -1,21 +0,0 @@
"""Fixtures for the process-wide speaker cache singleton."""
from __future__ import annotations
import pytest
@pytest.fixture
def fresh_speaker_cache():
"""Reset the process-wide speaker cache singleton before and after the test."""
import vllm_omni.utils.speaker_cache as _sc
def _reset():
with _sc._SINGLETON_LOCK:
if _sc._SINGLETON is not None:
_sc._SINGLETON.clear()
_sc._SINGLETON = None
_reset()
yield
_reset()
+2 -4
View File
@@ -1,5 +1,5 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
"""Pytest marks and decorators for hardware / resource selection (CUDA, ROCm, …)."""
from __future__ import annotations
@@ -180,10 +180,8 @@ def hardware_marks(*, res: dict[str, str], num_cards: int | dict[str, int] = 1):
all_marks: list[pytest.MarkDecorator] = []
for platform, resource in res.items():
cards = num_cards_dict[platform]
if platform in ("cuda", "rocm", "xpu"):
if platform in ("cuda", "rocm", "xpu", "musa"):
marks = gpu_marks(res=resource, num_cards=cards)
elif platform == "musa":
marks = musa_marks(res=resource, num_cards=cards)
elif platform == "npu":
marks = npu_marks(res=resource, num_cards=cards)
else:
+52 -21
View File
@@ -1,3 +1,6 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
"""Synthetic media generation and media/text utilities for tests."""
import atexit
@@ -7,18 +10,20 @@ import hashlib
import io
import logging
import math
import mimetypes
import multiprocessing
import os
import random
import re
import subprocess
import sys
import tempfile
import threading
import time
from concurrent.futures.process import BrokenProcessPool
from contextlib import contextmanager
from pathlib import Path
from typing import Any
from typing import Any, Literal, TypedDict, overload
import numpy as np
import soundfile as sf
@@ -392,8 +397,16 @@ def generate_synthetic_video(
import cv2
import imageio
class _BouncingBall(TypedDict):
x: float
y: float
vx: float
vy: float
radius: int
color_bgr: tuple[int, int, int]
num_balls = random.randint(3, 8)
balls = []
balls: list[_BouncingBall] = []
for _ in range(num_balls):
radius = min(width, height) // 8
if radius < 1:
@@ -405,7 +418,16 @@ def generate_synthetic_video(
vx = speed * math.cos(angle)
vy = speed * math.sin(angle)
color_bgr = (random.randint(50, 255), random.randint(50, 255), random.randint(50, 255))
balls.append({"x": x, "y": y, "vx": vx, "vy": vy, "radius": radius, "color_bgr": color_bgr})
balls.append(
{
"x": float(x),
"y": float(y),
"vx": float(vx),
"vy": float(vy),
"radius": radius,
"color_bgr": color_bgr,
}
)
video_frames = []
for _ in range(num_frames):
@@ -525,27 +547,34 @@ _AUDIO_MIME_BY_SUFFIX = {
}
def get_asset_path(relative_path: str | os.PathLike) -> Path:
"""Resolve a path under ``tests/assets/`` to its absolute on-disk location."""
return _TEST_ASSETS_ROOT / Path(relative_path)
def _asset_mime_type(path: Path) -> str:
mime = _AUDIO_MIME_BY_SUFFIX.get(path.suffix.lower())
if mime is not None:
return mime
guessed, _ = mimetypes.guess_type(path.name)
return guessed or "application/octet-stream"
def load_test_audio_data_url(relative_path: str | os.PathLike) -> str:
"""Load a vendored test audio file under ``tests/assets/`` as a base64 data URL.
@overload
def get_asset_path(relative_path: str | os.PathLike, *, as_data_url: Literal[False] = False) -> Path: ...
Used by tests that need real reference audio (e.g. voice cloning) without
relying on the server's ability to fetch external URLs at request time.
@overload
def get_asset_path(relative_path: str | os.PathLike, *, as_data_url: Literal[True]) -> str: ...
def get_asset_path(relative_path: str | os.PathLike, *, as_data_url: bool = False) -> Path | str:
"""Resolve a path under ``tests/assets/``.
When ``as_data_url`` is true, read the file and return a base64 data URL
suitable for embedding reference media in API requests without external URLs.
"""
path = get_asset_path(relative_path)
mime = _AUDIO_MIME_BY_SUFFIX.get(path.suffix.lower(), "application/octet-stream")
path = _TEST_ASSETS_ROOT / Path(relative_path)
if not as_data_url:
return path
encoded = base64.b64encode(path.read_bytes()).decode("ascii")
return f"data:{mime};base64,{encoded}"
def decode_b64_image(b64: str):
img = Image.open(io.BytesIO(base64.b64decode(b64)))
img.load()
return img
return f"data:{_asset_mime_type(path)};base64,{encoded}"
def concat_audio(audio_val) -> np.ndarray:
@@ -658,6 +687,10 @@ def _merge_base64_audio_to_segment(base64_list: list[str]) -> _AudioBuffer:
@contextmanager
def _serialize_whisper_model_download(model_size: str = "small"):
"""Serialize Whisper cache writes across processes (Linux/Unix), per model."""
if sys.platform == "win32":
yield
return
import fcntl
lock_path = Path.home() / ".cache" / "whisper" / f".{model_size}_model_download.lock"
@@ -836,12 +869,10 @@ __all__ = [
"convert_audio_bytes_to_text",
"convert_audio_file_to_text",
"cosine_similarity_text",
"decode_b64_image",
"generate_synthetic_audio",
"generate_synthetic_image",
"generate_synthetic_video",
"get_asset_path",
"load_test_audio_data_url",
"preprocess_text",
"release_audio_transcriber",
]
+55
View File
@@ -0,0 +1,55 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
"""Device memory monitoring helpers for tests."""
from __future__ import annotations
import threading
import time
from vllm_omni.platforms import current_omni_platform
class DeviceMemoryMonitor:
"""Poll global device memory usage."""
def __init__(self, device_index: int, interval: float = 0.05):
self.device_index = device_index
self.interval = interval
self._peak_used_mb = 0.0
self._stop_event = threading.Event()
self._thread: threading.Thread | None = None
def start(self) -> None:
def monitor_loop() -> None:
while not self._stop_event.is_set():
try:
with current_omni_platform.device(self.device_index):
free_bytes, total_bytes = current_omni_platform.mem_get_info()
used_mb = (total_bytes - free_bytes) / (1024**2)
self._peak_used_mb = max(self._peak_used_mb, used_mb)
except Exception:
pass
time.sleep(self.interval)
self._thread = threading.Thread(target=monitor_loop, daemon=False)
self._thread.start()
def stop(self) -> None:
if self._thread is None:
return
self._stop_event.set()
self._thread.join(timeout=2.0)
@property
def peak_used_mb(self) -> float:
fallback_alloc = current_omni_platform.max_memory_allocated(device=self.device_index) / (1024**2)
fallback_reserved = current_omni_platform.max_memory_reserved(device=self.device_index) / (1024**2)
return max(self._peak_used_mb, fallback_alloc, fallback_reserved)
def __del__(self):
self.stop()
__all__ = ["DeviceMemoryMonitor"]
-134
View File
@@ -1,134 +0,0 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
import functools
import os
import signal
import subprocess
import sys
import tempfile
from collections.abc import Callable
from contextlib import ExitStack, suppress
from typing import Any, Literal
import cloudpickle
from typing_extensions import ParamSpec
from vllm.platforms import current_platform
_P = ParamSpec("_P")
def fork_new_process_for_each_test(func: Callable[_P, None]) -> Callable[_P, None]:
"""Decorator to fork a new process for each test function."""
@functools.wraps(func)
def wrapper(*args: _P.args, **kwargs: _P.kwargs) -> None:
os.setpgrp()
from _pytest.outcomes import Skipped
with (
tempfile.NamedTemporaryFile(
delete=False, mode="w+b", prefix=f"vllm_test_{func.__name__}_{os.getpid()}_", suffix=".exc"
) as exc_file,
ExitStack() as delete_after,
):
exc_file_path = exc_file.name
delete_after.callback(os.remove, exc_file_path)
pid = os.fork()
if pid == 0:
delete_after.pop_all()
try:
func(*args, **kwargs)
except Skipped as e:
print(str(e))
os._exit(0)
except Exception as e:
import traceback
tb_string = traceback.format_exc()
exc_to_serialize: dict[str, Any]
try:
exc_to_serialize = {"pickled_exception": e}
cloudpickle.dumps(exc_to_serialize)
except (Exception, KeyboardInterrupt):
exc_to_serialize = {
"exception_type": type(e).__name__,
"exception_msg": str(e),
"traceback": tb_string,
}
try:
with open(exc_file_path, "wb") as f:
cloudpickle.dump(exc_to_serialize, f)
except Exception:
print(tb_string)
os._exit(1)
else:
os._exit(0)
else:
pgid = os.getpgid(pid)
_pid, _exitcode = os.waitpid(pid, 0)
old_signal_handler = signal.signal(signal.SIGTERM, signal.SIG_IGN)
os.killpg(pgid, signal.SIGTERM)
signal.signal(signal.SIGTERM, old_signal_handler)
if _exitcode != 0:
exc_info = {}
if os.path.exists(exc_file_path):
with suppress(Exception), open(exc_file_path, "rb") as f:
exc_info = cloudpickle.load(f)
if (original_exception := exc_info.get("pickled_exception")) is not None:
assert isinstance(original_exception, Exception)
raise original_exception
if (original_tb := exc_info.get("traceback")) is not None:
raise AssertionError(
f"Test {func.__name__} failed when called with args {args} and kwargs {kwargs}"
f" (exit code: {_exitcode}):\n{original_tb}"
) from None
raise AssertionError(
f"function {func.__name__} failed when called with args {args} and kwargs {kwargs}"
f" (exit code: {_exitcode})"
) from None
return wrapper
def spawn_new_process_for_each_test(f: Callable[_P, None]) -> Callable[_P, None]:
"""Decorator to spawn a new process for each test function."""
@functools.wraps(f)
def wrapper(*args: _P.args, **kwargs: _P.kwargs) -> None:
if os.environ.get("RUNNING_IN_SUBPROCESS") == "1":
return f(*args, **kwargs)
import torch.multiprocessing as mp
with suppress(RuntimeError):
mp.set_start_method("spawn")
module_name = f.__module__
env = os.environ.copy()
env["RUNNING_IN_SUBPROCESS"] = "1"
with tempfile.TemporaryDirectory() as tempdir:
output_filepath = os.path.join(tempdir, "new_process.tmp")
input_bytes = cloudpickle.dumps((f, output_filepath))
cmd = [sys.executable, "-m", f"{module_name}"]
returned = subprocess.run(cmd, input=input_bytes, capture_output=True, env=env)
try:
returned.check_returncode()
except Exception as e:
raise RuntimeError(f"Error raised in subprocess:\n{returned.stderr.decode()}") from e
return wrapper
def create_new_process_for_each_test(
method: Literal["spawn", "fork"] | None = None,
) -> Callable[[Callable[_P, None]], Callable[_P, None]]:
"""Creates a decorator that runs each test function in a new process."""
if method is None:
use_spawn = current_platform.is_xpu()
method = "spawn" if use_spawn else "fork"
assert method in ["spawn", "fork"], "Method must be either 'spawn' or 'fork'"
if method == "fork":
return fork_new_process_for_each_test
return spawn_new_process_for_each_test
+121 -2352
View File
File diff suppressed because it is too large. Load diff
+68 -51
View File
@@ -1,3 +1,6 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
"""Config/message construction helpers used by tests."""
import atexit
@@ -11,8 +14,8 @@ import yaml
def modify_stage_config(
yaml_path: str,
updates: dict[str, Any] = None,
deletes: dict[str, Any] = None,
updates: dict[str, Any] | None = None,
deletes: dict[str, Any] | None = None,
) -> str:
"""
Modify configurations in a YAML file, supporting both top-level and stage-specific modifications,
@@ -23,17 +26,17 @@ def modify_stage_config(
updates: Dictionary containing both top-level and stage-specific modifications to add or update.
Format: {
'async_chunk': True,
'stage_args': {
0: {'engine_args.max_model_len': 5800},
1: {'engine_args.max_num_seqs': 2}
'stages': {
0: {'max_model_len': 5800},
1: {'max_num_seqs': 2}
}
}
deletes: Dictionary containing configurations to delete.
Format: {
'old_config': None, # Delete entire key
'stage_args': {
0: ['engine_args.old_param'],
1: ['runtime.unused_setting']
'stages': {
0: ['old_param'],
1: ['unused_setting']
}
}
@@ -164,24 +167,20 @@ def modify_stage_config(
else:
print(f"Path {path} does not exist")
_stage_key = "stages" if "stages" in config else "stage_args"
stages = get_stage_entries(config)
# Apply deletions first
if deletes:
for key, value in deletes.items():
if key in ("stage_args", "stages"):
if key == "stages":
if value and isinstance(value, dict):
stage_args = config.get(_stage_key, [])
if not stage_args:
raise ValueError("stage_args does not exist in config")
for stage_id, delete_paths in value.items():
if not delete_paths:
continue
# Find stage by ID
target_stage = None
for stage in stage_args:
for stage in stages:
if stage.get("stage_id") == int(stage_id):
target_stage = stage
break
@@ -204,28 +203,24 @@ def modify_stage_config(
# Apply updates
if updates:
for key, value in updates.items():
if key in ("stage_args", "stages"):
if key == "stages":
if value and isinstance(value, dict):
stage_args = config.get(_stage_key, [])
if not stage_args:
raise ValueError("stage_args does not exist in config")
for stage_id, stage_updates in value.items():
# Find stage by ID
target_stage = None
for stage in stage_args:
for stage in stages:
if stage.get("stage_id") == int(stage_id):
target_stage = stage
break
if target_stage is None:
available_ids = [s.get("stage_id") for s in stage_args if "stage_id" in s]
available_ids = [s.get("stage_id") for s in stages if "stage_id" in s]
raise KeyError(f"Stage ID {stage_id} not found, available: {available_ids}")
# Apply updates to this stage
for update_path, val in stage_updates.items():
# Check if this is a simple key (not dot-separated)
# Example: 'engine_input_source' vs 'engine_args.max_model_len'
# Example: 'engine_input_source' vs nested 'default_sampling_params.max_tokens'
if "." not in update_path:
# Direct key assignment (e.g., updating a list value)
target_stage[update_path] = val
@@ -624,6 +619,36 @@ def _materialize_ci_overlay(model_type: str) -> Path:
return out
def get_stage_entries(cfg: dict[str, Any]) -> list[dict[str, Any]]:
"""Return the ``stages`` list from a new-schema deploy config dict.
Raises ``ValueError`` when the top-level ``stages`` key is missing.
"""
stages = cfg.get("stages")
if stages is None:
raise ValueError("Deploy config missing top-level 'stages'")
return list(stages)
def load_stage_ids(resolved_config: dict[str, Any]) -> list[int]:
"""Return ``stage_id`` values from a resolved deploy config dict."""
stage_ids = [stage["stage_id"] for stage in get_stage_entries(resolved_config) if "stage_id" in stage]
if not stage_ids:
raise ValueError("No stage IDs found in resolved config")
return stage_ids
def load_stage_replica_counts(resolved_config: dict[str, Any]) -> dict[int, int]:
"""Return ``{stage_id: num_replicas}`` from flat stage entries (default 1)."""
replica_counts: dict[int, int] = {}
for stage in get_stage_entries(resolved_config):
stage_id = stage.get("stage_id")
if stage_id is None:
continue
replica_counts[int(stage_id)] = max(1, int(stage.get("num_replicas", 1)))
return replica_counts
def get_deploy_config_path(rel_path: str) -> str:
"""Resolve a deploy yaml; ``ci/<model>.yaml`` materializes from ``_CI_OVERLAYS``."""
if rel_path.startswith("ci/") and rel_path.endswith(".yaml"):
@@ -638,33 +663,20 @@ def get_deploy_config_stage(rel_path: str, stage_id: int) -> dict[str, Any]:
with open(get_deploy_config_path(rel_path), encoding="utf-8") as f:
cfg = yaml.safe_load(f) or {}
stage_key = "stages" if "stages" in cfg else "stage_args"
for stage in cfg.get(stage_key, []):
for stage in get_stage_entries(cfg):
if stage.get("stage_id") == stage_id:
return stage
raise KeyError(f"No stage_id={stage_id} in deploy config {rel_path!r}")
def _get_config_value_by_path(config_dict: dict, path: str) -> Any:
"""Read a dot-separated path from a nested dict (e.g. ``engine_args.load_format``)."""
current: Any = config_dict
for key in path.split("."):
if not isinstance(current, dict) or key not in current:
return None
current = current[key]
return current
def _stage_load_format_paths(stage_config_path: str) -> tuple[str, str, list[int]]:
"""Return ``(stage_key, load_format_field_path, stage_ids)`` for a deploy YAML."""
def _stage_ids_from_deploy_yaml(stage_config_path: str) -> list[int]:
"""Return ``stage_id`` values from a new-schema deploy YAML (``stages``)."""
with open(stage_config_path, encoding="utf-8") as f:
cfg = yaml.safe_load(f) or {}
new_schema_stages = cfg.get("stages")
stage_key = "stages" if new_schema_stages is not None else "stage_args"
load_format_path = "load_format" if new_schema_stages is not None else "engine_args.load_format"
stage_entries = cfg.get(stage_key, [])
stage_ids = [stage["stage_id"] for stage in stage_entries if "stage_id" in stage]
return stage_key, load_format_path, stage_ids
try:
return load_stage_ids(cfg)
except ValueError as exc:
raise ValueError(f"Deploy YAML missing top-level 'stages': {stage_config_path}") from exc
def _add_dummy_load_format(
@@ -674,10 +686,10 @@ def _add_dummy_load_format(
"""For ``core_model`` runs, patch every stage in the deploy YAML to ``load_format: dummy``."""
if run_level != "core_model" or stage_config_path is None:
return stage_config_path
stage_key, load_format_path, stage_ids = _stage_load_format_paths(stage_config_path)
stage_ids = _stage_ids_from_deploy_yaml(stage_config_path)
return modify_stage_config(
stage_config_path,
updates={stage_key: {stage_id: {load_format_path: "dummy"} for stage_id in stage_ids}},
updates={"stages": {stage_id: {"load_format": "dummy"} for stage_id in stage_ids}},
)
@@ -688,25 +700,27 @@ def _delete_dummy_load_format(
"""For ``advanced_model`` / ``full_model``, strip ``load_format: dummy`` so real weights load."""
if run_level not in {"advanced_model", "full_model"} or stage_config_path is None:
return stage_config_path
stage_key, load_format_path, _stage_ids = _stage_load_format_paths(stage_config_path)
with open(stage_config_path, encoding="utf-8") as f:
cfg = yaml.safe_load(f) or {}
stage_entries = cfg.get(stage_key, [])
try:
stages = get_stage_entries(cfg)
except ValueError as exc:
raise ValueError(f"Deploy YAML missing top-level 'stages': {stage_config_path}") from exc
deletes: dict[int, list[str]] = {}
for stage in stage_entries:
for stage in stages:
stage_id = stage.get("stage_id")
if stage_id is None:
continue
if _get_config_value_by_path(stage, load_format_path) == "dummy":
deletes[stage_id] = [load_format_path]
if stage.get("load_format") == "dummy":
deletes[stage_id] = ["load_format"]
if not deletes:
return stage_config_path
return modify_stage_config(
stage_config_path,
deletes={stage_key: deletes},
deletes={"stages": deletes},
)
@@ -720,6 +734,9 @@ def stage_config_path_for_run_level(stage_config_path: str | None, run_level: st
__all__ = [
"get_deploy_config_path",
"get_deploy_config_stage",
"get_stage_entries",
"load_stage_ids",
"load_stage_replica_counts",
"modify_stage_config",
"stage_config_path_for_run_level",
]
@@ -0,0 +1,40 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
"""Tests for OmniRunner startup rollback."""
from __future__ import annotations
import pytest
from tests.helpers.runtime import OmniRunner
pytestmark = [pytest.mark.core_model, pytest.mark.cpu]
def test_runner_init_rolls_back_on_omni_startup_failure(monkeypatch: pytest.MonkeyPatch) -> None:
"""``__exit__`` is skipped when construction raises; rollback must still run."""
cleaned: list[str] = []
monkeypatch.setattr(
"tests.helpers.runtime.cleanup_test_environment",
lambda: cleaned.append("env"),
)
monkeypatch.setattr(
OmniRunner,
"_cleanup_process",
lambda self: cleaned.append("process"),
)
class _BoomOmni:
def __init__(self, *args, **kwargs) -> None:
raise RuntimeError("Orchestrator initialization failed")
monkeypatch.setattr("vllm_omni.entrypoints.omni.Omni", _BoomOmni)
with pytest.raises(RuntimeError, match="Orchestrator initialization failed"):
with OmniRunner("fake-model"):
raise AssertionError("context body must not run after a failed constructor")
assert cleaned.count("process") == 1
# Once at the start of ``__init__``, once from the constructor rollback.
assert cleaned.count("env") == 2
+43 -14
View File
@@ -1,8 +1,12 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
"""Tests for OmniServerStageCli process planning helpers."""
from __future__ import annotations
from pathlib import Path
from types import SimpleNamespace
import pytest
@@ -38,23 +42,48 @@ def test_stage_cli_builds_headless_replica_cmd(tmp_path: Path) -> None:
assert cmd[cmd.index("--deploy-config") + 1] == server.stage_config_path
def test_stage_cli_splits_devices_per_replica(tmp_path: Path) -> None:
def test_stage_cli_loads_stage_ids_and_replica_counts(tmp_path: Path) -> None:
server = OmniServerStageCli("fake-model", _write_stage_config(tmp_path), [])
assert server._devices_for_replica(1, "1,2,3", 0) == "1"
assert server._devices_for_replica(1, "1,2,3", 1) == "2"
assert server._devices_for_replica(1, "1,2,3", 2) == "3"
assert server.stage_ids == [0, 1]
assert server.stage_replica_counts == {0: 1, 1: 3}
def test_stage_cli_maps_visible_devices_after_replica_split(tmp_path: Path) -> None:
server = OmniServerStageCli(
"fake-model",
_write_stage_config(tmp_path),
[],
env_dict={"CUDA_VISIBLE_DEVICES": "4,5,6,7"},
)
env: dict[str, str] = {}
def test_stage_cli_enter_rolls_back_launched_stages_on_startup_failure(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""``__exit__`` is skipped when ``__enter__`` raises; rollback must still run."""
monkeypatch.setattr("tests.helpers.runtime.time.sleep", lambda *_args, **_kwargs: None)
monkeypatch.setattr("tests.helpers.runtime.cleanup_test_environment", lambda: None)
server._set_stage_device_env(1, env, "1,2,3", replica_id=1)
server = OmniServerStageCli("fake-model", _write_stage_config(tmp_path), ["--disable-log-stats"])
log_path = tmp_path / "stage0.log"
log_fh = open(log_path, "w", encoding="utf-8")
log_fh.write("partial launch\n")
log_fh.flush()
assert env["CUDA_VISIBLE_DEVICES"] == "6"
proc = SimpleNamespace(pid=4242, poll=lambda: None)
killed: list[int] = []
monkeypatch.setattr(server, "_kill_process_tree", lambda pid: killed.append(pid))
def _fake_launch(stage_id: int, *, headless: bool, replica_id: int = 0) -> None:
stage_key = (stage_id, replica_id)
server.stage_procs[stage_key] = proc # type: ignore[assignment]
server._stage_log_paths[stage_key] = log_path
server._stage_log_files[stage_key] = log_fh
if stage_id == 0 and replica_id == 0:
server.proc = proc # type: ignore[assignment]
def _fail_alive() -> None:
raise RuntimeError("Stage 0 replica 0 exited with code 1")
monkeypatch.setattr(server, "_launch_stage", _fake_launch)
monkeypatch.setattr(server, "_ensure_stage_processes_alive", _fail_alive)
with pytest.raises(RuntimeError, match="exited with code 1"):
with server:
raise AssertionError("context body must not run after a failed enter")
assert killed == [4242]
assert log_fh.closed
assert not log_path.exists()
@@ -1,5 +1,5 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
"""The server/runner fixtures free the Whisper judge around each instance.
@@ -48,7 +48,7 @@ def _server_generator():
param=OmniServerParams(model="fake-model"),
node=SimpleNamespace(get_closest_marker=lambda name: None),
)
return runtime.iter_omni_server(request, "core_model", "", threading.Lock())
return runtime.iter_omni_server(request, "core_model", threading.Lock())
def test_releases_before_construction_and_after_teardown(events):