kunlun.lua 3.61 KB
Newer Older
1
2
add_defines("ENABLE_KUNLUN_API")
local KUNLUN_HOME = os.getenv("KUNLUN_HOME")
zhangyue's avatar
zhangyue committed
3
4
5
local XRE_DIR = path.join(KUNLUN_HOME, "xre")
local XTDK_DIR = path.join(KUNLUN_HOME, "xtdk")
local XDNN_DIR = path.join(KUNLUN_HOME, "xhpc", "xdnn")
zhangyue's avatar
zhangyue committed
6
local XBLAS_DIR = path.join(KUNLUN_HOME, "xhpc", "xblas")
7
local XCCL_DIR = path.join(KUNLUN_HOME, "xccl")
8
9

-- Add include dirs
10
11
12
add_includedirs(path.join(XRE_DIR, "include"), {public = true})
add_includedirs(path.join(XDNN_DIR, "include"), {public = true})
add_includedirs(path.join(XTDK_DIR, "include"), {public = true})
zhangyue's avatar
zhangyue committed
13
add_includedirs(path.join(XBLAS_DIR, "include"), {public = true})
14
15

-- Add link dirs
zhangyue's avatar
zhangyue committed
16
17
add_linkdirs(path.join(XRE_DIR, "so"))
add_linkdirs(path.join(XDNN_DIR, "so"))
zhangyue's avatar
zhangyue committed
18
add_linkdirs(path.join(XBLAS_DIR, "so"))
19
20

-- Add links
zhangyue's avatar
zhangyue committed
21
add_links("xpurt", "xpuapi", "xpu_blas")
22

23
24
25
26
27
rule("xpu")
    set_extensions(".xpu")
    
    on_build_file(function (target, sourcefile)

zhangyue's avatar
zhangyue committed
28
29
30
        local sourcefile_config = target:fileconfig(sourcefile) or {}
        local includedirs = sourcefile_config.includedirs or {}

31
32
33
34
        local objectfile = target:objectfile(sourcefile)
        os.mkdir(path.directory(objectfile))
        local cc = path.join(XTDK_DIR, "bin/clang++")
        local includedirs = table.concat(target:get("includedirs"), " ")
35
36
37
38
39
        local arch_map = {
            ["x86_64"] = "x86_64-linux-gnu",
            ["arm64"] = "aarch64-linux-gnu"
        }

40
41
        local args = {
            "--sysroot=/",
42
            "--target=" .. arch_map[os.arch()],
43
            "-fPIC",
zhangyue's avatar
zhangyue committed
44
45
            "--xpu-arch=xpu3",
            "-std=c++17",
46
47
48
            "-O2",
            "-fno-builtin",
            "-c", sourcefile,
zhangyue's avatar
zhangyue committed
49
            "-o", objectfile
50
51
52
53
54
55
56
        }
        
        for _, includedir in ipairs(target:get("includedirs")) do
            table.insert(args, "-I" .. includedir)
        end

        -- print(args)
zhangyue's avatar
zhangyue committed
57
58
59
        local ok, code = os.execv(cc, args)
        assert(ok == 0, "Compile failed: " .. sourcefile)

60
61
62
63
64
65
66
        table.insert(target:objectfiles(), objectfile)
        print(target:objectfiles())
    end)
rule_end()

local src_dir = path.join(os.projectdir(), "src", "infiniop")

67
68
target("infiniop-kunlun")
    set_kind("static")
PanZezhong's avatar
PanZezhong committed
69
    add_deps("infini-utils")
70
71
    on_install(function (target) end)

zhangyue's avatar
zhangyue committed
72
    add_cxflags("-lstdc++ -fPIC -Wno-error=unused-function")
73
74
75
76
    set_warnings("all", "error")

    set_languages("cxx17")
    add_files("$(projectdir)/src/infiniop/devices/kunlun/*.cc", "$(projectdir)/src/infiniop/ops/*/kunlun/*.cc")
77
78
79
    -- compile handwriting kernel
    local xpu_files = os.files(src_dir .. "/ops/*/kunlun/*.xpu")
    if #xpu_files > 0 then
zhangyue's avatar
zhangyue committed
80
81
82
        add_files(xpu_files, {
            rule = "xpu",
            includedirs = {
83
                path.join(os.projectdir(), "include"),
zhangyue's avatar
zhangyue committed
84
85
86
87
88
                path.join(XRE_DIR, "include"),
                path.join(XDNN_DIR, "include"),
                path.join(XTDK_DIR, "include")
            }
        })
89
    end
90
target_end()
zhangyue's avatar
zhangyue committed
91
92
93
94
95
96
97

target("infinirt-kunlun")
    set_kind("static")
    add_deps("infini-utils")
    set_languages("cxx17")
    on_install(function (target) end)
    -- Add include dirs
zhangyue's avatar
zhangyue committed
98
    add_files("$(projectdir)/src/infinirt/kunlun/*.cc")
zhangyue's avatar
zhangyue committed
99
    add_cxflags("-lstdc++ -Wall -Werror -fPIC")
100
target_end()
zhangyue's avatar
zhangyue committed
101

102
103
104
105
106
107
108
109
110
111
112
113
114
115
target("infiniccl-kunlun")
    set_kind("static")
    add_deps("infinirt")
    add_deps("infini-utils")
    set_warnings("all", "error")
    set_languages("cxx17")
    on_install(function (target) end)
    if has_config("ccl") then
        add_includedirs(path.join(XCCL_DIR, "include"))
        add_linkdirs(path.join(XCCL_DIR, "so"))
        add_links("bkcl")
        add_files("$(projectdir)/src/infiniccl/kunlun/*.cc")
        add_cxflags("-lstdc++ -fPIC")
    end
zhangyue's avatar
zhangyue committed
116
target_end()