|
5 | 5 | os.environ["TORCH_DEVICE_BACKEND_AUTOLOAD"] = "0" |
6 | 6 |
|
7 | 7 | # Modern setuptools (>=64) uses pip for 'develop' which creates isolated build envs. |
8 | | -# For packages depending on torch, this often fails. |
| 8 | +# For packages depending on torch, this often fails. |
9 | 9 | # We try to disable build isolation if not explicitly set. |
10 | 10 | if "PIP_NO_BUILD_ISOLATION" not in os.environ: |
11 | 11 | os.environ["PIP_NO_BUILD_ISOLATION"] = "1" |
12 | 12 |
|
13 | 13 | from setuptools import setup, find_packages |
14 | | -from packaging.version import Version, parse as vparse |
15 | | - |
16 | | -adaptor = os.environ.get("FLAGCX_ADAPTOR", "nvidia") |
17 | | -if '--adaptor' in sys.argv: |
18 | | - arg_index = sys.argv.index('--adaptor') |
19 | | - sys.argv.remove("--adaptor") |
20 | | - if arg_index < len(sys.argv): |
21 | | - adaptor = sys.argv[arg_index] |
22 | | - sys.argv.remove(adaptor) |
23 | | - else: |
24 | | - print("No adaptor provided after '--adaptor'. Using default nvidia adaptor") |
| 14 | +from _build_config import ( |
| 15 | + ADAPTOR_MAP, |
| 16 | + detect_adaptor, |
| 17 | + detect_torch_flag, |
| 18 | + get_device_config, |
| 19 | + get_ext_classes, |
| 20 | +) |
25 | 21 |
|
26 | | -valid_adaptors = ["nvidia", "iluvatar_corex", "cambricon", "metax", "du", "klx", "ascend", "musa", "amd", "enflame"] |
27 | | -assert adaptor in valid_adaptors, f"Invalid adaptor: {adaptor}" |
| 22 | +adaptor = detect_adaptor() |
28 | 23 | print(f"Using {adaptor} adaptor") |
29 | 24 |
|
30 | | -adaptor_map = { |
31 | | - "nvidia": "-DUSE_NVIDIA_ADAPTOR", |
32 | | - "iluvatar_corex": "-DUSE_ILUVATAR_COREX_ADAPTOR", |
33 | | - "cambricon": "-DUSE_CAMBRICON_ADAPTOR", |
34 | | - "metax": "-DUSE_METAX_ADAPTOR", |
35 | | - "musa": "-DUSE_MUSA_ADAPTOR", |
36 | | - "du": "-DUSE_DU_ADAPTOR", |
37 | | - "klx": "-DUSE_KUNLUNXIN_ADAPTOR", |
38 | | - "ascend": "-DUSE_ASCEND_ADAPTOR", |
39 | | - "amd": "-DUSE_AMD_ADAPTOR", |
40 | | - "tsm": "-DUSE_TSM_ADAPTOR", |
41 | | - "enflame": "-DUSE_ENFLAME_ADAPTOR" |
42 | | -} |
43 | | -adaptor_flag = adaptor_map[adaptor] |
44 | | -torch_flag = "-DTORCH_VER_LT_250" |
| 25 | +adaptor_flag = ADAPTOR_MAP[adaptor] |
| 26 | +torch_flag = detect_torch_flag() |
| 27 | + |
| 28 | +plugin_dir = os.path.dirname(os.path.abspath(__file__)) |
| 29 | +repo_root = os.path.join(plugin_dir, "..", "..") |
45 | 30 |
|
46 | 31 | sources = ["flagcx/src/backend_flagcx.cpp", "flagcx/src/utils_flagcx.cpp"] |
47 | 32 | include_dirs = [ |
48 | | - f"{os.path.dirname(os.path.abspath(__file__))}/flagcx/include", |
49 | | - f"{os.path.dirname(os.path.abspath(__file__))}/../../flagcx/include", |
50 | | - f"{os.path.dirname(os.path.abspath(__file__))}/../../third-party/json/single_include", |
| 33 | + os.path.join(plugin_dir, "flagcx", "include"), |
| 34 | + os.path.join(repo_root, "flagcx", "include"), |
| 35 | + os.path.join(repo_root, "third-party", "json", "single_include"), |
51 | 36 | ] |
52 | 37 |
|
53 | 38 | library_dirs = [ |
54 | | - f"{os.path.dirname(os.path.abspath(__file__))}/../../build/lib", |
| 39 | + os.path.join(repo_root, "build", "lib"), |
55 | 40 | ] |
56 | 41 |
|
57 | 42 | libs = ["flagcx"] |
58 | 43 |
|
59 | | -try: |
60 | | - import torch |
61 | | - torch_version = vparse(torch.__version__.split("+")[0]) |
62 | | - if torch_version >= Version("2.5.0"): |
63 | | - print("torch version >= 2.5.0, set TORCH_VER_GE_250 flag") |
64 | | - torch_flag = "-DTORCH_VER_GE_250" |
65 | | -except ImportError: |
66 | | - torch = None |
67 | | - print("Warning: torch not found.") |
68 | | - |
69 | | -if adaptor_flag == "-DUSE_NVIDIA_ADAPTOR": |
70 | | - include_dirs += ["/usr/local/cuda/include"] |
71 | | - library_dirs += ["/usr/local/cuda/lib64"] |
72 | | - libs += ["cuda", "cudart", "c10_cuda", "torch_cuda"] |
73 | | -elif adaptor_flag == "-DUSE_ILUVATAR_COREX_ADAPTOR": |
74 | | - include_dirs += ["/usr/local/corex/include"] |
75 | | - library_dirs += ["/usr/local/corex/lib64"] |
76 | | - libs += ["cuda", "cudart", "c10_cuda", "torch_cuda"] |
77 | | -elif adaptor_flag == "-DUSE_CAMBRICON_ADAPTOR": |
78 | | - import torch_mlu |
79 | | - neuware_home_path=os.getenv("NEUWARE_HOME") |
80 | | - pytorch_home_path=os.getenv("PYTORCH_HOME") |
81 | | - torch_mlu_path = torch_mlu.__file__.split("__init__")[0] |
82 | | - torch_mlu_lib_dir = os.path.join(torch_mlu_path, "csrc/lib/") |
83 | | - torch_mlu_include_dir = os.path.join(torch_mlu_path, "csrc/") |
84 | | - torch_mlu_include_dir2 = os.path.join(torch_mlu_path, "csrc", "include") |
85 | | - include_dirs += [f"{neuware_home_path}/include", torch_mlu_include_dir, torch_mlu_include_dir2] |
86 | | - library_dirs += [f"{neuware_home_path}/lib64", torch_mlu_lib_dir] |
87 | | - libs += ["cnrt", "cncl", "torch_mlu"] |
88 | | -elif adaptor_flag == "-DUSE_METAX_ADAPTOR": |
89 | | - include_dirs += ["/opt/maca/include"] |
90 | | - library_dirs += ["/opt/maca/lib64"] |
91 | | - libs += ["cuda", "cudart", "c10_cuda", "torch_cuda"] |
92 | | -elif adaptor_flag == "-DUSE_MUSA_ADAPTOR": |
93 | | - import torch_musa |
94 | | - pytorch_musa_install_path = os.path.dirname(os.path.abspath(torch_musa.__file__)) |
95 | | - pytorch_library_path = os.path.join(pytorch_musa_install_path, "lib") |
96 | | - library_dirs += ['/usr/local/musa/lib/',pytorch_library_path] |
97 | | - libs += ["musa","musart"] |
98 | | -elif adaptor_flag == "-DUSE_DU_ADAPTOR": |
99 | | - include_dirs += ["${CUDA_PATH}/include"] |
100 | | - library_dirs += ["${CUDA_PATH}/lib64"] |
101 | | - libs += ["cuda", "cudart", "c10_cuda", "torch_cuda"] |
102 | | -elif adaptor_flag == "-DUSE_KUNLUNXIN_ADAPTOR": |
103 | | - include_dirs += ["/opt/kunlun/include"] |
104 | | - library_dirs += ["/opt/kunlun/lib"] |
105 | | - libs += ["cuda", "cudart", "c10_cuda", "torch_cuda"] |
106 | | -elif adaptor_flag == "-DUSE_ASCEND_ADAPTOR": |
107 | | - import torch_npu |
108 | | - pytorch_npu_install_path = os.path.dirname(os.path.abspath(torch_npu.__file__)) |
109 | | - pytorch_library_path = os.path.join(pytorch_npu_install_path, "lib") |
110 | | - include_dirs += [os.path.join(pytorch_npu_install_path, "include")] |
111 | | - library_dirs += [pytorch_library_path] |
112 | | - libs += ["torch_npu"] |
113 | | -elif adaptor_flag == "-DUSE_AMD_ADAPTOR": |
114 | | - include_dirs += ["/opt/rocm/include"] |
115 | | - library_dirs += ["/opt/rocm/lib"] |
116 | | - libs += ["hiprtc", "c10_hip", "torch_hip"] |
117 | | -elif adaptor_flag == "-DUSE_TSM_ADAPTOR": |
118 | | - import torch_txda |
119 | | - txda_install_path = os.path.dirname(os.path.abspath(torch_txda.__file__)) |
120 | | - txda_library_path = os.path.join(txda_install_path, "lib") |
121 | | - include_dirs += ["/usr/local/kuiper/include", os.path.join(txda_install_path, "include")] |
122 | | - library_dirs += ["/usr/local/kuiper/lib",txda_library_path] |
123 | | - libs += ["torch_txda", "hpgr"] |
124 | | -elif adaptor_flag == "-DUSE_ENFLAME_ADAPTOR": |
125 | | - import torch_gcu |
126 | | - pytorch_gcu_install_path = os.path.dirname(os.path.abspath(torch_gcu.__file__)) |
127 | | - pytorch_library_path = os.path.join(pytorch_gcu_install_path, "lib") |
128 | | - include_dirs += ["/opt/tops/include", os.path.join(pytorch_gcu_install_path, "include")] |
129 | | - library_dirs += ["/opt/tops/lib", pytorch_library_path] |
130 | | - libs += ["topsrt", "torch_gcu"] |
| 44 | +# Add device-specific paths |
| 45 | +dev_includes, dev_libdirs, dev_libs = get_device_config(adaptor_flag) |
| 46 | +include_dirs += dev_includes |
| 47 | +library_dirs += dev_libdirs |
| 48 | +libs += dev_libs |
131 | 49 |
|
132 | | -try: |
133 | | - if adaptor_flag == "-DUSE_MUSA_ADAPTOR": |
134 | | - from torch_musa.utils.musa_extension import MUSAExtension as CppExtension |
135 | | - from torch_musa.utils.musa_extension import BuildExtension |
136 | | - elif adaptor_flag == "-DUSE_ENFLAME_ADAPTOR": |
137 | | - from tops_extension import TopsBuildExtension as BuildExtension |
138 | | - from tops_extension.torch import TopsTorchExtension as CppExtension |
139 | | - else: |
140 | | - from torch.utils.cpp_extension import CppExtension, BuildExtension |
141 | | -except ImportError: |
142 | | - CppExtension = None |
143 | | - BuildExtension = None |
144 | | - print("Warning: CppExtension or BuildExtension not found.") |
| 50 | +CppExtension, BuildExtension = get_ext_classes(adaptor_flag) |
145 | 51 |
|
146 | 52 | ext_modules = [] |
147 | 53 | if CppExtension is not None: |
|
152 | 58 | extra_compile_args={ |
153 | 59 | 'cxx': [adaptor_flag, torch_flag] |
154 | 60 | }, |
155 | | - extra_link_args=["-Wl,-rpath,"+f"{os.path.dirname(os.path.abspath(__file__))}/../../build/lib"], |
| 61 | + extra_link_args=["-Wl,-rpath," + os.path.join(repo_root, "build", "lib")], |
156 | 62 | library_dirs=library_dirs, |
157 | 63 | libraries=libs, |
158 | 64 | ) |
|
164 | 70 |
|
165 | 71 | setup( |
166 | 72 | name="flagcx", |
167 | | - version="0.8.0", |
| 73 | + version="0.10.0", |
168 | 74 | ext_modules=ext_modules, |
169 | 75 | cmdclass=cmdclass, |
170 | 76 | packages=find_packages(), |
|
0 commit comments