-
Notifications
You must be signed in to change notification settings - Fork 161
/
setup.py
240 lines (210 loc) · 7.98 KB
/
setup.py
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
"""
Copyright (c) 2023 by FlashInfer team.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import argparse
import os
import platform
import re
import subprocess
import sys
from pathlib import Path
import setuptools
root = Path(__file__).parent.resolve()
gen_dir = root / "csrc" / "generated"
head_dims = os.environ.get("FLASHINFER_HEAD_DIMS", "64,128,256").split(",")
pos_encoding_modes = os.environ.get("FLASHINFER_POS_ENCODING_MODES", "0").split(",")
allow_fp16_qk_reductions = os.environ.get(
"FLASHINFER_ALLOW_FP16_QK_REDUCTION_OPTIONS", "0"
).split(",")
mask_modes = os.environ.get("FLASHINFER_MASK_MODES", "0,1,2").split(",")
head_dims = list(map(int, head_dims))
pos_encoding_modes = list(map(int, pos_encoding_modes))
pos_encoding_modes_sm90 = [mode for mode in pos_encoding_modes if mode != 2]
allow_fp16_qk_reductions = list(map(int, allow_fp16_qk_reductions))
allow_fp16_qk_reductions_sm90 = [mode for mode in allow_fp16_qk_reductions if mode != 1]
mask_modes = list(map(int, mask_modes))
enable_aot = os.environ.get("FLASHINFER_ENABLE_AOT", "0") == "1"
enable_bf16 = os.environ.get("FLASHINFER_ENABLE_BF16", "1") == "1"
enable_fp8 = os.environ.get("FLASHINFER_ENABLE_FP8", "1") == "1"
enable_sm90 = os.environ.get("FLASHINFER_ENABLE_SM90", "1") == "1"
def get_version():
package_version = (root / "version.txt").read_text().strip()
local_version = os.environ.get("FLASHINFER_LOCAL_VERSION")
if local_version is None:
return package_version
return f"{package_version}+{local_version}"
def generate_build_meta(aot_build_meta: dict) -> None:
build_meta_str = f"__version__ = {get_version()!r}\n"
if len(aot_build_meta) != 0:
build_meta_str += f"build_meta = {aot_build_meta!r}\n"
(root / "flashinfer" / "_build_meta.py").write_text(build_meta_str)
def generate_cuda() -> None:
try: # no aot_build_utils in sdist
sys.path.append(str(root))
from aot_build_utils.generate import get_instantiation_cu
from aot_build_utils.generate_sm90 import get_sm90_instantiation_cu
except ImportError:
return
aot_kernel_uris = get_instantiation_cu(
argparse.Namespace(
path=gen_dir,
head_dims=head_dims,
pos_encoding_modes=pos_encoding_modes,
allow_fp16_qk_reductions=allow_fp16_qk_reductions,
mask_modes=mask_modes,
enable_bf16=enable_bf16,
enable_fp8=enable_fp8,
)
)
if enable_sm90:
aot_kernel_uris += get_sm90_instantiation_cu(
argparse.Namespace(
path=gen_dir,
head_dims=head_dims,
pos_encoding_modes=pos_encoding_modes_sm90,
allow_fp16_qk_reductions=allow_fp16_qk_reductions_sm90,
mask_modes=mask_modes,
enable_bf16=enable_bf16,
)
)
aot_config_str = f"""prebuilt_ops_uri = set({aot_kernel_uris})"""
(root / "flashinfer" / "jit" / "aot_config.py").write_text(aot_config_str)
ext_modules = []
cmdclass = {}
install_requires = ["torch", "ninja"]
generate_build_meta({})
generate_cuda()
if enable_aot:
import torch
import torch.utils.cpp_extension as torch_cpp_ext
from packaging.version import Version
def get_cuda_version() -> Version:
if torch_cpp_ext.CUDA_HOME is None:
nvcc = "nvcc"
else:
nvcc = os.path.join(torch_cpp_ext.CUDA_HOME, "bin/nvcc")
txt = subprocess.check_output([nvcc, "--version"], text=True)
return Version(re.findall(r"release (\d+\.\d+),", txt)[0])
class NinjaBuildExtension(torch_cpp_ext.BuildExtension):
def __init__(self, *args, **kwargs) -> None:
# do not override env MAX_JOBS if already exists
if not os.environ.get("MAX_JOBS"):
max_num_jobs_cores = max(1, os.cpu_count())
os.environ["MAX_JOBS"] = str(max_num_jobs_cores)
super().__init__(*args, **kwargs)
# cuda arch check for fp8 at the moment.
for cuda_arch_flags in torch_cpp_ext._get_cuda_arch_flags():
arch = int(re.search(r"compute_(\d+)", cuda_arch_flags).group(1))
if arch < 75:
raise RuntimeError("FlashInfer requires sm75+")
cuda_version = get_cuda_version()
torch_full_version = Version(torch.__version__)
torch_version = f"{torch_full_version.major}.{torch_full_version.minor}"
cmdclass["build_ext"] = NinjaBuildExtension
install_requires = [f"torch == {torch_version}"]
aot_build_meta = {}
aot_build_meta["cuda_major"] = cuda_version.major
aot_build_meta["cuda_minor"] = cuda_version.minor
aot_build_meta["torch"] = torch_version
aot_build_meta["python"] = platform.python_version()
aot_build_meta["TORCH_CUDA_ARCH_LIST"] = os.environ.get("TORCH_CUDA_ARCH_LIST")
generate_build_meta(aot_build_meta)
if enable_bf16:
torch_cpp_ext.COMMON_NVCC_FLAGS.append("-DFLASHINFER_ENABLE_BF16")
if enable_fp8:
torch_cpp_ext.COMMON_NVCC_FLAGS.append("-DFLASHINFER_ENABLE_FP8")
for flag in [
"-D__CUDA_NO_HALF_OPERATORS__",
"-D__CUDA_NO_HALF_CONVERSIONS__",
"-D__CUDA_NO_BFLOAT16_CONVERSIONS__",
"-D__CUDA_NO_HALF2_OPERATORS__",
]:
try:
torch_cpp_ext.COMMON_NVCC_FLAGS.remove(flag)
except ValueError:
pass
cutlass = root / "3rdparty" / "cutlass"
include_dirs = [
root.resolve() / "include",
cutlass.resolve() / "include", # for group gemm
cutlass.resolve() / "tools" / "util" / "include",
]
cxx_flags = [
"-O3",
"-Wno-switch-bool",
]
nvcc_flags = [
"-O3",
"-std=c++17",
"--threads=1",
"-Xfatbin",
"-compress-all",
"-use_fast_math",
]
sm90a_flags = "-gencode arch=compute_90a,code=sm_90a".split()
kernel_sources = [
"csrc/bmm_fp8.cu",
"csrc/cascade.cu",
"csrc/group_gemm.cu",
"csrc/norm.cu",
"csrc/page.cu",
"csrc/quantization.cu",
"csrc/rope.cu",
"csrc/sampling.cu",
"csrc/renorm.cu",
"csrc/activation.cu",
"csrc/batch_decode.cu",
"csrc/batch_prefill.cu",
"csrc/single_decode.cu",
"csrc/single_prefill.cu",
"csrc/flashinfer_ops.cu",
]
kernel_sm90_sources = [
"csrc/group_gemm_sm90.cu",
"csrc/single_prefill_sm90.cu",
"csrc/batch_prefill_sm90.cu",
"csrc/flashinfer_ops_sm90.cu",
]
decode_sources = list(gen_dir.glob("*decode_head*.cu"))
prefill_sources = [
f for f in gen_dir.glob("*prefill_head*.cu") if "_sm90" not in f.name
]
prefill_sm90_sources = list(gen_dir.glob("*prefill_head*_sm90.cu"))
ext_modules = [
torch_cpp_ext.CUDAExtension(
name="flashinfer._kernels",
sources=kernel_sources + decode_sources + prefill_sources,
include_dirs=include_dirs,
extra_compile_args={
"cxx": cxx_flags,
"nvcc": nvcc_flags,
},
)
]
if enable_sm90:
ext_modules += [
torch_cpp_ext.CUDAExtension(
name="flashinfer._kernels_sm90",
sources=kernel_sm90_sources + prefill_sm90_sources,
include_dirs=include_dirs,
extra_compile_args={
"cxx": cxx_flags,
"nvcc": nvcc_flags + sm90a_flags,
},
),
]
setuptools.setup(
version=get_version(),
ext_modules=ext_modules,
cmdclass=cmdclass,
install_requires=install_requires,
)