94057c3d3e
PR Test (NPU) / check-changes (push) Has been cancelled
PR Test (NPU) / pr-gate (push) Has been cancelled
PR Test (NPU) / set-image-config (push) Has been cancelled
PR Test (NPU) / stage-b-test-1-npu-a2 (0) (push) Has been cancelled
PR Test (NPU) / stage-b-test-1-npu-a2 (1) (push) Has been cancelled
PR Test (NPU) / stage-b-test-2-npu-a2 (0) (push) Has been cancelled
PR Test (NPU) / stage-b-test-2-npu-a2 (1) (push) Has been cancelled
PR Test (NPU) / stage-b-test-4-npu-a3 (push) Has been cancelled
PR Test (NPU) / stage-b-test-16-npu-a3 (push) Has been cancelled
PR Test (NPU) / multimodal-gen-test-1-npu-a3 (push) Has been cancelled
PR Test (NPU) / multimodal-gen-test-2-npu-a3 (push) Has been cancelled
PR Test (Arm64) / pr-gate (push) Has been cancelled
PR Test (Arm64) / check-changes (push) Has been cancelled
PR Test (Arm64) / build-test (push) Has been cancelled
PR Test (sgl-router) / gate (push) Has been cancelled
PR Test (sgl-router) / tier-1 — lint (push) Has been cancelled
PR Test (sgl-router) / tier-2 — build + test (push) Has been cancelled
PR Test (sgl-router) / tier-3 — docker (placeholder) (push) Has been cancelled
PR Test (sgl-router) / tier-3 — k8s integration (push) Has been cancelled
PR Test (sgl-router) / tier-3 — e2e (push) Has been cancelled
PR Test (sgl-router) / finish (push) Has been cancelled
PR Test (NPU) / single-node-poc (map[name:qwen3_6_27b_w8a8_1p_in64k_out1k_50ms runner:linux-aarch64-a3-2 test_case:test/registered/ascend/performance/qwen3_6_27b/test_npu_qwen3_6_27b_w8a8_1p_in64k_out1k_50ms.py test_type:perf]) (push) Has been cancelled
PR Test (NPU) / pr-test-npu-finish (push) Has been cancelled
PR Test (Xeon) / pr-gate (push) Has been cancelled
PR Test (Xeon) / check-changes (push) Has been cancelled
PR Test (Xeon) / build-test (, xeon-gnr, base-b-test-cpu) (push) Has been cancelled
PR Test (XPU) / check-changes (push) Has been cancelled
PR Test (XPU) / pr-gate (push) Has been cancelled
PR Test (XPU) / stage-a-test-1-gpu-xpu (push) Has been cancelled
PR Test (XPU) / wait-for-stage-a (push) Has been cancelled
PR Test (XPU) / stage-b-test-1-gpu-xpu (push) Has been cancelled
PR Test (XPU) / finish (push) Has been cancelled
CI Model Inventory / build-inventory (push) Has been cancelled
Lint / lint (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark Compilation Check (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark - Manual Policy (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark - Request Processing (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark Summary (push) Has been cancelled
PR Test (SMG) / build-wheel (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on windows (x86_64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on macos (x86_64 - auto) (push) Has been cancelled
PR Test (SMG) / python-unit-tests (push) Has been cancelled
PR Test (SMG) / unit-tests (push) Has been cancelled
PR Test (SMG) / benchmarks (push) Has been cancelled
PR Test (SMG) / chat-completions (push) Has been cancelled
PR Test (SMG) / chat-completions-4gpu (push) Has been cancelled
PR Test (SMG) / e2e (push) Has been cancelled
PR Test (SMG) / docker-build-test (push) Has been cancelled
PR Test (SMG) / k8s-integration (push) Has been cancelled
PR Test (SMG) / finish (push) Has been cancelled
PR Test (SMG) / summarize-benchmarks (push) Has been cancelled
Release SGLang Model Gateway Docker Image / publish (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on macos (aarch64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (aarch64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (x86_64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (aarch64 - musllinux_1_1) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (x86_64 - musllinux_1_1) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / Build SDist (push) Has been cancelled
Release SGLang Model Gateway to PyPI / Upload to PyPI (push) Has been cancelled
Release SGLang Kernels / build-cu129-matrix (aarch64, 12.9, 3.10, arm-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / build-cu129-matrix (x86_64, 12.9, 3.10, x64-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / release-cu129 (push) Has been cancelled
Release SGLang Kernels / build-cu130-matrix (aarch64, 13.0, 3.10, arm-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / build-cu130-matrix (x86_64, 13.0, 3.10, x64-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / release-cu130 (push) Has been cancelled
Release SGLang Kernels / build-rocm-matrix (3.10, 700) (push) Has been cancelled
Release SGLang Kernels / build-rocm-matrix (3.10, 720) (push) Has been cancelled
Release SGLang Kernels / release-rocm700 (push) Has been cancelled
Release SGLang Kernels / release-rocm720 (push) Has been cancelled
Release SGLang Kernels / build-musa43 (43, 3.10) (push) Has been cancelled
Release SGLang Kernels / release-musa43 (push) Has been cancelled
165 lines
5.9 KiB
Python
165 lines
5.9 KiB
Python
"""
|
|
SGLang Platform Discovery and Lazy Initialization.
|
|
|
|
Provides `current_platform` as a module-level lazy singleton. On first access,
|
|
it discovers platform plugins via entry_points and instantiates the appropriate
|
|
SRTPlatform subclass.
|
|
|
|
Usage:
|
|
from sglang.srt.platforms import current_platform
|
|
print(current_platform.device_name)
|
|
"""
|
|
|
|
import logging
|
|
import os
|
|
import pkgutil
|
|
from importlib.metadata import entry_points
|
|
|
|
import torch
|
|
|
|
from sglang.srt.environ import envs
|
|
from sglang.srt.platforms.cpu import CpuSRTPlatform
|
|
from sglang.srt.platforms.cuda import CudaSRTPlatform
|
|
from sglang.srt.platforms.interface import SRTPlatform
|
|
from sglang.srt.platforms.rocm import RocmSRTPlatform
|
|
from sglang.srt.plugins import PLATFORM_PLUGINS_GROUP, load_plugins_by_group
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
_current_platform: SRTPlatform | None = None
|
|
|
|
|
|
def _is_cuda_available() -> bool:
|
|
return bool(torch.cuda.is_available() and torch.version.hip is None)
|
|
|
|
|
|
def _is_rocm_available() -> bool:
|
|
return bool(torch.cuda.is_available() and torch.version.hip is not None)
|
|
|
|
|
|
def _is_cpu_available() -> bool:
|
|
return os.getenv("SGLANG_USE_CPU_ENGINE", "0") == "1"
|
|
|
|
|
|
def _resolve_platform() -> SRTPlatform:
|
|
"""
|
|
Discover and instantiate the active platform.
|
|
|
|
Discovery flow:
|
|
1. Branch on SGLANG_PLATFORM:
|
|
|
|
SGLANG_PLATFORM set (front-loading filter):
|
|
- Enumerate entry_points without importing any plugin modules
|
|
- Only ep.load() + activate() the named plugin
|
|
- Other plugins are never imported (avoids pulling their dependencies)
|
|
- Plugin name not found → RuntimeError
|
|
- activate() returns None → RuntimeError (hardware unavailable)
|
|
|
|
SGLANG_PLATFORM unset (auto-discover):
|
|
- Import and activate all discovered plugins
|
|
- 0 activated + SGLANG_USE_CPU_ENGINE=1 → fallback CpuSRTPlatform
|
|
(checked first; an explicit opt-in wins over CUDA/ROCm availability,
|
|
so developers on GPU hosts can intentionally exercise the CPU path)
|
|
- 0 activated + CUDA available → fallback CudaSRTPlatform
|
|
- 0 activated + ROCm available → fallback RocmSRTPlatform
|
|
- 0 activated + none of the above → fallback base SRTPlatform
|
|
- 1 activated → use it
|
|
- N activated → RuntimeError (must set SGLANG_PLATFORM)
|
|
|
|
SGLANG_PLATFORM matches against entry_point names.
|
|
"""
|
|
selected = envs.SGLANG_PLATFORM.get()
|
|
|
|
if selected:
|
|
# Front-loading filter: only import and activate the specified plugin.
|
|
# Other plugins' modules are never loaded — avoids pulling their deps.
|
|
discovered = entry_points(group=PLATFORM_PLUGINS_GROUP)
|
|
ep_map = {ep.name: ep for ep in discovered}
|
|
|
|
if selected not in ep_map:
|
|
available = ", ".join(f"'{n}'" for n in ep_map) if ep_map else "none"
|
|
raise RuntimeError(
|
|
f"SGLANG_PLATFORM={selected!r} not found in discovered platform plugins "
|
|
f"(available: {available}). Install the plugin with 'pip install -e' "
|
|
f"to register its entry_points."
|
|
)
|
|
|
|
try:
|
|
plugin_fn = ep_map[selected].load()
|
|
result = plugin_fn()
|
|
except Exception:
|
|
logger.exception("Failed to activate platform plugin: %s", selected)
|
|
raise
|
|
|
|
if result is None:
|
|
raise RuntimeError(
|
|
f"Platform plugin {selected!r} is installed but activate() returned None "
|
|
f"(hardware not available on this machine?)."
|
|
)
|
|
logger.info("OOT platform plugin activated: %s -> %s", selected, result)
|
|
return _load_platform_class(result)()
|
|
|
|
# Auto-discover: import and activate all plugins, expect exactly one
|
|
all_plugins = load_plugins_by_group(PLATFORM_PLUGINS_GROUP)
|
|
|
|
activated: dict[str, str] = {}
|
|
for name, (plugin_fn, _dist) in all_plugins.items():
|
|
try:
|
|
result = plugin_fn()
|
|
if result is not None:
|
|
activated[name] = result
|
|
logger.info("OOT platform plugin activated: %s -> %s", name, result)
|
|
except Exception:
|
|
logger.exception("Failed to activate platform plugin: %s", name)
|
|
|
|
if len(activated) == 0:
|
|
if _is_cpu_available():
|
|
logger.debug("SGLANG_USE_CPU_ENGINE=1. Using CPU SRTPlatform defaults.")
|
|
return CpuSRTPlatform()
|
|
if _is_cuda_available():
|
|
logger.debug(
|
|
"No platform plugin detected. Using CUDA SRTPlatform defaults."
|
|
)
|
|
return CudaSRTPlatform()
|
|
if _is_rocm_available():
|
|
logger.debug(
|
|
"No platform plugin detected. Using ROCm SRTPlatform defaults."
|
|
)
|
|
return RocmSRTPlatform()
|
|
logger.debug("No platform detected. Using base SRTPlatform.")
|
|
return SRTPlatform()
|
|
|
|
if len(activated) == 1:
|
|
name, qualname = next(iter(activated.items()))
|
|
return _load_platform_class(qualname)()
|
|
|
|
# Multiple activated without SGLANG_PLATFORM
|
|
names_str = ", ".join(f"'{n}'" for n in activated)
|
|
raise RuntimeError(
|
|
f"Multiple platform plugins activated: {names_str}. "
|
|
f"Set SGLANG_PLATFORM to select one."
|
|
)
|
|
|
|
|
|
def _load_platform_class(qualname: str) -> type:
|
|
"""Load an SRTPlatform subclass from its fully-qualified class name."""
|
|
cls = pkgutil.resolve_name(qualname)
|
|
if not isinstance(cls, type) or not issubclass(cls, SRTPlatform):
|
|
raise TypeError(
|
|
f"Expected an SRTPlatform subclass, got {type(cls)}: {qualname}"
|
|
)
|
|
return cls
|
|
|
|
|
|
current_platform: SRTPlatform
|
|
|
|
|
|
def __getattr__(name: str):
|
|
"""Lazy initialization of current_platform on first access."""
|
|
if name == "current_platform":
|
|
global _current_platform
|
|
if _current_platform is None:
|
|
_current_platform = _resolve_platform()
|
|
return _current_platform
|
|
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|