user-7 commited on
Commit
a07907a
·
verified ·
1 Parent(s): b5ad516

Upload 2 files

Browse files
.gitattributes CHANGED
@@ -49,3 +49,4 @@ wheel/python3.12/torch2.9.0/vllm/vllm-0.18.0rc3.dev0+g89138b21c.d20260319.cu131-
49
  wheel/python3.12/torch2.9.0/vllm/vllm-0.18.0+cu131-cp312-cp312-linux_x86_64.whl filter=lfs diff=lfs merge=lfs -text
50
  wheel/python3.12/torch2.9.0/vllm/vllm-0.18.0+precompiled-cp312-cp312-linux_x86_64.whl filter=lfs diff=lfs merge=lfs -text
51
  wheel/python3.12/torch2.9.0/facenet-0.1.0-py3-none-any.whl filter=lfs diff=lfs merge=lfs -text
 
 
49
  wheel/python3.12/torch2.9.0/vllm/vllm-0.18.0+cu131-cp312-cp312-linux_x86_64.whl filter=lfs diff=lfs merge=lfs -text
50
  wheel/python3.12/torch2.9.0/vllm/vllm-0.18.0+precompiled-cp312-cp312-linux_x86_64.whl filter=lfs diff=lfs merge=lfs -text
51
  wheel/python3.12/torch2.9.0/facenet-0.1.0-py3-none-any.whl filter=lfs diff=lfs merge=lfs -text
