Files
vllm-omni/vllm_omni/platforms/__init__.py
T
tzhouamandClaude Fable 5.1 35e43b9983 [Edge][WP3] Add CPU OmniPlatform (AR + generation stages on vLLM's CPU backend)
* vllm_omni/platforms/cpu/platform.py: CPUOmniPlatform(OmniPlatform, CpuPlatform)
  selected when VLLM_TARGET_DEVICE=cpu or a +cpu vLLM wheel is installed
  (builtin plugin "cpu"; explicit CPU target short-circuits accelerator probes
  like vLLM does). NUMA-based memory queries, one logical device, no graph
  wrapper (sentinel type for isinstance checks), inductor opt-in via
  VLLM_OMNI_CPU_INDUCTOR=1, diffusion stages unsupported.
* vllm_omni/platforms/cpu/worker: CPUARWorker / CPUGenerationWorker compose
  vLLM's CPUWorker with the Omni mixin (XPU pattern); CPUARModelRunner /
  CPUGenerationModelRunner compose the Omni runners with CPUModelRunner
  (no graphs, no async omni output; sleep/wake return error ACKs).
* patch.py skips the CuMem patch on CPU-only torch; Qwen3-TTS speaker encoder
  and prompt-embeds dtype follow the model dtype (float32/float16 on CPU).
* vllm_omni/deploy/qwen3_tts.yaml: `platforms: cpu:` overlay (eager, no async
  scheduling, absolute kv_cache_memory_bytes, disjoint OMP thread ranges).
* requirements/cpu.txt (+psutil, intel-openmp), docs/getting_started/installation/cpu.md.

Measured (this x86 Xeon 8480C host, vllm 0.28.0+cpu, bf16, 12+4 threads):
Qwen3-TTS 0.6B CustomVoice end-to-end on CPU, 12 prompts: TTFA p50 292 ms,
RTF ~1.7 (25-frame chunk ~4.6 s), init 185 s. Result JSON + init timeline
committed under benchmarks/tts/edge_results/.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-09 11:02:14 +08:00

244 lines
8.5 KiB
Python

# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
import logging
import traceback
from itertools import chain
from typing import TYPE_CHECKING
from vllm.utils.import_utils import resolve_obj_by_qualname
from vllm_omni.platforms.interface import OmniPlatform, OmniPlatformEnum
from vllm_omni.plugins import (
OMNI_PLATFORM_PLUGINS_GROUP,
load_omni_plugins_by_group,
)
logger = logging.getLogger(__name__)
def cuda_omni_platform_plugin() -> str | None:
"""Check if CUDA OmniPlatform should be activated."""
is_cuda = False
logger.debug("Checking if CUDA OmniPlatform is available.")
try:
from vllm.utils.import_utils import import_pynvml
pynvml = import_pynvml()
pynvml.nvmlInit()
try:
if pynvml.nvmlDeviceGetCount() > 0:
is_cuda = True
logger.debug("Confirmed CUDA OmniPlatform is available.")
else:
logger.debug("CUDA OmniPlatform is not available because no GPU is found.")
finally:
pynvml.nvmlShutdown()
except Exception as e:
logger.debug("CUDA OmniPlatform is not available because: %s", str(e))
return "vllm_omni.platforms.cuda.platform.CudaOmniPlatform" if is_cuda else None
def rocm_omni_platform_plugin() -> str | None:
"""Check if ROCm OmniPlatform should be activated."""
is_rocm = False
logger.debug("Checking if ROCm OmniPlatform is available.")
try:
import amdsmi
amdsmi.amdsmi_init()
try:
if len(amdsmi.amdsmi_get_processor_handles()) > 0:
is_rocm = True
logger.debug("Confirmed ROCm OmniPlatform is available.")
else:
logger.debug("ROCm OmniPlatform is not available because no GPU is found.")
finally:
amdsmi.amdsmi_shut_down()
except Exception as e:
logger.debug("ROCm OmniPlatform is not available because: %s", str(e))
return "vllm_omni.platforms.rocm.platform.RocmOmniPlatform" if is_rocm else None
def npu_omni_platform_plugin() -> str | None:
"""Check if NPU OmniPlatform should be activated."""
is_npu = False
logger.debug("Checking if NPU OmniPlatform is available.")
try:
import torch
if hasattr(torch, "npu") and torch.npu.is_available():
is_npu = True
logger.debug("Confirmed NPU OmniPlatform is available.")
except Exception as e:
logger.debug("NPU OmniPlatform is not available because: %s", str(e))
return "vllm_omni.platforms.npu.platform.NPUOmniPlatform" if is_npu else None
def xpu_omni_platform_plugin() -> str | None:
"""Check if XPU OmniPlatform should be activated."""
is_xpu = False
logger.debug("Checking if XPU OmniPlatform is available.")
try:
import torch
if torch.distributed.is_xccl_available():
dist_backend = "xccl"
else:
dist_backend = "ccl"
import oneccl_bindings_for_pytorch # noqa: F401
if hasattr(torch, "xpu") and torch.xpu.is_available():
is_xpu = True
from vllm_omni.platforms.xpu import XPUOmniPlatform
XPUOmniPlatform.dist_backend = dist_backend
logger.debug("Confirmed %s backend is available.", XPUOmniPlatform.dist_backend)
logger.debug("Confirmed XPU platform is available.")
except Exception as e:
logger.debug("XPU omni platform is not available because: %s", str(e))
return "vllm_omni.platforms.xpu.platform.XPUOmniPlatform" if is_xpu else None
def musa_omni_platform_plugin() -> str | None:
"""Check if MUSA OmniPlatform should be activated."""
is_musa = False
logger.debug("Checking if MUSA OmniPlatform is available.")
try:
import torchada
if torchada.is_musa_platform():
is_musa = True
logger.debug("Confirmed MUSA OmniPlatform is available.")
except Exception as e:
logger.debug("MUSA OmniPlatform is not available because: %s", str(e))
return "vllm_omni.platforms.musa.platform.MUSAOmniPlatform" if is_musa else None
_CPU_OMNI_PLATFORM = "vllm_omni.platforms.cpu.platform.CPUOmniPlatform"
def _cpu_target_requested() -> bool:
"""True when vLLM itself would select its CPU platform (env or ``+cpu`` wheel)."""
try:
import vllm.envs as envs
if getattr(envs, "VLLM_TARGET_DEVICE", None) == "cpu":
return True
except Exception: # pragma: no cover - envs import never fails in practice
pass
try:
from vllm.platforms import vllm_version_matches_substr
return bool(vllm_version_matches_substr("cpu"))
except Exception:
return False
def cpu_omni_platform_plugin() -> str | None:
"""Check if the CPU OmniPlatform should be activated.
Mirrors vLLM's own ``cpu_platform_plugin``: active when
``VLLM_TARGET_DEVICE=cpu`` or a ``+cpu`` vLLM wheel is installed.
"""
logger.debug("Checking if CPU OmniPlatform is available.")
return _CPU_OMNI_PLATFORM if _cpu_target_requested() else None
builtin_omni_platform_plugins = {
"cuda": cuda_omni_platform_plugin,
"rocm": rocm_omni_platform_plugin,
"npu": npu_omni_platform_plugin,
"xpu": xpu_omni_platform_plugin,
"musa": musa_omni_platform_plugin,
"cpu": cpu_omni_platform_plugin,
}
def resolve_current_omni_platform_cls_qualname() -> str:
"""Resolve the current OmniPlatform class qualified name."""
# Like vLLM (vllm/platforms/__init__.py), an explicit CPU target wins before
# any accelerator probe so a +cpu wheel on a GPU host does not activate two
# builtin platforms.
if _cpu_target_requested():
logger.debug("CPU target requested; activating CPU OmniPlatform.")
return _CPU_OMNI_PLATFORM
platform_plugins = load_omni_plugins_by_group(OMNI_PLATFORM_PLUGINS_GROUP)
activated_plugins = []
for name, func in chain(builtin_omni_platform_plugins.items(), platform_plugins.items()):
try:
assert callable(func)
platform_cls_qualname = func()
if platform_cls_qualname is not None:
activated_plugins.append(name)
except Exception:
pass
activated_builtin_plugins = list(set(activated_plugins) & set(builtin_omni_platform_plugins.keys()))
activated_oot_plugins = list(set(activated_plugins) & set(platform_plugins.keys()))
if len(activated_oot_plugins) >= 2:
raise RuntimeError(f"Only one OmniPlatform plugin can be activated, but got: {activated_oot_plugins}")
elif len(activated_oot_plugins) == 1:
platform_cls_qualname = platform_plugins[activated_oot_plugins[0]]()
logger.info("OmniPlatform plugin %s is activated", activated_oot_plugins[0])
elif len(activated_builtin_plugins) >= 2:
raise RuntimeError(f"Only one OmniPlatform plugin can be activated, but got: {activated_builtin_plugins}")
elif len(activated_builtin_plugins) == 1:
platform_cls_qualname = builtin_omni_platform_plugins[activated_builtin_plugins[0]]()
logger.debug("Automatically detected OmniPlatform %s.", activated_builtin_plugins[0])
else:
platform_cls_qualname = "vllm_omni.platforms.interface.UnspecifiedOmniPlatform"
logger.debug("No platform detected, vLLM-Omni is running on UnspecifiedOmniPlatform")
return platform_cls_qualname
_current_omni_platform = None
_init_trace: str = ""
if TYPE_CHECKING:
current_omni_platform: OmniPlatform
def __getattr__(name: str):
if name == "current_omni_platform":
# Lazy init current_omni_platform
global _current_omni_platform
if _current_omni_platform is None:
platform_cls_qualname = resolve_current_omni_platform_cls_qualname()
_current_omni_platform = resolve_obj_by_qualname(platform_cls_qualname)()
global _init_trace
_init_trace = "".join(traceback.format_stack())
return _current_omni_platform
elif name in globals():
return globals()[name]
else:
raise AttributeError(f"No attribute named '{name}' exists in {__name__}.")
def __setattr__(name: str, value): # noqa: N807
if name == "current_omni_platform":
global _current_omni_platform
_current_omni_platform = value
elif name in globals():
globals()[name] = value
else:
raise AttributeError(f"No attribute named '{name}' exists in {__name__}.")
__all__ = [
"OmniPlatform",
"OmniPlatformEnum",
"current_omni_platform",
"_init_trace",
]