-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsetup.py
More file actions
260 lines (245 loc) · 7.87 KB
/
Copy pathsetup.py
File metadata and controls
260 lines (245 loc) · 7.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
"""Package discovery for candle.
The main project metadata lives in pyproject.toml. This file handles package
discovery because setuptools' ``packages.find`` in pyproject.toml conflicts
with ``py_modules`` — we need both to install the candle package tree AND the
standalone ``_candle_torch_compat`` bootstrap module and its ``.pth`` trigger.
"""
import os
import platform
import shutil
from setuptools import setup, find_packages, Extension
from setuptools.command.build_py import build_py
from setuptools.command.build_ext import build_ext
from Cython.Build import cythonize
# ---------------------------------------------------------------------------
# Required Cython extensions (must build on every supported platform)
# ---------------------------------------------------------------------------
required_extensions = [
Extension(
"candle._C._stream",
["src/candle/_C/_stream.pyx"],
),
Extension(
"candle._C._future",
["src/candle/_C/_future.pyx"],
),
]
# ---------------------------------------------------------------------------
# Cross-platform extensions (Linux + macOS)
# ---------------------------------------------------------------------------
_system = platform.system()
_has_npu_cython = _system == "Linux"
cross_platform_extensions = []
if _system in ("Linux", "Darwin"):
cross_platform_extensions = [
Extension(
"candle._C._dispatch",
["src/candle/_C/_dispatch.pyx"],
),
Extension(
"candle._C._allocator",
["src/candle/_C/_allocator.pyx"],
),
Extension(
"candle._C._storage",
["src/candle/_C/_storage.pyx"],
),
Extension(
"candle._C._storage_impl",
["src/candle/_C/_storage_impl.pyx"],
),
Extension(
"candle._C._tensor_impl",
["src/candle/_C/_tensor_impl.pyx"],
),
Extension(
"candle._C._dispatcher_core",
["src/candle/_C/_dispatcher_core.pyx"],
),
Extension(
"candle._C._device",
["src/candle/_C/_device.pyx"],
),
Extension(
"candle._C._dtype",
["src/candle/_C/_dtype.pyx"],
),
Extension(
"candle._C._autograd_node",
["src/candle/_C/_autograd_node.pyx"],
),
Extension(
"candle._C._autograd_graph",
["src/candle/_C/_autograd_graph.pyx"],
),
Extension(
"candle._C._autograd_checkpoint",
["src/candle/_C/_autograd_checkpoint.pyx"],
),
Extension(
"candle._C._autograd_engine",
["src/candle/_C/_autograd_engine.pyx"],
),
Extension(
"candle._C._autograd_function",
["src/candle/_C/_autograd_function.pyx"],
),
Extension(
"candle._C._autograd_functions",
["src/candle/_C/_autograd_functions.pyx"],
),
Extension(
"candle._C._autograd_ops",
["src/candle/_C/_autograd_ops.pyx"],
),
Extension(
"candle._C._autograd_profiler",
["src/candle/_C/_autograd_profiler.pyx"],
),
Extension(
"candle._C._autograd_utils",
["src/candle/_C/_autograd_utils.pyx"],
),
Extension(
"candle._C._grad_mode_state",
["src/candle/_C/_grad_mode_state.pyx"],
),
Extension(
"candle._C._forward_ad",
["src/candle/_C/_forward_ad.pyx"],
),
Extension(
"candle._C._functional_ops",
["src/candle/_C/_functional_ops.pyx"],
),
Extension(
"candle._C._creation_ops",
["src/candle/_C/_creation_ops.pyx"],
),
Extension(
"candle._C._fast_ops",
["src/candle/_C/_fast_ops.pyx"],
),
Extension(
"candle._C._tensor_api",
["src/candle/_C/_tensor_api.pyx"],
),
Extension(
"candle._C._TensorBase",
["src/candle/_C/_TensorBase.pyx"],
),
Extension(
"candle._C._cpu_kernels",
["src/candle/_C/_cpu_kernels.pyx"],
extra_compile_args=["-O3", "-ffast-math"],
),
Extension(
"candle.distributed._c10d",
["src/candle/distributed/_c10d.pyx"],
),
Extension(
"candle.distributed._c10d_gloo",
["src/candle/distributed/_c10d_gloo.pyx"],
),
Extension(
"candle._C._mps_helpers",
["src/candle/_C/_mps_helpers.pyx"],
),
Extension(
"candle._C._mps_compute",
["src/candle/_C/_mps_compute.pyx"],
),
Extension(
"candle._C._mps_ops",
["src/candle/_C/_mps_ops.pyx"],
),
Extension(
"candle._C._dataloader_ops",
["src/candle/_C/_dataloader_ops.pyx"],
),
Extension(
"candle._generated._functions_cy",
["src/candle/_generated/_functions_cy.pyx"],
),
Extension(
"candle._generated._variable_type_cy",
["src/candle/_generated/_variable_type_cy.pyx"],
),
]
# ---------------------------------------------------------------------------
# Linux-only extensions (NPU/CANN/HCCL — not available on macOS)
# ---------------------------------------------------------------------------
linux_only_extensions = []
if _system == "Linux":
linux_only_extensions = [
Extension(
"candle._C._aclnn_ffi",
["src/candle/_C/_aclnn_ffi.pyx"],
libraries=["dl"],
),
Extension(
"candle._C._aclrt_ffi",
["src/candle/_C/_aclrt_ffi.pyx"],
libraries=["dl"],
),
Extension(
"candle._C._aclgraph",
["src/candle/_C/_aclgraph.pyx"],
libraries=["dl"],
),
Extension(
"candle._C._npu_ops",
["src/candle/_C/_npu_ops.pyx"],
),
Extension(
"candle._C._npu_storage",
["src/candle/_C/_npu_storage.pyx"],
),
Extension(
"candle.distributed._c10d_hccl",
["src/candle/distributed/_c10d_hccl.pyx"],
),
Extension(
"candle.distributed._ddp_fastpath",
["src/candle/distributed/_ddp_fastpath.pyx"],
),
Extension(
"candle.distributed._fsdp_fastpath",
["src/candle/distributed/_fsdp_fastpath.pyx"],
),
Extension(
"candle.distributed._dtensor_fastpath",
["src/candle/distributed/_dtensor_fastpath.pyx"],
),
]
ext_modules = cythonize(
required_extensions + cross_platform_extensions + linux_only_extensions,
compiler_directives={
"language_level": "3",
"boundscheck": False,
"wraparound": False,
},
compile_time_env={"HAS_NPU_CYTHON": _has_npu_cython},
nthreads=os.cpu_count() or 1,
)
class _BuildExt(build_ext):
"""Enable parallel C compilation by default."""
def finalize_options(self):
super().finalize_options()
if self.parallel is None:
self.parallel = os.cpu_count() or 1
class _BuildPy(build_py):
"""Copy the .pth file into build_lib so it lands in site-packages."""
def run(self):
super().run()
src = os.path.join("src", "candle-torch-compat.pth")
dst = os.path.join(self.build_lib, "candle-torch-compat.pth")
shutil.copy2(src, dst)
setup(
packages=find_packages(where="src", include=["candle*"]),
package_dir={"": "src"},
package_data={"candle": ["*.py", "*/*.py", "*/*/*.py", "*/*/*/*.py"]},
py_modules=["_candle_torch_compat"],
ext_modules=ext_modules,
cmdclass={"build_py": _BuildPy, "build_ext": _BuildExt},
)