-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathsetup.py
More file actions
47 lines (37 loc) · 1.18 KB
/
setup.py
File metadata and controls
47 lines (37 loc) · 1.18 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
"""Build configuration for optional C extension.
The C extension provides accelerated array operations for the pure Python
backend. If compilation fails (no C compiler), the package installs
normally and falls back to pure Python.
"""
import os
from setuptools import Extension, setup
from setuptools.command.build_ext import build_ext
_c_source = os.path.join(
"src",
"gradient_free_optimizers",
"_array_backend",
"_fast_ops.c",
)
class OptionalBuildExt(build_ext):
"""Build C extensions, silently skip on failure."""
def build_extension(self, ext): # noqa: D102
try:
super().build_extension(ext)
except Exception as e:
print(
f"WARNING: Could not build C extension '{ext.name}': {e}\n"
f"Falling back to pure Python array backend."
)
_ext_modules = []
if not os.environ.get("GFO_DISABLE_C_EXTENSION"):
_ext_modules.append(
Extension(
"gradient_free_optimizers._array_backend._fast_ops",
sources=[_c_source],
extra_compile_args=["-O2"],
),
)
setup(
cmdclass={"build_ext": OptionalBuildExt},
ext_modules=_ext_modules,
)