[plugin][distributed] use active platform's backend in get_default_distributed_backend (#23969)

This commit is contained in:
Yihao Wang
2026-06-11 20:05:17 -07:00
committed by GitHub
parent 7074704c0c
commit f5c9f88ee2
4 changed files with 109 additions and 13 deletions
@@ -42,12 +42,14 @@ import torch
import torch.distributed
from torch.distributed import Backend, ProcessGroup
from sglang.srt import platforms
from sglang.srt.compilation.compilation_config import register_split_op
from sglang.srt.distributed.utils import set_global_tcp_store
from sglang.srt.environ import envs
from sglang.srt.model_executor.runner_backend_utils.tc_piecewise_cuda_graph import (
is_in_tc_piecewise_cuda_graph,
)
from sglang.srt.platforms.device_mixin import _DEVICE_TO_DISTRIBUTED_BACKEND
from sglang.srt.utils import (
get_current_device_stream_fast,
get_int_env_var,
@@ -1690,17 +1692,14 @@ def set_torch_symm_mem_all_reduce(enable: bool):
_ENABLE_TORCH_SYMM_MEM_ALL_REDUCE = enable
_DEVICE_TO_DISTRIBUTED_BACKEND = {
"cuda": "nccl",
"xpu": "xccl",
"hpu": "hccl",
"cpu": "gloo",
"npu": "hccl" if not envs.SGLANG_ZBAL_LOCAL_MEM_SIZE.get() > 0 else "zbal",
"musa": "mccl",
}
# TODO: refactor in-tree platforms to get rid of this wrapper
def get_default_distributed_backend(device: str) -> str:
# We deliberately go through ``platforms.current_platform`` (rather than
# ``from ... import current_platform``) so each call resolves through the
# platforms package's lazy ``__getattr__`` and picks up runtime overrides
# of ``_current_platform`` (e.g. in tests).
if device == platforms.current_platform.device_type:
return platforms.current_platform.get_torch_distributed_backend_str()
return _DEVICE_TO_DISTRIBUTED_BACKEND.get(device, "gloo")
+4 -1
View File
@@ -139,7 +139,10 @@ def _load_platform_class(qualname: str) -> type:
return cls
def __getattr__(name: str) -> SRTPlatform:
current_platform: SRTPlatform
def __getattr__(name: str):
"""Lazy initialization of current_platform on first access."""
if name == "current_platform":
global _current_platform
+19 -2
View File
@@ -32,6 +32,8 @@ from typing import NamedTuple, Optional
import numpy as np
import torch
from sglang.srt.environ import envs
class PlatformEnum(enum.Enum):
"""Enumeration of known platform types.
@@ -79,6 +81,16 @@ class DeviceCapability(NamedTuple):
return self.major * 10 + self.minor
_DEVICE_TO_DISTRIBUTED_BACKEND: dict[str, str] = {
"cuda": "nccl",
"xpu": "xccl",
"hpu": "hccl",
"cpu": "gloo",
"npu": "hccl" if not envs.SGLANG_ZBAL_LOCAL_MEM_SIZE.get() > 0 else "zbal",
"musa": "mccl",
}
class DeviceMixin:
"""Mixin providing device identity queries and basic device operations.
@@ -192,8 +204,13 @@ class DeviceMixin:
# ---- Distributed ----
def get_torch_distributed_backend_str(self) -> str:
"""[Planned] Return the torch.distributed backend string (e.g. "nccl", "hccl")."""
raise NotImplementedError
"""Return the torch.distributed backend string (e.g. "nccl", "hccl").
Default: lookup ``self.device_type`` in ``_DEVICE_TO_DISTRIBUTED_BACKEND``,
falling back to ``"gloo"``. Subclasses override only when they need a
non-default backend (e.g. mooncake, or a brand-new device).
"""
return _DEVICE_TO_DISTRIBUTED_BACKEND.get(self.device_type, "gloo")
def get_communicator_class(self) -> type | None:
"""[Planned] Return platform-specific communicator class, or None for default."""