Add SGLang MACA backend detection - #1
Conversation
There was a problem hiding this comment.
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.
| 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()) |
There was a problem hiding this comment.
当前的 is_maca_available 实现中,如果 get_torch_maca_version 不为 None,会立即返回 True,而没有检查 GPU 设备是否实际可用。如果安装了 MACA PyTorch 但没有可见或可用的 MACA GPU,这会错误地将 MACA 识别为可用,从而导致 SGLang 在后续尝试运行 GPU 操作时失败。
优先检查 cuda.is_available() 可以确保只有在存在可用 GPU 设备时才将 MACA 识别为可用,这与 is_cuda() 的实现逻辑保持一致。
| 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) |
| 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) |
There was a problem hiding this comment.
使用 importlib.util.spec_from_file_location 动态加载 maca.py 是一种反模式(anti-pattern),这会使测试代码变得脆弱、难以阅读,并破坏 IDE 的自动补全和导航功能。由于 sglang 是一个标准包,且 maca.py 本身也会导入 torch,因此我们应该直接使用标准的 import 语句。
| 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 |
?? API ?? 2 ??????
- Detect MACA as CUDA-like backend - tighten MACA backend detection
Summary
Validation
Review notes