setup.py 3.46 KB
Newer Older
1
# Copyright (c) 2022-2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2
3
4
#
# See LICENSE for license information.

5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
"""Installation script for Transformer Engine JAX extensions.

This module handles the build and installation of the JAX-specific components
of Transformer Engine. It manages:
- JAX extension compilation with pybind11
- Common header file management
- Build tool dependencies
- Package metadata and dependencies

The script supports both development and release builds, with different
behaviors for:
- Build tool management
- Header file copying
- Extension compilation
- Package distribution
"""
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

# pylint: disable=wrong-import-position,wrong-import-order

import sys
import os
import shutil
from pathlib import Path

import setuptools

try:
    import jax  # pylint: disable=unused-import
except ImportError as e:
    raise RuntimeError("This package needs Jax to build.") from e


current_file_path = Path(__file__).parent.resolve()
build_tools_dir = current_file_path.parent.parent / "build_tools"
if bool(int(os.getenv("NVTE_RELEASE_BUILD", "0"))) or os.path.isdir(build_tools_dir):
40
41
42
43
    build_tools_copy = current_file_path / "build_tools"
    if build_tools_copy.exists():
        shutil.rmtree(build_tools_copy)
    shutil.copytree(build_tools_dir, build_tools_copy)
44
45
46


from build_tools.build_ext import get_build_ext
47
from build_tools.utils import copy_common_headers, min_python_version_str
48
from build_tools.te_version import te_version
49
from build_tools.jax import setup_jax_extension, install_requirements, test_requirements
50
51
52

from pybind11.setup_helpers import build_ext as BuildExtension

53
os.environ["NVTE_PROJECT_BUILDING"] = "1"
54
CMakeBuildExtension = get_build_ext(BuildExtension, True)
55
56
57


if __name__ == "__main__":
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
    """Main entry point for JAX extension installation.

    This section handles:
    1. Common header file management
       - Creates a temporary directory for common headers
       - Copies necessary header files from the common library

    2. Extension module setup
       - Configures the JAX-specific C++ extension
       - Sets up build paths and dependencies

    3. Package configuration
       - Sets package metadata
       - Configures build and install requirements
       - Sets up extension modules

    4. Cleanup
       - Removes temporary directories after build
       - Cleans up build tools if not in release mode

    Environment variables:
    - NVTE_RELEASE_BUILD: Controls release build behavior
    - NVTE_PROJECT_BUILDING: Set to "1" during build

    Note:
        The script requires JAX to be installed for building.
        It will raise a RuntimeError if JAX is not available.
    """
86

87
88
    # Extensions
    common_headers_dir = "common_headers"
89
    copy_common_headers(current_file_path.parent, str(current_file_path / common_headers_dir))
90
91
    ext_modules = [
        setup_jax_extension(
92
93
94
            "csrc", current_file_path / "csrc", current_file_path / common_headers_dir
        )
    ]
95
96
97
98
99
100
101
102

    # Configure package
    setuptools.setup(
        name="transformer_engine_jax",
        version=te_version(),
        description="Transformer acceleration library - Jax Lib",
        ext_modules=ext_modules,
        cmdclass={"build_ext": CMakeBuildExtension},
103
        python_requires=f">={min_python_version_str()}",
104
105
        install_requires=install_requirements(),
        tests_require=test_requirements(),
106
107
108
    )
    if any(x in sys.argv for x in (".", "sdist", "bdist_wheel")):
        shutil.rmtree(common_headers_dir)
109
        shutil.rmtree("build_tools")