Files
vllm-omni/vllm_omni/worker/gpu_ar_worker.py
T

144 lines
6.5 KiB
Python
Raw Normal View History

2025-10-22 15:25:17 +08:00
import gc
2025-10-24 16:49:19 +08:00
import os
import torch
from vllm.logger import init_logger
2025-10-24 16:49:19 +08:00
from vllm.platforms import current_platform
2026-02-13 18:05:07 +08:00
from vllm.tracing import instrument
+13
2026-01-21 00:25:25 +08:00
from vllm.utils.mem_utils import MemorySnapshot, format_gib
from vllm.utils.torch_utils import set_random_seed
2025-10-22 15:25:17 +08:00
from vllm.v1.utils import report_usage_stats
2025-10-24 16:49:19 +08:00
from vllm.v1.worker.gpu_worker import init_worker_distributed_environment
+13
2026-01-21 00:25:25 +08:00
from vllm.v1.worker.workspace import init_workspace_manager
2025-11-18 20:54:26 +08:00
from vllm_omni.diffusion.data import OmniACK, OmniSleepTask, OmniWakeTask
from vllm_omni.platforms import current_omni_platform
from vllm_omni.worker.base import OmniGPUWorkerBase
2025-10-23 14:35:23 +08:00
from vllm_omni.worker.gpu_ar_model_runner import GPUARModelRunner
2026-06-14 00:43:02 +08:00
from vllm_omni.worker.memory_utils import request_memory_tolerant
from vllm_omni.worker.mixins import OmniWorkerMixin
2025-10-22 15:25:17 +08:00
logger = init_logger(__name__)
2025-10-22 15:25:17 +08:00
class GPUARWorker(OmniWorkerMixin, OmniGPUWorkerBase):
"""GPU worker for autoregressive omni model stages.
Extends the base GPUWorker to initialize and manage autoregressive
model runners for text generation stages (e.g., thinker stages).
"""
2026-02-13 18:05:07 +08:00
@instrument(span_name="Init device")
2025-10-22 15:25:17 +08:00
def init_device(self):
if self.device_config.device_type in ("cuda", "musa"):
2025-10-22 15:25:17 +08:00
# This env var set by Ray causes exceptions with graph building.
os.environ.pop("NCCL_ASYNC_ERROR_HANDLING", None)
+13
2026-01-21 00:25:25 +08:00
parallel_config = self.parallel_config
2025-12-19 16:42:02 +08:00
if (
+13
2026-01-21 00:25:25 +08:00
parallel_config.distributed_executor_backend not in ("ray", "external_launcher")
and parallel_config.data_parallel_backend != "ray"
and parallel_config.nnodes_within_dp == 1
2025-12-19 16:42:02 +08:00
):
# Use local DP rank if available, otherwise use global DP rank.
dp_local_rank = self.parallel_config.data_parallel_rank_local
if dp_local_rank is None:
+13
2026-01-21 00:25:25 +08:00
dp_local_rank = self.parallel_config.data_parallel_index
2025-12-19 16:42:02 +08:00
tp_pp_world_size = (
self.parallel_config.pipeline_parallel_size * self.parallel_config.tensor_parallel_size
)
# DP_LOCAL_RANK * TP_PP_WORLD_SIZE + TP_LOCAL_RANK
self.local_rank += dp_local_rank * tp_pp_world_size
2026-06-30 19:41:06 +08:00
# Publish the logical-to-physical mapping for topology queries
# such as NIC affinity and P2P checks (upstream PR #45026).
assigned_physical_gpu_ids = parallel_config.assigned_physical_gpu_ids
if assigned_physical_gpu_ids is not None:
from vllm.platforms.interface import set_assigned_physical_gpu_ids
set_assigned_physical_gpu_ids(assigned_physical_gpu_ids)
assert self.local_rank < len(assigned_physical_gpu_ids), (
f"local_rank {self.local_rank} is out of bounds for "
f"assigned_physical_gpu_ids {assigned_physical_gpu_ids}"
)
if parallel_config.distributed_executor_backend not in ("ray", "external_launcher"):
assert self.parallel_config.local_world_size <= len(assigned_physical_gpu_ids), (
f"local_world_size ({self.parallel_config.local_world_size}) "
"exceeds assigned_physical_gpu_ids count "
f"({len(assigned_physical_gpu_ids)})"
)
else:
2026-03-21 20:37:38 +08:00
assert self.local_rank < torch.accelerator.device_count(), (
2026-06-30 19:41:06 +08:00
f"DP adjusted local rank {self.local_rank} is out of "
f"bounds for {torch.accelerator.device_count()} devices."
2025-12-19 16:42:02 +08:00
)
2026-06-30 19:41:06 +08:00
visible_device_index = current_platform.logical_device_id_to_visible_device_id(self.local_rank)
self.device = current_omni_platform.get_torch_device(visible_device_index)
2026-03-21 20:37:38 +08:00
torch.accelerator.set_device_index(self.device)
2025-10-22 15:25:17 +08:00
2025-11-04 22:06:41 +08:00
current_platform.check_if_supports_dtype(self.model_config.dtype)
# Initialize the distributed environment BEFORE taking
# memory snapshot
# This ensures NCCL buffers are allocated before we measure
# available memory
init_worker_distributed_environment(
self.vllm_config,
self.rank,
self.distributed_init_method,
self.local_rank,
current_platform.dist_backend,
)
# Set random seed.
set_random_seed(self.model_config.seed)
# Now take memory snapshot after NCCL is initialized
2025-10-22 15:25:17 +08:00
gc.collect()
2026-03-21 20:37:38 +08:00
torch.accelerator.empty_cache()
2025-10-22 15:25:17 +08:00
# take current memory snapshot
+13
2026-01-21 00:25:25 +08:00
self.init_snapshot = init_snapshot = MemorySnapshot(device=self.device)
2026-06-14 00:43:02 +08:00
self.requested_memory = request_memory_tolerant(init_snapshot, self.cache_config)
+13
2026-01-21 00:25:25 +08:00
logger.debug("worker init memory snapshot: %r", self.init_snapshot)
logger.debug("worker requested memory: %sGiB", format_gib(self.requested_memory))
2025-10-22 15:25:17 +08:00
else:
2025-10-24 16:49:19 +08:00
raise RuntimeError(f"Not support device type: {self.device_config.device}")
2025-10-22 15:25:17 +08:00
+13
2026-01-21 00:25:25 +08:00
# Initialize workspace manager
num_ubatches = 2 if self.vllm_config.parallel_config.enable_dbo else 1
init_workspace_manager(self.device, num_ubatches)
2026-02-03 12:24:45 +08:00
if self.use_v2_model_runner:
# OMNI: v2 model runner does not yet include omni hooks.
logger.warning("OMNI GPUARWorker forces v1 model runner for omni hooks.")
self.use_v2_model_runner = False
2025-10-22 15:25:17 +08:00
# Construct the model runner
2025-12-19 16:42:02 +08:00
self.model_runner = GPUARModelRunner(self.vllm_config, self.device)
2025-10-22 15:25:17 +08:00
if self.rank == 0:
# If usage stat is enabled, collect relevant info.
2025-10-24 16:49:19 +08:00
report_usage_stats(self.vllm_config)
def handle_sleep_task(self, task: OmniSleepTask | dict) -> OmniACK:
"""
Explicitly handle sleep commands.
Calls the implementation in the base class OmniGPUWorkerBase.
"""
logger.debug(f"[AR Worker {self.rank}] Resolving handle_sleep_task dispatch")
if isinstance(task, dict):
task = OmniSleepTask(**task)
return super().handle_sleep_task(task)
def handle_wake_task(self, task: OmniWakeTask | dict) -> OmniACK:
"""
Explicitly handle wake-up commands.
Calls the implementation in the base class OmniGPUWorkerBase.
"""
logger.debug(f"[AR Worker {self.rank}] Resolving handle_wake_task dispatch")
if isinstance(task, dict):
task = OmniWakeTask(**task)
return super().handle_wake_task(task)