|
| 1 | +"""This script is used to install flash-attn from a pre-built wheel hosted on an OSS bucket. |
| 2 | +Useful for mainland China users who have difficulty installing flash-attn from PyPI due to network issues. |
| 3 | +""" |
| 4 | +import os |
| 5 | +import platform |
| 6 | +import subprocess |
| 7 | +import sys |
| 8 | +import tempfile |
| 9 | + |
| 10 | +import torch |
| 11 | +import typer |
| 12 | + |
| 13 | +app = typer.Typer() |
| 14 | +FLASH_VERSION = "2.8.1" |
| 15 | + |
| 16 | + |
| 17 | +def check_flash_attn_installed(): |
| 18 | + try: |
| 19 | + import flash_attn |
| 20 | + |
| 21 | + print(f"flash_attn version: {flash_attn.__version__}") |
| 22 | + return True |
| 23 | + except ImportError: |
| 24 | + return False |
| 25 | + |
| 26 | + |
| 27 | +def install_flash_attn(uv: bool = False, keep_wheel: bool = False): |
| 28 | + # Get torch version |
| 29 | + TORCH_VERSION_RAW = torch.__version__ |
| 30 | + torch_major, torch_minor = TORCH_VERSION_RAW.split(".")[:2] |
| 31 | + torch_version = f"{torch_major}.{torch_minor}" |
| 32 | + |
| 33 | + # Get python version |
| 34 | + python_version = f"cp{sys.version_info.major}{sys.version_info.minor}" |
| 35 | + |
| 36 | + # Get platform name |
| 37 | + platform_name = platform.system().lower() + "_" + platform.machine() |
| 38 | + |
| 39 | + # Get cxx11_abi |
| 40 | + cxx11_abi = str(torch._C._GLIBCXX_USE_CXX11_ABI).upper() |
| 41 | + |
| 42 | + # Is ROCM |
| 43 | + # torch.version.hip/cuda are runtime attributes not in type stubs |
| 44 | + IS_ROCM = hasattr(torch.version, "hip") and torch.version.hip is not None # type: ignore[attr-defined] |
| 45 | + |
| 46 | + if IS_ROCM: |
| 47 | + print("We currently do not host ROCm wheels for flash-attn.") |
| 48 | + sys.exit(1) |
| 49 | + else: |
| 50 | + torch_cuda_version = torch.version.cuda # type: ignore[attr-defined] |
| 51 | + cuda_major = torch_cuda_version.split(".")[0] if torch_cuda_version else None |
| 52 | + if cuda_major != "12": |
| 53 | + print("Only CUDA 12 wheels are hosted for flash-attn.") |
| 54 | + sys.exit(1) |
| 55 | + cuda_version = "12" |
| 56 | + wheel_filename = ( |
| 57 | + f"flash_attn-{FLASH_VERSION}%2Bcu{cuda_version}torch{torch_version}" |
| 58 | + f"cxx11abi{cxx11_abi}-{python_version}-{python_version}-{platform_name}.whl" |
| 59 | + ) |
| 60 | + local_filename = ( |
| 61 | + f"flash_attn-{FLASH_VERSION}-{python_version}-{python_version}-{platform_name}.whl" |
| 62 | + ) |
| 63 | + |
| 64 | + wheel_url = ( |
| 65 | + "https://dail-wlcb.oss-cn-wulanchabu.aliyuncs.com" |
| 66 | + f"/AgentScope/download/flash-attn/{FLASH_VERSION}/{wheel_filename}" |
| 67 | + ) |
| 68 | + |
| 69 | + print(f"wheel_url: {wheel_url}") |
| 70 | + print(f"target_local_file: {local_filename}") |
| 71 | + |
| 72 | + def _install_helper(local_path: str): |
| 73 | + subprocess.run(["wget", wheel_url, "-O", local_path], check=True) |
| 74 | + install_cmd = ( |
| 75 | + ["uv", "pip", "install", local_path] |
| 76 | + if uv |
| 77 | + else [sys.executable, "-m", "pip", "install", local_path] |
| 78 | + ) |
| 79 | + subprocess.run(install_cmd, check=True) |
| 80 | + |
| 81 | + if keep_wheel: |
| 82 | + local_path = os.path.abspath(local_filename) |
| 83 | + _install_helper(local_path) |
| 84 | + else: |
| 85 | + with tempfile.TemporaryDirectory() as tempdir: |
| 86 | + local_path = os.path.join(tempdir, local_filename) |
| 87 | + _install_helper(local_path) |
| 88 | + |
| 89 | + # Try to import flash_attn |
| 90 | + if not check_flash_attn_installed(): |
| 91 | + print("Failed to install flash_attn.") |
| 92 | + sys.exit(1) |
| 93 | + |
| 94 | + |
| 95 | +@app.command() |
| 96 | +def main( |
| 97 | + uv: bool = typer.Option(False, help="Use uv pip to install instead of pip"), |
| 98 | + keep_wheel: bool = typer.Option( |
| 99 | + False, help="Keep the downloaded wheel file in current directory" |
| 100 | + ), |
| 101 | +): |
| 102 | + """Install flash-attn from a pre-built wheel.""" |
| 103 | + if check_flash_attn_installed(): |
| 104 | + print("flash_attn is already installed. Skipping installation.") |
| 105 | + return |
| 106 | + install_flash_attn(uv=uv, keep_wheel=keep_wheel) |
| 107 | + |
| 108 | + |
| 109 | +if __name__ == "__main__": |
| 110 | + typer.run(main) |
0 commit comments