52
+ wheel/python3.12/torch2.9.0/block_sparse_attn-0.0.2-cp312-cp312-linux_x86_64.whl filter=lfs diff=lfs merge=lfs -text
wheel/python3.12/torch2.9.0/Block-Sparse-Attention_setup.py ADDED
@@ -0,0 +1,393 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (c) 2023, Tri Dao.
2
+ # Adapted from https://github.com/Dao-AILab/flash-attention/blob/main/setup.py
3
+
4
+ import sys
5
+ import functools
6
+ import warnings
7
+ import os
8
+ import re
9
+ import ast
10
+ from pathlib import Path
11
+ from packaging.version import parse, Version
12
+ import platform
13
+
14
+ from setuptools import setup, find_packages
15
+ import subprocess
16
+
17
+ import urllib.request
18
+ import urllib.error
19
+ from wheel.bdist_wheel import bdist_wheel as _bdist_wheel
20
+
21
+ import torch
22
+ from torch.utils.cpp_extension import (
23
+ BuildExtension,
24
+ CppExtension,
25
+ CUDAExtension,
26
+ CUDA_HOME,
27
+ )
28
+
29
+
30
+ with open("README.md", "r", encoding="utf-8") as fh:
31
+ long_description = fh.read()
32
+
33
+
34
+ # ninja build does not work unless include_dirs are abs path
35
+ this_dir = os.path.dirname(os.path.abspath(__file__))
36
+
37
+ PACKAGE_NAME = "block_sparse_attn"
38
+
39
+ BASE_WHEEL_URL = (
40
+ "https://github.com/mit-han-lab/Block-Sparse-Attention/releases/download/{tag_name}/{wheel_name}"
41
+ )
42
+
43
+ # FORCE_BUILD: Force a fresh build locally, instead of attempting to find prebuilt wheels
44
+ # 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
45
+ FORCE_BUILD = os.getenv("BLOCK_SPARSE_ATTN_FORCE_BUILD", "FALSE") == "TRUE"
46
+ SKIP_CUDA_BUILD = os.getenv("BLOCK_SPARSE_ATTN_SKIP_CUDA_BUILD", "FALSE") == "TRUE"
47
+ # For CI, we want the option to build with C++11 ABI since the nvcr images use C++11 ABI
48
+ FORCE_CXX11_ABI = os.getenv("BLOCK_SPARSE_ATTN_FORCE_CXX11_ABI", "FALSE") == "TRUE"
49
+
50
+ @functools.lru_cache(maxsize=None)
51
+ def cuda_archs() -> str:
52
+ # 默认仅编译 sm_89 (RTX 4080 Super)
53
+ return os.getenv("BLOCK_SPARSE_ATTN_CUDA_ARCHS", "89").split(";")
54
+
55
+
56
+ def get_platform():
57
+ """
58
+ Returns the platform name as used in wheel filenames.
59
+ """
60
+ if sys.platform.startswith("linux"):
61
+ return f'linux_{platform.uname().machine}'
62
+ elif sys.platform == "darwin":
63
+ mac_version = ".".join(platform.mac_ver()[0].split(".")[:2])
64
+ return f"macosx_{mac_version}_x86_64"
65
+ elif sys.platform == "win32":
66
+ return "win_amd64"
67
+ else:
68
+ raise ValueError("Unsupported platform: {}".format(sys.platform))
69
+
70
+
71
+ def get_cuda_bare_metal_version(cuda_dir):
72
+ raw_output = subprocess.check_output([cuda_dir + "/bin/nvcc", "-V"], universal_newlines=True)
73
+ output = raw_output.split()
74
+ release_idx = output.index("release") + 1
75
+ bare_metal_version = parse(output[release_idx].split(",")[0])
76
+
77
+ return raw_output, bare_metal_version
78
+
79
+
80
+ def add_cuda_gencodes(cc_flag, archs, bare_metal_version):
81
+ """
82
+ Adds -gencode flags based on nvcc capabilities:
83
+ - sm_80/89/90 (regular)
84
+ - sm_100/120 on CUDA >= 12.8
85
+ - Use 100f on CUDA >= 12.9 (Blackwell family-specific)
86
+ - Map requested 110 -> 101 if CUDA < 13.0 (Thor rename)
87
+ - Embed PTX for newest arch for forward compatibility
88
+ """
89
+ # sm_80
90
+ if "80" in archs:
91
+ cc_flag += ["-gencode", "arch=compute_80,code=sm_80"]
92
+
93
+ # sm_89 (Ada Lovelace, e.g., RTX 4080 Super) - needs CUDA >= 11.8
94
+ if "89" in archs:
95
+ cc_flag += ["-gencode", "arch=compute_89,code=sm_89"]
96
+
97
+ # Hopper 9.0 needs >= 11.8
98
+ if bare_metal_version >= Version("11.8") and "90" in archs:
99
+ cc_flag += ["-gencode", "arch=compute_90,code=sm_90"]
100
+
101
+ # Blackwell 10.x requires >= 12.8
102
+ if bare_metal_version >= Version("12.8"):
103
+ if "100" in archs:
104
+ # CUDA 12.9 introduced "family-specific" for Blackwell (100f)
105
+ if bare_metal_version >= Version("12.9"):
106
+ cc_flag += ["-gencode", "arch=compute_100f,code=sm_100"]
107
+ else:
108
+ cc_flag += ["-gencode", "arch=compute_100,code=sm_100"]
109
+
110
+ if "120" in archs:
111
+ # sm_120 is supported in CUDA 12.8/12.9+ toolkits
112
+ if bare_metal_version >= Version("12.9"):
113
+ cc_flag += ["-gencode", "arch=compute_120f,code=sm_120"]
114
+ else:
115
+ cc_flag += ["-gencode", "arch=compute_120,code=sm_120"]
116
+
117
+ # Thor rename: 12.9 uses sm_101; 13.0+ uses sm_110
118
+ if "110" in archs:
119
+ if bare_metal_version >= Version("13.0"):
120
+ cc_flag += ["-gencode", "arch=compute_110f,code=sm_110"]
121
+ else:
122
+ # Provide Thor support for CUDA 12.9 via sm_101
123
+ if bare_metal_version >= Version("12.8"):
124
+ cc_flag += ["-gencode", "arch=compute_101,code=sm_101"]
125
+ # else: no Thor support in older toolkits
126
+
127
+ # PTX for newest requested arch (forward-compat)
128
+ numeric = [a for a in archs if a.isdigit()]
129
+ if numeric:
130
+ newest = max(numeric, key=int)
131
+ cc_flag += ["-gencode", f"arch=compute_{newest},code=compute_{newest}"]
132
+
133
+ return cc_flag
134
+
135
+
136
+ def check_if_cuda_home_none(global_option: str) -> None:
137
+ if CUDA_HOME is not None:
138
+ return
139
+ # warn instead of error because user could be downloading prebuilt wheels, so nvcc won't be necessary
140
+ # in that case.
141
+ warnings.warn(
142
+ f"{global_option} was requested, but nvcc was not found. Are you sure your environment has nvcc available? "
143
+ "If you're installing within a container from https://hub.docker.com/r/pytorch/pytorch, "
144
+ "only images whose names contain 'devel' will provide nvcc."
145
+ )
146
+
147
+
148
+ def append_nvcc_threads(nvcc_extra_args):
149
+ nvcc_threads = os.getenv("NVCC_THREADS") or "4"
150
+ return nvcc_extra_args + ["--threads", nvcc_threads]
151
+
152
+
153
+ cmdclass = {}
154
+ ext_modules = []
155
+
156
+ # We want this even if SKIP_CUDA_BUILD because when we run python setup.py sdist we want the .hpp
157
+ # files included in the source distribution, in case the user compiles from source.
158
+ subprocess.run(["git", "submodule", "update", "--init", "csrc/cutlass"])
159
+
160
+ if not SKIP_CUDA_BUILD:
161
+ print("\n\ntorch.__version__ = {}\n\n".format(torch.__version__))
162
+ TORCH_MAJOR = int(torch.__version__.split(".")[0])
163
+ TORCH_MINOR = int(torch.__version__.split(".")[1])
164
+
165
+ check_if_cuda_home_none("block_sparse_attn")
166
+ # Check, if CUDA11 is installed for compute capability 8.0
167
+ cc_flag = []
168
+ if CUDA_HOME is not None:
169
+ _, bare_metal_version = get_cuda_bare_metal_version(CUDA_HOME)
170
+ if bare_metal_version < Version("11.7"):
171
+ raise RuntimeError(
172
+ "Block Sparse Attention is only supported on CUDA 11.7 and above. "
173
+ "Note: make sure nvcc has a supported version by running nvcc -V."
174
+ )
175
+ # Build -gencode (regular + PTX + family-specific 'f' when available)
176
+ add_cuda_gencodes(cc_flag, set(cuda_archs()), bare_metal_version)
177
+ else:
178
+ # No nvcc present; warnings already emitted above
179
+ pass
180
+
181
+ # HACK: The compiler flag -D_GLIBCXX_USE_CXX11_ABI is set to be the same as
182
+ # torch._C._GLIBCXX_USE_CXX11_ABI
183
+ # https://github.com/pytorch/pytorch/blob/8472c24e3b5b60150096486616d98b7bea01500b/torch/utils/cpp_extension.py#L920
184
+ if FORCE_CXX11_ABI:
185
+ torch._C._GLIBCXX_USE_CXX11_ABI = True
186
+
187
+ nvcc_flags = [
188
+ "-O3",
189
+ "-std=c++17",
190
+ "-U__CUDA_NO_HALF_OPERATORS__",
191
+ "-U__CUDA_NO_HALF_CONVERSIONS__",
192
+ "-U__CUDA_NO_HALF2_OPERATORS__",
193
+ "-U__CUDA_NO_BFLOAT16_CONVERSIONS__",
194
+ "--expt-relaxed-constexpr",
195
+ "--expt-extended-lambda",
196
+ "--use_fast_math",
197
+ # "--ptxas-options=-v",
198
+ # "--ptxas-options=-O2",
199
+ # "-lineinfo",
200
+ # "-DFLASHATTENTION_DISABLE_BACKWARD",
201
+ # "-DFLASHATTENTION_DISABLE_DROPOUT",
202
+ # "-DFLASHATTENTION_DISABLE_ALIBI",
203
+ # "-DFLASHATTENTION_DISABLE_SOFTCAP",
204
+ # "-DFLASHATTENTION_DISABLE_UNEVEN_K",
205
+ # "-DFLASHATTENTION_DISABLE_LOCAL",
206
+ ]
207
+
208
+ compiler_c17_flag=["-O3", "-std=c++17"]
209
+ # Add Windows-specific flags
210
+ if sys.platform == "win32" and os.getenv('DISTUTILS_USE_SDK') == '1':
211
+ nvcc_flags.extend(["-Xcompiler", "/Zc:__cplusplus"])
212
+ compiler_c17_flag=["-O2", "/std:c++17", "/Zc:__cplusplus"]
213
+
214
+ ext_modules.append(
215
+ CUDAExtension(
216
+ name="block_sparse_attn_cuda",
217
+ sources=[
218
+ "csrc/block_sparse_attn/flash_api.cpp",
219
+ # add by JXGuo
220
+ "csrc/block_sparse_attn/src/flash_fwd_block_hdim32_fp16_sm80.cu",
221
+ "csrc/block_sparse_attn/src/flash_fwd_block_hdim32_fp16_causal_sm80.cu",
222
+ "csrc/block_sparse_attn/src/flash_fwd_block_hdim32_bf16_sm80.cu",
223
+ "csrc/block_sparse_attn/src/flash_fwd_block_hdim32_bf16_causal_sm80.cu",
224
+ "csrc/block_sparse_attn/src/flash_fwd_block_hdim64_fp16_sm80.cu",
225
+ "csrc/block_sparse_attn/src/flash_fwd_block_hdim64_fp16_causal_sm80.cu",
226
+ "csrc/block_sparse_attn/src/flash_fwd_block_hdim64_bf16_sm80.cu",
227
+ "csrc/block_sparse_attn/src/flash_fwd_block_hdim64_bf16_causal_sm80.cu",
228
+ "csrc/block_sparse_attn/src/flash_fwd_block_hdim128_fp16_sm80.cu",
229
+ "csrc/block_sparse_attn/src/flash_fwd_block_hdim128_fp16_causal_sm80.cu",
230
+ "csrc/block_sparse_attn/src/flash_fwd_block_hdim128_bf16_sm80.cu",
231
+ "csrc/block_sparse_attn/src/flash_fwd_block_hdim128_bf16_causal_sm80.cu",
232
+
233
+ "csrc/block_sparse_attn/src/flash_bwd_block_hdim32_fp16_sm80.cu",
234
+ "csrc/block_sparse_attn/src/flash_bwd_block_hdim32_fp16_causal_sm80.cu",
235
+ "csrc/block_sparse_attn/src/flash_bwd_block_hdim32_bf16_sm80.cu",
236
+ "csrc/block_sparse_attn/src/flash_bwd_block_hdim32_bf16_causal_sm80.cu",
237
+ "csrc/block_sparse_attn/src/flash_bwd_block_hdim64_fp16_sm80.cu",
238
+ "csrc/block_sparse_attn/src/flash_bwd_block_hdim64_fp16_causal_sm80.cu",
239
+ "csrc/block_sparse_attn/src/flash_bwd_block_hdim64_bf16_sm80.cu",
240
+ "csrc/block_sparse_attn/src/flash_bwd_block_hdim64_bf16_causal_sm80.cu",
241
+ "csrc/block_sparse_attn/src/flash_bwd_block_hdim128_fp16_sm80.cu",
242
+ "csrc/block_sparse_attn/src/flash_bwd_block_hdim128_fp16_causal_sm80.cu",
243
+ "csrc/block_sparse_attn/src/flash_bwd_block_hdim128_bf16_sm80.cu",
244
+ "csrc/block_sparse_attn/src/flash_bwd_block_hdim128_bf16_causal_sm80.cu",
245
+ ],
246
+ extra_compile_args={
247
+ "cxx": compiler_c17_flag,
248
+ "nvcc": append_nvcc_threads(nvcc_flags + cc_flag),
249
+ },
250
+ include_dirs=[
251
+ Path(this_dir) / "csrc" / "block_sparse_attn",
252
+ Path(this_dir) / "csrc" / "block_sparse_attn" / "src",
253
+ Path(this_dir) / "csrc" / "cutlass" / "include",
254
+ ],
255
+ )
256
+ )
257
+
258
+
259
+ def get_package_version():
260
+ with open(Path(this_dir) / "block_sparse_attn" / "__init__.py", "r") as f:
261
+ version_match = re.search(r"^__version__\s*=\s*(.*)$", f.read(), re.MULTILINE)
262
+ public_version = ast.literal_eval(version_match.group(1))
263
+ local_version = os.environ.get("FLASH_ATTN_LOCAL_VERSION")
264
+ if local_version:
265
+ return f"{public_version}+{local_version}"
266
+ else:
267
+ return str(public_version)
268
+
269
+
270
+ def get_wheel_url():
271
+ torch_version_raw = parse(torch.__version__)
272
+ python_version = f"cp{sys.version_info.major}{sys.version_info.minor}"
273
+ platform_name = get_platform()
274
+ flash_version = get_package_version()
275
+ torch_version = f"{torch_version_raw.major}.{torch_version_raw.minor}"
276
+ cxx11_abi = str(torch._C._GLIBCXX_USE_CXX11_ABI).upper()
277
+
278
+ # Determine the version numbers that will be used to determine the correct wheel
279
+ # We're using the CUDA version used to build torch, not the one currently installed
280
+ # _, cuda_version_raw = get_cuda_bare_metal_version(CUDA_HOME)
281
+ torch_cuda_version = parse(torch.version.cuda)
282
+ # For CUDA 11, we only compile for CUDA 11.8, and for CUDA 12 we only compile for CUDA 12.3
283
+ # to save CI time. Minor versions should be compatible.
284
+ torch_cuda_version = parse("11.8") if torch_cuda_version.major == 11 else parse("12.3")
285
+ # cuda_version = f"{cuda_version_raw.major}{cuda_version_raw.minor}"
286
+ cuda_version = f"{torch_cuda_version.major}"
287
+
288
+ # Determine wheel URL based on CUDA version, torch version, python version and OS
289
+ wheel_filename = f"{PACKAGE_NAME}-{flash_version}+cu{cuda_version}torch{torch_version}cxx11abi{cxx11_abi}-{python_version}-{python_version}-{platform_name}.whl"
290
+
291
+ wheel_url = BASE_WHEEL_URL.format(tag_name=f"v{flash_version}", wheel_name=wheel_filename)
292
+
293
+ return wheel_url, wheel_filename
294
+
295
+
296
+ class CachedWheelsCommand(_bdist_wheel):
297
+ """
298
+ The CachedWheelsCommand plugs into the default bdist wheel, which is ran by pip when it cannot
299
+ find an existing wheel (which is currently the case for all flash attention installs). We use
300
+ the environment parameters to detect whether there is already a pre-built version of a compatible
301
+ wheel available and short-circuits the standard full build pipeline.
302
+ """
303
+
304
+ def run(self):
305
+ if FORCE_BUILD:
306
+ return super().run()
307
+
308
+ wheel_url, wheel_filename = get_wheel_url()
309
+ print("Guessing wheel URL: ", wheel_url)
310
+ try:
311
+ urllib.request.urlretrieve(wheel_url, wheel_filename)
312
+
313
+ # Make the archive
314
+ # Lifted from the root wheel processing command
315
+ # https://github.com/pypa/wheel/blob/cf71108ff9f6ffc36978069acb28824b44ae028e/src/wheel/bdist_wheel.py#LL381C9-L381C85
316
+ if not os.path.exists(self.dist_dir):
317
+ os.makedirs(self.dist_dir)
318
+
319
+ impl_tag, abi_tag, plat_tag = self.get_tag()
320
+ archive_basename = f"{self.wheel_dist_name}-{impl_tag}-{abi_tag}-{plat_tag}"
321
+
322
+ wheel_path = os.path.join(self.dist_dir, archive_basename + ".whl")
323
+ print("Raw wheel path", wheel_path)
324
+ os.rename(wheel_filename, wheel_path)
325
+ except (urllib.error.HTTPError, urllib.error.URLError):
326
+ print("Precompiled wheel not found. Building from source...")
327
+ # If the wheel could not be downloaded, build from source
328
+ super().run()
329
+
330
+
331
+ class NinjaBuildExtension(BuildExtension):
332
+ def __init__(self, *args, **kwargs) -> None:
333
+ # do not override env MAX_JOBS if already exists
334
+ if not os.environ.get("MAX_JOBS"):
335
+ import psutil
336
+
337
+ # calculate the maximum allowed NUM_JOBS based on cores
338
+ max_num_jobs_cores = max(1, os.cpu_count() // 2)
339
+
340
+ # calculate the maximum allowed NUM_JOBS based on free memory
341
+ free_memory_gb = psutil.virtual_memory().available / (1024 ** 3) # free memory in GB
342
+ max_num_jobs_memory = int(free_memory_gb / 9) # each JOB peak memory cost is ~8-9GB when threads = 4
343
+
344
+ # pick lower value of jobs based on cores vs memory metric to minimize oom and swap usage during compilation
345
+ max_jobs = max(1, min(max_num_jobs_cores, max_num_jobs_memory))
346
+ os.environ["MAX_JOBS"] = str(max_jobs)
347
+
348
+ super().__init__(*args, **kwargs)
349
+
350
+
351
+ setup(
352
+ name=PACKAGE_NAME,
353
+ version=get_package_version(),
354
+ packages=find_packages(
355
+ exclude=(
356
+ "build",
357
+ "csrc",
358
+ "include",
359
+ "tests",
360
+ "dist",
361
+ "docs",
362
+ "benchmarks",
363
+ "block_sparse_attn.egg-info",
364
+ )
365
+ ),
366
+ author="Junxian Guo",
367
+ author_email="junxian@mit.edu",
368
+ description="Block Sparse Attention",
369
+ long_description=long_description,
370
+ long_description_content_type="text/markdown",
371
+ url="https://github.com/mit-han-lab/Block-Sparse-Attention",
372
+ classifiers=[
373
+ "Programming Language :: Python :: 3",
374
+ "License :: OSI Approved :: BSD License",
375
+ "Operating System :: Unix",
376
+ ],
377
+ ext_modules=ext_modules,
378
+ cmdclass={"bdist_wheel": CachedWheelsCommand, "build_ext": NinjaBuildExtension}
379
+ if ext_modules
380
+ else {
381
+ "bdist_wheel": CachedWheelsCommand,
382
+ },
383
+ python_requires=">=3.9",
384
+ install_requires=[
385
+ "torch",
386
+ "einops",
387
+ ],
388
+ setup_requires=[
389
+ "packaging",
390
+ "psutil",
391
+ "ninja",
392
+ ],
393
+ )
wheel/python3.12/torch2.9.0/block_sparse_attn-0.0.2-cp312-cp312-linux_x86_64.whl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:81048058bd9be7252410fa521cddffc980ca26544313ef792e0f5e83c4deea51
3
+ size 54884254