forked from flagos-ai/TransformerEngine-FL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
96 lines (79 loc) · 3.38 KB
/
Copy path__init__.py
File metadata and controls
96 lines (79 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# Copyright (c) 2022-2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
#
# See LICENSE for license information.
"""Top level package"""
# pylint: disable=unused-import
import os
from importlib import metadata
import transformer_engine.common
import torch
# Public, simple global (kept for backward compatibility).
TE_DEVICE_TYPE = "cuda"
TE_PLATFORM = torch.cuda
# Apply MUSA (VENDOR) Patches, such as torch.cuda.device -> torch.musa.device
try:
from .plugin.core.backends.vendor.musa.patches import apply_patch as _musa_apply_patch
_musa_apply_patch()
except Exception as e:
pass
def te_device_type(default: str = "cuda") -> str:
try:
return TE_DEVICE_TYPE
except Exception:
return default
def te_platform(default=torch.cuda):
try:
return TE_PLATFORM
except Exception:
return default
try:
from . import pytorch
except ImportError:
pass
except FileNotFoundError as e:
if "Could not find shared object file" not in str(e):
raise e # Unexpected error
else:
if os.getenv("NVTE_FRAMEWORK"):
frameworks = os.getenv("NVTE_FRAMEWORK").split(",")
if "pytorch" in frameworks or "all" in frameworks:
raise e
else:
# If we got here, we could import `torch` but could not load the framework extension.
# This can happen when a user wants to work only with `transformer_engine.jax` on a system that
# also has a PyTorch installation. In order to enable that use case, we issue a warning here
# about the missing PyTorch extension in case the user hasn't set NVTE_FRAMEWORK.
import warnings
warnings.warn(
"Detected a PyTorch installation but could not find the shared object file for the "
"Transformer Engine PyTorch extension library. If this is not intentional, please "
"reinstall Transformer Engine with `pip install transformer_engine[pytorch]` or "
"build from source with `NVTE_FRAMEWORK=pytorch`.",
category=RuntimeWarning,
)
try:
from . import jax
except ImportError:
pass
except FileNotFoundError as e:
if "Could not find shared object file" not in str(e):
raise e # Unexpected error
else:
if os.getenv("NVTE_FRAMEWORK"):
frameworks = os.getenv("NVTE_FRAMEWORK").split(",")
if "jax" in frameworks or "all" in frameworks:
raise e
else:
# If we got here, we could import `jax` but could not load the framework extension.
# This can happen when a user wants to work only with `transformer_engine.pytorch` on a system
# that also has a Jax installation. In order to enable that use case, we issue a warning here
# about the missing Jax extension in case the user hasn't set NVTE_FRAMEWORK.
import warnings
warnings.warn(
"Detected a Jax installation but could not find the shared object file for the "
"Transformer Engine Jax extension library. If this is not intentional, please "
"reinstall Transformer Engine with `pip install transformer_engine[jax]` or "
"build from source with `NVTE_FRAMEWORK=jax`.",
category=RuntimeWarning,
)
__version__ = str(metadata.version("transformer_engine"))