-
Notifications
You must be signed in to change notification settings - Fork 61
[Hardware] broadcast support for Huawei Ascend NPU #39
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
adb4075
[Hardware] broadcast support for Huawei Ascend NPU
f083dbd
[modify] check npu is availble
bed9862
[Fix] fix the pre-commit lint error
c9d3d42
[modify] address code view feedback
c7c2d8d
[modify] generate uuid by npu smi info
56f0ec2
[modify] fix the variable name
b2c8251
[modify] get ip
b973808
Merge branch 'main' into main
kip-cxj b225b62
[fix] pre-commit fix
kip-cxj 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
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,84 @@ | ||
| import os | ||
| import re | ||
| import socket | ||
| import subprocess | ||
| from functools import lru_cache | ||
|
|
||
| import torch | ||
| from loguru import logger | ||
|
|
||
|
|
||
| @lru_cache(maxsize=1) | ||
| def get_ip() -> str: | ||
| try: | ||
| # try to get ip from network interface | ||
| with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s: | ||
| s.connect(("8.8.8.8", 80)) | ||
| return s.getsockname()[0] | ||
| except Exception as e: # noqa: BLE001 | ||
| # fallback to get ip from hostname | ||
| logger.warning( | ||
| f"fail to get ip from network interface, fallback to get ip from hostname: {e}" | ||
| ) | ||
| return socket.gethostbyname(socket.gethostname()) | ||
|
|
||
|
|
||
| def npu_generate_uuid() -> str: | ||
| str_pid = str(os.getpid()) | ||
| npu_num = 8 | ||
| try: | ||
| for npu_id in range(npu_num): | ||
| cmd = ["npu-smi", "info", "-t", "proc-mem", "-i", str(npu_id)] | ||
| result = subprocess.run(cmd, check=True, capture_output=True, text=True) # noqa: S603 | ||
| str_result = str(result.stdout) | ||
| if str_pid in str_result: | ||
| # In A3 server, one NPU has two chips. | ||
| match_chip_count = re.search(r"Chip Count[^\d]*(\d+)", str_result) | ||
| chip_count = int(match_chip_count.group(1)) | ||
| search_after_pid = str_result[str_result.find(str_pid) + len(str_pid) :] | ||
| match_chip_id = re.search(r"Chip ID[^\d]*(\d+)", search_after_pid) | ||
| chip_id = int(match_chip_id.group(1)) | ||
| return f"{get_ip()}-{npu_id * chip_count + chip_id}" | ||
| ValueError("The current process is not running on the npu device") | ||
| except subprocess.CalledProcessError: | ||
| ValueError("The current process is not running on the npu device") | ||
|
|
||
|
|
||
| class DeviceManager: | ||
| def __init__(self): | ||
| self.device_type = self._detect_device_type() | ||
| self._setup_device_module() | ||
|
|
||
| def _is_torch_npu_available(self) -> bool: | ||
| try: | ||
| if hasattr(torch, "npu") and callable(getattr(torch.npu, "is_available", None)): | ||
| return torch.npu.is_available() | ||
| else: | ||
| return False | ||
| except ImportError: | ||
| return False | ||
|
|
||
| def _detect_device_type(self) -> str: | ||
| if self._is_torch_npu_available(): | ||
| return "npu" | ||
| elif torch.cuda.is_available(): | ||
| return "cuda" | ||
| else: | ||
| raise TypeError("The current device type is not supported") | ||
|
|
||
| def _setup_device_module(self): | ||
| if self.device_type == "npu": | ||
| import torch_npu | ||
|
|
||
| self.device_module = torch_npu.npu | ||
| elif self.device_type == "cuda": | ||
| self.device_module = torch.cuda | ||
| else: | ||
| raise TypeError("The current device type is not supported") | ||
|
|
||
| @property | ||
| def backend(self) -> str: | ||
| if self.device_type == "npu": | ||
| return "hccl" | ||
| elif self.device_type == "cuda": | ||
| return "nccl" | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.