Repository navigation
Expand file tree
/
Copy pathsetup.py
More file actions
executable file
·155 lines (141 loc) · 5.09 KB
/
Copy pathsetup.py
File metadata and controls
executable file
·155 lines (141 loc) · 5.09 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
#!/usr/bin/env python3
"""Build sgl-kernel-npu from selected AICORE, AICPU and HOST operators and Python sources."""
import argparse
import os
import re
import shutil
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent / "python/sgl_kernel_npu"))
from build_tools.cann import PROJECT_ROOT, _remove_tree, build_cann, parse_operators
def arguments(argv=None):
argv = list(sys.argv[1:] if argv is None else argv)
parser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument(
"--arch-version",
choices=("2201", "3510"),
help="required architecture coverage, except for standalone --make-clean",
)
parser.add_argument(
"--ops",
default="all",
help="all supported AICORE/AICPU/HOST operators, or comma-separated operator IDs",
)
parser.add_argument(
"--artifact",
choices=("run", "wheel"),
default="wheel",
help="AICORE/AICPU/HOST RUN installer or complete sgl-kernel-npu wheel",
)
parser.add_argument(
"-j", "--jobs", type=int, default=16, help="parallel compilation jobs"
)
third_party = parser.add_mutually_exclusive_group()
third_party.add_argument(
"--third-party",
help="comma-separated third-party profile filenames without .json; default: all",
)
third_party.add_argument(
"--without-third-party",
action="store_true",
help="build only repository-owned operators",
)
parser.add_argument(
"--plan",
action="store_true",
help="print third-party build commands without building",
)
parser.add_argument(
"--cann-version",
help="CANN profile version for --plan only; builds detect the SDK version",
)
parser.add_argument(
"--make-clean",
action="store_true",
help="remove this build's intermediates; used alone, clean and exit",
)
args = parser.parse_args(argv)
if args.cann_version and not args.plan:
parser.error("--cann-version is only available with --plan")
if not args.arch_version and argv != ["--make-clean"]:
parser.error("--arch-version is required")
if args.jobs < 1:
parser.error("--jobs must be positive")
if args.third_party is not None:
if args.artifact != "wheel":
parser.error("--third-party requires --artifact wheel")
args.third_party = tuple(name.strip() for name in args.third_party.split(","))
if any(
not re.fullmatch(r"[a-z][a-z0-9_]*", name) for name in args.third_party
) or len(set(args.third_party)) != len(args.third_party):
parser.error("--third-party requires distinct, nonempty profile names")
try:
args.native_operators = parse_operators(args.ops)
except ValueError as error:
parser.error(str(error))
return args
def main(argv=None):
args = arguments(argv)
if (PROJECT_ROOT / "build").is_symlink():
raise RuntimeError("Refusing a symlinked build root")
if args.make_clean:
for name in ("cann", "torch", "wheel"):
_remove_tree(PROJECT_ROOT / "build" / name)
if not args.arch_version:
print("sgl-kernel-npu intermediates removed")
return
from build_tools.third_party import (
build_dependencies,
detect_cann_version,
resolve_plan,
)
plan = []
if not args.without_third_party and args.artifact == "wheel":
plan = resolve_plan(
args.cann_version or detect_cann_version(),
args.arch_version,
args.jobs,
third_party=args.third_party,
)
if args.plan:
import json
print(json.dumps(plan, indent=2))
return
dependencies = build_dependencies(plan) if plan else []
output = PROJECT_ROOT / "output"
output.mkdir(exist_ok=True)
extension = "whl" if args.artifact == "wheel" else "run"
for path in output.glob(f"sgl_kernel_npu-*.{extension}"):
path.unlink()
native_build = build_cann(
args.arch_version,
args.native_operators,
jobs=args.jobs,
)
if args.artifact == "run":
destination = (
output
/ f"sgl_kernel_npu-{args.arch_version}-{native_build.selection_kind}.run"
)
shutil.copy2(native_build.package, destination)
else:
from build_tools.torch_wheel import build_torch, build_wheel
package = build_torch(
native_build, args.arch_version, jobs=args.jobs, dependencies=dependencies
)
wheel = build_wheel(package)
destination = output / wheel.name
shutil.copy2(wheel, destination)
print(f"ARTIFACT: {destination}")
if __name__ == "__main__":
if os.environ.get("SGL_REFACTOR_STAGED_PACKAGE"):
from build_tools.torch_wheel import setup_refactor
setup_refactor()
else:
try:
main()
except (OSError, RuntimeError, ValueError) as error:
print(f"FAIL: {error}", file=sys.stderr)
sys.exit(1)