|
3 | 3 | # SPDX-License-Identifier: Apache-2.0 |
4 | 4 |
|
5 | 5 | import os |
| 6 | +from pathlib import Path |
6 | 7 |
|
7 | 8 | import build_hooks # our build backend |
8 | 9 | from setuptools import setup |
|
11 | 12 |
|
12 | 13 | nthreads = int(os.environ.get("CUDA_PYTHON_PARALLEL_LEVEL", os.cpu_count() // 2)) |
13 | 14 | coverage_mode = bool(int(os.environ.get("CUDA_PYTHON_COVERAGE", "0"))) |
| 15 | +_ROOT_DIR = Path(__file__).resolve().parent |
| 16 | +_AOTI_SHIM_DEF_FILE = _ROOT_DIR / "cuda" / "core" / "_include" / "aoti_shim.def" |
| 17 | +_AOTI_SHIM_LIB_FILE = _ROOT_DIR / "build" / "aoti_shim.lib" |
| 18 | +_TENSOR_BRIDGE_EXT_NAME = "cuda.core._tensor_bridge" |
| 19 | + |
| 20 | + |
| 21 | +def _ensure_compiler_initialized(compiler, plat_name): |
| 22 | + initialize = getattr(compiler, "initialize", None) |
| 23 | + if callable(initialize) and not getattr(compiler, "initialized", False): |
| 24 | + if plat_name is None: |
| 25 | + initialize() |
| 26 | + else: |
| 27 | + initialize(plat_name) |
| 28 | + |
| 29 | + |
| 30 | +def _build_aoti_shim_lib(compiler): |
| 31 | + # Reuse setuptools' initialized MSVC compiler instead of rediscovering |
| 32 | + # lib.exe separately in the build backend. |
| 33 | + lib_exe = getattr(compiler, "lib", None) |
| 34 | + if not lib_exe: |
| 35 | + raise RuntimeError("MSVC compiler did not expose lib.exe after initialization.") |
| 36 | + |
| 37 | + _AOTI_SHIM_LIB_FILE.parent.mkdir(exist_ok=True) |
| 38 | + compiler.spawn( |
| 39 | + [ |
| 40 | + lib_exe, |
| 41 | + f"/DEF:{_AOTI_SHIM_DEF_FILE}", |
| 42 | + f"/OUT:{_AOTI_SHIM_LIB_FILE}", |
| 43 | + "/MACHINE:X64", |
| 44 | + ] |
| 45 | + ) |
| 46 | + return str(_AOTI_SHIM_LIB_FILE) |
14 | 47 |
|
15 | 48 |
|
16 | 49 | class build_ext(_build_ext): # noqa: N801 |
| 50 | + def _configure_windows_tensor_bridge(self): |
| 51 | + if os.name != "nt" or getattr(self.compiler, "compiler_type", None) != "msvc": |
| 52 | + return |
| 53 | + |
| 54 | + # _tensor_bridge imports AOTI symbols from torch_cpu.dll, which on |
| 55 | + # Windows requires a stub import library for the MSVC linker. |
| 56 | + for ext in self.extensions: |
| 57 | + if ext.name != _TENSOR_BRIDGE_EXT_NAME: |
| 58 | + continue |
| 59 | + |
| 60 | + _ensure_compiler_initialized(self.compiler, self.plat_name) |
| 61 | + shim_lib = _build_aoti_shim_lib(self.compiler) |
| 62 | + link_args = list(ext.extra_link_args or []) |
| 63 | + if shim_lib not in link_args: |
| 64 | + ext.extra_link_args = [*link_args, shim_lib] |
| 65 | + return |
| 66 | + |
| 67 | + raise RuntimeError(f"Failed to find extension {_TENSOR_BRIDGE_EXT_NAME!r} for Windows build.") |
| 68 | + |
17 | 69 | def build_extensions(self): |
18 | 70 | self.parallel = nthreads |
| 71 | + self._configure_windows_tensor_bridge() |
19 | 72 | super().build_extensions() |
20 | 73 |
|
21 | 74 |
|
|
0 commit comments