xmake.lua 15.3 KB
Newer Older
PanZezhongQY's avatar
PanZezhongQY committed
1
add_rules("mode.debug", "mode.release")
2
add_requires("boost", {configs = {stacktrace = true}})
3
4
add_requires("pybind11")

PanZezhongQY's avatar
PanZezhongQY committed
5
6
7
8
9
-- Define color codes
local GREEN = '\27[0;32m'
local YELLOW = '\27[1;33m'
local NC = '\27[0m'  -- No Color

10
set_encodings("utf-8")
PanZezhongQY's avatar
PanZezhongQY committed
11

12
add_includedirs("include")
13
add_includedirs("third_party/spdlog/include")
qinyiqun's avatar
qinyiqun committed
14
add_includedirs("third_party/nlohmann_json/single_include/")
15

PanZezhongQY's avatar
PanZezhongQY committed
16
17
18
19
if is_mode("debug") then
    add_defines("DEBUG_MODE")
end

20
21
if is_plat("windows") then
    set_runtimes("MD")
22
    add_ldflags("/utf-8", {force = true})
23
    add_cxxflags("/utf-8", {force = true})
24
25
end

PanZezhongQY's avatar
PanZezhongQY committed
26
27
28
29
-- CPU
option("cpu")
    set_default(true)
    set_showmenu(true)
YdrMaster's avatar
YdrMaster committed
30
    set_description("Whether to compile implementations for CPU")
PanZezhongQY's avatar
PanZezhongQY committed
31
32
33
option_end()

option("omp")
34
    set_default(true)
PanZezhongQY's avatar
PanZezhongQY committed
35
36
37
38
39
40
41
42
43
    set_showmenu(true)
    set_description("Enable or disable OpenMP support for cpu kernel")
option_end()

if has_config("cpu") then
    includes("xmake/cpu.lua")
    add_defines("ENABLE_CPU_API")
end

44
45
46
47
if has_config("omp") then
    add_defines("ENABLE_OMP")
end

PanZezhongQY's avatar
PanZezhongQY committed
48
49
50
51
-- 英伟达
option("nv-gpu")
    set_default(false)
    set_showmenu(true)
YdrMaster's avatar
YdrMaster committed
52
    set_description("Whether to compile implementations for Nvidia GPU")
PanZezhongQY's avatar
PanZezhongQY committed
53
54
55
option_end()

if has_config("nv-gpu") then
56
    add_defines("ENABLE_NVIDIA_API")
57
    includes("xmake/nvidia.lua")
PanZezhongQY's avatar
PanZezhongQY committed
58
59
end

60
61
option("cudnn")
    set_default(true)
YdrMaster's avatar
YdrMaster committed
62
    set_showmenu(true)
YdrMaster's avatar
YdrMaster committed
63
    set_description("Whether to compile cudnn for Nvidia GPU")
YdrMaster's avatar
YdrMaster committed
64
65
option_end()

66
67
if has_config("cudnn") then
    add_defines("ENABLE_CUDNN_API")
YdrMaster's avatar
YdrMaster committed
68
69
end

70
71
72
73
74
75
76
77
78
79
option("cutlass")
    set_default(false)
    set_showmenu(true)
    set_description("Whether to compile cutlass for Nvidia GPU")
option_end()

if has_config("cutlass") then 
    add_defines("ENABLE_CUTLASS_API")
end

80
81
82
83
84
85
86
option("cuda_arch")
    set_showmenu(true)
    set_description("Set CUDA GPU architecture (e.g. sm_90)")
    set_values("sm_50", "sm_60", "sm_70", "sm_75", "sm_80", "sm_86", "sm_89", "sm_90", "sm_90a")
    set_category("option")
option_end()

PanZezhongQY's avatar
PanZezhongQY committed
87
88
89
90
-- 寒武纪
option("cambricon-mlu")
    set_default(false)
    set_showmenu(true)
YdrMaster's avatar
YdrMaster committed
91
    set_description("Whether to compile implementations for Cambricon MLU")
