__init__.py 14.2 KB
Newer Older
1
# Copyright (c) 2022-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
Przemek Tredak's avatar
Przemek Tredak committed
2
3
4
5
#
# See LICENSE for license information.

"""FW agnostic user-end APIs"""
6

7
import ctypes
8
9
10
import functools
import glob
import importlib
11
from importlib.metadata import version, distribution, PackageNotFoundError
12
import os
13
from pathlib import Path
14
15
16
17
import platform
import subprocess
import sys
import sysconfig
18
from typing import Optional, Tuple
yuguo's avatar
yuguo committed
19
from torch.utils.cpp_extension import IS_HIP_EXTENSION
Przemek Tredak's avatar
Przemek Tredak committed
20

21
22

@functools.lru_cache(maxsize=None)
23
def _is_package_installed(package) -> bool:
24
25
26
27
28
29
30
    """Check if the given package is installed via pip."""

    # This is needed because we only want to return true
    # if the python package is installed via pip, and not
    # if it's importable in the current directory due to
    # the presence of the shared library module.
    try:
31
        distribution(package)
32
33
34
35
36
    except PackageNotFoundError:
        return False
    return True


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
@functools.lru_cache(maxsize=None)
def _is_package_installed_from_wheel(package) -> bool:
    """Check if the given package is installed via PyPI."""

    if not _is_package_installed(package):
        return False

    te_dist = distribution(package)
    te_wheel_file = ""
    for file_path in te_dist.files:
        if file_path.name == "WHEEL":
            te_wheel_file = te_dist.locate_file("") / file_path
    if not te_wheel_file:
        return False

    with te_wheel_file.open("r") as f:
        for line in f:
            if line.startswith("Root-Is-Purelib:"):
                return line.strip().split(":")[1].strip().lower() == "true"
    return False


59
@functools.lru_cache(maxsize=None)
60
def _find_shared_object_in_te_dir(te_path: Path, prefix: str) -> Optional[Path]:
61
    """
62
63
64
65
66
67
    Find a shared object file with the given prefix within the top level TE directory.

    The following locations are searched:
        1. Top level directory (editable install).
        2. `transformer_engine` directory (source install).
        3. `wheel_lib` directory (PyPI install).
68
69
70
71
72

    Returns None if no shared object files are found.
    Raises an error if multiple shared object files are found.
    """

73
74
    # Ensure top level dir exists and has the module before searching.
    if not te_path.is_dir() or not (te_path / "transformer_engine").exists():
75
76
77
78
        return None

    files = []
    search_paths = (
79
80
81
        te_path,  # Editable build.
        te_path / "transformer_engine",  # Regular source build.
        te_path / "transformer_engine/wheel_lib",  # PyPI.
82
83
    )

84
    # Search.
85
86
87
88
89
90
    for dir_path in search_paths:
        if not dir_path.is_dir():
            continue
        for file_path in dir_path.iterdir():
            if file_path.name.startswith(prefix) and file_path.suffix == _get_sys_extension():
                files.append(file_path)
91
92
93
94
95
96
97
98
99
100
101

    if len(files) == 0:
        return None
    if len(files) == 1:
        return files[0]
    raise RuntimeError(f"Multiple files found: {files}")


@functools.lru_cache(maxsize=None)
def _get_shared_object_file(library: str) -> Path:
    """
102
103
104
105
106
107
    Path to shared object file for a Transformer Engine library.

    TE libraries are 'core', 'torch', or 'jax'. This function first
    searches in the imported TE directory, and then in the
    site-packages directory.

108
109
110
111
112
113
114
115
116
    """

    # Check provided input and determine the correct prefix for .so.
    assert library in ("core", "torch", "jax"), f"Unsupported TE library {library}."
    if library == "core":
        so_prefix = "libtransformer_engine"
    else:
        so_prefix = f"transformer_engine_{library}"

117
118
119
120
121
    # Search for shared lib in imported directory
    te_path = Path(importlib.util.find_spec("transformer_engine").origin).parent.parent
    so_path = _find_shared_object_in_te_dir(te_path, so_prefix)
    if so_path is not None:
        return so_path
122

