forked from thu-ml/SageAttention
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
187 lines (165 loc) · 6.37 KB
/
setup.py
File metadata and controls
187 lines (165 loc) · 6.37 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
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
import warnings
import os
from pathlib import Path
from packaging.version import parse, Version
from setuptools import setup, find_packages
import subprocess
from wheel.bdist_wheel import bdist_wheel as _bdist_wheel
import torch
from torch.utils.cpp_extension import BuildExtension, CppExtension, CUDAExtension, CUDA_HOME
this_dir = os.path.dirname(os.path.abspath(__file__))
PACKAGE_NAME = "sageattn3"
# FORCE_BUILD: Force a fresh build locally, instead of attempting to find prebuilt wheels
# SKIP_CUDA_BUILD: Intended to allow CI to use a simple `python setup.py sdist` run to copy over raw files, without any cuda compilation
FORCE_BUILD = os.getenv("FAHOPPER_FORCE_BUILD", "FALSE") == "TRUE"
SKIP_CUDA_BUILD = os.getenv("FAHOPPER_SKIP_CUDA_BUILD", "FALSE") == "TRUE"
# For CI, we want the option to build with C++11 ABI since the nvcr images use C++11 ABI
FORCE_CXX11_ABI = os.getenv("FAHOPPER_FORCE_CXX11_ABI", "FALSE") == "TRUE"
def get_cuda_bare_metal_version(cuda_dir):
raw_output = subprocess.check_output([cuda_dir + "/bin/nvcc", "-V"], universal_newlines=True)
output = raw_output.split()
release_idx = output.index("release") + 1
bare_metal_version = parse(output[release_idx].split(",")[0])
return raw_output, bare_metal_version
def check_if_cuda_home_none(global_option: str) -> None:
if CUDA_HOME is not None:
return
# warn instead of error because user could be downloading prebuilt wheels, so nvcc won't be necessary
# in that case.
warnings.warn(
f"{global_option} was requested, but nvcc was not found. Are you sure your environment has nvcc available? "
"If you're installing within a container from https://hub.docker.com/r/pytorch/pytorch, "
"only images whose names contain 'devel' will provide nvcc."
)
def append_nvcc_threads(nvcc_extra_args):
return nvcc_extra_args + ["--threads", "4"]
cmdclass = {}
ext_modules = []
if not SKIP_CUDA_BUILD:
print("\n\ntorch.__version__ = {}\n\n".format(torch.__version__))
TORCH_MAJOR = int(torch.__version__.split(".")[0])
TORCH_MINOR = int(torch.__version__.split(".")[1])
cc_flag = []
_, bare_metal_version = get_cuda_bare_metal_version(CUDA_HOME)
if bare_metal_version < Version("12.8"):
raise RuntimeError("Sage3 is only supported on CUDA 12.8 and above")
cc_major, cc_minor = torch.cuda.get_device_capability()
if (cc_major, cc_minor) == (10, 0): # sm_100
cc_flag.append("-gencode")
cc_flag.append("arch=compute_100a,code=sm_100a")
elif (cc_major, cc_minor) == (12, 0): # sm_120
cc_flag.append("-gencode")
cc_flag.append("arch=compute_120a,code=sm_120a")
elif (cc_major, cc_minor) == (12, 1): # sm_121
cc_flag.append("-gencode")
cc_flag.append("arch=compute_121a,code=sm_121a")
else:
raise RuntimeError("Unsupported GPU")
# HACK: The compiler flag -D_GLIBCXX_USE_CXX11_ABI is set to be the same as
# torch._C._GLIBCXX_USE_CXX11_ABI
# https://github.com/pytorch/pytorch/blob/8472c24e3b5b60150096486616d98b7bea01500b/torch/utils/cpp_extension.py#L920
if FORCE_CXX11_ABI:
torch._C._GLIBCXX_USE_CXX11_ABI = True
repo_dir = Path(this_dir)
cutlass_dir = repo_dir / "csrc" / "cutlass"
(repo_dir / "csrc").mkdir(parents=True, exist_ok=True)
if not cutlass_dir.exists():
subprocess.run(
["git", "clone", "--depth", "1", "https://github.com/NVIDIA/cutlass.git", str(cutlass_dir)],
check=True
)
nvcc_flags = [
"-O3",
# "-O0",
"-std=c++17",
"-U__CUDA_NO_HALF_OPERATORS__",
"-U__CUDA_NO_HALF_CONVERSIONS__",
"-U__CUDA_NO_BFLOAT16_OPERATORS__",
"-U__CUDA_NO_BFLOAT16_CONVERSIONS__",
"-U__CUDA_NO_BFLOAT162_OPERATORS__",
"-U__CUDA_NO_BFLOAT162_CONVERSIONS__",
"--expt-relaxed-constexpr",
"--expt-extended-lambda",
"--use_fast_math",
# "--ptxas-options=-v", # printing out number of registers
"--ptxas-options=--verbose,--warn-on-local-memory-usage", # printing out number of registers
"-lineinfo",
"-DCUTLASS_DEBUG_TRACE_LEVEL=0", # Can toggle for debugging
"-DNDEBUG", # Important, otherwise performance is severely impacted
"-DQBLKSIZE=128",
"-DKBLKSIZE=128",
"-DCTA256",
"-DDQINRMEM",
]
include_dirs = [
repo_dir / "sageattn3",
cutlass_dir / "include",
cutlass_dir / "tools" / "util" / "include",
]
ext_modules.append(
CUDAExtension(
name="fp4attn_cuda",
sources=["sageattn3/blackwell/api.cu"],
extra_compile_args={
"cxx": ["-O3", "-std=c++17"],
"nvcc": append_nvcc_threads(
nvcc_flags + ["-DEXECMODE=0"] + cc_flag
),
},
include_dirs=include_dirs,
# Without this we get and error about cuTensorMapEncodeTiled not defined
libraries=["cuda"]
)
)
ext_modules.append(
CUDAExtension(
name="fp4quant_cuda",
sources=["sageattn3/quantization/fp4_quantization_4d.cu"],
extra_compile_args={
"cxx": ["-O3", "-std=c++17"],
"nvcc": append_nvcc_threads(
nvcc_flags + ["-DEXECMODE=0"] + cc_flag
),
},
include_dirs=include_dirs,
# Without this we get and error about cuTensorMapEncodeTiled not defined
libraries=["cuda"]
)
)
class CachedWheelsCommand(_bdist_wheel):
def run(self):
super().run()
setup(
name=PACKAGE_NAME,
version="1.0.0",
packages=find_packages(
exclude=(
"build",
"csrc",
"tests",
"dist",
"docs",
"benchmarks",
)
),
description="FP4FlashAttention",
long_description_content_type="text/markdown",
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: Apache Software License",
"Operating System :: Unix",
],
ext_modules=ext_modules,
cmdclass={"bdist_wheel": CachedWheelsCommand, "build_ext": BuildExtension}
if ext_modules
else {
"bdist_wheel": CachedWheelsCommand,
},
python_requires=">=3.8",
install_requires=[
"torch",
"einops",
"packaging",
"ninja",
],
)