PanZezhongQY's avatar
PanZezhongQY committed
92
93
94
95
option_end()

if has_config("cambricon-mlu") then
    add_defines("ENABLE_CAMBRICON_API")
PanZezhong's avatar
PanZezhong committed
96
    includes("xmake/bang.lua")
PanZezhongQY's avatar
PanZezhongQY committed
97
98
99
100
101
102
end

-- 华为昇腾
option("ascend-npu")
    set_default(false)
    set_showmenu(true)
YdrMaster's avatar
YdrMaster committed
103
    set_description("Whether to compile implementations for Huawei Ascend NPU")
PanZezhongQY's avatar
PanZezhongQY committed
104
105
106
107
option_end()

if has_config("ascend-npu") then
    add_defines("ENABLE_ASCEND_API")
Pan Zezhong's avatar
Pan Zezhong committed
108
    includes("xmake/ascend.lua")
PanZezhongQY's avatar
PanZezhongQY committed
109
110
end

111
112
113
114
-- 天数智芯
option("iluvatar-gpu")
    set_default(false)
    set_showmenu(true)
YdrMaster's avatar
YdrMaster committed
115
    set_description("Whether to compile implementations for Iluvatar GPU")
116
117
option_end()

118
119
option("iluvatar_arch")
    set_default("ivcore20")
120
    set_showmenu(true)
121
122
123
    set_description("Set Iluvatar GPU architecture (e.g. ivcore20)")
    set_values("ivcore20")
    set_category("option")
124
125
option_end()

126
if has_config("iluvatar-gpu") then
127
    add_defines("ENABLE_ILUVATAR_API")
128
129
130
    includes("xmake/iluvatar.lua")
end

wooway777's avatar
wooway777 committed
131
132
133
134
135
136
137
138
139
140
141
142
-- ali
option("ali-ppu")
    set_default(false)
    set_showmenu(true)
    set_description("Whether to compile implementations for Ali PPU")
option_end()

if has_config("ali-ppu") then
    add_defines("ENABLE_ALI_API")
    includes("xmake/ali.lua")
end

143
144
145
146
147
148
149
150
151
152
153
154
-- qy
option("qy-gpu")
    set_default(false)
    set_showmenu(true)
    set_description("Whether to compile implementations for Qy GPU")
option_end()

if has_config("qy-gpu") then
    add_defines("ENABLE_QY_API")
    includes("xmake/qy.lua")
end

PanZezhongQY's avatar
PanZezhongQY committed
155
156
157
158
-- 沐曦
option("metax-gpu")
    set_default(false)
    set_showmenu(true)
YdrMaster's avatar
YdrMaster committed
159
    set_description("Whether to compile implementations for MetaX GPU")
PanZezhongQY's avatar
PanZezhongQY committed
160
161
option_end()

162
163
164
165
166
167
option("use-mc")
    set_default(false)
    set_showmenu(true)
    set_description("Use MC version")
option_end()

PanZezhongQY's avatar
PanZezhongQY committed
168
if has_config("metax-gpu") then
169
    add_defines("ENABLE_METAX_API")
170
171
172
    if has_config("use-mc") then
        add_defines("ENABLE_METAX_MC_API")
    end
173
    includes("xmake/metax.lua")
PanZezhongQY's avatar
PanZezhongQY committed
174
175
176
177
178
179
end

-- 摩尔线程
option("moore-gpu")
    set_default(false)
    set_showmenu(true)
YdrMaster's avatar
YdrMaster committed
180
    set_description("Whether to compile implementations for Moore Threads GPU")
PanZezhongQY's avatar
PanZezhongQY committed
181
182
option_end()

183
184
185
186
187
188
option("moore-gpu-arch")
    set_default("mp_31")
    set_showmenu(true)
    set_description("Set Moore GPU architecture (e.g. mp_31)")
option_end()

189
if has_config("moore-gpu") then
190
    add_defines("ENABLE_MOORE_API")
191
    includes("xmake/moore.lua")
192
end
PanZezhongQY's avatar
PanZezhongQY committed
193

