forked from YdrMaster/operators
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathxmake.lua
383 lines (315 loc) · 11.3 KB
/
xmake.lua
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
add_rules("mode.debug", "mode.release")
-- Define color codes
local GREEN = '\27[0;32m'
local YELLOW = '\27[1;33m'
local NC = '\27[0m' -- No Color
add_includedirs("include")
option("cpu")
set_default(true)
set_showmenu(true)
set_description("Enable or disable cpu kernel")
add_defines("ENABLE_CPU")
option_end()
option("omp")
set_default(false)
set_showmenu(true)
set_description("Enable or disable OpenMP support for cpu kernel")
option_end()
option("nv-gpu")
set_default(false)
set_showmenu(true)
set_description("Enable or disable Nvidia GPU kernel")
add_defines("ENABLE_NV_GPU")
option_end()
option("cambricon-mlu")
set_default(false)
set_showmenu(true)
set_description("Enable or disable Cambricon MLU kernel")
add_defines("ENABLE_CAMBRICON_MLU")
option_end()
option("ascend-npu")
set_default(false)
set_showmenu(true)
set_description("Enable or disable Ascend NPU kernel")
add_defines("ENABLE_ASCEND_NPU")
option_end()
option("metax-gpu")
set_default(false)
set_showmenu(true)
set_description("Enable or disable Metax GPU kernel")
add_defines("ENABLE_METAX_GPU")
option_end()
option("mthreads-gpu")
set_default(false)
set_showmenu(true)
set_description("Enable or disable MThreads GPU kernel")
add_defines("ENABLE_MTHREADS_GPU")
option_end()
option("sugon-dcu")
set_default(false)
set_showmenu(true)
set_description("Enable or disable Sugon DCU kernel")
add_defines("ENABLE_SUGON_DCU")
add_defines("ENABLE_NV_GPU")
option_end()
if is_mode("debug") then
add_cxflags("-g -O0")
add_defines("DEBUG_MODE")
end
if has_config("cpu") then
add_defines("ENABLE_CPU")
target("cpu")
on_install(function (target) end)
set_kind("static")
if not is_plat("windows") then
add_cxflags("-fPIC")
end
set_languages("cxx17")
add_files("src/devices/cpu/*.cc", "src/ops/*/cpu/*.cc")
if has_config("omp") then
add_cxflags("-fopenmp")
add_ldflags("-fopenmp")
end
target_end()
end
if has_config("nv-gpu", "sugon-dcu") then
add_defines("ENABLE_NV_GPU")
if has_config("sugon-dcu") then
add_defines("ENABLE_SUGON_DCU")
end
local CUDA_ROOT = os.getenv("CUDA_ROOT") or os.getenv("CUDA_HOME") or os.getenv("CUDA_PATH")
local CUDNN_ROOT = os.getenv("CUDNN_ROOT") or os.getenv("CUDNN_HOME") or os.getenv("CUDNN_PATH")
if CUDA_ROOT ~= nil then
add_includedirs(CUDA_ROOT .. "/include")
end
if CUDNN_ROOT ~= nil then
add_includedirs(CUDNN_ROOT .. "/include")
end
target("nv-gpu")
set_kind("static")
on_install(function (target) end)
set_policy("build.cuda.devlink", true)
set_toolchains("cuda")
add_links("cublas")
add_links("cudnn")
add_cugencodes("native")
if is_plat("windows") then
add_cuflags("-Xcompiler=/utf-8", "--expt-relaxed-constexpr", "--allow-unsupported-compiler")
if CUDNN_ROOT ~= nil then
add_linkdirs(CUDNN_ROOT .. "\\lib\\x64")
end
else
add_cuflags("-Xcompiler=-fPIC")
add_culdflags("-Xcompiler=-fPIC")
add_cxxflags("-fPIC")
end
set_languages("cxx17")
add_files("src/devices/cuda/*.cc", "src/ops/*/cuda/*.cu")
add_files("src/ops/*/cuda/*.cc")
target_end()
end
if has_config("cambricon-mlu") then
add_defines("ENABLE_CAMBRICON_MLU")
add_includedirs("/usr/local/neuware/include")
add_linkdirs("/usr/local/neuware/lib64")
add_linkdirs("/usr/local/neuware/lib")
add_links("libcnrt.so")
add_links("libcnnl.so")
add_links("libcnnl_extra.so")
add_links("libcnpapi.so")
rule("mlu")
set_extensions(".mlu")
on_load(function (target)
target:add("includedirs", "include")
end)
on_build_file(function (target, sourcefile)
local objectfile = target:objectfile(sourcefile)
os.mkdir(path.directory(objectfile))
local cc = "/usr/local/neuware/bin/cncc"
local includedirs = table.concat(target:get("includedirs"), " ")
local args = {"-c", sourcefile, "-o", objectfile, "-I/usr/local/neuware/include", "--bang-mlu-arch=mtp_592", "-O3", "-fPIC", "-Wall", "-Werror", "-std=c++17", "-pthread"}
for _, includedir in ipairs(target:get("includedirs")) do
table.insert(args, "-I" .. includedir)
end
os.execv(cc, args)
table.insert(target:objectfiles(), objectfile)
end)
rule_end()
target("cambricon-mlu")
set_kind("static")
on_install(function (target) end)
set_languages("cxx17")
add_files("src/devices/bang/*.cc", "src/ops/*/bang/*.cc")
add_files("src/ops/*/bang/*.mlu", {rule = "mlu"})
add_cxflags("-lstdc++ -Wall -Werror -fPIC")
target_end()
end
if has_config("mthreads-gpu") then
add_defines("ENABLE_MTHREADS_GPU")
local musa_home = os.getenv("MUSA_INSTALL_PATH")
-- Add include dirs
add_includedirs(musa_home .. "/include")
-- Add shared lib
add_linkdirs(musa_home .. "/lib")
add_links("libmusa.so")
add_links("libmusart.so")
add_links("libmudnn.so")
add_links("libmublas.so")
rule("mu")
set_extensions(".mu")
on_load(function (target)
target:add("includedirs", "include")
end)
on_build_file(function (target, sourcefile)
local objectfile = target:objectfile(sourcefile)
os.mkdir(path.directory(objectfile))
local mcc = "/usr/local/musa/bin/mcc"
local includedirs = table.concat(target:get("includedirs"), " ")
local args = {"-c", sourcefile, "-o", objectfile, "-I/usr/local/musa/include", "-O3", "-fPIC", "-Wall", "-std=c++17", "-pthread"}
for _, includedir in ipairs(target:get("includedirs")) do
table.insert(args, "-I" .. includedir)
end
os.execv(mcc, args)
table.insert(target:objectfiles(), objectfile)
end)
rule_end()
target("mthreads-gpu")
set_kind("static")
set_languages("cxx17")
add_files("src/devices/musa/*.cc", "src/ops/*/musa/*.cc")
add_files("src/ops/*/musa/*.mu", {rule = "mu"})
add_cxflags("-lstdc++ -Wall -fPIC")
target_end()
end
if has_config("ascend-npu") then
add_defines("ENABLE_ASCEND_NPU")
local ASCEND_HOME = os.getenv("ASCEND_HOME")
local SOC_VERSION = os.getenv("SOC_VERSION")
-- Add include dirs
add_includedirs(ASCEND_HOME .. "/include")
add_includedirs(ASCEND_HOME .. "/include/aclnn")
add_linkdirs(ASCEND_HOME .. "/lib64")
add_links("libascendcl.so")
add_links("libnnopbase.so")
add_links("libopapi.so")
add_links("libruntime.so")
add_linkdirs(ASCEND_HOME .. "/../../driver/lib64/driver")
add_links("libascend_hal.so")
local builddir = string.format(
"%s/build/%s/%s/%s",
os.projectdir(),
get_config("plat"),
get_config("arch"),
get_config("mode")
)
rule("ascend-kernels")
before_link(function ()
local ascend_build_dir = path.join(os.projectdir(), "src/devices/ascend")
os.cd(ascend_build_dir)
os.exec("make")
os.exec("cp $(projectdir)/src/devices/ascend/build/lib/libascend_kernels.a "..builddir.."/")
os.cd(os.projectdir())
end)
after_clean(function ()
local ascend_build_dir = path.join(os.projectdir(), "src/devices/ascend")
os.cd(ascend_build_dir)
os.exec("make clean")
os.cd(os.projectdir())
os.rm(builddir.. "/libascend_kernels.a")
end)
rule_end()
target("ascend-npu")
-- Other configs
set_kind("static")
set_languages("cxx17")
on_install(function (target) end)
-- Add files
add_files("src/devices/ascend/*.cc", "src/ops/*/ascend/*.cc")
add_cxflags("-lstdc++ -Wall -Werror -fPIC")
-- Add operator
add_rules("ascend-kernels")
add_links(builddir.."/libascend_kernels.a")
target_end()
end
if has_config("metax-gpu") then
add_defines("ENABLE_METAX_GPU")
local MACA_ROOT = os.getenv("MACA_PATH") or os.getenv("MACA_HOME") or os.getenv("MACA_ROOT")
add_includedirs(MACA_ROOT .. "/include")
add_linkdirs(MACA_ROOT .. "/lib")
-- add_linkdirs(MACA_ROOT .. "htgpu_llvm/lib")
add_links("libhcdnn.so")
add_links("libhcblas.so")
add_links("libhcruntime.so")
rule("maca")
set_extensions(".maca")
on_load(function (target)
target:add("includedirs", "include")
end)
on_build_file(function (target, sourcefile)
local objectfile = target:objectfile(sourcefile)
os.mkdir(path.directory(objectfile))
local htcc = "/opt/hpcc/htgpu_llvm/bin/htcc"
local includedirs = table.concat(target:get("includedirs"), " ")
local args = { "-x", "hpcc", "-c", sourcefile, "-o", objectfile, "-I/opt/hpcc/include", "-O3", "-fPIC", "-Werror", "-std=c++17"}
for _, includedir in ipairs(target:get("includedirs")) do
table.insert(args, "-I" .. includedir)
end
os.execv(htcc, args)
table.insert(target:objectfiles(), objectfile)
end)
rule_end()
target("metax-gpu")
set_kind("static")
on_install(function (target) end)
set_languages("cxx17")
add_files("src/devices/maca/*.cc", "src/ops/*/maca/*.cc")
add_files("src/ops/*/maca/*.maca", {rule = "maca"})
add_cxflags("-lstdc++ -Werror -fPIC")
target_end()
end
toolchain("sugon-dcu-linker")
set_toolset("sh", "nvcc")
toolchain_end()
target("infiniop")
set_kind("shared")
if has_config("cpu") then
add_deps("cpu")
end
if has_config("nv-gpu") then
add_deps("nv-gpu")
end
if has_config("sugon-dcu") then
local builddir = string.format(
"build/%s/%s/%s",
get_config("plat"),
get_config("arch"),
get_config("mode")
)
add_shflags("-s", "-shared", "-fPIC")
add_links("cublas", "cudnn", "cudadevrt", "cudart_static", "rt", "pthread", "dl")
-- Using -lnv-gpu will fail, manually link the target using full path
add_deps("nv-gpu", {inherit = false})
add_links(builddir.."/libnv-gpu.a")
set_toolchains("sugon-dcu-linker")
end
if has_config("cambricon-mlu") then
add_deps("cambricon-mlu")
end
if has_config("ascend-npu") then
add_deps("ascend-npu")
end
if has_config("metax-gpu") then
add_deps("metax-gpu")
end
if has_config("mthreads-gpu") then
add_deps("mthreads-gpu")
end
set_languages("cxx17")
add_files("src/devices/handle.cc")
add_files("src/ops/*/operator.cc")
add_files("src/tensor/*.cc")
after_build(function (target) print(YELLOW .. "You can install the libraries with \"xmake install\"" .. NC) end)
set_installdir(os.getenv("INFINI_ROOT") or (os.getenv(is_host("windows") and "HOMEPATH" or "HOME") .. "/.infini"))
add_installfiles("include/(**/*.h)", {prefixdir = "include"})
add_installfiles("include/*.h", {prefixdir = "include"})
target_end()