forked from NVIDIA/TransformerEngine
-
Notifications
You must be signed in to change notification settings - Fork 28
[ascend]Native Integration of MegatronAdaptor (TransformerEngine-FL Module) on FlagOS #79
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
+98
−0
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
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
81 changes: 81 additions & 0 deletions
81
transformer_engine/plugin/core/backends/vendor/npu/patches.py
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,81 @@ | ||
| """Python-side compatibility patches for the NPU vendor backend.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Callable | ||
|
|
||
| import torch | ||
| import torch_npu | ||
|
|
||
| from types import SimpleNamespace | ||
|
|
||
| def _noop(*args, **kwargs): | ||
| return None | ||
|
|
||
| def get_npu_device_properties(device=None): | ||
| return SimpleNamespace( | ||
| name="Fake NPU", | ||
| total_memory=16 * 1024**3, | ||
| major=9, | ||
| minor=0, | ||
| multi_processor_count=80, | ||
| uuid="fake-uuid-12345" | ||
| ) | ||
|
|
||
| _PATCH_CALLS: list[tuple[object, str, Callable[..., object]]] = [ | ||
| # We do not recommend replace is_available, due to its device-related behavior. | ||
| (torch.cuda, "get_device_properties", get_npu_device_properties), | ||
| (torch.cuda, "device", torch_npu.npu.device), | ||
| (torch.cuda, "current_device", torch_npu.npu.current_device), | ||
| (torch.cuda, "synchronize", torch_npu.npu.synchronize), | ||
| (torch.cuda, "is_current_stream_capturing", torch_npu.npu.is_current_stream_capturing), | ||
| # TODO: Add NVTX patches for NPU. | ||
| # NVTX is CUDA-specific; make it a no-op on NPU. | ||
| (torch.cuda.nvtx, "range_push", _noop), | ||
| (torch.cuda.nvtx, "range_pop", _noop), | ||
| # TODO: Add other patches for NPU. | ||
| ] | ||
|
|
||
| def apply_patch() -> None: | ||
| """Apply NPU Python-side patches (idempotent, best-effort).""" | ||
| try: | ||
| import torch_npu | ||
| if not torch_npu.npu.is_available(): | ||
| return | ||
|
|
||
| except Exception as e: | ||
| print(f"[TE-FL] NPU backend not available: {e}") | ||
| # If backend availability can't be determined, don't patch. | ||
| return | ||
|
|
||
| # Mark TE global device type for Python-side callers. | ||
| # IMPORTANT: do not import `transformer_engine` here, because TE's `__init__.py` | ||
| # imports this module to run patches and that would cause a circular import. | ||
| try: | ||
| import transformer_engine | ||
|
|
||
| transformer_engine.TE_DEVICE_TYPE = "npu" | ||
| transformer_engine.TE_PLATFORM = torch_npu.npu | ||
| except Exception as e: | ||
| print(f"[TE-FL NPU Patches] Error setting TE device type or platform: {e}") | ||
| # Best-effort: don't fail patching if we can't set the global. | ||
| pass | ||
|
|
||
| # Only patch when torch_npu.npu exists and is usable. | ||
| if not hasattr(torch_npu, "npu"): | ||
| return | ||
| try: | ||
| if not torch_npu.npu.is_available(): | ||
| return | ||
| except Exception: | ||
| return | ||
|
|
||
| for parent, attr, replacement in _PATCH_CALLS: | ||
| if not hasattr(parent, attr): | ||
| continue | ||
| try: | ||
| setattr(parent, attr, replacement) | ||
| except Exception: | ||
| # Best-effort: patching should never crash import/initialization. | ||
| continue | ||
| print(f"[TE-FL] NPU backend patches applied") | ||
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.
add try-catch
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.
Thanks for the suggestion. I've added try-catch blocks for module imports as suggested. Please review the updated code.