194
195
-- 海光DCU
option("hygon-dcu")
PanZezhongQY's avatar
PanZezhongQY committed
196
197
    set_default(false)
    set_showmenu(true)
198
    set_description("Whether to compile implementations for Hygon DCU")
PanZezhongQY's avatar
PanZezhongQY committed
199
200
option_end()

201
202
203
if has_config("hygon-dcu") then
    add_defines("ENABLE_HYGON_API")
    includes("xmake/hygon.lua")
PanZezhongQY's avatar
PanZezhongQY committed
204
205
end

206
207
208
209
210
211
212
213
214
215
216
-- 昆仑芯
option("kunlun-xpu")
    set_default(false)
    set_showmenu(true)
    set_description("Enable or disable Kunlun XPU kernel")
option_end()

if has_config("kunlun-xpu") then
    add_defines("ENABLE_KUNLUN_API")
    includes("xmake/kunlun.lua")
end
PanZezhongQY's avatar
PanZezhongQY committed
217

218
219
220
221
222
223
224
225
226
227
228
-- 九齿
option("ninetoothed")
    set_default(false)
    set_showmenu(true)
    set_description("Whether to complie NineToothed implementations")
option_end()

if has_config("ninetoothed") then
    add_defines("ENABLE_NINETOOTHED")
end

229
230
231
232
233
234
235
236
237
-- ATen
option("aten")
    set_default(false)
    set_showmenu(true)
    set_description("Wether to link aten and torch libraries")
option_end()

-- Flash-Attn
option("flash-attn")
238
    set_default("")
239
240
241
242
243
244
    set_showmenu(true)
    set_description("Path to flash-attention repo. If not set, flash-attention will not used.")
option_end()

if has_config("aten") then
    add_defines("ENABLE_ATEN")
245
    if get_config("flash-attn") ~= false then
246
247
248
249
250
        add_defines("ENABLE_FLASH_ATTN")
    end
end


251
252
253
254
255
256
257
258
259
260
261
262
-- cuda graph
option("graph")
    set_default(false)
    set_showmenu(true)
    set_description("Whether to use device graph instantiating feature, such as cuda graph for nvidia")
option_end()

if has_config("graph") then
    add_defines("USE_INFINIRT_GRAPH")
end


263
264
265
266
-- InfiniCCL
option("ccl")
    set_default(false)
    set_showmenu(true)
YdrMaster's avatar
YdrMaster committed
267
    set_description("Wether to compile implementations for InfiniCCL")
268
269
270
271
272
273
option_end()

if has_config("ccl") then
    add_defines("ENABLE_CCL")
end

PanZezhong's avatar
PanZezhong committed
274
275
target("infini-utils")
    set_kind("static")
PanZezhong's avatar
PanZezhong committed
276
    on_install(function (target) end)
PanZezhong's avatar
PanZezhong committed
277
    set_languages("cxx17")
278
279
280
281

    set_warnings("all", "error")

    if is_plat("windows") then
282
        add_cxxflags("/wd4068")
283
        if has_config("omp") then
284
            add_cxxflags("/openmp")
285
286
287
        end
    else
        add_cxflags("-fPIC", "-Wno-unknown-pragmas")
288
        add_cxxflags("-fPIC", "-Wno-unknown-pragmas")
289
        if has_config("omp") then
290
            add_cxxflags("-fopenmp")
PanZezhong's avatar
PanZezhong committed
291
            add_ldflags("-fopenmp", {force = true})
292
293
294
        end
    end

PanZezhong's avatar
PanZezhong committed
295
296
297
    add_files("src/utils/*.cc")
target_end()

298
299
300
301
302
303
304
target("infinirt")
    set_kind("shared")

    if has_config("cpu") then
        add_deps("infinirt-cpu")
    end
    if has_config("nv-gpu") then
305
        add_deps("infinirt-nvidia")
306
    end
307
308
309
    if has_config("cambricon-mlu") then
        add_deps("infinirt-cambricon")
    end
