bowmanchow commited on
Commit
07cd515
·
1 Parent(s): c61b458

install pre-built flash atten

Browse files
Files changed (1) hide show
  1. app.py +66 -5
app.py CHANGED
@@ -2,12 +2,73 @@ import os
2
  import subprocess
3
  import sys
4
 
5
- # Install flash-attn before other imports
6
- subprocess.run(
7
- [sys.executable, "-m", "pip", "install", "flash-attn", "--no-build-isolation"],
8
- check=True,
9
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
 
11
 
12
  os.environ["CUDA_VISIBLE_DEVICES"] = "0"
13
  import copy
 
2
  import subprocess
3
  import sys
4
 
5
+ def install_flash_attn():
6
+ """Auto-detect CUDA, PyTorch, Python versions and install the matching pre-built flash-attn wheel."""
7
+ import torch
8
+
9
+ # Python version (e.g., "cp310")
10
+ py_major = sys.version_info.major
11
+ py_minor = sys.version_info.minor
12
+ cp_tag = f"cp{py_major}{py_minor}"
13
+
14
+ # PyTorch version (e.g., "2.4" from "2.4.0")
15
+ torch_version = torch.__version__.split("+")[0] # strip +cu121 if present
16
+ torch_major_minor = ".".join(torch_version.split(".")[:2]) # "2.4"
17
+
18
+ # CUDA version (e.g., "cu124" from "12.4")
19
+ cuda_version = torch.version.cuda
20
+ if cuda_version is None:
21
+ print("No CUDA detected, skipping flash-attn installation.")
22
+ return
23
+ cuda_major_minor = cuda_version.replace(".", "") # "124"
24
+ # flash-attn wheels use shortened CUDA tags like "cu12" (just major) or "cu121", "cu124"
25
+ # Check available tags: most wheels use "cu12" for any 12.x
26
+ cuda_tag_short = f"cu{cuda_version.split('.')[0]}" # "cu12"
27
+
28
+ # CXX11 ABI
29
+ cxx11_abi = torch._C._GLIBCXX_USE_CXX11_ABI
30
+ abi_tag = "cxx11abiTRUE" if cxx11_abi else "cxx11abiFALSE"
31
+
32
+ # flash-attn version to install
33
+ flash_attn_version = "2.7.4.post1"
34
+
35
+ # Construct the wheel filename
36
+ # Example: flash_attn-2.7.4.post1+cu12torch2.4cxx11abiFALSE-cp310-cp310-linux_x86_64.whl
37
+ wheel_name = (
38
+ f"flash_attn-{flash_attn_version}+"
39
+ f"{cuda_tag_short}torch{torch_major_minor}{abi_tag}-"
40
+ f"{cp_tag}-{cp_tag}-linux_x86_64.whl"
41
+ )
42
+
43
+ base_url = f"https://github.com/Dao-AILab/flash-attention/releases/download/v{flash_attn_version}"
44
+ wheel_url = f"{base_url}/{wheel_name}"
45
+
46
+ print(f"Detected environment:")
47
+ print(f" Python: {py_major}.{py_minor} ({cp_tag})")
48
+ print(f" PyTorch: {torch_version} (torch{torch_major_minor})")
49
+ print(f" CUDA: {cuda_version} ({cuda_tag_short})")
50
+ print(f" CXX11 ABI: {cxx11_abi} ({abi_tag})")
51
+ print(f" Wheel URL: {wheel_url}")
52
+
53
+ result = subprocess.run(
54
+ [sys.executable, "-m", "pip", "install", wheel_url],
55
+ capture_output=True,
56
+ text=True,
57
+ )
58
+
59
+ if result.returncode != 0:
60
+ print(f"Pre-built wheel failed:\n{result.stderr}")
61
+ print("Falling back to building flash-attn from source (this may take a while)...")
62
+ subprocess.run(
63
+ [sys.executable, "-m", "pip", "install", "flash-attn", "--no-build-isolation"],
64
+ check=True,
65
+ )
66
+ else:
67
+ print("flash-attn installed successfully from pre-built wheel.")
68
+ print(result.stdout)
69
+
70
 
71
+ install_flash_attn()
72
 
73
  os.environ["CUDA_VISIBLE_DEVICES"] = "0"
74
  import copy