Merge origin/main into dev/vllm-align; resolve omni_stage.py conflicts
Co-authored-by: Cursor <cursoragent@cursor.com> Signed-off-by: tzhouam <tzhouam@connect.ust.hk>
This commit is contained in:
commit
03c6a58f69
18 files changed
+761
-381
No files matched your search
@@ -0,0 +1,102 @@
|
||||
"""Base worker class for vLLM-Omni with process-scoped GPU memory accounting."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
|
||||
import torch
|
||||
from vllm.logger import init_logger
|
||||
from vllm.utils.mem_utils import format_gib, memory_profiling
|
||||
from vllm.v1.worker.gpu_worker import Worker as GPUWorker
|
||||
|
||||
from vllm_omni.worker.gpu_memory_utils import (
|
||||
get_process_gpu_memory,
|
||||
)
|
||||
|
||||
logger = init_logger(__name__)
|
||||
|
||||
|
||||
class OmniGPUWorkerBase(GPUWorker):
|
||||
"""Base GPU worker for vLLM-Omni with process-scoped memory accounting.
|
||||
|
||||
This class overrides determine_available_memory() to use per-process GPU
|
||||
memory tracking via pynvml, allowing multiple stages to initialize
|
||||
concurrently on the same GPU without memory accounting interference.
|
||||
"""
|
||||
|
||||
@torch.inference_mode()
|
||||
def determine_available_memory(self) -> int:
|
||||
"""Process-scoped GPU memory profiling for concurrent stage initialization.
|
||||
|
||||
Algorithm:
|
||||
1. requested_memory = total_gpu_memory * gpu_memory_utilization
|
||||
(computed in init_device from cache_config)
|
||||
|
||||
2. process_memory = memory used by THIS process only (via pynvml)
|
||||
- Uses nvmlDeviceGetComputeRunningProcesses to get per-PID memory
|
||||
- Supports CUDA_VISIBLE_DEVICES with indices, UUIDs, or MIG IDs
|
||||
|
||||
3. available_kv_cache = requested_memory - process_memory
|
||||
|
||||
Fallback:
|
||||
If NVML is unavailable, falls back to profiling data:
|
||||
available = requested - (weights + activations + non_torch)
|
||||
"""
|
||||
if kv_cache_memory_bytes := self.cache_config.kv_cache_memory_bytes:
|
||||
self.model_runner.profile_run()
|
||||
logger.info(
|
||||
"Using explicit kv_cache_memory_bytes: %s GiB",
|
||||
format_gib(kv_cache_memory_bytes),
|
||||
)
|
||||
return kv_cache_memory_bytes
|
||||
|
||||
with memory_profiling(
|
||||
self.init_snapshot,
|
||||
weights_memory=int(self.model_runner.model_memory_usage),
|
||||
) as profile_result:
|
||||
self.model_runner.profile_run()
|
||||
|
||||
self.non_torch_memory = profile_result.non_torch_increase
|
||||
self.peak_activation_memory = profile_result.torch_peak_increase
|
||||
|
||||
process_memory = get_process_gpu_memory(self.local_rank)
|
||||
|
||||
if process_memory is not None:
|
||||
# NVML available: use per-process memory
|
||||
self.available_kv_cache_memory_bytes = max(0, self.requested_memory - process_memory)
|
||||
logger.debug(
|
||||
"Process-scoped memory (PID %d, GPU %d): requested=%s, used=%s, available=%s",
|
||||
os.getpid(),
|
||||
self.local_rank,
|
||||
format_gib(self.requested_memory),
|
||||
format_gib(process_memory),
|
||||
format_gib(self.available_kv_cache_memory_bytes),
|
||||
)
|
||||
logger.info_once(
|
||||
"Available KV cache memory: %s GiB (process-scoped)",
|
||||
format_gib(self.available_kv_cache_memory_bytes),
|
||||
scope="local",
|
||||
)
|
||||
else:
|
||||
# NVML unavailable: use profiling data as conservative fallback
|
||||
profiled_usage = (
|
||||
int(self.model_runner.model_memory_usage)
|
||||
+ profile_result.torch_peak_increase
|
||||
+ profile_result.non_torch_increase
|
||||
)
|
||||
self.available_kv_cache_memory_bytes = max(0, self.requested_memory - profiled_usage)
|
||||
logger.debug(
|
||||
"Profiling fallback (PID %d, GPU %d): requested=%s, profiled=%s, available=%s",
|
||||
os.getpid(),
|
||||
self.local_rank,
|
||||
format_gib(self.requested_memory),
|
||||
format_gib(profiled_usage),
|
||||
format_gib(self.available_kv_cache_memory_bytes),
|
||||
)
|
||||
logger.info_once(
|
||||
"Available KV cache memory: %s GiB (profiling fallback)",
|
||||
format_gib(self.available_kv_cache_memory_bytes),
|
||||
scope="local",
|
||||
)
|
||||
|
||||
return int(self.available_kv_cache_memory_bytes)
|
||||
@@ -7,18 +7,18 @@ from vllm.platforms import current_platform
|
||||
from vllm.utils.mem_utils import MemorySnapshot, format_gib
|
||||
from vllm.utils.torch_utils import set_random_seed
|
||||
from vllm.v1.utils import report_usage_stats
|
||||
from vllm.v1.worker.gpu_worker import Worker as GPUWorker
|
||||
from vllm.v1.worker.gpu_worker import init_worker_distributed_environment
|
||||
from vllm.v1.worker.utils import request_memory
|
||||
from vllm.v1.worker.workspace import init_workspace_manager
|
||||
|
||||
from vllm_omni.worker.base import OmniGPUWorkerBase
|
||||
from vllm_omni.worker.gpu_ar_model_runner import GPUARModelRunner
|
||||
from vllm_omni.worker.mixins import OmniWorkerMixin
|
||||
|
||||
logger = init_logger(__name__)
|
||||
|
||||
|
||||
class GPUARWorker(OmniWorkerMixin, GPUWorker):
|
||||
class GPUARWorker(OmniWorkerMixin, OmniGPUWorkerBase):
|
||||
"""GPU worker for autoregressive omni model stages.
|
||||
|
||||
Extends the base GPUWorker to initialize and manage autoregressive
|
||||
|
||||
@@ -7,18 +7,18 @@ from vllm.platforms import current_platform
|
||||
from vllm.utils.mem_utils import MemorySnapshot, format_gib
|
||||
from vllm.utils.torch_utils import set_random_seed
|
||||
from vllm.v1.utils import report_usage_stats
|
||||
from vllm.v1.worker.gpu_worker import Worker as GPUWorker
|
||||
from vllm.v1.worker.gpu_worker import init_worker_distributed_environment
|
||||
from vllm.v1.worker.utils import request_memory
|
||||
from vllm.v1.worker.workspace import init_workspace_manager
|
||||
|
||||
from vllm_omni.worker.base import OmniGPUWorkerBase
|
||||
from vllm_omni.worker.gpu_generation_model_runner import GPUGenerationModelRunner
|
||||
from vllm_omni.worker.mixins import OmniWorkerMixin
|
||||
|
||||
logger = init_logger(__name__)
|
||||
|
||||
|
||||
class GPUGenerationWorker(OmniWorkerMixin, GPUWorker):
|
||||
class GPUGenerationWorker(OmniWorkerMixin, OmniGPUWorkerBase):
|
||||
"""GPU Worker for Generation model (non-autoregressive waveform generation).
|
||||
|
||||
Usage in stage config:
|
||||
|
||||
@@ -0,0 +1,124 @@
|
||||
"""NVML-based per-process GPU memory utilities.
|
||||
|
||||
Shared across worker types (OmniGPUWorkerBase, DiffusionWorker, etc.)
|
||||
for process-scoped GPU memory accounting.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
|
||||
from vllm.logger import init_logger
|
||||
from vllm.third_party.pynvml import (
|
||||
nvmlDeviceGetComputeRunningProcesses,
|
||||
nvmlDeviceGetHandleByIndex,
|
||||
nvmlInit,
|
||||
nvmlShutdown,
|
||||
)
|
||||
|
||||
logger = init_logger(__name__)
|
||||
|
||||
|
||||
def is_process_scoped_memory_available() -> bool:
|
||||
"""Check if NVML process-scoped memory tracking is available.
|
||||
|
||||
When True, concurrent stage initialization is safe because each
|
||||
process can accurately measure its own GPU memory via NVML.
|
||||
When False, sequential initialization (file locks) is needed.
|
||||
"""
|
||||
try:
|
||||
nvmlInit()
|
||||
nvmlShutdown()
|
||||
return True
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
|
||||
def parse_cuda_visible_devices() -> list[str | int]:
|
||||
"""Parse CUDA_VISIBLE_DEVICES into a list of device identifiers.
|
||||
|
||||
Returns list of integers (physical indices) or strings (UUIDs/MIG IDs).
|
||||
"""
|
||||
visible_devices = os.environ.get("CUDA_VISIBLE_DEVICES", "")
|
||||
if not visible_devices:
|
||||
return []
|
||||
|
||||
result: list[str | int] = []
|
||||
for item in visible_devices.split(","):
|
||||
item = item.strip()
|
||||
if not item:
|
||||
continue
|
||||
try:
|
||||
result.append(int(item))
|
||||
except ValueError:
|
||||
# UUID (GPU-xxx) or MIG ID (MIG-xxx)
|
||||
result.append(item)
|
||||
return result
|
||||
|
||||
|
||||
def get_device_handle(device_id: str | int):
|
||||
"""Get NVML device handle by index or UUID."""
|
||||
if isinstance(device_id, int):
|
||||
return nvmlDeviceGetHandleByIndex(device_id)
|
||||
else:
|
||||
from vllm.third_party.pynvml import nvmlDeviceGetHandleByUUID
|
||||
|
||||
return nvmlDeviceGetHandleByUUID(device_id)
|
||||
|
||||
|
||||
def get_process_gpu_memory(local_rank: int) -> int | None:
|
||||
"""Get GPU memory used by current process via pynvml.
|
||||
|
||||
Supports CUDA_VISIBLE_DEVICES with integer indices, UUIDs, or MIG IDs.
|
||||
|
||||
Returns:
|
||||
Memory in bytes used by this process, or None if NVML unavailable.
|
||||
|
||||
Raises:
|
||||
RuntimeError: If device validation fails (invalid index or UUID).
|
||||
"""
|
||||
from vllm.third_party.pynvml import nvmlDeviceGetCount
|
||||
|
||||
my_pid = os.getpid()
|
||||
visible_devices = parse_cuda_visible_devices()
|
||||
|
||||
try:
|
||||
nvmlInit()
|
||||
except Exception as e:
|
||||
logger.warning("NVML init failed, will use profiling fallback: %s", e)
|
||||
return None
|
||||
|
||||
try:
|
||||
if visible_devices and local_rank < len(visible_devices):
|
||||
device_id = visible_devices[local_rank]
|
||||
try:
|
||||
handle = get_device_handle(device_id)
|
||||
except Exception as e:
|
||||
raise RuntimeError(
|
||||
f"Failed to get NVML handle for device '{device_id}' (local_rank={local_rank}). "
|
||||
f"Check CUDA_VISIBLE_DEVICES or stage config 'devices' setting."
|
||||
) from e
|
||||
else:
|
||||
# No CUDA_VISIBLE_DEVICES or local_rank out of range: use index directly
|
||||
device_count = nvmlDeviceGetCount()
|
||||
if local_rank >= device_count:
|
||||
raise RuntimeError(
|
||||
f"Invalid GPU device {local_rank}. Only {device_count} GPU(s) available. "
|
||||
f"Check CUDA_VISIBLE_DEVICES or stage config 'devices' setting."
|
||||
)
|
||||
handle = nvmlDeviceGetHandleByIndex(local_rank)
|
||||
|
||||
for proc in nvmlDeviceGetComputeRunningProcesses(handle):
|
||||
if proc.pid == my_pid:
|
||||
return proc.usedGpuMemory
|
||||
return 0
|
||||
except RuntimeError:
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.warning("NVML query failed, will use profiling fallback: %s", e)
|
||||
return None
|
||||
finally:
|
||||
try:
|
||||
nvmlShutdown()
|
||||
except Exception:
|
||||
pass
|
||||
Reference in new issue
Block a user