123
124
125
126
127
    # Search for shared lib in site-packages directory
    te_path = Path(sysconfig.get_paths()["purelib"])
    so_path = _find_shared_object_in_te_dir(te_path, so_prefix)
    if so_path is not None:
        return so_path
128

129
130
131
    raise FileNotFoundError(
        f"Could not find shared object file for Transformer Engine {library} lib."
    )
Przemek Tredak's avatar
Przemek Tredak committed
132
133


134
135
136
137
138
139
140
141
142
143
144
145
146
def get_te_core_package_info() -> Tuple[bool, str, str]:
    """
    Check if Tranformer Engine core package is installed.
    Returns the module name and version if found.
    """

    te_core_packages = ("transformer-engine-cu12", "transformer-engine-cu13")
    for package in te_core_packages:
        if _is_package_installed(package):
            return True, package, version(package)
    return False, "", ""


147
@functools.lru_cache(maxsize=None)
148
def load_framework_extension(framework: str) -> None:
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
    """
    Load shared library with Transformer Engine framework bindings
    and check verify correctness if installed via PyPI.
    """

    # Supported frameworks.
    assert framework in ("jax", "torch"), f"Unsupported framework {framework}"

    # Name of the framework extension library.
    module_name = f"transformer_engine_{framework}"

    # Name of the pip extra dependency for framework extensions from PyPI.
    extra_dep_name = module_name
    if framework == "torch":
        extra_dep_name = "pytorch"

165
166
167
168
169
170
171
172
173
    # Find the TE packages. The core and framework packages can only be installed via PyPI.
    # For the `transformer-engine` package, we need to check explicity.
    te_core_installed, te_core_package_name, te_core_version = get_te_core_package_info()
    te_framework_installed = _is_package_installed(module_name)
    te_installed = _is_package_installed("transformer_engine")
    te_installed_via_pypi = _is_package_installed_from_wheel("transformer_engine")

    assert te_installed, "Could not find `transformer_engine`."

174
175
    # If the framework extension pip package is installed, it means that TE is installed via
    # PyPI. For this case we need to make sure that the metapackage, the core lib, and framework
176
177
178
179
180
181
182
    # extension are all installed via PyPI and have matching versions.
    if te_framework_installed:
        assert te_installed_via_pypi, "Could not find `transformer-engine` PyPI package."
        assert te_core_installed, "Could not find TE core package `transformer-engine-cu*`."

        assert version(module_name) == version("transformer-engine") == te_core_version, (
            "Transformer Engine package version mismatch. Found"
183
            f" {module_name} v{version(module_name)}, transformer-engine"
184
185
186
            f" v{version('transformer-engine')}, and {te_core_package_name}"
            f" v{te_core_version}. Install transformer-engine using "
            f"'pip3 install --no-build-isolation transformer-engine[{extra_dep_name}]==VERSION'"
187
188
189
190
191
192
193
        )

    # After all checks are completed, load the shared object file.
    spec = importlib.util.spec_from_file_location(module_name, _get_shared_object_file(framework))
    solib = importlib.util.module_from_spec(spec)
    sys.modules[module_name] = solib
    spec.loader.exec_module(solib)
Przemek Tredak's avatar
Przemek Tredak committed
194
195


196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
def sanity_checks_for_pypi_installation() -> None:
    """Ensure that package is installed correctly if using PyPI."""

    te_core_installed, te_core_package_name, te_core_version = get_te_core_package_info()
    te_installed = _is_package_installed("transformer_engine")
    te_installed_via_pypi = _is_package_installed_from_wheel("transformer_engine")

    assert te_installed, "Could not find `transformer-engine`."

    # If the core package is installed via PyPI.
    if te_core_installed:
        assert te_installed_via_pypi, "Could not find `transformer-engine` PyPI package."
        assert version("transformer-engine") == te_core_version, (
            "Transformer Engine package version mismatch. Found "
            f"transformer-engine v{version('transformer-engine')} "
            f"and {te_core_package_name} v{te_core_version}."
        )

    # Only the metapackage is found, invalid usecase.
    elif te_installed_via_pypi:
        raise RuntimeError(
            "Found empty `transformer-engine` meta package installed. "
            "Install `transformer-engine` with framework extensions via"
            "'pip3 install --no-build-isolation transformer-engine[pytorch,jax]==VERSION'"
            " or 'pip3 install transformer-engine[core]` for the TE core lib only. The `core_cu12`"
            " or `core_cu13` extra deps can be used to specify CUDA version for the TE core lib."
        )


