|
| 1 | +"""Build the precompiled Torch-TensorRT backend for ExecuTorch Python.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import importlib.metadata |
| 6 | +import os |
| 7 | +import pathlib |
| 8 | +import re |
| 9 | +import subprocess |
| 10 | +import sys |
| 11 | + |
| 12 | +import torch |
| 13 | +from torch.utils.cpp_extension import include_paths |
| 14 | +from setuptools import Extension, find_packages, setup |
| 15 | +from setuptools.command.build_ext import build_ext |
| 16 | + |
| 17 | +HERE = pathlib.Path(__file__).resolve().parent |
| 18 | +REPO_ROOT = HERE.parents[1] |
| 19 | + |
| 20 | + |
| 21 | +def torchtrt_version() -> str: |
| 22 | + if value := os.getenv("TORCH_TENSORRT_EXECUTORCH_DELEGATE_VERSION"): |
| 23 | + return value |
| 24 | + source = (REPO_ROOT / "py/torch_tensorrt/_version.py").read_text() |
| 25 | + match = re.search(r'^__version__\s*=\s*["\']([^"\']+)', source, re.MULTILINE) |
| 26 | + if not match: |
| 27 | + raise RuntimeError("Could not determine the Torch-TensorRT version") |
| 28 | + return match.group(1) |
| 29 | + |
| 30 | + |
| 31 | +class CMakeExtension(Extension): |
| 32 | + def __init__(self, name: str) -> None: |
| 33 | + super().__init__(name, sources=[]) |
| 34 | + |
| 35 | + |
| 36 | +class CMakeBuild(build_ext): |
| 37 | + def build_extension(self, ext: Extension) -> None: |
| 38 | + if sys.platform != "linux": |
| 39 | + raise RuntimeError("The ExecuTorch TensorRT delegate supports Linux only") |
| 40 | + executorch_source = os.getenv("EXECUTORCH_SOURCE_DIR") |
| 41 | + if not executorch_source: |
| 42 | + raise RuntimeError( |
| 43 | + "Set EXECUTORCH_SOURCE_DIR to the pinned ExecuTorch source tree" |
| 44 | + ) |
| 45 | + |
| 46 | + output = pathlib.Path(self.get_ext_fullpath(ext.name)).resolve() |
| 47 | + if output.exists(): |
| 48 | + return |
| 49 | + build_dir = pathlib.Path(self.build_temp) / "executorch_delegate_native" |
| 50 | + output.parent.mkdir(parents=True, exist_ok=True) |
| 51 | + build_dir.mkdir(parents=True, exist_ok=True) |
| 52 | + configure = [ |
| 53 | + "cmake", |
| 54 | + "-S", |
| 55 | + str(HERE / "native"), |
| 56 | + "-B", |
| 57 | + str(build_dir), |
| 58 | + f"-DEXECUTORCH_SOURCE_DIR={pathlib.Path(executorch_source).resolve()}", |
| 59 | + f"-DTORCH_TENSORRT_SOURCE_DIR={REPO_ROOT}", |
| 60 | + f"-DPYTHON_EXECUTABLE={sys.executable}", |
| 61 | + f"-DTORCH_PRIMARY_INCLUDE_DIR={include_paths()[0]}", |
| 62 | + f"-DDELEGATE_LIBRARY_OUTPUT_DIRECTORY={output.parent}", |
| 63 | + f"-DCMAKE_BUILD_TYPE={'Debug' if self.debug else 'Release'}", |
| 64 | + ] |
| 65 | + if value := os.getenv("TensorRT_ROOT"): |
| 66 | + configure.append(f"-DTensorRT_ROOT={value}") |
| 67 | + configure.extend(os.getenv("CMAKE_ARGS", "").split()) |
| 68 | + subprocess.run(configure, check=True) |
| 69 | + subprocess.run( |
| 70 | + [ |
| 71 | + "cmake", |
| 72 | + "--build", |
| 73 | + str(build_dir), |
| 74 | + "--target", |
| 75 | + "torch_tensorrt_executorch_portable_lib", |
| 76 | + "--parallel", |
| 77 | + str(self.parallel or os.cpu_count() or 1), |
| 78 | + ], |
| 79 | + check=True, |
| 80 | + ) |
| 81 | + library_stem = ( |
| 82 | + "_portable_lib" if ext.name.endswith("._portable_lib") else "data_loader" |
| 83 | + ) |
| 84 | + built = next(output.parent.glob(f"{library_stem}*.so"), None) |
| 85 | + if built is None: |
| 86 | + raise RuntimeError( |
| 87 | + f"CMake did not produce {library_stem} in {output.parent}" |
| 88 | + ) |
| 89 | + if built != output: |
| 90 | + built.replace(output) |
| 91 | + |
| 92 | + |
| 93 | +executorch_version = importlib.metadata.version("executorch") |
| 94 | +setup( |
| 95 | + name="torch-tensorrt-executorch-delegate", |
| 96 | + version=torchtrt_version(), |
| 97 | + description="Torch-TensorRT delegate for the ExecuTorch Python runtime", |
| 98 | + packages=find_packages(), |
| 99 | + ext_modules=[ |
| 100 | + CMakeExtension("torch_tensorrt_executorch_delegate._portable_lib"), |
| 101 | + CMakeExtension("torch_tensorrt_executorch_delegate.data_loader"), |
| 102 | + ], |
| 103 | + cmdclass={"build_ext": CMakeBuild}, |
| 104 | + python_requires=">=3.10", |
| 105 | + install_requires=[ |
| 106 | + f"torch=={torch.__version__}", |
| 107 | + f"executorch=={executorch_version}", |
| 108 | + ], |
| 109 | + zip_safe=False, |
| 110 | +) |
0 commit comments