|
5 | 5 |
|
6 | 6 | import argparse |
7 | 7 | import distutils.command.clean |
8 | | -import glob |
9 | 8 | import logging |
10 | 9 | import os |
11 | 10 | import shutil |
|
15 | 14 | from pathlib import Path |
16 | 15 | from typing import List |
17 | 16 |
|
18 | | -from setuptools import find_packages, setup |
19 | | -from torch.utils.cpp_extension import BuildExtension, CppExtension |
| 17 | +from setuptools import Extension, find_packages, setup |
| 18 | +from setuptools.command.build_ext import build_ext |
20 | 19 |
|
21 | 20 | ROOT_DIR = Path(__file__).parent.resolve() |
22 | 21 |
|
| 22 | + |
| 23 | +def get_python_executable(): |
| 24 | + # Check if we're running in a virtual environment |
| 25 | + if "VIRTUAL_ENV" in os.environ: |
| 26 | + # Get the virtual environment's Python executable |
| 27 | + python_executable = os.path.join(os.environ["VIRTUAL_ENV"], "bin", "python") |
| 28 | + else: |
| 29 | + # Fall back to sys.executable |
| 30 | + python_executable = sys.executable |
| 31 | + return python_executable |
| 32 | + |
| 33 | + |
23 | 34 | try: |
24 | 35 | sha = ( |
25 | 36 | subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=ROOT_DIR) |
@@ -69,7 +80,7 @@ def _get_pytorch_version(is_nightly, is_local): |
69 | 80 | return "torch>=2.7.0.dev" |
70 | 81 | if is_local: |
71 | 82 | return "torch" |
72 | | - return "torch>=2.6.0" |
| 83 | + return "torch>=2.5.0" |
73 | 84 |
|
74 | 85 |
|
75 | 86 | def _get_packages(): |
@@ -99,51 +110,38 @@ def run(self): |
99 | 110 | shutil.rmtree(str(path), ignore_errors=True) |
100 | 111 |
|
101 | 112 |
|
102 | | -def get_extensions(): |
103 | | - extension = CppExtension |
104 | | - |
105 | | - extra_link_args = [] |
106 | | - extra_compile_args = { |
107 | | - "cxx": [ |
108 | | - "-O3", |
109 | | - "-std=c++17", |
110 | | - "-fdiagnostics-color=always", |
| 113 | +class CMakeExtension(Extension): |
| 114 | + def __init__(self, name, sourcedir=""): |
| 115 | + super().__init__(name, sources=[]) |
| 116 | + self.sourcedir = os.path.abspath(sourcedir) |
| 117 | + |
| 118 | + |
| 119 | +class CMakeBuild(build_ext): |
| 120 | + def run(self): |
| 121 | + for ext in self.extensions: |
| 122 | + self.build_extension(ext) |
| 123 | + |
| 124 | + def build_extension(self, ext): |
| 125 | + extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name))) |
| 126 | + cmake_args = [ |
| 127 | + f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={extdir}", |
| 128 | + f"-DPYTHON_EXECUTABLE={get_python_executable()}", |
| 129 | + f"-DPython3_EXECUTABLE={get_python_executable()}", |
111 | 130 | ] |
112 | | - } |
113 | | - debug_mode = os.getenv("DEBUG", "0") == "1" |
114 | | - if debug_mode: |
115 | | - logging.info("Compiling in debug mode") |
116 | | - extra_compile_args = { |
117 | | - "cxx": [ |
118 | | - "-O0", |
119 | | - "-fno-inline", |
120 | | - "-g", |
121 | | - "-std=c++17", |
122 | | - "-fdiagnostics-color=always", |
123 | | - ] |
124 | | - } |
125 | | - extra_link_args = ["-O0", "-g"] |
126 | | - |
127 | | - this_dir = os.path.dirname(os.path.abspath(__file__)) |
128 | | - extensions_dir = os.path.join(this_dir, "tensordict", "csrc") |
129 | | - |
130 | | - extension_sources = { |
131 | | - os.path.join(extensions_dir, p) |
132 | | - for p in glob.glob(os.path.join(extensions_dir, "*.cpp")) |
133 | | - } |
134 | | - sources = list(extension_sources) |
135 | | - |
136 | | - ext_modules = [ |
137 | | - extension( |
138 | | - "tensordict._C", |
139 | | - sources, |
140 | | - include_dirs=[this_dir], |
141 | | - extra_compile_args=extra_compile_args, |
142 | | - extra_link_args=extra_link_args, |
| 131 | + build_args = [] |
| 132 | + if not os.path.exists(self.build_temp): |
| 133 | + os.makedirs(self.build_temp) |
| 134 | + subprocess.check_call( |
| 135 | + ["cmake", ext.sourcedir] + cmake_args, cwd=self.build_temp |
| 136 | + ) |
| 137 | + subprocess.check_call( |
| 138 | + ["cmake", "--build", "."] + build_args, cwd=self.build_temp |
143 | 139 | ) |
144 | | - ] |
145 | 140 |
|
146 | | - return ext_modules |
| 141 | + |
| 142 | +def get_extensions(): |
| 143 | + extensions_dir = os.path.join(ROOT_DIR, "tensordict", "csrc") |
| 144 | + return [CMakeExtension("tensordict._C", sourcedir=extensions_dir)] |
147 | 145 |
|
148 | 146 |
|
149 | 147 | def _main(argv): |
@@ -181,7 +179,7 @@ def _main(argv): |
181 | 179 | ), |
182 | 180 | ext_modules=get_extensions(), |
183 | 181 | cmdclass={ |
184 | | - "build_ext": BuildExtension.with_options(no_python_abi_suffix=True), |
| 182 | + "build_ext": CMakeBuild, |
185 | 183 | "clean": clean, |
186 | 184 | }, |
187 | 185 | install_requires=[ |
|
0 commit comments