225
@functools.lru_cache(maxsize=None)
226
227
def _get_sys_extension() -> str:
    """File extension for shared objects."""
Przemek Tredak's avatar
Przemek Tredak committed
228
229
230
    system = platform.system()

    if system == "Linux":
231
232
233
234
235
236
        return ".so"
    if system == "Darwin":
        return ".dylib"
    if system == "Windows":
        return ".dll"
    raise RuntimeError(f"Unsupported operating system ({system})")
237
238


239
@functools.lru_cache(maxsize=None)
240
def _nvidia_cudart_include_dir() -> str:
241
242
243
244
245
246
247
    """Returns the include directory for cuda_runtime.h if exists in python environment."""

    try:
        import nvidia
    except ModuleNotFoundError:
        return ""

248
    # Installing some nvidia-* packages, like nvshmem, create nvidia name, so "import nvidia"
249
250
251
252
253
    # above doesn't throw. However, they don't set "__file__" attribute.
    if nvidia.__file__ is not None:
        nvidia_root = Path(nvidia.__file__).parent
    else:
        nvidia_root = Path(nvidia.__path__[0])  # namespace package
254

255
    include_dir = nvidia_root / "cuda_runtime"
256
257
258
    return str(include_dir) if include_dir.exists() else ""


259
@functools.lru_cache(maxsize=None)
260
261
262
def _load_cuda_library_from_python(lib_name: str, strict: bool = False):
    """
    Attempts to load shared object file installed via python packages.
263

264
265
266
    `lib_name` : Name of package as found in the `nvidia` dir in python environment.
    `strict` : If set to `True`, throw an error if lib is not found.
    """
267

268
269
    ext = _get_sys_extension()
    nvidia_dir = os.path.join(sysconfig.get_path("purelib"), "nvidia")
270

271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
    # PyPI packages provided by nvidia libs exist
    # in 4 possible locations inside `nvidia`.
    # Check by order of priority.
    path_found = False
    if os.path.isdir(os.path.join(nvidia_dir, "cu13", lib_name)):
        so_paths = glob.glob(os.path.join(nvidia_dir, "cu13", lib_name, f"lib/lib*{ext}.*[0-9]"))
        path_found = len(so_paths) > 0

    if not path_found and os.path.isdir(os.path.join(nvidia_dir, "cu13")):
        so_paths = glob.glob(os.path.join(nvidia_dir, "cu13", f"lib/lib{lib_name}*{ext}.*[0-9]"))
        path_found = len(so_paths) > 0

    if not path_found and os.path.isdir(os.path.join(nvidia_dir, lib_name)):
        so_paths = glob.glob(os.path.join(nvidia_dir, lib_name, f"lib/lib*{ext}.*[0-9]"))
        path_found = len(so_paths) > 0
286

287
288
289
    if not path_found:
        so_paths = glob.glob(os.path.join(nvidia_dir, f"cuda_{lib_name}", f"lib/lib*{ext}.*[0-9]"))
        path_found = len(so_paths) > 0
290

291
292
293
294
295
296
297
298
299
300
    ctypes_handles = []

    if path_found:
        for so_path in so_paths:
            ctypes_handles.append(ctypes.CDLL(so_path, mode=ctypes.RTLD_GLOBAL))

    if strict and not path_found:
        raise RuntimeError(f"{lib_name} shared object not found.")

    return path_found, ctypes_handles
301
302


303
@functools.lru_cache(maxsize=None)
304
305
306
307
308
309
def _load_cuda_library_from_system(lib_name: str):
    """
    Attempts to load shared object file installed via system/cuda-toolkit.

    `lib_name`: Name of library to load without extension or `lib` prefix.
    """
310

