Files
vllm-omni/vllm_omni/platforms/__init__.py
T
tzhouamandClaude Fable 5.1 33de5d8629 Merge vLLM-Omni v0.29.0rc1 into edge-infer/omni-0.29.0
The pair for vLLM v0.29.0: rc1's docs pin vllm==0.29.0 and a static scan of
rc1's vllm.* imports against the v0.29.0 tree finds nothing missing, while
our 0.28-based tree needs seven modules v0.29.0 no longer has
(vllm.inputs.preprocess, entrypoints.launcher, openai.cli_args,
openai.engine.protocol, serve.utils.server_utils, layers.batch_invariant,
models.hunyuan_v1).

Ten files conflicted. Upstream's structure is the base everywhere; the
fork's features are re-attached to it:
- deploy_profile / apply_deploy_profile run before rc1's resolve_omni_config
  pops deploy_config (async_omni_engine, cli/serve); the deploy-profile
  selector and the resolve_model_config_path chain it needs stay in
  entrypoints/utils.py; stage_overrides takes rc1's dict type.
- config_factory keeps the lazy diffusion output-type import; the diffusion
  registry import in rc1's new config/resolver.py is made lazy for the same
  WP6 reason (TTS path must not load diffusion).
- requirements: our core/media split kept; rc1's transformers>=5.13 floor
  and kernels==0.16.1 applied in core.txt / media.txt.
- TTS: rc1's split_granularity and bounded seed plus our output_mode=
  codec_tokens, _SessionInbox cancel handling and codec DELTA accumulation;
  our private _conditioning_cache_salt is superseded by rc1's public
  tts_adapters/base.py::conditioning_cache_salt.
- stage_runtime: rc1's single ExitStack G2 path (lock released before READY)
  replaces our two-path block, with the four worker_phase names the init
  profiler consumes still emitted; _cpu_placed / host admission kept.

Not yet run against vLLM 0.29.0: the tests listed in the W6 record.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-18 09:57:20 +08:00

248 lines
8.6 KiB
Python

# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni 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:
logger.debug(
"OmniPlatform plugin %s failed during detection.",
name,
exc_info=True,
)
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",
]