Skip to content

Commit 4efaaa8

Browse files
authored
Add KunLunXin platform support (#63)
# Description Add KunLunXin platform support for Megatron-LM-FL so XPU environments can be detected and routed through XME initialization before falling back to CUDA. ## Type of change - [x] New feature (non-breaking change which adds functionality) - [ ] Infra/Build change (changes to CI/CD workflows or build scripts) - [ ] Code refactoring - [ ] Documentation change - [ ] Bug fix - [ ] Breaking change ## Changes - Add `PlatformKunLunXin`, inheriting from `PlatformCUDA` for XMLIR CUDA-compatible execution. - Detect KunLunXin/XPU environments via `XPU=1` or `xpu-smi`. - Register the KunLunXin platform before CUDA so XPU machines select it first. - Defer XME initialization through `ensure_xme_init()` to avoid circular imports during platform registration. ## Checklist - [x] I have read and followed the contributing guidelines - [x] The functionality is complete - [ ] I have commented my code, particularly in coverage report uploading steps - [ ] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [ ] I have added/updated tests that prove my feature works - [ ] New and existing unit tests pass locally
1 parent 01545ff commit 4efaaa8

3 files changed

Lines changed: 63 additions & 1 deletion

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Copyright (c) 2026, Baidu Inc. All rights reserved.
2+
"""KunLunXin XPU Platform for Megatron-LM-FL.
3+
4+
Inherits from PlatformCUDA since XMLIR makes XPU appear as CUDA.
5+
XME init is deferred to first use (via _ensure_xme_init) to avoid circular import
6+
during platform registration (parallel_state calls get_platform() at module level).
7+
"""
8+
9+
import os
10+
import shutil
11+
12+
from .platform_cuda import PlatformCUDA
13+
14+
_XME_INITIALIZED = False
15+
16+
17+
class PlatformKunLunXin(PlatformCUDA):
18+
19+
def __init__(self):
20+
super().__init__()
21+
self._name = "kunlunxin"
22+
23+
def is_available(self):
24+
"""Detect KunLunXin XPU. Does NOT trigger XME init to avoid circular import."""
25+
xpu_flag = os.getenv("XPU", "")
26+
if xpu_flag in ("1", "True", "true", "TRUE"):
27+
return True
28+
if shutil.which("xpu-smi") is not None and shutil.which("nvidia-smi") is None:
29+
return True
30+
return False
31+
32+
@staticmethod
33+
def ensure_xme_init():
34+
"""Call once after get_platform() to init XME patches. Idempotent."""
35+
global _XME_INITIALIZED
36+
if _XME_INITIALIZED:
37+
return
38+
_XME_INITIALIZED = True
39+
try:
40+
from xmegatron_ext import megatron_plugin_init
41+
megatron_plugin_init(use_version="0.17.1", check_version=True)
42+
except Exception:
43+
pass
44+
45+
def device_name(self, device_index=None):
46+
if device_index is None:
47+
return "kunlunxin"
48+
return f"kunlunxin:{device_index}"
49+
50+
def current_device_name(self):
51+
return f"kunlunxin:{super().current_device()}"

megatron/plugin/platform/platform_manager.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ def get_platform():
3030
elif "enflame" in PLATFORMS.keys() and PLATFORMS["enflame"].is_available():
3131
cur_platform = PLATFORMS["enflame"]
3232
print(f"Megatron-LM-FL Platform: enflame Selected")
33+
elif "kunlunxin" in PLATFORMS.keys() and PLATFORMS["kunlunxin"].is_available():
34+
cur_platform = PLATFORMS["kunlunxin"]
35+
print(f"Megatron-LM-FL Platform: kunlunxin Selected")
36+
# Deferred XME init (after platform selection to avoid circular import)
37+
cur_platform.ensure_xme_init()
3338
elif "cpu" in PLATFORMS.keys() and PLATFORMS["cpu"].is_available():
3439
cur_platform = PLATFORMS["cpu"]
3540
print(f"Megatron-LM-FL Platform: cpu Selected")

megatron/plugin/platform/platform_register.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,10 @@ def register_platforms() -> None:
5151
if platform_enflame.is_available():
5252
PLATFORMS["enflame"] = platform_enflame # use lower keys: enflame
5353
print(f"Megatron-LM-FL Platform: enflame Registered")
54-
54+
55+
# Register KunLunXin Platform
56+
from .platform_kunlunxin import PlatformKunLunXin
57+
platform_kunlunxin = PlatformKunLunXin()
58+
if platform_kunlunxin.is_available():
59+
PLATFORMS["kunlunxin"] = platform_kunlunxin
60+
print(f"Megatron-LM-FL Platform: kunlunxin Registered")

0 commit comments

Comments
 (0)