310
311
312
    if has_config("ascend-npu") then
        add_deps("infinirt-ascend")
    end
313
314
315
    if has_config("metax-gpu") then
        add_deps("infinirt-metax")
    end
qinyiqun's avatar
qinyiqun committed
316
317
318
    if has_config("moore-gpu") then
        add_deps("infinirt-moore")
    end
YdrMaster's avatar
YdrMaster committed
319
320
321
    if has_config("iluvatar-gpu") then
        add_deps("infinirt-iluvatar")
    end
wooway777's avatar
wooway777 committed
322
323
324
    if has_config("ali-ppu") then
        add_deps("infinirt-ali")
    end
325
326
327
328
    if has_config("qy-gpu") then
        add_deps("infinirt-qy")
        add_files("build/.objs/infinirt-qy/rules/qy.cuda/src/infinirt/cuda/*.cu.o", {public = true})
    end
zhangyue's avatar
zhangyue committed
329
330
331
    if has_config("kunlun-xpu") then
        add_deps("infinirt-kunlun")
    end
332
333
334
    if has_config("hygon-dcu") then
        add_deps("infinirt-hygon")
    end
335
    set_languages("cxx17")
336
337
    if not is_plat("windows") then
        add_cxflags("-fPIC")
338
        add_cxxflags("-fPIC")
339
        add_ldflags("-fPIC", {force = true})
340
    end
341
342
    set_installdir(os.getenv("INFINI_ROOT") or (os.getenv(is_host("windows") and "HOMEPATH" or "HOME") .. "/.infini"))
    add_files("src/infinirt/*.cc")
343
    add_installfiles("include/infinirt.h", {prefixdir = "include"})
344
345
target_end()

PanZezhongQY's avatar
PanZezhongQY committed
346
347
target("infiniop")
    set_kind("shared")
348
349
    add_deps("infinirt")

PanZezhongQY's avatar
PanZezhongQY committed
350
351
352
353
    if has_config("cpu") then
        add_deps("infiniop-cpu")
    end
    if has_config("nv-gpu") then
354
        add_deps("infiniop-nvidia")
PanZezhongQY's avatar
PanZezhongQY committed
355
    end
YdrMaster's avatar
YdrMaster committed
356
357
358
    if has_config("iluvatar-gpu") then
        add_deps("infiniop-iluvatar")
    end
wooway777's avatar
wooway777 committed
359
360
361
    if has_config("ali-ppu") then
        add_deps("infiniop-ali")
    end
362
363
364
    if has_config("qy-gpu") then
        add_deps("infiniop-qy")
        add_files("build/.objs/infiniop-qy/rules/qy.cuda/src/infiniop/ops/*/nvidia/*.cu.o", {public = true})
qinyiqun's avatar
qinyiqun committed
365
        add_files("build/.objs/infiniop-qy/rules/qy.cuda/src/infiniop/ops/*/*/nvidia/*.cu.o", {public = true})
366
367
        add_files("build/.objs/infiniop-qy/rules/qy.cuda/src/infiniop/devices/nvidia/*.cu.o", {public = true})
    end
PanZezhongQY's avatar
PanZezhongQY committed
368
369

    if has_config("cambricon-mlu") then
PanZezhong's avatar
PanZezhong committed
370
        add_deps("infiniop-cambricon")
PanZezhongQY's avatar
PanZezhongQY committed
371
372
    end
    if has_config("ascend-npu") then
Pan Zezhong's avatar
Pan Zezhong committed
373
        add_deps("infiniop-ascend")
PanZezhongQY's avatar
PanZezhongQY committed
374
375
    end
    if has_config("metax-gpu") then
376
        add_deps("infiniop-metax")
PanZezhongQY's avatar
PanZezhongQY committed
377
    end
378
    if has_config("moore-gpu") then
379
        add_deps("infiniop-moore")
380
    end
381
382
383
    if has_config("kunlun-xpu") then
        add_deps("infiniop-kunlun")
    end
384
385
386
    if has_config("hygon-dcu") then
        add_deps("infiniop-hygon")
    end
