-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathsetup.py
More file actions
557 lines (511 loc) Β· 22.2 KB
/
setup.py
File metadata and controls
557 lines (511 loc) Β· 22.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
"""
NumKong build configuration.
This file configures wheels compilation for NumKong 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 glob
import os
import platform
import re
import subprocess
import sys
from pathlib import Path
from setuptools import Extension, setup
from setuptools.command.build_ext import build_ext
__lib_name__ = "numkong"
__version__ = Path("VERSION").read_text().strip()
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"[NumKong] Ignoring invalid DEVELOPER_DIR={_bad_dev_dir!r}")
os.environ.pop("DEVELOPER_DIR", None)
def is_64bit_x86() -> bool:
"""Detect x86-64 architecture with environment override support."""
override = os.environ.get("NK_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("NK_TARGET_ARM_")
if override is not None:
return override == "1"
arch = platform.machine().lower()
return (arch in ("arm64", "aarch64")) and (sys.maxsize > 2**32)
def is_64bit_riscv() -> bool:
"""Detect RISC-V 64-bit architecture with environment override support."""
override = os.environ.get("NK_TARGET_RISCV_")
if override is not None:
return override == "1"
arch = platform.machine().lower()
return (arch in ("riscv64",)) and (sys.maxsize > 2**32)
def has_darwin_sme_support() -> bool:
"""Check whether the host compiler supports the AArch64 SME ABI on Darwin.
Apple Clang (Xcode 16+, AppleClang 16+) backported the Darwin SME ABI.
Upstream LLVM only gained Darwin SME ABI support in version 19, so
Homebrew / upstream Clang 18 will crash with:
"Calling convention AArch64_SME_ABI_Support_Routines_PreserveMost_From_X0
is unsupported on Darwin."
"""
if not is_64bit_arm():
return False
try:
result = subprocess.run(["clang", "--version"], capture_output=True, text=True)
output = result.stdout
for line in output.split("\n"):
if "version" not in line.lower():
continue
match = re.search(r"version\s+(\d+)\.(\d+)", line)
if not match:
continue
major = int(match.group(1))
# Apple Clang identifies itself with "Apple" in the version string
if "Apple" in output:
return major >= 16
# Upstream / Homebrew LLVM: Darwin SME ABI landed in LLVM 19
return major >= 19
except Exception:
pass
return False
def linux_settings() -> tuple[list[str], list[str], list[tuple[str, str]]]:
"""Build settings for Linux."""
compile_args = [
"-std=c11",
"-O3",
"-fdiagnostics-color=always",
"-fvisibility=default",
"-fPIC",
"-w", # Hush warnings
]
# On RISC-V, GCC needs `-march` with the V extension for vector types to be
# available at translation-unit scope (`#pragma GCC target` only affects
# codegen, not type declarations).
# Keep the module-wide baseline portable (`rv64gcv`) so wheels can import on
# weaker emulated CPUs. Richer kernels still compile through the explicit
# NK_TARGET_* defines below plus per-function target attributes.
if is_64bit_riscv():
compile_args.append("-march=rv64gcv")
link_args = [
"-shared",
"-lm", # Add vectorized `logf` implementation from the `glibc`
]
# On Linux with GCC, enable all SIMD targets for the detected architecture
macros = [
("NK_DYNAMIC_DISPATCH", "1"),
("NK_NATIVE_F16", "0"),
("NK_NATIVE_BF16", "0"),
# x86 targets
("NK_TARGET_HASWELL", "1" if is_64bit_x86() else "0"),
("NK_TARGET_SKYLAKE", "1" if is_64bit_x86() else "0"),
("NK_TARGET_ICELAKE", "1" if is_64bit_x86() else "0"),
("NK_TARGET_GENOA", "1" if is_64bit_x86() else "0"),
("NK_TARGET_SAPPHIRE", "1" if is_64bit_x86() else "0"),
("NK_TARGET_TURIN", "1" if is_64bit_x86() else "0"),
("NK_TARGET_ALDER", "1" if is_64bit_x86() else "0"),
("NK_TARGET_SIERRA", "1" if is_64bit_x86() else "0"),
("NK_TARGET_SAPPHIREAMX", "1" if is_64bit_x86() else "0"),
("NK_TARGET_GRANITEAMX", "1" if is_64bit_x86() else "0"),
# ARM NEON targets
("NK_TARGET_NEON", "1" if is_64bit_arm() else "0"),
("NK_TARGET_NEONHALF", "1" if is_64bit_arm() else "0"),
("NK_TARGET_NEONSDOT", "1" if is_64bit_arm() else "0"),
("NK_TARGET_NEONBFDOT", "1" if is_64bit_arm() else "0"),
("NK_TARGET_NEONFHM", "1" if is_64bit_arm() else "0"),
# ARM SVE targets
("NK_TARGET_SVE", "1" if is_64bit_arm() else "0"),
("NK_TARGET_SVEHALF", "1" if is_64bit_arm() else "0"),
("NK_TARGET_SVEBFDOT", "1" if is_64bit_arm() else "0"),
("NK_TARGET_SVESDOT", "1" if is_64bit_arm() else "0"),
("NK_TARGET_SVE2", "1" if is_64bit_arm() else "0"),
("NK_TARGET_SVE2P1", "1" if is_64bit_arm() else "0"),
# ARM SME targets
("NK_TARGET_SME", "1" if is_64bit_arm() else "0"),
("NK_TARGET_SME2", "1" if is_64bit_arm() else "0"),
("NK_TARGET_SME2P1", "1" if is_64bit_arm() else "0"),
("NK_TARGET_SMEF64", "1" if is_64bit_arm() else "0"),
("NK_TARGET_SMEHALF", "1" if is_64bit_arm() else "0"),
("NK_TARGET_SMEBF16", "1" if is_64bit_arm() else "0"),
("NK_TARGET_SMEBI32", "1" if is_64bit_arm() else "0"),
("NK_TARGET_SMELUT2", "1" if is_64bit_arm() else "0"),
("NK_TARGET_SMEFA64", "1" if is_64bit_arm() else "0"),
# RISC-V targets
("NK_TARGET_RVV", "1" if is_64bit_riscv() else "0"),
("NK_TARGET_RVVHALF", "1" if is_64bit_riscv() else "0"),
("NK_TARGET_RVVBF16", "1" if is_64bit_riscv() else "0"),
("NK_TARGET_RVVBB", "1" if is_64bit_riscv() 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",
"-w", # Hush warnings
]
link_args: list[str] = []
# SME available on M4+ with AppleClang 16+ (Xcode 16) or upstream Clang 19+
has_sme = has_darwin_sme_support()
# macOS: no SVE, conservative AVX-512 (not widely available)
macros = [
("NK_DYNAMIC_DISPATCH", "1"),
("NK_NATIVE_F16", "0"),
("NK_NATIVE_BF16", "0"),
# x86 targets - conservative for macOS compatibility
("NK_TARGET_HASWELL", "1" if is_64bit_x86() else "0"),
("NK_TARGET_SKYLAKE", "0"), # AVX-512 not common on Mac
("NK_TARGET_ICELAKE", "0"),
("NK_TARGET_GENOA", "0"),
("NK_TARGET_SAPPHIRE", "0"),
("NK_TARGET_TURIN", "0"),
("NK_TARGET_ALDER", "0"),
("NK_TARGET_SIERRA", "0"),
("NK_TARGET_SAPPHIREAMX", "0"),
("NK_TARGET_GRANITEAMX", "0"),
# ARM NEON targets - NEON only on Apple Silicon
("NK_TARGET_NEON", "1" if is_64bit_arm() else "0"),
("NK_TARGET_NEONHALF", "1" if is_64bit_arm() else "0"),
("NK_TARGET_NEONSDOT", "1" if is_64bit_arm() else "0"),
("NK_TARGET_NEONBFDOT", "1" if is_64bit_arm() else "0"),
("NK_TARGET_NEONFHM", "1" if is_64bit_arm() else "0"),
# ARM SVE targets - not available on Apple Silicon
("NK_TARGET_SVE", "0"),
("NK_TARGET_SVEHALF", "0"),
("NK_TARGET_SVEBFDOT", "0"),
("NK_TARGET_SVESDOT", "0"),
("NK_TARGET_SVE2", "0"),
("NK_TARGET_SVE2P1", "0"),
# ARM SME targets - M4+ with AppleClang 16+ (Xcode 16)
("NK_TARGET_SME", "1" if has_sme else "0"),
("NK_TARGET_SME2", "1" if has_sme else "0"),
("NK_TARGET_SME2P1", "1" if has_sme else "0"),
("NK_TARGET_SMEF64", "1" if has_sme else "0"),
("NK_TARGET_SMEHALF", "1" if has_sme else "0"),
("NK_TARGET_SMEBF16", "1" if has_sme else "0"),
("NK_TARGET_SMEBI32", "1" if has_sme else "0"),
("NK_TARGET_SMELUT2", "1" if has_sme else "0"),
("NK_TARGET_SMEFA64", "1" if has_sme else "0"),
# RISC-V targets - not available on macOS
("NK_TARGET_RVV", "0"),
("NK_TARGET_RVVHALF", "0"),
("NK_TARGET_RVVBF16", "0"),
("NK_TARGET_RVVBB", "0"),
]
return compile_args, link_args, macros
def freebsd_settings() -> tuple[list[str], list[str], list[tuple[str, str]]]:
"""Build settings for FreeBSD."""
compile_args = [
"-std=c11",
"-O3",
"-fdiagnostics-color=always",
"-fvisibility=default",
"-fPIC",
"-w", # Hush warnings
]
link_args = [
"-shared",
"-lm", # Math library
]
# FreeBSD: Similar to Linux, enable all SIMD targets for detected architecture
macros = [
("NK_DYNAMIC_DISPATCH", "1"),
("NK_NATIVE_F16", "0"),
("NK_NATIVE_BF16", "0"),
# x86 targets
("NK_TARGET_HASWELL", "1" if is_64bit_x86() else "0"),
("NK_TARGET_SKYLAKE", "1" if is_64bit_x86() else "0"),
("NK_TARGET_ICELAKE", "1" if is_64bit_x86() else "0"),
("NK_TARGET_GENOA", "1" if is_64bit_x86() else "0"),
("NK_TARGET_SAPPHIRE", "1" if is_64bit_x86() else "0"),
("NK_TARGET_TURIN", "1" if is_64bit_x86() else "0"),
("NK_TARGET_ALDER", "1" if is_64bit_x86() else "0"),
("NK_TARGET_SIERRA", "1" if is_64bit_x86() else "0"),
("NK_TARGET_SAPPHIREAMX", "0"), # AMX may not be available on FreeBSD
("NK_TARGET_GRANITEAMX", "0"),
# ARM NEON targets
("NK_TARGET_NEON", "1" if is_64bit_arm() else "0"),
("NK_TARGET_NEONHALF", "1" if is_64bit_arm() else "0"),
("NK_TARGET_NEONSDOT", "1" if is_64bit_arm() else "0"),
("NK_TARGET_NEONBFDOT", "1" if is_64bit_arm() else "0"),
("NK_TARGET_NEONFHM", "1" if is_64bit_arm() else "0"),
# ARM SVE targets
("NK_TARGET_SVE", "1" if is_64bit_arm() else "0"),
("NK_TARGET_SVEHALF", "1" if is_64bit_arm() else "0"),
("NK_TARGET_SVEBFDOT", "1" if is_64bit_arm() else "0"),
("NK_TARGET_SVESDOT", "1" if is_64bit_arm() else "0"),
("NK_TARGET_SVE2", "1" if is_64bit_arm() else "0"),
("NK_TARGET_SVE2P1", "1" if is_64bit_arm() else "0"),
# ARM SME targets (may require newer FreeBSD)
("NK_TARGET_SME", "1" if is_64bit_arm() else "0"),
("NK_TARGET_SME2", "1" if is_64bit_arm() else "0"),
("NK_TARGET_SME2P1", "1" if is_64bit_arm() else "0"),
("NK_TARGET_SMEF64", "1" if is_64bit_arm() else "0"),
("NK_TARGET_SMEHALF", "1" if is_64bit_arm() else "0"),
("NK_TARGET_SMEBF16", "1" if is_64bit_arm() else "0"),
("NK_TARGET_SMEBI32", "1" if is_64bit_arm() else "0"),
("NK_TARGET_SMELUT2", "1" if is_64bit_arm() else "0"),
("NK_TARGET_SMEFA64", "1" if is_64bit_arm() else "0"),
# RISC-V targets
("NK_TARGET_RVV", "1" if is_64bit_riscv() else "0"),
("NK_TARGET_RVVHALF", "1" if is_64bit_riscv() else "0"),
("NK_TARGET_RVVBF16", "1" if is_64bit_riscv() else "0"),
("NK_TARGET_RVVBB", "1" if is_64bit_riscv() else "0"),
]
return compile_args, link_args, macros
def detect_msvc_version() -> tuple[int, int]:
"""Detect MSVC version from cl.exe or environment variables."""
try:
result = subprocess.run(["cl"], capture_output=True, text=True, shell=True)
# Parse version from output like "Microsoft (R) C/C++ Optimizing Compiler Version 19.30.30705"
for line in result.stderr.split("\n"):
if "Version" in line:
parts = line.split()
for i, part in enumerate(parts):
if part == "Version" and i + 1 < len(parts):
version_str = parts[i + 1]
# Version format is like 19.30.30705
version_parts = version_str.split(".")
if len(version_parts) >= 2:
major = int(version_parts[0])
minor = int(version_parts[1])
print(f"[NumKong] Detected MSVC version {major}.{minor}")
return (major, minor)
except Exception:
pass
# Fallback to checking _MSC_VER from environment or defaults
# MSVC 2019: 19.20-19.29
# MSVC 2022: 19.30-19.39
print("[NumKong] MSVC version detection failed, using conservative defaults (MSVC 2019)")
return (19, 20) # Conservative default to MSVC 2019
def windows_settings() -> tuple[list[str], list[str], list[tuple[str, str]]]:
"""Build settings for Windows."""
compile_args = [
"/std:c11",
"/O2",
# 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] = []
# Detect MSVC version for feature support
msvc_major, msvc_minor = detect_msvc_version()
# MSVC 19.44+ (VS 2022 17.14+): all AVX-512 intrinsics available without /arch:AVX512
has_full_avx512 = msvc_major >= 19 and msvc_minor >= 44
# Windows: SVE/SME not supported, x86 SIMD support varies by MSVC version
macros = [
("NK_DYNAMIC_DISPATCH", "1"),
("NK_NATIVE_F16", "0"),
("NK_NATIVE_BF16", "0"),
# x86 targets - base support
("NK_TARGET_HASWELL", "1" if is_64bit_x86() else "0"),
("NK_TARGET_SKYLAKE", "1" if is_64bit_x86() else "0"),
("NK_TARGET_ICELAKE", "1" if is_64bit_x86() else "0"),
# Advanced x86 targets - require MSVC 19.44+ for full AVX-512 FP16/BF16/VNNI
("NK_TARGET_GENOA", "1" if (is_64bit_x86() and has_full_avx512) else "0"),
("NK_TARGET_SAPPHIRE", "1" if (is_64bit_x86() and has_full_avx512) else "0"),
("NK_TARGET_TURIN", "1" if (is_64bit_x86() and has_full_avx512) else "0"),
("NK_TARGET_ALDER", "1" if (is_64bit_x86() and has_full_avx512) else "0"),
("NK_TARGET_SIERRA", "1" if (is_64bit_x86() and has_full_avx512) else "0"),
("NK_TARGET_SAPPHIREAMX", "1" if (is_64bit_x86() and has_full_avx512) else "0"),
("NK_TARGET_GRANITEAMX", "1" if (is_64bit_x86() and has_full_avx512) else "0"),
# ARM NEON targets
("NK_TARGET_NEON", "1" if is_64bit_arm() else "0"),
("NK_TARGET_NEONHALF", "0"), # MSVC lacks `float16_t` intrinsics
("NK_TARGET_NEONSDOT", "1" if is_64bit_arm() else "0"),
("NK_TARGET_NEONBFDOT", "0"), # MSVC lacks `bfloat16x8_t` intrinsics
("NK_TARGET_NEONFHM", "0"), # MSVC lacks FHM intrinsics
# ARM SVE targets - not supported on Windows
("NK_TARGET_SVE", "0"),
("NK_TARGET_SVEHALF", "0"),
("NK_TARGET_SVEBFDOT", "0"),
("NK_TARGET_SVESDOT", "0"),
("NK_TARGET_SVE2", "0"),
("NK_TARGET_SVE2P1", "0"),
# ARM SME targets - not supported on Windows
("NK_TARGET_SME", "0"),
("NK_TARGET_SME2", "0"),
("NK_TARGET_SME2P1", "0"),
("NK_TARGET_SMEF64", "0"),
("NK_TARGET_SMEHALF", "0"),
("NK_TARGET_SMEBF16", "0"),
("NK_TARGET_SMEBI32", "0"),
("NK_TARGET_SMELUT2", "0"),
("NK_TARGET_SMEFA64", "0"),
# RISC-V targets - not supported on Windows
("NK_TARGET_RVV", "0"),
("NK_TARGET_RVVHALF", "0"),
("NK_TARGET_RVVBF16", "0"),
("NK_TARGET_RVVBB", "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
def emscripten_settings() -> tuple[list[str], list[str], list[tuple[str, str]]]:
"""Build settings for Emscripten/Pyodide (WASM)."""
compile_args = [
"-std=c11",
"-O3",
"-w",
]
link_args: list[str] = []
# Dynamic dispatch is needed for the Python bindings (nk_find_kernel_punned).
# The EM_JS runtime probes in c/numkong.c are guarded by NK_DYNAMIC_DISPATCH
# and __EMSCRIPTEN__; when building as a Pyodide side module, we define
# NK_PYODIDE_SIDE_MODULE to replace them with conservative stubs (serial only).
macros = [
("NK_DYNAMIC_DISPATCH", "1"),
("NK_PYODIDE_SIDE_MODULE", "1"),
("NK_NATIVE_F16", "0"),
("NK_NATIVE_BF16", "0"),
# No x86, ARM, or RISC-V targets in WASM
("NK_TARGET_HASWELL", "0"),
("NK_TARGET_SKYLAKE", "0"),
("NK_TARGET_ICELAKE", "0"),
("NK_TARGET_GENOA", "0"),
("NK_TARGET_SAPPHIRE", "0"),
("NK_TARGET_TURIN", "0"),
("NK_TARGET_ALDER", "0"),
("NK_TARGET_SIERRA", "0"),
("NK_TARGET_SAPPHIREAMX", "0"),
("NK_TARGET_GRANITEAMX", "0"),
("NK_TARGET_NEON", "0"),
("NK_TARGET_NEONHALF", "0"),
("NK_TARGET_NEONSDOT", "0"),
("NK_TARGET_NEONBFDOT", "0"),
("NK_TARGET_NEONFHM", "0"),
("NK_TARGET_SVE", "0"),
("NK_TARGET_SVEHALF", "0"),
("NK_TARGET_SVEBFDOT", "0"),
("NK_TARGET_SVESDOT", "0"),
("NK_TARGET_SVE2", "0"),
("NK_TARGET_SVE2P1", "0"),
("NK_TARGET_SME", "0"),
("NK_TARGET_SME2", "0"),
("NK_TARGET_SME2P1", "0"),
("NK_TARGET_SMEF64", "0"),
("NK_TARGET_SMEHALF", "0"),
("NK_TARGET_SMEBF16", "0"),
("NK_TARGET_SMEBI32", "0"),
("NK_TARGET_SMELUT2", "0"),
("NK_TARGET_SMEFA64", "0"),
("NK_TARGET_RVV", "0"),
("NK_TARGET_RVVHALF", "0"),
("NK_TARGET_RVVBF16", "0"),
("NK_TARGET_RVVBB", "0"),
]
return compile_args, link_args, macros
# pyodide-build sets _PYTHON_HOST_PLATFORM to "emscripten-wasm32" during cross-compilation.
# sys.platform remains "darwin" or "linux" on the host, so we check this env var first.
_host_platform = os.environ.get("_PYTHON_HOST_PLATFORM", "")
if "emscripten" in _host_platform:
compile_args, link_args, macros = emscripten_settings()
elif sys.platform == "linux":
compile_args, link_args, macros = linux_settings()
elif sys.platform.startswith("freebsd"):
# FreeBSD platform strings can be "freebsd11", "freebsd12", etc.
compile_args, link_args, macros = freebsd_settings()
elif sys.platform == "darwin":
compile_args, link_args, macros = darwin_settings()
elif sys.platform == "win32":
compile_args, link_args, macros = windows_settings()
else:
# Default to minimal settings for unknown platforms
compile_args, link_args, macros = [], [], []
def _is_editable_install() -> bool:
if "develop" in sys.argv or ("install" in sys.argv and "-e" in sys.argv):
return True
return any(Path(p, f"{__lib_name__}.egg-link").exists() for p in sys.path)
SETUP_KWARGS = (
{
"packages": ["numkong"],
"package_dir": {"numkong": "python/annotations"},
"package_data": {"numkong": ["__init__.pyi", "py.typed"]},
}
if not _is_editable_install()
else {}
)
if _is_editable_install():
print("[NumKong] Editable install detected - skipping bundled type stubs.")
# Use glob to find all dispatch files
base_sources = [
"python/numkong.c",
"python/tensor.c",
"python/matrix.c",
"python/types.c",
"python/distance.c",
"python/each.c",
"python/mesh.c",
"python/maxsim.c",
"python/numpy_interop.c",
"c/numkong.c",
]
dispatch_sources = sorted(glob.glob("c/dispatch_*.c"))
ext_modules = [
Extension(
"numkong",
sources=base_sources + dispatch_sources,
include_dirs=["include", "python"],
language="c",
extra_compile_args=compile_args,
extra_link_args=link_args,
define_macros=macros,
)
]
class ParallelBuildExt(build_ext):
def initialize_options(self):
super().initialize_options()
# In Docker containers (e.g. cibuildwheel), `os.cpu_count()` returns the
# *host* core count, not the container's allocated vCPUs. Launching dozens
# of heavy SIMD compilation jobs in parallel OOMs the container (exit 143).
self.parallel = int(os.environ.get("NK_BUILD_PARALLEL", min(os.cpu_count() or 1, 4)))
setup(
name=__lib_name__,
cmdclass={"build_ext": ParallelBuildExt},
version=__version__,
author="Ash Vardanian",
author_email="1983160+ashvardanian@users.noreply.github.com",
url="https://github.com/ashvardanian/NumKong",
description="Portable mixed-precision BLAS-like vector math library for x86 and ARM",
long_description=(
Path("python/README.md").read_text(encoding="utf8")
+ "\n\n"
+ Path("README.md").read_text(encoding="utf8")
),
long_description_content_type="text/markdown",
license="Apache-2.0",
classifiers=[
"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",
],
python_requires=">=3.9",
ext_modules=ext_modules,
zip_safe=False,
include_package_data=True,
**SETUP_KWARGS,
)