Repository navigation
Expand file tree
/
Copy pathsetup.py
More file actions
1451 lines (1266 loc) · 62.1 KB
/
Copy pathsetup.py
File metadata and controls
1451 lines (1266 loc) · 62.1 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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2026 FlagOS Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import glob
import importlib.machinery
import importlib.util
import json
import multiprocessing
import os
import platform
import re
import shutil
import subprocess
import sys
import sysconfig
from distutils.command.clean import clean
from setuptools import Extension, find_packages, setup
from setuptools.command.build_ext import build_ext as _build_ext
from setuptools.command.editable_wheel import editable_wheel as _editable_wheel
# Env Variables
IS_DARWIN = platform.system() == "Darwin"
IS_WINDOWS = platform.system() == "Windows"
# Accelerator platform: "cuda" (default), "ppu", "metax", "kunlunxin",
# "iluvatar", "ascend", "tsingmicro", "dcu", "gcu", "musa", or "bpu"
FLAGOS_ACCELERATOR = os.environ.get("FLAGOS_ACCELERATOR", "cuda").lower()
# Where the sources live. Distinct from BASE_DIR, which is repointed at a
# scratch tree when the generated files are redirected somewhere else -- the
# files this build reads back (torch_fl/_env.py, cmake/flagos_platforms.json) do
# not move with them.
SOURCE_DIR = os.path.dirname(os.path.realpath(__file__))
BASE_DIR = SOURCE_DIR
_PLATFORM_TABLE = None
def _platform_table() -> dict:
"""cmake/flagos_platforms.json: the platform/build matrix shared with CMake.
CMakeLists.txt reads the same file, so the kernel switches, the
bundled-libtorch dir, the platform list and the pins have one definition.
"""
global _PLATFORM_TABLE
if _PLATFORM_TABLE is None:
path = os.path.join(SOURCE_DIR, "cmake", "flagos_platforms.json")
with open(path, encoding="utf-8") as handle:
_PLATFORM_TABLE = json.load(handle)
return _PLATFORM_TABLE
def _platform_entry(accelerator: str) -> dict:
"""One accelerator's row from the shared table (raises on an unknown id)."""
table = _platform_table()
try:
return table["accelerators"][accelerator]
except KeyError:
known = ", ".join(sorted(table["accelerators"]))
raise ValueError(
f"Unknown FLAGOS_ACCELERATOR '{accelerator}'. Expected one of: {known}"
) from None
# The kernel sets a build can compile in, and their build-record names, from the
# shared table. Each name is simultaneously an environment variable, a CMake
# option() and one entry of the generated build_config.KERNELS, so no
# env-name -> -D-name -> record-name translation exists to fall out of sync.
KERNEL_SWITCHES = tuple(_platform_table()["kernel_switches"])
KERNEL_SET_NAME = dict(_platform_table()["kernel_set_names"])
# Directory inside the wheel holding a bundled forked libtorch, for the backends
# that ship one (see scripts/vendor/bundle_*_libtorch.sh). "lib" means "no separate
# bundle dir": the CUDA backend drops its extra .so straight into torch_fl/lib/.
# From the table -- the same value CMake's FLAGOS_BUNDLE_LIBDIR gets. _C.so's
# RUNPATH has to reach the bundle or its auditwheel-mangled deps
# (libglog-*.so.0) go missing.
_BUNDLE_LIBDIR = _platform_entry(FLAGOS_ACCELERATOR)["bundle_libdir"]
# Only run cmake build for actual build commands, not metadata collection
BUILD_COMMANDS = {
"build",
"build_ext",
"install",
"develop",
"bdist_wheel",
"bdist_egg",
"editable_wheel",
}
RUN_BUILD_DEPS = any(arg in BUILD_COMMANDS for arg in sys.argv)
def _ensure_metax_cudart_shim():
"""On MetaX, compile and load a complete cudart shim before importing torch.
MetaX's libsymbol_cu.so provides CUDA runtime symbols but without the
@@libcudart.so.12 version tags that PyTorch's .so files require.
We build a single shared library (csrc/runtime/accelerator/metax/cudart_shim.c) that:
1. Forwards ~79 symbols to libsymbol_cu.so via dlsym
2. Stubs ~11 symbols for APIs missing from MetaX entirely
3. Tags ALL exported symbols with @@libcudart.so.12 via a version script
"""
import ctypes
csrc = os.path.join(BASE_DIR, "csrc", "runtime", "accelerator", "metax")
build_dir = os.path.join(BASE_DIR, "build")
os.makedirs(build_dir, exist_ok=True)
shim_so = os.path.join(build_dir, "libcudart_shim.so")
shim_src = os.path.join(csrc, "cudart_shim.c")
version_script = os.path.join(csrc, "libcudart.version")
inputs = [shim_src, version_script]
if not os.path.exists(shim_so) or any(
os.path.exists(s) and os.path.getmtime(s) > os.path.getmtime(shim_so)
for s in inputs
):
subprocess.check_call(
[
"gcc",
"-shared",
"-fPIC",
"-o",
shim_so,
shim_src,
f"-Wl,--version-script={version_script}",
"-Wl,-soname,libcudart.so.12",
"-ldl",
]
)
ctypes.CDLL(shim_so, mode=ctypes.RTLD_GLOBAL)
if FLAGOS_ACCELERATOR == "metax":
_ensure_metax_cudart_shim()
def make_relative_rpath_args(path):
if IS_DARWIN:
return ["-Wl,-rpath,@loader_path/" + path]
elif IS_WINDOWS:
return []
else:
return ["-Wl,-rpath,$ORIGIN/" + path]
def get_pytorch_dir():
import torch
return os.path.dirname(os.path.realpath(torch.__file__))
def _cuda_toolkit_root() -> str | None:
"""Locate CUDA toolkit root (directory containing include/cuda_runtime.h)."""
candidates: list[str] = []
for key in ("CUDA_HOME", "CUDA_PATH"):
val = os.environ.get(key)
if val:
candidates.append(val)
conda_prefix = os.environ.get("CONDA_PREFIX")
if conda_prefix:
candidates.extend(
[
os.path.join(conda_prefix, "targets", "x86_64-linux"),
conda_prefix,
]
)
candidates.append("/usr/local/cuda")
seen: set[str] = set()
for root in candidates:
root = os.path.realpath(root)
if root in seen:
continue
seen.add(root)
if os.path.isfile(os.path.join(root, "include", "cuda_runtime.h")):
return root
return None
def _find_nvcc(cuda_root: str) -> str | None:
conda_prefix = os.environ.get("CONDA_PREFIX", "")
for candidate in (
os.path.join(cuda_root, "bin", "nvcc"),
os.path.join(conda_prefix, "bin", "nvcc") if conda_prefix else None,
shutil.which("nvcc"),
):
if candidate and os.path.isfile(candidate):
return os.path.realpath(candidate)
return None
def _prepend_env_path(env: dict, key: str, *paths: str) -> None:
parts = [p for p in paths if p and os.path.isdir(p)]
existing = env.get(key, "")
if existing:
parts.append(existing)
if parts:
env[key] = os.pathsep.join(parts)
def _pip_nvidia_include_dirs() -> list[str]:
"""Headers from pip nvidia-* wheels when conda toolkit is minimal."""
import pathlib
import site
dirs: list[str] = []
for sp in site.getsitepackages():
nvidia = pathlib.Path(sp) / "nvidia"
if not nvidia.is_dir():
continue
for pkg in sorted(nvidia.iterdir()):
inc = pkg / "include"
if inc.is_dir():
dirs.append(str(inc))
return dirs
def _setup_cuda_build_env(env: dict) -> str | None:
"""Export CUDA paths for cmake/nvcc (incl. conda pip wheel layout)."""
cuda_root = _cuda_toolkit_root()
if not cuda_root:
return None
env.setdefault("CUDA_HOME", cuda_root)
env.setdefault("CUDA_PATH", cuda_root)
_prepend_env_path(env, "CPATH", os.path.join(cuda_root, "include"))
_prepend_env_path(env, "CPATH", *_pip_nvidia_include_dirs())
_prepend_env_path(env, "LIBRARY_PATH", os.path.join(cuda_root, "lib"))
_prepend_env_path(env, "LD_LIBRARY_PATH", os.path.join(cuda_root, "lib"))
_prepend_env_path(env, "CMAKE_PREFIX_PATH", cuda_root)
return cuda_root
def _find_nvrtc_library() -> str | None:
try:
import importlib.util
import pathlib
spec = importlib.util.find_spec("nvidia.cuda_nvrtc")
if spec is None or not spec.origin:
return None
lib = pathlib.Path(spec.origin).resolve().parent / "lib" / "libnvrtc.so.12"
return str(lib) if lib.is_file() else None
except Exception:
return None
def _append_cuda_cmake_args(cmake_args: list[str], cuda_root: str) -> None:
nvcc = _find_nvcc(cuda_root)
if nvcc:
cmake_args.append(f"-DCMAKE_CUDA_COMPILER={nvcc}")
cmake_args.append(f"-DCUDAToolkit_ROOT={cuda_root}")
cmake_args.append(f"-DCUDA_TOOLKIT_ROOT_DIR={cuda_root}")
nvrtc = _find_nvrtc_library()
if nvrtc:
cmake_args.append(f"-DCUDA_nvrtc_LIBRARY={nvrtc}")
def _find_flaggems_dir() -> str | None:
env_dir = os.environ.get("FLAGGEMS_DIR")
if env_dir and os.path.isfile(os.path.join(env_dir, "FlagGemsConfig.cmake")):
return env_dir
import site
search_roots = list(site.getsitepackages())
user_site = site.getusersitepackages()
if user_site:
search_roots.append(user_site)
for sp in search_roots:
cand = os.path.join(sp, "flag_gems", "lib", "cmake", "FlagGems")
if os.path.isfile(os.path.join(cand, "FlagGemsConfig.cmake")):
return cand
return None
def _metax_path_from_env() -> str:
return os.environ.get("MACA_PATH") or os.environ.get("MACA_HOME") or "/opt/maca"
def _setup_metax_build_env(env: dict) -> str:
"""PATH/LD_LIBRARY_PATH for the MetaX SDK. Returns MACA_PATH.
MetaX is a CUDA-boxing build: host g++ compiles the generated boxing kernels
against maca's cu-bridge headers, so the SDK's include/lib directories have
to be reachable. The mxcc/cucc device compiler belonged to the retired
native-kernel path, so its absence is no longer an error here.
"""
metax_path = _metax_path_from_env()
cu_bridge = os.path.join(metax_path, "tools", "cu-bridge")
if not os.path.isdir(os.path.join(cu_bridge, "include")):
raise RuntimeError(f"MetaX cu-bridge headers not found: {cu_bridge}/include")
env.setdefault("MACA_PATH", metax_path)
env["PATH"] = os.pathsep.join(
p
for p in (
os.path.join(cu_bridge, "bin"),
os.path.join(metax_path, "bin"),
os.path.join(metax_path, "mxgpu_llvm", "bin"),
env.get("PATH", ""),
)
if p
)
ld_parts = [
os.path.join(metax_path, "lib"),
os.path.join(cu_bridge, "lib"),
os.path.join(metax_path, "mxgpu_llvm", "lib"),
env.get("LD_LIBRARY_PATH", ""),
]
env["LD_LIBRARY_PATH"] = os.pathsep.join(p for p in ld_parts if p)
return metax_path
def _dtk_root() -> str:
"""Hygon DTK install root, from ROCM_PATH (what DTK's env.sh exports), else
the default install location."""
path = os.environ.get("ROCM_PATH")
if path and os.path.isdir(path):
return path
default = "/opt/dtk"
if not os.path.isdir(default):
raise RuntimeError(
"FLAGOS_ACCELERATOR=dcu selected, but no DTK installation was found. "
"Source DTK's env.sh (which sets ROCM_PATH) or install DTK at /opt/dtk."
)
return default
def _kunlunxin_roots() -> tuple[str, str]:
"""Return the validated Kunlunxin XPU SDK root and libcudart directory.
The XPU SDK ships a CUDA-shaped runtime: cuda_runtime.h under
``XPU_ROOT/include`` and the libcudart shim under ``XCUDART_ROOT`` (the
SDK's own ``so/`` directory on a P800 install). Both are host-only build
inputs -- the runtime libraries are never copied into the wheel -- so this
only locates them. Returns ``(XPU_ROOT, <directory holding libcudart.so*>)``.
"""
xpu_root = os.path.realpath(os.environ.get("XPU_ROOT", "/usr/local/xpu"))
xcudart_root = os.path.realpath(
os.environ.get("XCUDART_ROOT", os.path.join(xpu_root, "so"))
)
if not os.path.isfile(os.path.join(xpu_root, "include", "cuda_runtime.h")):
raise RuntimeError(
"FLAGOS_ACCELERATOR=kunlunxin selected, but cuda_runtime.h was not "
f"found under {xpu_root}/include. Set XPU_ROOT to the Kunlunxin XPU "
"SDK root."
)
# CMakeLists.txt additionally requires the native xpu/ headers (the runtime
# the de-boxed backend actually calls); reject the same roots here so a
# CUDA-facade-only tree does not pass this precheck only to die at configure.
if not os.path.isfile(os.path.join(xpu_root, "include", "xpu", "runtime.h")):
raise RuntimeError(
"FLAGOS_ACCELERATOR=kunlunxin selected, but xpu/runtime.h was not "
f"found under {xpu_root}/include/xpu. Set XPU_ROOT to the Kunlunxin "
"XPU SDK root (the native libxpurt headers the backend links)."
)
# The shim's soname tracks the SDK's CUDA-compat version (libcudart.so.11.0
# on XPU SDK 5.0.21) rather than a fixed one, and it sits either directly in
# XCUDART_ROOT or one level down in lib/, so match the whole family in both
# spots instead of pinning a filename.
for cudart_dir in (xcudart_root, os.path.join(xcudart_root, "lib")):
if glob.glob(os.path.join(cudart_dir, "libcudart.so*")):
return xpu_root, cudart_dir
raise RuntimeError(
"FLAGOS_ACCELERATOR=kunlunxin selected, but no libcudart.so* was found "
f"under {xcudart_root} or {xcudart_root}/lib. Set XCUDART_ROOT to the "
"directory holding the XPU CUDA-compat runtime."
)
def _setup_kunlunxin_build_env(env: dict) -> tuple[str, str]:
"""CPATH/LIBRARY_PATH/LD_LIBRARY_PATH for the Kunlunxin XPU SDK.
Kunlunxin is a native, de-boxed backend: no tensor is re-dispatched onto the
CUDA dispatch key, so nothing here compiles boxing kernels. The SDK's
``include/`` and the libcudart shim's directory still have to be reachable
because the runtime sources link libxpurt and a hooks.h probe calls into the
CUDA-compat runtime; neither carries a kernel. Returns
``(XPU_ROOT, <directory holding libcudart.so*>)``.
"""
xpu_root, xcudart_dir = _kunlunxin_roots()
env.setdefault("XPU_ROOT", xpu_root)
env.setdefault("XCUDART_ROOT", xcudart_dir)
_prepend_env_path(env, "CPATH", os.path.join(xpu_root, "include"))
_prepend_env_path(env, "LIBRARY_PATH", xcudart_dir)
_prepend_env_path(env, "LD_LIBRARY_PATH", xcudart_dir)
return xpu_root, xcudart_dir
def _iluvatar_roots() -> str:
"""Return the validated Iluvatar corex SDK root.
corex is CUDA-shaped: ``cuda_runtime.h`` under ``COREX_PATH/include`` and
``libcudart.so.10.2`` under ``COREX_PATH/lib64`` (corex 4.5.0). Both are
host-only build inputs -- the runtime libraries are already installed on any
machine with the card and are never copied into the wheel -- so this only
locates them. Iluvatar is not a boxing build, so no toolchain probe is done;
the host g++ compiles the runtime shim against these headers.
"""
corex_root = os.path.realpath(os.environ.get("COREX_PATH", "/usr/local/corex"))
if not os.path.isfile(os.path.join(corex_root, "include", "cuda_runtime.h")):
raise RuntimeError(
"FLAGOS_ACCELERATOR=iluvatar selected, but cuda_runtime.h was not "
f"found under {corex_root}/include. Set COREX_PATH to the corex SDK "
"root."
)
for lib_dir in (os.path.join(corex_root, "lib64"), os.path.join(corex_root, "lib")):
if glob.glob(os.path.join(lib_dir, "libcudart.so*")):
return corex_root
raise RuntimeError(
"FLAGOS_ACCELERATOR=iluvatar selected, but no libcudart.so* was found "
f"under {corex_root}/lib64 or {corex_root}/lib. Set COREX_PATH to the "
"corex SDK root."
)
def _setup_iluvatar_build_env(env: dict) -> str:
"""CPATH/LIBRARY_PATH/LD_LIBRARY_PATH for the Iluvatar corex SDK.
The host g++ compiles the shared cuda/ runtime sources against corex's
CUDA-shaped headers and links its libcudart shim, so both the include and lib
directories have to be reachable. The same two directories carry the vendor
op libraries: ``include/cudnn.h`` + ``include/cublas_v2.h`` (thin umbrellas
over ``IX/ixdnn`` + ``IX/ixblas``) and ``lib64/libcudnn.so`` +
``lib64/libcublas.so``, which CMakeLists.txt's iluvatar arm locates under
``COREX_PATH`` -- so a FLAGOS_BUILD_VENDOR build compiles and links with no
extra paths. Returns the corex root.
"""
corex_root = _iluvatar_roots()
env.setdefault("COREX_PATH", corex_root)
_prepend_env_path(env, "CPATH", os.path.join(corex_root, "include"))
for lib_dir in (os.path.join(corex_root, "lib64"), os.path.join(corex_root, "lib")):
_prepend_env_path(env, "LIBRARY_PATH", lib_dir)
_prepend_env_path(env, "LD_LIBRARY_PATH", lib_dir)
return corex_root
def _kunlunxin_xdnn_root() -> str:
"""Resolve XDNN_ROOT: the vendor xdnn_pytorch op-library package root.
Expected layout, matching what CMakeLists.txt's find_library/find_path
consume: ``libxdnn_pytorch.so`` (under the root or its ``lib/``) and
``include/xdnn_pytorch/xdnn_pytorch.h``. This library does not ship in the
XPU SDK root -- it is installed inside the vendor torch package
(``torch_xmlir/``) -- so XDNN_ROOT has to be discovered rather than derived
from XPU_ROOT. Precedence: ``$XDNN_ROOT`` (validated), then
``$FLAGOS_VENDOR_TORCH_LIB``, then the vendor torch package in site-packages.
"""
def _valid(root: str) -> bool:
return os.path.isfile(
os.path.join(root, "include", "xdnn_pytorch", "xdnn_pytorch.h")
) and any(
os.path.isfile(os.path.join(root, sub, "libxdnn_pytorch.so"))
for sub in ("", "lib")
)
env_root = os.environ.get("XDNN_ROOT")
if env_root:
env_root = os.path.realpath(env_root)
if not _valid(env_root):
raise RuntimeError(
"XDNN_ROOT is set but does not contain libxdnn_pytorch.so and "
f"include/xdnn_pytorch/xdnn_pytorch.h: {env_root}"
)
return env_root
candidates: list[str] = []
hint = os.environ.get("FLAGOS_VENDOR_TORCH_LIB")
if hint:
hint = os.path.realpath(hint)
candidates.extend([hint, os.path.dirname(hint)])
import site
for sp in list(site.getsitepackages()) + [site.getusersitepackages()]:
if sp:
candidates.append(os.path.join(sp, "torch_xmlir"))
for root in candidates:
if _valid(root):
return os.path.realpath(root)
raise RuntimeError(
"FLAGOS_ACCELERATOR=kunlunxin needs the vendor xdnn op library "
"(libxdnn_pytorch.so + include/xdnn_pytorch/xdnn_pytorch.h), which ships "
"inside the vendor torch package (torch_xmlir/), not the XPU SDK. Set "
"XDNN_ROOT (or FLAGOS_VENDOR_TORCH_LIB) to that package root."
)
# The values an explicit environment variable may not contradict, from the
# shared table's per-platform "pins". Each pin is a set(... CACHE BOOL ... FORCE)
# in CMakeLists.txt: the FORCE would win over the -D silently, so a build record
# derived from the requested value would describe a wheel that was never built.
# Asking for the other value is an error rather than a silent override, on both
# sides (here and in CMakeLists.txt).
#
# Only genuinely impossible combinations are pinned. metax, gcu, ppu and
# tsingmicro are not, because FLAGOS_BUILD_FLAGGEMS_CPP=OFF there is a default an
# explicit =1 (with a vendor-built liboperators.so) may replace -- MetaX's MACA
# build is the documented case. DCU, MUSA and BPU pin it off for the opposite
# reason: no FlagGems C++ build exists for DTK, the MUSA toolkit or the BPU at
# all, so there is nothing an explicit 1 could link against. Ascend/musa
# FLAGOS_BUILD_VENDOR is pinned because their whole kernel story is the vendor
# library.
def _pinned_kernel_switches(accelerator: str) -> dict[str, bool]:
pins = _platform_entry(accelerator).get("pins", {})
return {name: spec["value"] for name, spec in pins.items()}
_ENV_MODULE = None
def _env_module():
"""torch_fl/_env.py, loaded by path so the build side shares its truth table.
setup.py cannot ``import torch_fl._env``: that would execute
torch_fl/__init__.py, which imports torch. Loading the file directly costs
nothing (the module is stdlib-only by design) and means a build with
FLAGOS_BUILD_VENDOR=2 rejects it exactly the way the run time would, instead of
the build keeping a second, more permissive copy of the parser.
"""
global _ENV_MODULE
if _ENV_MODULE is None:
path = os.path.join(SOURCE_DIR, "torch_fl", "_env.py")
loader = importlib.machinery.SourceFileLoader("torch_fl._env", path)
spec = importlib.util.spec_from_loader(loader.name, loader)
module = importlib.util.module_from_spec(spec)
loader.exec_module(module)
_ENV_MODULE = module
return _ENV_MODULE
def _vendor_kernel_switches(accelerator: str) -> dict[str, bool]:
"""What one accelerator builds by default, before any explicit environment value.
The middle of the three layers in _kernel_switches, and the only one that
differs per vendor. The values -- and the per-platform rationale (why MetaX
has no native kernels, why Ascend/GCU/MUSA turn boxing off, why BPU drops
FlagGems, ...) -- live in cmake/flagos_platforms.json, next to the CMake side
that consumes the same table.
"""
return dict(_platform_entry(accelerator)["kernel_defaults"])
def _kernel_switches(accelerator: str) -> dict[str, bool]:
"""Resolve the five kernel-set switches for one accelerator.
The single place that decides what a build compiles in. Both the -D list
handed to CMake and the KERNELS tuple written into the build record come
from here, so the wheel cannot be described as something it is not.
Order: the CMake option() default, then what this accelerator builds by
default, then an explicit environment value, then a pin. An explicit value
that contradicts a pin raises rather than being dropped, because CMake would
ignore it and the resulting record would be wrong.
"""
resolved = dict.fromkeys(KERNEL_SWITCHES, True)
resolved.update(_vendor_kernel_switches(accelerator))
# The runtime's own accessor, not a second copy of its truth table: an
# unparseable value (FLAGOS_BUILD_VENDOR=2) warns and keeps the vendor default
# here for the same reason it does at run time, instead of the build
# reading it as "on" and compiling a set the user never asked for.
env = _env_module()
pinned = _pinned_kernel_switches(accelerator)
for name in KERNEL_SWITCHES:
requested = env.flag(name, resolved[name])
if name in pinned and requested != pinned[name]:
raise ValueError(
f"{name}={os.environ.get(name)} is not possible with "
f"FLAGOS_ACCELERATOR={accelerator}: this platform always builds with "
f"{name}={'ON' if pinned[name] else 'OFF'}"
)
resolved[name] = requested
return resolved
def _cmake_build_jobs() -> int:
"""Parallel compile jobs for cmake/ninja. Set FLAGOS_BUILD_JOBS=1 to stay serial."""
for key in ("FLAGOS_BUILD_JOBS", "MAX_JOBS", "CMAKE_BUILD_PARALLEL_LEVEL"):
raw = os.environ.get(key)
if raw is not None and str(raw).strip() != "":
jobs = int(raw)
if jobs < 1:
raise ValueError(f"{key} must be >= 1, got {raw!r}")
return jobs
return multiprocessing.cpu_count()
def _torch_cxx11_abi() -> str:
"""The C++ ABI of the torch this build links, as "1" or "0".
Returns ``""`` when torch cannot be imported, so the CMake-side probe decides
instead of this failing the configure with an unrelated error. setup.py is a
script, not a module: the other torch reads below import it locally, and a
reference from here (or from ``build_deps``, which runs in the same scope)
would be a NameError rather than the module-global it looks like.
"""
try:
import torch
except ImportError:
return ""
return "1" if torch._C._GLIBCXX_USE_CXX11_ABI else "0"
def build_deps():
build_dir = os.path.join(BASE_DIR, "build")
os.makedirs(build_dir, exist_ok=True)
cmake_args = [
"-DCMAKE_INSTALL_PREFIX="
+ os.path.realpath(os.path.join(BASE_DIR, "torch_fl")),
"-DPYTHON_INCLUDE_DIR=" + sysconfig.get_paths().get("include"),
# CMake probes the environment for optional packages (torch_musa,
# flag_gems). It must use *this* interpreter, not whatever python is
# first on PATH, or the probe reads a different site-packages than the
# one we are building against.
"-DPYTHON_EXECUTABLE=" + sys.executable,
"-DPYTORCH_INSTALL_DIR=" + get_pytorch_dir(),
]
# The ABI derives from the torch this build links. Passing it removes the
# CMake-side probe (which would run the same query in a subprocess) from the
# picture entirely, so the compile flag and the value recorded in
# compatibility.json cannot disagree. Only iluvatar consults the flag today
# (the platform that can be built against more than one kind of torch), so
# an unreadable torch is left to CMake rather than made fatal here.
torch_cxx11_abi = _torch_cxx11_abi()
if torch_cxx11_abi:
cmake_args.append("-DFLAGOS_TORCH_CXX11_ABI=" + torch_cxx11_abi)
cmake_args.append(f"-DFLAGOS_ACCELERATOR={FLAGOS_ACCELERATOR}")
# What gets compiled in, resolved in one place so the wheel can describe
# itself accurately. _kernel_switches() is also what _write_build_config()
# reads, so the -D list and the KERNELS record cannot disagree.
#
# The per-vendor rationale (why MetaX has no native kernels, why Ascend
# turns boxing off, ...) lives in cmake/flagos_platforms.json.
kernels = _kernel_switches(FLAGOS_ACCELERATOR)
for switch in KERNEL_SWITCHES:
cmake_args.append(f"-D{switch}={'ON' if kernels[switch] else 'OFF'}")
build_env = os.environ.copy()
build_jobs = _cmake_build_jobs()
build_env["CMAKE_BUILD_PARALLEL_LEVEL"] = str(build_jobs)
cmake = "cmake"
# FlagGems C++ library path (optional, enables low-overhead C++ dispatch)
flaggems_dir = os.environ.get("FLAGGEMS_DIR")
if flaggems_dir:
cmake_args.append(f"-DFlagGems_DIR={flaggems_dir}")
flaggems_source_dir = os.environ.get("FLAGGEMS_SOURCE_DIR")
if flaggems_source_dir:
cmake_args.append(f"-DFLAGGEMS_SOURCE_DIR={flaggems_source_dir}")
if FLAGOS_ACCELERATOR == "metax":
metax_path = _setup_metax_build_env(build_env)
cmake_args.append(f"-DMACA_PATH={metax_path}")
cmake_args.append("-G")
cmake_args.append("Ninja")
elif FLAGOS_ACCELERATOR == "cuda":
cuda_root = _setup_cuda_build_env(build_env)
if cuda_root:
_append_cuda_cmake_args(cmake_args, cuda_root)
flaggems_dir = _find_flaggems_dir()
if flaggems_dir:
cmake_args.append(f"-DFLAGGEMS_DIR={flaggems_dir}")
elif FLAGOS_ACCELERATOR == "dcu":
cmake_args.append(f"-DROCM_PATH={_dtk_root()}")
elif FLAGOS_ACCELERATOR == "kunlunxin":
xpu_root, xcudart_dir = _setup_kunlunxin_build_env(build_env)
cmake_args.append(f"-DXPU_ROOT={xpu_root}")
cmake_args.append(f"-DXCUDART_ROOT={xcudart_dir}")
# The vendor op library (xdnn) lives in the vendor torch package, not the
# XPU SDK, so CMake cannot derive it from XPU_ROOT. Resolve and pass it;
# without it a vendor build FATAL_ERRORs at configure.
if kernels["FLAGOS_BUILD_VENDOR"]:
cmake_args.append(f"-DXDNN_ROOT={_kunlunxin_xdnn_root()}")
elif FLAGOS_ACCELERATOR == "iluvatar":
corex_root = _setup_iluvatar_build_env(build_env)
cmake_args.append(f"-DCOREX_PATH={corex_root}")
subprocess.check_call([cmake, BASE_DIR] + cmake_args, cwd=build_dir, env=build_env)
build_args = [
"--build",
".",
"--target",
"install",
"--config", # For multi-config generators
"Release",
"--",
]
if IS_WINDOWS:
build_args += ["/m:" + str(build_jobs)]
else:
build_args += ["-j", str(build_jobs)]
subprocess.check_call([cmake] + build_args, cwd=build_dir, env=build_env)
_verify_built_native_libs()
_bundle_cuda_assets()
_write_build_config()
def _write_build_config() -> None:
"""Record what this wheel was built for.
torch_fl._select_backend_config() runs at import time, before `import torch`,
so it cannot sniff torch.version.hip to tell a DCU build apart. Recording the
accelerator as ACCELERATOR here lets it pick backends_dcu.conf without the
user having to keep FLAGOS_ACCELERATOR set at run time.
The two attribute names are the record's own vocabulary, not the switches':
ACCELERATOR holds a platform value and KERNELS holds kernel-set values, while
the build inputs a user sets are FLAGOS_ACCELERATOR and the five
FLAGOS_BUILD_* flags.
KERNELS records the compiled kernel sets, because the accelerator alone does
not describe a wheel: two MetaX wheels built with different kernel-set flags
share a FLAGOS_ACCELERATOR and are not the same wheel. Run-time code that
used to infer the kernel set from an environment variable reads this
instead, so there is nothing left to keep in sync by hand.
Both values come from _kernel_switches(), the same function whose output
becomes the -D list handed to CMake, so this file cannot describe a build
other than the one that produced it.
"""
kernels = _kernel_switches(FLAGOS_ACCELERATOR)
compiled = tuple(sorted(KERNEL_SET_NAME[s] for s in KERNEL_SWITCHES if kernels[s]))
body = ", ".join(f'"{name}"' for name in compiled)
if len(compiled) == 1:
body += ","
path = os.path.join(BASE_DIR, "torch_fl", "_build_config.py")
content = (
"# AUTO-GENERATED by setup.py at build time. Do not edit.\n"
f'ACCELERATOR = "{FLAGOS_ACCELERATOR}"\n'
f"KERNELS = ({body})\n"
)
with open(path, "w", encoding="utf-8") as f:
f.write(content)
def _write_compatibility_manifest(wheel_version: str) -> None:
"""Write artifact facts after the native build, before wheel staging."""
import torch
# setuptools' PEP 517 backend executes setup.py with the source root absent
# from sys.path, even for --no-isolation builds. Load this source file by
# absolute path instead of relying on an import that only works in a shell.
preflight_path = os.path.join(SOURCE_DIR, "scripts", "tools", "torch-fl-preflight")
loader = importlib.machinery.SourceFileLoader("torch_fl_preflight", preflight_path)
spec = importlib.util.spec_from_loader(loader.name, loader)
if spec is None or spec.loader is None:
raise RuntimeError(f"Cannot load wheel preflight from {preflight_path}")
preflight = importlib.util.module_from_spec(spec)
spec.loader.exec_module(preflight)
kernels = _kernel_switches(FLAGOS_ACCELERATOR)
compiled = [KERNEL_SET_NAME[name] for name in KERNEL_SWITCHES if kernels[name]]
# vendor_torch_libraries is the platform's policy; a `vendor_torch_gate` names
# the kernel switch the build has to compile for the wheel to actually source
# and ship a vendor libtorch. CUDA-ABI platforms gate on boxing: the generated
# boxing kernels are the only consumer of a vendor libtorch there, so a
# FlagGems-primary wheel (the default) ships none and must not be asked for
# vendor provenance at release time.
entry = _platform_entry(FLAGOS_ACCELERATOR)
gate = entry.get("vendor_torch_gate")
manifest = preflight.make_manifest(
platform=FLAGOS_ACCELERATOR,
wheel_version=wheel_version,
kernels=compiled,
bundle_libdir=_BUNDLE_LIBDIR,
vendor_torch_libraries=entry["vendor_torch_libraries"]
and (gate is None or kernels[gate]),
requirements=_install_requires(),
torch_abi=bool(torch._C._GLIBCXX_USE_CXX11_ABI),
sdk_version=os.environ.get("FLAGOS_SDK_VERSION"),
vendor_torch_version=os.environ.get("FLAGOS_VENDOR_TORCH_VERSION"),
)
if manifest["build"]["distributions"]["torch"] is None:
raise RuntimeError("Build-time PyTorch distribution is missing")
path = os.path.join(BASE_DIR, "torch_fl", "compatibility.json")
with open(path, "w", encoding="utf-8") as handle:
json.dump(manifest, handle, indent=2, sort_keys=True)
handle.write("\n")
def _bundle_cuda_assets() -> None:
"""Copy the external CUDA .so assets into torch_fl/lib so the wheel is
self-contained.
torch_fl's CUDA backend reuses PyTorch's registered CUDA kernels via an
externally-supplied libtorch_cuda.so (CPU-only pip torch does not ship it).
Historically this was LD_PRELOAD-ed by scripts/vendor/with_cuda_libtorch.sh; for a
single self-contained wheel we bundle the assets and preload them from
torch_fl/__init__.py before `import torch` (see that doc, constraint 1).
CUDA only.
Set FLAGOS_SKIP_CUDA_ASSETS=1 to skip (e.g. a slim build for a machine that
supplies libtorch_cuda.so out-of-band).
"""
if FLAGOS_ACCELERATOR != "cuda":
return
# A FlagGems-primary CUDA wheel is built with FLAGOS_BUILD_BOXING=OFF (the
# default). It compiles no boxing kernels and has no vendor torch to feed, so
# the bundled libtorch_cuda.so and its nvidia-*-cu12 runtime wheels are not
# part of it. An explicit boxing opt-in restores them.
if not _kernel_switches(FLAGOS_ACCELERATOR)["FLAGOS_BUILD_BOXING"]:
return
if os.environ.get("FLAGOS_SKIP_CUDA_ASSETS", "0") == "1":
return
assets_dir = os.environ.get(
"FLAGOS_CUDA_ASSETS_DIR",
os.path.join(BASE_DIR, ".libtorch_cuda_assets"),
)
if not os.path.isdir(assets_dir):
print(
f"[setup] warning: CUDA assets dir {assets_dir} not found; wheel "
"will require an externally-supplied libtorch_cuda.so at runtime."
)
return
dst_dir = os.path.join(BASE_DIR, "torch_fl", "lib")
os.makedirs(dst_dir, exist_ok=True)
import glob
copied = []
for src in sorted(glob.glob(os.path.join(assets_dir, "*.so*"))):
dst = os.path.join(dst_dir, os.path.basename(src))
# Skip if already present and identical size (avoid re-copying ~1GB).
if os.path.exists(dst) and os.path.getsize(dst) == os.path.getsize(src):
copied.append(os.path.basename(src))
continue
shutil.copy2(src, dst)
copied.append(os.path.basename(src))
if copied:
print(f"[setup] bundled CUDA assets into torch_fl/lib: {', '.join(copied)}")
_NCCL_EXT_BUILD_PATH = os.path.join(
SOURCE_DIR, "torch_fl", "comm", "_nccl_ext", "build.py"
)
def _load_nccl_ext_builder():
"""Import torch_fl/comm/_nccl_ext/build.py without importing torch_fl.
Loaded from its path rather than as ``torch_fl.comm._nccl_ext.build``: that
form imports torch_fl, which imports torch, which -- with device-backend
autoloading on -- imports the half-built native package this build is in the
middle of producing. build.py is stdlib-only at import time for exactly this
reason; tests/unit/test_nccl_ext_build.py loads it the same way.
"""
spec = importlib.util.spec_from_file_location(
"torch_fl_nccl_ext_build", _NCCL_EXT_BUILD_PATH
)
if spec is None or spec.loader is None:
raise RuntimeError(
f"Cannot load the _flagos_nccl builder at {_NCCL_EXT_BUILD_PATH}"
)
module = importlib.util.module_from_spec(spec)
sys.modules[spec.name] = module
spec.loader.exec_module(module)
return module
def _nccl_ext_extension():
"""The ``_flagos_nccl`` Extension for this accelerator, or None.
None means the accelerator does not wire the native comm bridge into its
wheel (the builder's docstring names the DCU-only scope, and why); the
standalone builder still covers that platform. A configuration error on an
accelerator that *is* wired is raised instead of skipped: a released DCU
wheel without this extension is the defect, so an unresolvable DTK link set
has to stop the build rather than quietly ship a wheel whose
ProcessGroupFlagOS has no native RCCL fallback.
Called from _get_setup_kwargs() under RUN_BUILD_DEPS, so a real build pass
always reaches it. That is deliberate -- the DTK link set is resolvable
exactly when a DCU build is possible at all, and setup.py must not be able
to describe a DCU wheel it cannot build.
"""
if not os.path.isfile(_NCCL_EXT_BUILD_PATH):
return None
builder = _load_nccl_ext_builder()
try:
extension = builder.wheel_extension()
except RuntimeError as exc:
raise RuntimeError(
f"FLAGOS_ACCELERATOR={FLAGOS_ACCELERATOR} selected, but the "
f"_flagos_nccl native comm bridge cannot be configured: {exc}"
) from exc
if extension is not None:
print(f"[setup] native comm bridge target: {extension.name}")
return extension
def _verify_built_native_libs() -> None:
lib_dir = os.path.join(BASE_DIR, "torch_fl", "lib")
# macOS names shared libraries .dylib; every Linux platform the build targets
# uses .so. Prefer the native suffix so the same check serves both. The
# setuptools -ltorch_bindings lookup that links _C already follows the host
# suffix, so this is the only place that has to know the difference.
lib = None
for suffix in (".dylib", ".so") if IS_DARWIN else (".so",):
candidate = os.path.join(lib_dir, f"libtorch_fl{suffix}")
if os.path.isfile(candidate):
lib = candidate
break
if lib is None:
raise RuntimeError(
f"Native build finished but no libtorch_fl.(so|dylib) is present in "
f"{lib_dir}. Check cmake/ninja output above."
)
if FLAGOS_ACCELERATOR == "dcu":
shim = os.path.join(
BASE_DIR, "torch_fl", "lib_dcu", "libflagos_dtk_core_compat.so"
)
if not os.path.isfile(shim):
raise RuntimeError(
f"DCU native build finished but {shim} is missing. The shim is "
"required when DTK's device libraries run on the official "
"PyTorch core. Check the flagos_dtk_core_compat cmake target."
)
return
if FLAGOS_ACCELERATOR != "metax":
return
try:
undef = subprocess.check_output(
["nm", "-u", lib], text=True, stderr=subprocess.DEVNULL
)
except (OSError, subprocess.CalledProcessError):
return
if "get_maca_enable_elementwise_kernel_info" in undef:
raise RuntimeError(
f"{lib} still references at::maca::* (mcPytorch). Remove build/ and "
"torch_fl/lib/*.so, then rebuild with FLAGOS_ACCELERATOR=metax."
)
class BuildExtWithCmake(_build_ext):
"""Run cmake before setuptools builds torch_fl._C."""
def _prepare_nccl_extension(self):
"""Finish the torch_fl.comm._nccl_ext._flagos_nccl target in place.
The target itself is appended in _get_setup_kwargs() so setuptools'
bookkeeping (finalize_options' ext_map, _needs_stub, get_outputs) sees
it. What can only be added here is torch's own include and library
paths, which belong to the interpreter build_deps() has just run with.
"""
builder = _load_nccl_ext_builder()
for extension in self.extensions:
if extension.name != builder.WHEEL_EXTENSION_NAME:
continue
from torch.utils.cpp_extension import include_paths, library_paths
# Torch's paths go last so DTK's cuda.h/nccl.h win over any
# torch-shipped copy, the ordering build.py's CppExtension gives.
extension.include_dirs += include_paths()
extension.library_dirs += library_paths()
# The c10/torch/torch_cpu the factory's pybind module links against,
# mirroring CppExtension in torch_fl/comm/_nccl_ext/build.py.
extension.libraries += ["c10", "torch", "torch_cpu"]
print(
"[setup] _flagos_nccl link set: "
+ ", ".join(extension.libraries)
+ " | "
+ ", ".join(extension.extra_link_args)
)
return
def run(self):
build_deps()
_write_compatibility_manifest(self.distribution.get_version())
self._prepare_nccl_extension()
# ``build`` runs build_py before build_ext, but CMake installs package
# data into torch_fl/ during build_ext. Setuptools caches build_py's file
# list, so copy late-generated files explicitly into wheel staging.
relative_paths = [
"_build_config.py",