|
15 | 15 | from pathlib import Path |
16 | 16 | from typing import List |
17 | 17 |
|
18 | | -from setuptools import find_packages, setup |
19 | | -from torch.utils.cpp_extension import BuildExtension, CppExtension |
| 18 | +from setuptools import Extension, find_packages, setup |
| 19 | +from setuptools.command.build_ext import build_ext |
20 | 20 |
|
21 | 21 | ROOT_DIR = Path(__file__).parent.resolve() |
22 | 22 |
|
| 23 | + |
| 24 | +def get_python_executable(): |
| 25 | + # Check if we're running in a virtual environment |
| 26 | + if "VIRTUAL_ENV" in os.environ: |
| 27 | + # Get the virtual environment's Python executable |
| 28 | + python_executable = os.path.join(os.environ["VIRTUAL_ENV"], "bin", "python") |
| 29 | + else: |
| 30 | + # Fall back to sys.executable |
| 31 | + python_executable = sys.executable |
| 32 | + return python_executable |
| 33 | + |
| 34 | + |
23 | 35 | try: |
24 | 36 | sha = ( |
25 | 37 | subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=ROOT_DIR) |
@@ -69,7 +81,7 @@ def _get_pytorch_version(is_nightly, is_local): |
69 | 81 | return "torch>=2.7.0.dev" |
70 | 82 | if is_local: |
71 | 83 | return "torch" |
72 | | - return "torch>=2.6.0" |
| 84 | + return "torch>=2.5.0" |
73 | 85 |
|
74 | 86 |
|
75 | 87 | def _get_packages(): |
@@ -99,51 +111,60 @@ def run(self): |
99 | 111 | shutil.rmtree(str(path), ignore_errors=True) |
100 | 112 |
|
101 | 113 |
|
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", |
| 114 | +class CMakeExtension(Extension): |
| 115 | + def __init__(self, name, sourcedir=""): |
| 116 | + super().__init__(name, sources=[]) |
| 117 | + self.sourcedir = os.path.abspath(sourcedir) |
| 118 | + |
| 119 | + |
| 120 | +class CMakeBuild(build_ext): |
| 121 | + def run(self): |
| 122 | + for ext in self.extensions: |
| 123 | + self.build_extension(ext) |
| 124 | + |
| 125 | + def build_extension(self, ext): |
| 126 | + extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name))) |
| 127 | + cmake_args = [ |
| 128 | + f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={extdir}", |
| 129 | + f"-DPYTHON_EXECUTABLE={get_python_executable()}", |
| 130 | + f"-DPython3_EXECUTABLE={get_python_executable()}", |
111 | 131 | ] |
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, |
| 132 | + CONDA_PREFIX = os.environ.get("CONDA_PREFIX") |
| 133 | + # if CONDA_PREFIX: |
| 134 | + # CMAKE_PREFIX_PATH = os.environ.get("CMAKE_PREFIX_PATH") |
| 135 | + # if CMAKE_PREFIX_PATH: |
| 136 | + # cmake_args.append(f"-DCMAKE_PREFIX_PATH={CONDA_PREFIX}:{CMAKE_PREFIX_PATH}") |
| 137 | + # else: |
| 138 | + # cmake_args.append(f"-DCMAKE_PREFIX_PATH={CONDA_PREFIX}") |
| 139 | + if CONDA_PREFIX: |
| 140 | + # Find pybind11 |
| 141 | + pybind11_dir = None |
| 142 | + for config_file in ["pybind11Config.cmake", "pybind11-config.cmake"]: |
| 143 | + config_path = glob.glob( |
| 144 | + os.path.join(CONDA_PREFIX, "**", config_file), recursive=True |
| 145 | + ) |
| 146 | + if config_path: |
| 147 | + pybind11_dir = os.path.dirname(config_path[0]) |
| 148 | + break |
| 149 | + else: |
| 150 | + raise RuntimeError(f"could not find any of 'pybind11Config.cmake', 'pybind11-config.cmake'") |
| 151 | + if pybind11_dir: |
| 152 | + cmake_args.append(f"-DPYBIND11_DIR={pybind11_dir}") |
| 153 | + |
| 154 | + build_args = [] |
| 155 | + if not os.path.exists(self.build_temp): |
| 156 | + os.makedirs(self.build_temp) |
| 157 | + subprocess.check_call( |
| 158 | + ["cmake", ext.sourcedir] + cmake_args, cwd=self.build_temp |
| 159 | + ) |
| 160 | + subprocess.check_call( |
| 161 | + ["cmake", "--build", "."] + build_args, cwd=self.build_temp |
143 | 162 | ) |
144 | | - ] |
145 | 163 |
|
146 | | - return ext_modules |
| 164 | + |
| 165 | +def get_extensions(): |
| 166 | + extensions_dir = os.path.join(ROOT_DIR, "tensordict", "csrc") |
| 167 | + return [CMakeExtension("tensordict._C", sourcedir=extensions_dir)] |
147 | 168 |
|
148 | 169 |
|
149 | 170 | def _main(argv): |
@@ -181,7 +202,7 @@ def _main(argv): |
181 | 202 | ), |
182 | 203 | ext_modules=get_extensions(), |
183 | 204 | cmdclass={ |
184 | | - "build_ext": BuildExtension.with_options(no_python_abi_suffix=True), |
| 205 | + "build_ext": CMakeBuild, |
185 | 206 | "clean": clean, |
186 | 207 | }, |
187 | 208 | install_requires=[ |
|
0 commit comments