Skip to content

Add SGLang MACA backend detection - #1

Open
ghangz wants to merge 3 commits into
MetaX-MACA:mainfrom
ghangz:mengz/detect-maca-backend
Open

Add SGLang MACA backend detection#1
ghangz wants to merge 3 commits into
MetaX-MACA:mainfrom
ghangz:mengz/detect-maca-backend

Conversation

@ghangz

@ghangz ghangz commented Jun 25, 2026

Copy link
Copy Markdown

Summary

  • This change adds explicit MACA backend detection helpers for SGLang so MetaX GPU containers can identify the runtime path before server launch or kernel tests fail later in the workflow.
  • The implementation is scoped to diagnostics and runtime setup paths used by MetaX MACA validation, so existing non-MACA behavior remains compatible.
  • Main files: python/sglang/srt/utils/common.py, python/sglang/srt/utils/maca.py, test/registered/unit/utils/test_maca_utils.py

Validation

  • Verified on Gitee.AI MetaX GPU resources in the SGLang/PyTorch MACA validation batch.
  • Branch validation command: python -m pytest test/registered/unit/utils/test_maca_utils.py
  • Pull request text is ASCII-only to avoid encoding issues on web forms and API clients.

Review notes

  • Source branch: ghangz:mengz/detect-maca-backend
  • Target branch: MetaX-MACA/sglang:main
  • Maintainers can modify this branch if follow-up adjustments are needed.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces MACA (Moore Threads) GPU detection utilities, adding a new maca.py utility module, integrating is_maca() into the is_cuda_alike() check, and adding corresponding unit tests. The review feedback highlights two key improvements: first, refactoring is_maca_available to ensure GPU device availability is verified before checking the MACA version or environment; second, replacing the dynamic module loading in the unit tests with standard imports to improve code readability and maintainability.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread python/sglang/srt/utils/maca.py Outdated
Comment on lines +25 to +31
def is_maca_available(torch_module=torch, env: Mapping[str, str] | None = None) -> bool:
if get_torch_maca_version(torch_module) is not None:
return True
if not has_maca_toolkit_env(env):
return False
cuda = getattr(torch_module, "cuda", None)
return bool(cuda is not None and cuda.is_available())

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

当前的 is_maca_available 实现中,如果 get_torch_maca_version 不为 None,会立即返回 True,而没有检查 GPU 设备是否实际可用。如果安装了 MACA PyTorch 但没有可见或可用的 MACA GPU,这会错误地将 MACA 识别为可用,从而导致 SGLang 在后续尝试运行 GPU 操作时失败。

优先检查 cuda.is_available() 可以确保只有在存在可用 GPU 设备时才将 MACA 识别为可用,这与 is_cuda() 的实现逻辑保持一致。

Suggested change
def is_maca_available(torch_module=torch, env: Mapping[str, str] | None = None) -> bool:
if get_torch_maca_version(torch_module) is not None:
return True
if not has_maca_toolkit_env(env):
return False
cuda = getattr(torch_module, "cuda", None)
return bool(cuda is not None and cuda.is_available())
def is_maca_available(torch_module=torch, env: Mapping[str, str] | None = None) -> bool:
cuda = getattr(torch_module, "cuda", None)
if cuda is None or not cuda.is_available():
return False
return get_torch_maca_version(torch_module) is not None or has_maca_toolkit_env(env)

Comment on lines +1 to +15
import importlib.util
import types
import unittest
import sys
from pathlib import Path
from tempfile import TemporaryDirectory

REPO_ROOT = Path(__file__).resolve().parents[4]
MACA_UTILS_PATH = REPO_ROOT / "python" / "sglang" / "srt" / "utils" / "maca.py"

spec = importlib.util.spec_from_file_location("sglang_srt_utils_maca", MACA_UTILS_PATH)
maca_utils = importlib.util.module_from_spec(spec)
sys.modules["sglang_srt_utils_maca"] = maca_utils
assert spec.loader is not None
spec.loader.exec_module(maca_utils)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

使用 importlib.util.spec_from_file_location 动态加载 maca.py 是一种反模式(anti-pattern),这会使测试代码变得脆弱、难以阅读,并破坏 IDE 的自动补全和导航功能。由于 sglang 是一个标准包,且 maca.py 本身也会导入 torch,因此我们应该直接使用标准的 import 语句。

Suggested change
import importlib.util
import types
import unittest
import sys
from pathlib import Path
from tempfile import TemporaryDirectory
REPO_ROOT = Path(__file__).resolve().parents[4]
MACA_UTILS_PATH = REPO_ROOT / "python" / "sglang" / "srt" / "utils" / "maca.py"
spec = importlib.util.spec_from_file_location("sglang_srt_utils_maca", MACA_UTILS_PATH)
maca_utils = importlib.util.module_from_spec(spec)
sys.modules["sglang_srt_utils_maca"] = maca_utils
assert spec.loader is not None
spec.loader.exec_module(maca_utils)
import types
import unittest
from pathlib import Path
from tempfile import TemporaryDirectory
from sglang.srt.utils import maca as maca_utils

@ghangz ghangz changed the title 将 MACA 识别为 CUDA 类后端 ?? GPU ????? MACA ?? Jun 26, 2026
@ghangz ghangz changed the title ?? GPU ????? MACA ?? 仅在 GPU 可用时识别 MACA 后端 Jun 26, 2026
@ghangz ghangz changed the title 仅在 GPU 可用时识别 MACA 后端 Add SGLang MACA backend detection Jul 1, 2026
- Detect MACA as CUDA-like backend
- tighten MACA backend detection
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant