|
| 1 | +# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +# Adapted from https://github.com/NVIDIA/apex/blob/master/setup.py |
| 16 | +import ast |
| 17 | +import os |
| 18 | +import re |
| 19 | +import subprocess |
| 20 | +import sys |
| 21 | +from pathlib import Path |
| 22 | + |
| 23 | +from env_dict import env_dict |
| 24 | +from setuptools import setup |
| 25 | +from wheel.bdist_wheel import bdist_wheel as _bdist_wheel |
| 26 | + |
| 27 | + |
| 28 | +with open("../../README.md", "r", encoding="utf-8") as fh: |
| 29 | + long_description = fh.read() |
| 30 | + |
| 31 | + |
| 32 | +cur_dir = os.path.dirname(os.path.abspath(__file__)) |
| 33 | +PACKAGE_NAME = "paddle-flash-attn" |
| 34 | + |
| 35 | + |
| 36 | +def get_platform(): |
| 37 | + """ |
| 38 | + Returns the platform name as used in wheel filenames. |
| 39 | + """ |
| 40 | + if sys.platform.startswith('linux'): |
| 41 | + return 'linux_x86_64' |
| 42 | + elif sys.platform == 'win32': |
| 43 | + return 'win_amd64' |
| 44 | + else: |
| 45 | + raise ValueError(f'Unsupported platform: {sys.platform}') |
| 46 | + |
| 47 | + |
| 48 | +def get_cuda_version(): |
| 49 | + try: |
| 50 | + result = subprocess.run( |
| 51 | + ['nvcc', '--version'], |
| 52 | + stdout=subprocess.PIPE, |
| 53 | + stderr=subprocess.PIPE, |
| 54 | + text=True, |
| 55 | + ) |
| 56 | + if result.returncode == 0: |
| 57 | + output_lines = result.stdout.split('\n') |
| 58 | + for line in output_lines: |
| 59 | + if line.startswith('Cuda compilation tools'): |
| 60 | + cuda_version = ( |
| 61 | + line.split('release')[1].strip().split(',')[0] |
| 62 | + ) |
| 63 | + return cuda_version |
| 64 | + else: |
| 65 | + print("Error:", result.stderr) |
| 66 | + |
| 67 | + except Exception as e: |
| 68 | + print("Error:", str(e)) |
| 69 | + |
| 70 | + return None |
| 71 | + |
| 72 | + |
| 73 | +def get_package_version(): |
| 74 | + with open(Path(cur_dir) / "../flash_attn" / "__init__.py", "r") as f: |
| 75 | + version_match = re.search( |
| 76 | + r"^__version__\s*=\s*(.*)$", f.read(), re.MULTILINE |
| 77 | + ) |
| 78 | + public_version = ast.literal_eval(version_match.group(1)) |
| 79 | + local_version = os.environ.get("FLASH_ATTN_LOCAL_VERSION") |
| 80 | + if local_version: |
| 81 | + return f"{public_version}+{local_version}" |
| 82 | + else: |
| 83 | + return str(public_version) |
| 84 | + |
| 85 | + |
| 86 | +def get_package_data(): |
| 87 | + binary_dir = env_dict.get("CMAKE_BINARY_DIR") |
| 88 | + lib = os.path.join( |
| 89 | + os.path.abspath(os.path.dirname(__file__)), |
| 90 | + binary_dir + '/paddle_flash_attn/*', |
| 91 | + ) |
| 92 | + package_data = {'paddle_flash_attn': [lib]} |
| 93 | + return package_data |
| 94 | + |
| 95 | + |
| 96 | +class CustomWheelsCommand(_bdist_wheel): |
| 97 | + """ |
| 98 | + The CachedWheelsCommand plugs into the default bdist wheel, which is ran by pip when it cannot |
| 99 | + find an existing wheel (which is currently the case for all flash attention installs). We use |
| 100 | + the environment parameters to detect whether there is already a pre-built version of a compatible |
| 101 | + wheel available and short-circuits the standard full build pipeline. |
| 102 | + """ |
| 103 | + |
| 104 | + def run(self): |
| 105 | + self.run_command('build_ext') |
| 106 | + super().run() |
| 107 | + cuda_version = get_cuda_version() |
| 108 | + python_version = f"cp{sys.version_info.major}{sys.version_info.minor}" |
| 109 | + platform_name = get_platform() |
| 110 | + flash_version = get_package_version() |
| 111 | + wheel_name = 'paddle_flash_attn' |
| 112 | + |
| 113 | + # Determine wheel URL based on CUDA version, python version and OS |
| 114 | + impl_tag, abi_tag, plat_tag = self.get_tag() |
| 115 | + archive_basename = ( |
| 116 | + f"{self.wheel_dist_name}-{impl_tag}-{abi_tag}-{plat_tag}" |
| 117 | + ) |
| 118 | + wheel_path = os.path.join(self.dist_dir, archive_basename + ".whl") |
| 119 | + print("Raw wheel path", wheel_path) |
| 120 | + wheel_filename = f'{wheel_name}-{flash_version}+cu{cuda_version}-{impl_tag}-{abi_tag}-{platform_name}.whl' |
| 121 | + os.rename(wheel_path, os.path.join(self.dist_dir, wheel_filename)) |
| 122 | + |
| 123 | + |
| 124 | +setup( |
| 125 | + name=PACKAGE_NAME, |
| 126 | + version=get_package_version(), |
| 127 | + packages=['paddle_flash_attn'], |
| 128 | + package_data=get_package_data(), |
| 129 | + |
| 130 | + description="Flash Attention: Fast and Memory-Efficient Exact Attention", |
| 131 | + long_description=long_description, |
| 132 | + long_description_content_type="text/markdown", |
| 133 | + url="https://github.com/PaddlePaddle/flash-attention", |
| 134 | + classifiers=[ |
| 135 | + "Programming Language :: Python :: 3", |
| 136 | + "License :: OSI Approved :: BSD License", |
| 137 | + "Operating System :: Unix", |
| 138 | + ], |
| 139 | + cmdclass={ |
| 140 | + 'bdist_wheel': CustomWheelsCommand, |
| 141 | + }, |
| 142 | + python_requires=">=3.7", |
| 143 | +) |
0 commit comments