forked from ashvardanian/SimSIMD
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
285 lines (250 loc) · 11.2 KB
/
setup.py
File metadata and controls
285 lines (250 loc) · 11.2 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# -*- coding: utf-8 -*-
"""
SimSIMD build configuration.
This file configures wheels compilation for SimSIMD CPython bindings.
The architecture detection uses environment variable overrides (set via cibuildwheel)
to support cross-compilation scenarios like building ARM64 wheels on x64 hosts.
"""
from __future__ import annotations
import os
import sys
import platform
from pathlib import Path
from typing import List, Tuple
from setuptools import setup, Extension
__lib_name__ = "simsimd"
__version__ = Path("VERSION").read_text().strip()
# --------------------------------------------------------------------------- #
# macOS developer-tools sanity check #
# --------------------------------------------------------------------------- #
if sys.platform == "darwin":
_bad_dev_dir = os.environ.get("DEVELOPER_DIR")
if _bad_dev_dir and (_bad_dev_dir == "public" or not Path(_bad_dev_dir).exists()):
print(f"[SimSIMD] Ignoring invalid DEVELOPER_DIR={_bad_dev_dir!r}")
os.environ.pop("DEVELOPER_DIR", None)
# --------------------------------------------------------------------------- #
# Architecture detection with environment override support #
# --------------------------------------------------------------------------- #
def is_64bit_x86() -> bool:
"""Detect x86-64 architecture with environment override support."""
override = os.environ.get("SIMSIMD_TARGET_X86")
if override is not None:
return override == "1"
arch = platform.machine().lower()
return (arch in ("x86_64", "x64", "amd64")) and (sys.maxsize > 2**32)
def is_64bit_arm() -> bool:
"""Detect ARM64 architecture with environment override support."""
override = os.environ.get("SIMSIMD_TARGET_ARM64")
if override is not None:
return override == "1"
arch = platform.machine().lower()
return (arch in ("arm64", "aarch64")) and (sys.maxsize > 2**32)
# --------------------------------------------------------------------------- #
# Per-platform build settings #
# --------------------------------------------------------------------------- #
def linux_settings() -> Tuple[List[str], List[str], List[Tuple[str, str]]]:
"""Build settings for Linux."""
compile_args = [
"-std=c11",
"-O3",
"-ffast-math",
"-fdiagnostics-color=always",
"-fvisibility=default",
"-fPIC",
"-w", # Hush warnings
"-fopenmp", # Enable OpenMP for parallelization
]
link_args = [
"-shared",
"-fopenmp", # Link against OpenMP
"-lm", # Add vectorized `logf` implementation from the `glibc`
]
# On Linux with GCC, enable all SIMD targets for the detected architecture
macros = [
("SIMSIMD_DYNAMIC_DISPATCH", "1"),
("SIMSIMD_NATIVE_F16", "0"),
("SIMSIMD_NATIVE_BF16", "0"),
# x86 targets
("SIMSIMD_TARGET_HASWELL", "1" if is_64bit_x86() else "0"),
("SIMSIMD_TARGET_SKYLAKE", "1" if is_64bit_x86() else "0"),
("SIMSIMD_TARGET_ICE", "1" if is_64bit_x86() else "0"),
("SIMSIMD_TARGET_GENOA", "1" if is_64bit_x86() else "0"),
("SIMSIMD_TARGET_SAPPHIRE", "1" if is_64bit_x86() else "0"),
("SIMSIMD_TARGET_TURIN", "1" if is_64bit_x86() else "0"),
("SIMSIMD_TARGET_SIERRA", "0"), # avx2vnni not supported by manylinux GCC
# ARM targets
("SIMSIMD_TARGET_NEON", "1" if is_64bit_arm() else "0"),
("SIMSIMD_TARGET_NEON_I8", "1" if is_64bit_arm() else "0"),
("SIMSIMD_TARGET_NEON_F16", "1" if is_64bit_arm() else "0"),
("SIMSIMD_TARGET_NEON_BF16", "1" if is_64bit_arm() else "0"),
("SIMSIMD_TARGET_SVE", "1" if is_64bit_arm() else "0"),
("SIMSIMD_TARGET_SVE_I8", "1" if is_64bit_arm() else "0"),
("SIMSIMD_TARGET_SVE_F16", "1" if is_64bit_arm() else "0"),
("SIMSIMD_TARGET_SVE_BF16", "1" if is_64bit_arm() else "0"),
("SIMSIMD_TARGET_SVE2", "1" if is_64bit_arm() else "0"),
]
return compile_args, link_args, macros
def darwin_settings() -> Tuple[List[str], List[str], List[Tuple[str, str]]]:
"""Build settings for macOS."""
compile_args = [
"-std=c11",
"-O3",
"-ffast-math",
"-w", # Hush warnings
]
link_args: List[str] = []
# macOS: no SVE, conservative AVX-512 (not widely available)
macros = [
("SIMSIMD_DYNAMIC_DISPATCH", "1"),
("SIMSIMD_NATIVE_F16", "0"),
("SIMSIMD_NATIVE_BF16", "0"),
# x86 targets - conservative for macOS compatibility
("SIMSIMD_TARGET_HASWELL", "1" if is_64bit_x86() else "0"),
("SIMSIMD_TARGET_SKYLAKE", "0"), # AVX-512 not common on Mac
("SIMSIMD_TARGET_ICE", "0"),
("SIMSIMD_TARGET_GENOA", "0"),
("SIMSIMD_TARGET_SAPPHIRE", "0"),
("SIMSIMD_TARGET_TURIN", "0"),
("SIMSIMD_TARGET_SIERRA", "0"),
# ARM targets - NEON only, no SVE on Apple Silicon
("SIMSIMD_TARGET_NEON", "1" if is_64bit_arm() else "0"),
("SIMSIMD_TARGET_NEON_I8", "1" if is_64bit_arm() else "0"),
("SIMSIMD_TARGET_NEON_F16", "1" if is_64bit_arm() else "0"),
("SIMSIMD_TARGET_NEON_BF16", "1" if is_64bit_arm() else "0"),
("SIMSIMD_TARGET_SVE", "0"),
("SIMSIMD_TARGET_SVE_I8", "0"),
("SIMSIMD_TARGET_SVE_F16", "0"),
("SIMSIMD_TARGET_SVE_BF16", "0"),
("SIMSIMD_TARGET_SVE2", "0"),
]
return compile_args, link_args, macros
def windows_settings() -> Tuple[List[str], List[str], List[Tuple[str, str]]]:
"""Build settings for Windows."""
compile_args = [
"/std:c11",
"/O2",
"/fp:fast",
# Dealing with MinGW linking errors
# https://cibuildwheel.readthedocs.io/en/stable/faq/#windows-importerror-dll-load-failed-the-specific-module-could-not-be-found
"/d2FH4-",
"/w",
]
link_args: List[str] = []
# Windows: no SVE, conservative x86 SIMD, as MSVC lacks BF16/FP16 intrinsics support
macros = [
("SIMSIMD_DYNAMIC_DISPATCH", "1"),
("SIMSIMD_NATIVE_F16", "0"),
("SIMSIMD_NATIVE_BF16", "0"),
# x86 targets - conservative for MSVC compatibility
("SIMSIMD_TARGET_HASWELL", "1" if is_64bit_x86() else "0"),
("SIMSIMD_TARGET_SKYLAKE", "1" if is_64bit_x86() else "0"),
("SIMSIMD_TARGET_ICE", "1" if is_64bit_x86() else "0"),
("SIMSIMD_TARGET_GENOA", "0"), # BF16 intrinsics broken in MSVC
("SIMSIMD_TARGET_SAPPHIRE", "0"), # FP16 intrinsics broken in MSVC
("SIMSIMD_TARGET_TURIN", "0"), # `VP2INTERSECT` limited in MSVC
("SIMSIMD_TARGET_SIERRA", "0"), # AVX2 VNNI limits in MSVC
("SIMSIMD_TARGET_NEON", "1" if is_64bit_arm() else "0"),
("SIMSIMD_TARGET_NEON_I8", "1" if is_64bit_arm() else "0"),
("SIMSIMD_TARGET_NEON_F16", "0"), # MSVC lacks `float16_t` intrinsics
("SIMSIMD_TARGET_NEON_BF16", "0"), # MSVC lacks `bfloat16x8_t` intrinsics
("SIMSIMD_TARGET_SVE", "0"),
("SIMSIMD_TARGET_SVE_I8", "0"),
("SIMSIMD_TARGET_SVE_F16", "0"),
("SIMSIMD_TARGET_SVE_BF16", "0"),
("SIMSIMD_TARGET_SVE2", "0"),
]
# MSVC requires architecture-specific macros for winnt.h
if is_64bit_arm():
macros.append(("_ARM64_", "1"))
elif is_64bit_x86():
macros.append(("_AMD64_", "1"))
return compile_args, link_args, macros
# --------------------------------------------------------------------------- #
# Platform dispatch #
# --------------------------------------------------------------------------- #
if sys.platform == "linux":
compile_args, link_args, macros = linux_settings()
elif sys.platform == "darwin":
compile_args, link_args, macros = darwin_settings()
elif sys.platform == "win32":
compile_args, link_args, macros = windows_settings()
else:
compile_args, link_args, macros = [], [], []
# --------------------------------------------------------------------------- #
# Editable install detection #
# --------------------------------------------------------------------------- #
def _is_editable_install() -> bool:
if "develop" in sys.argv or ("install" in sys.argv and "-e" in sys.argv):
return True
for p in sys.path:
if Path(p, f"{__lib_name__}.egg-link").exists():
return True
return False
SETUP_KWARGS = (
{
"packages": ["simsimd"],
"package_dir": {"simsimd": "python/annotations"},
"package_data": {"simsimd": ["__init__.pyi", "py.typed"]},
}
if not _is_editable_install()
else {}
)
if _is_editable_install():
print("[SimSIMD] Editable install detected - skipping bundled type stubs.")
# --------------------------------------------------------------------------- #
# Extension module #
# --------------------------------------------------------------------------- #
ext_modules = [
Extension(
"simsimd",
sources=["python/lib.c", "c/lib.c"],
include_dirs=["include"],
language="c",
extra_compile_args=compile_args,
extra_link_args=link_args,
define_macros=macros,
)
]
# --------------------------------------------------------------------------- #
# Setup #
# --------------------------------------------------------------------------- #
setup(
name=__lib_name__,
version=__version__,
author="Ash Vardanian",
author_email="1983160+ashvardanian@users.noreply.github.com",
url="https://github.com/ashvardanian/simsimd",
description="Portable mixed-precision BLAS-like vector math library for x86 and ARM",
long_description=Path("README.md").read_text(encoding="utf8"),
long_description_content_type="text/markdown",
license="Apache-2.0",
classifiers=[
"License :: OSI Approved :: Apache Software License",
"Operating System :: POSIX :: Linux",
"Operating System :: Microsoft :: Windows",
"Operating System :: MacOS",
"Development Status :: 5 - Production/Stable",
"Natural Language :: English",
"Intended Audience :: Developers",
"Intended Audience :: Information Technology",
"Programming Language :: C",
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
"Programming Language :: Python :: 3.14",
"Programming Language :: Python :: Free Threading :: 3 - Stable",
"Topic :: Scientific/Engineering :: Mathematics",
"Topic :: Scientific/Engineering :: Information Analysis",
"Topic :: Scientific/Engineering :: Bio-Informatics",
"Topic :: Scientific/Engineering :: Chemistry",
"Topic :: Scientific/Engineering :: Artificial Intelligence",
],
ext_modules=ext_modules,
zip_safe=False,
include_package_data=True,
**SETUP_KWARGS,
)