-
Notifications
You must be signed in to change notification settings - Fork 268
feat: use post4 libtorch_npu with early Python init and native ProcessGroupHCCL. #2006
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
yingxudeng
wants to merge
2
commits into
xLLM-AI:main
Choose a base branch
from
yingxudeng:feat/npu-python-graph-tp-post4
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| """torch_npu wheel management for xLLM NPU builds. | ||
|
|
||
| Ensures the correct torch_npu version is installed before compilation. | ||
| Auto-installs or upgrades as needed — mirrors the TileLang pattern in | ||
| xllm/compiler/tilelang/tilelang_ascend_install.py. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import importlib.metadata | ||
| import os | ||
| import platform | ||
| import subprocess | ||
| import sys | ||
| import tempfile | ||
| import urllib.request | ||
|
|
||
| from scripts.logger import logger | ||
|
|
||
| TORCH_NPU_WHEELS: dict[tuple[str, str], str] = { | ||
| # (cann_version, arch) → wheel URL | ||
| ("9.0", "arm"): ( | ||
| "http://storage.jd.local/bmp-packages/NPU_CICD/compile-base/" | ||
| "compile-ascend_9.0.0-arm64/GR/" | ||
| "torch_npu-2.9.0.post4_git44b5898-cp311-cp311-linux_aarch64.whl" | ||
| ), | ||
| } | ||
|
|
||
| # PEP 440 compliant local filename for pip install (upstream wheel name | ||
| # contains underscores in the version tag that pip 26+ rejects). | ||
| _LOCAL_WHEEL_NAME = "torch_npu-2.9.0.post4-cp311-cp311-linux_aarch64.whl" | ||
|
|
||
| REQUIRED_VERSION = "2.9.0.post4" | ||
|
|
||
|
|
||
| def _get_cann_version() -> str: | ||
| """Detect CANN major.minor from toolkit path.""" | ||
| toolkit = os.environ.get( | ||
| "NPU_TOOLKIT_HOME", "/usr/local/Ascend/ascend-toolkit/latest" | ||
| ) | ||
| version_file = os.path.join(toolkit, "version.cfg") | ||
| try: | ||
| with open(version_file) as f: | ||
| for line in f: | ||
| if "=" in line: | ||
| _, ver = line.strip().split("=", 1) | ||
| parts = ver.split(".") | ||
| if len(parts) >= 2: | ||
| return f"{parts[0]}.{parts[1]}" | ||
| except (OSError, ValueError): | ||
| pass | ||
| return "9.0" | ||
|
|
||
|
|
||
| def _get_arch() -> str: | ||
| machine = platform.machine() | ||
| if machine in ("aarch64", "arm64"): | ||
| return "arm" | ||
| return "x86" | ||
|
|
||
|
|
||
| def _installed_version() -> str | None: | ||
| try: | ||
| return importlib.metadata.version("torch_npu") | ||
| except importlib.metadata.PackageNotFoundError: | ||
| return None | ||
|
|
||
|
|
||
| def _resolve_wheel(cann_version: str, arch: str) -> str: | ||
| key = (cann_version, arch) | ||
| url = TORCH_NPU_WHEELS.get(key) | ||
| if url is None: | ||
| raise RuntimeError( | ||
| f"No torch_npu wheel configured for CANN {cann_version}, arch={arch}.\n" | ||
| f"Available: {list(TORCH_NPU_WHEELS.keys())}" | ||
| ) | ||
| return url | ||
|
|
||
|
|
||
| def _download_and_install(wheel_url: str) -> None: | ||
| """Download wheel, rename to PEP 440 compliant name, then pip install.""" | ||
| with tempfile.TemporaryDirectory() as tmpdir: | ||
| local_path = os.path.join(tmpdir, _LOCAL_WHEEL_NAME) | ||
| logger.info(f"Downloading {wheel_url} ...") | ||
| urllib.request.urlretrieve(wheel_url, local_path) | ||
| subprocess.check_call([ | ||
| sys.executable, "-m", "pip", "install", | ||
| "--force-reinstall", "--no-deps", local_path, | ||
| ]) | ||
|
|
||
|
|
||
| def ensure_torch_npu_ready() -> None: | ||
| """Ensure torch_npu matches REQUIRED_VERSION; install or upgrade if not.""" | ||
| installed = _installed_version() | ||
| if installed is not None and REQUIRED_VERSION in installed: | ||
| logger.info(f"torch_npu {installed} is ready.") | ||
| return | ||
|
|
||
| cann_version = _get_cann_version() | ||
| arch = _get_arch() | ||
| wheel_url = _resolve_wheel(cann_version, arch) | ||
|
|
||
| action = f"Upgrading torch_npu {installed}" if installed else "Installing torch_npu" | ||
| logger.info(f"{action} → {REQUIRED_VERSION}") | ||
|
|
||
| _download_and_install(wheel_url) | ||
|
|
||
| new_version = _installed_version() | ||
| if new_version is None or REQUIRED_VERSION not in new_version: | ||
| raise RuntimeError( | ||
| f"torch_npu installation failed: expected {REQUIRED_VERSION}, " | ||
| f"got {new_version}" | ||
| ) | ||
| logger.info(f"torch_npu {new_version} installed successfully.") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个链接可以吗