311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
    # Where to look for the shared lib in decreasing order of preference.
    paths = (
        os.environ.get(f"{lib_name.upper()}_HOME"),
        os.environ.get(f"{lib_name.upper()}_PATH"),
        os.environ.get("CUDA_HOME"),
        os.environ.get("CUDA_PATH"),
        "/usr/local/cuda",
    )

    for path in paths:
        if path is None:
            continue
        libs = glob.glob(f"{path}/**/lib{lib_name}{_get_sys_extension()}*", recursive=True)
        libs = [lib for lib in libs if "stub" not in lib]
        libs.sort(reverse=True, key=os.path.basename)
        if libs:
            return True, ctypes.CDLL(libs[0], mode=ctypes.RTLD_GLOBAL)
328

329
330
331
332
333
334
    # Search in LD_LIBRARY_PATH.
    try:
        _lib_handle = ctypes.CDLL(f"lib{lib_name}{_get_sys_extension()}", mode=ctypes.RTLD_GLOBAL)
        return True, _lib_handle
    except OSError:
        return False, None
335
336


vasunvidia's avatar
vasunvidia committed
337
@functools.lru_cache(maxsize=None)
338
339
340
341
342
343
344
345
346
def _load_cuda_library(lib_name: str):
    """
    Load given shared library.
    Prioritize loading from system/toolkit
    before checking python packages.
    """

    # Attempt to locate library in system.
    found, handle = _load_cuda_library_from_system(lib_name)
vasunvidia's avatar
vasunvidia committed
347
    if found:
348
        return True, handle
vasunvidia's avatar
vasunvidia committed
349

350
351
352
353
    # Attempt to locate library in Python dist-packages.
    found, handle = _load_cuda_library_from_python(lib_name)
    if found:
        return False, handle
vasunvidia's avatar
vasunvidia committed
354

355
    raise RuntimeError(f"{lib_name} shared object not found.")
vasunvidia's avatar
vasunvidia committed
356
357


358
359
360
361
362
363
@functools.lru_cache(maxsize=None)
def _load_core_library():
    """Load shared library with Transformer Engine C extensions"""
    return ctypes.CDLL(_get_shared_object_file("core"), mode=ctypes.RTLD_GLOBAL)


364
if "NVTE_PROJECT_BUILDING" not in os.environ or bool(int(os.getenv("NVTE_RELEASE_BUILD", "0"))):
yuguo's avatar
yuguo committed
365
    try:
wenjh's avatar
wenjh committed
366
        sanity_checks_for_pypi_installation()
wenjh's avatar
wenjh committed
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382

        # `_load_cuda_library` is used for packages that must be loaded
        # during runtime. Both system and pypi packages are searched
        # and an error is thrown if not found.
        _, _CUDNN_LIB_CTYPES = _load_cuda_library("cudnn")
        system_nvrtc, _NVRTC_LIB_CTYPES = _load_cuda_library("nvrtc")
        system_curand, _CURAND_LIB_CTYPES = _load_cuda_library("curand")

        # This additional step is necessary to be able to install TE wheels
        # and import TE (without any guards) in an environment where the cuda
        # toolkit might be absent without being guarded
        load_libs_for_no_ctk = not system_nvrtc and not system_curand
        if load_libs_for_no_ctk:
            _CUBLAS_LIB_CTYPES = _load_cuda_library_from_python("cublas", strict=True)
            _CUDART_LIB_CTYPES = _load_cuda_library_from_python("cudart", strict=True)
            _CUDNN_ALL_LIB_CTYPES = _load_cuda_library_from_python("cudnn", strict=True)
wenjh's avatar
wenjh committed
383

384
385
386
        # Needed to find the correct headers for NVRTC kernels.
        if not os.getenv("NVTE_CUDA_INCLUDE_DIR") and _nvidia_cudart_include_dir():
            os.environ["NVTE_CUDA_INCLUDE_DIR"] = _nvidia_cudart_include_dir()
yuguo's avatar
yuguo committed
387
    except OSError:
wenjh's avatar
wenjh committed
388
        pass
maxiao3's avatar
maxiao3 committed
389
    _TE_LIB_CTYPES = _load_core_library()