PanZezhongQY's avatar
PanZezhongQY committed
387
388
    set_languages("cxx17")
    add_files("src/infiniop/devices/handle.cc")
qinyiqun's avatar
qinyiqun committed
389
    add_files("src/infiniop/ops/*/operator.cc", "src/infiniop/ops/*/*/operator.cc")
PanZezhongQY's avatar
PanZezhongQY committed
390
391
392
393
394
395
396
397
    add_files("src/infiniop/*.cc")

    set_installdir(os.getenv("INFINI_ROOT") or (os.getenv(is_host("windows") and "HOMEPATH" or "HOME") .. "/.infini"))
    add_installfiles("include/infiniop/(**/*.h)", {prefixdir = "include/infiniop"})
    add_installfiles("include/infiniop/*.h", {prefixdir = "include/infiniop"})
    add_installfiles("include/infiniop.h", {prefixdir = "include"})
    add_installfiles("include/infinicore.h", {prefixdir = "include"})
target_end()
398

399
400
401
402
403
target("infiniccl")
    set_kind("shared")
    add_deps("infinirt")

    if has_config("nv-gpu") then
404
        add_deps("infiniccl-nvidia")
405
    end
Pan Zezhong's avatar
Pan Zezhong committed
406
407
408
    if has_config("ascend-npu") then
        add_deps("infiniccl-ascend")
    end
wooway777's avatar
wooway777 committed
409
410
411
    if has_config("cambricon-mlu") then
        add_deps("infiniccl-cambricon")
    end
PanZezhong's avatar
PanZezhong committed
412
413
414
    if has_config("metax-gpu") then
        add_deps("infiniccl-metax")
    end
YdrMaster's avatar
YdrMaster committed
415
416
417
    if has_config("iluvatar-gpu") then
        add_deps("infiniccl-iluvatar")
    end
wooway777's avatar
wooway777 committed
418
419
420
    if has_config("ali-ppu") then
        add_deps("infiniccl-ali")
    end
421
422
    if has_config("qy-gpu") then
        add_deps("infiniccl-qy")
423
        add_files("build/.objs/infiniccl-qy/rules/qy.cuda/src/infiniccl/cuda/*.cu.o", {public = true})
424
    end
YdrMaster's avatar
YdrMaster committed
425

spike-zhu's avatar
spike-zhu committed
426
427
428
    if has_config("moore-gpu") then
        add_deps("infiniccl-moore")
    end
429

430
431
432
    if has_config("kunlun-xpu") then
        add_deps("infiniccl-kunlun")
    end
433
434
435
    if has_config("hygon-dcu") then
        add_deps("infiniccl-hygon")
    end
436

437
438
439
440
441
442
443
    set_languages("cxx17")

    add_files("src/infiniccl/*.cc")
    add_installfiles("include/infiniccl.h", {prefixdir = "include"})

    set_installdir(os.getenv("INFINI_ROOT") or (os.getenv(is_host("windows") and "HOMEPATH" or "HOME") .. "/.infini"))
target_end()
444

445
target("infinicore_c_api")
446
    set_kind("phony")
447
    add_deps("infiniop", "infinirt", "infiniccl")
448
449
    after_build(function (target) print(YELLOW .. "[Congratulations!] Now you can install the libraries with \"xmake install\"" .. NC) end)
target_end()
450

451
452
453
454
target("infinicore_cpp_api")
    set_kind("shared")
    add_deps("infiniop", "infinirt", "infiniccl")
    set_languages("cxx17")
455
    set_symbols("visibility")
456
457
458
459
460
461
462
463
464

    local INFINI_ROOT = os.getenv("INFINI_ROOT") or (os.getenv(is_host("windows") and "HOMEPATH" or "HOME") .. "/.infini")

    add_includedirs("include")
    add_includedirs(INFINI_ROOT.."/include", { public = true })

    add_linkdirs(INFINI_ROOT.."/lib")
    add_links("infiniop", "infinirt", "infiniccl")

465
    if get_config("flash-attn") ~= "" then
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
        add_installfiles("(builddir)/$(plat)/$(arch)/$(mode)/flash-attn*.so", {prefixdir = "lib"})
        if has_config("nv-gpu") then
            add_deps("flash-attn-nvidia")
        end
    end

    before_build(function (target)
        if has_config("aten") then
            local outdata = os.iorunv("python", {"-c", "import torch, os; print(os.path.dirname(torch.__file__))"}):trim()
            local TORCH_DIR = outdata

            target:add(
                "includedirs", 
                path.join(TORCH_DIR, "include"), 
                path.join(TORCH_DIR, "include/torch/csrc/api/include"),
                { public = true })
            
            target:add(
                "linkdirs",
                path.join(TORCH_DIR, "lib"),
                { public = true }
            )
            target:add(
                "links",
                "torch",
                "c10",
                "torch_cuda",
                "c10_cuda",
                { public = true }
            )
        end

    end)
499

500
501
    -- Add InfiniCore C++ source files (needed for RoPE and other nn modules)
    add_files("src/infinicore/*.cc")
502
    add_files("src/infinicore/adaptor/*.cc")
503
504
505
    add_files("src/infinicore/context/*.cc")
    add_files("src/infinicore/context/*/*.cc")
    add_files("src/infinicore/tensor/*.cc")
506
    add_files("src/infinicore/graph/*.cc")
507
508
    add_files("src/infinicore/nn/*.cc")
    add_files("src/infinicore/ops/*/*.cc")
509
    add_files("src/utils/*.cc")
510
511
512
513
514
515
516
517
518
519
520

    set_installdir(INFINI_ROOT)
    add_installfiles("include/infinicore/(**.h)",    {prefixdir = "include/infinicore"})
    add_installfiles("include/infinicore/(**.hpp)",    {prefixdir = "include/infinicore"})
    add_installfiles("include/infinicore/(**/*.h)",  {prefixdir = "include/infinicore"})
    add_installfiles("include/infinicore/(**/*.hpp)",{prefixdir = "include/infinicore"})
    add_installfiles("include/infinicore.h",          {prefixdir = "include"})
    add_installfiles("include/infinicore.hpp",        {prefixdir = "include"})
    after_build(function (target) print(YELLOW .. "[Congratulations!] Now you can install the libraries with \"xmake install\"" .. NC) end)
target_end()

521
522
523
524
525
526
527
528
529
530
target("_infinicore")
    add_packages("boost")
    if is_mode("debug") then
        add_defines("BOOST_STACKTRACE_USE_BACKTRACE")
        add_links("backtrace")
    else
        add_defines("BOOST_STACKTRACE_USE_NOOP")
    end

    set_default(false)
531
532
    add_rules("python.library", {soabi = true})
    add_packages("pybind11")
533
    set_languages("cxx17")
534

535
536
    add_deps("infinicore_cpp_api")

537
    set_kind("shared")
538
539
540
541
542
    local INFINI_ROOT = os.getenv("INFINI_ROOT") or (os.getenv(is_host("windows") and "HOMEPATH" or "HOME") .. "/.infini")
    add_includedirs(INFINI_ROOT.."/include", { public = true })

    add_linkdirs(INFINI_ROOT.."/lib")
    add_links("infiniop", "infinirt", "infiniccl")
543

544
    add_files("src/infinicore/pybind11/**.cc")
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
    set_installdir("python/infinicore")
target_end()

option("editable")
    set_default(false)
    set_showmenu(true)
    set_description("Install the `infinicore` Python package in editable mode")
option_end()

target("infinicore")
    set_kind("phony")

    set_default(false)

    add_deps("_infinicore")

    on_install(function (target)
        local pip_install_args = {}

        if has_config("editable") then
            table.insert(pip_install_args, "--editable")
        end

        os.execv("python", table.join({"-m", "pip", "install"}, pip_install_args, {"."}))
    end)
571
572
target_end()

573
574
-- Tests
includes("xmake/test.lua")