setup.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import io
  2. import os
  3. import re
  4. import subprocess
  5. from typing import List, Set
  6. from packaging.version import parse, Version
  7. import setuptools
  8. import torch
  9. from torch.utils.cpp_extension import BuildExtension, CUDAExtension, CUDA_HOME
  10. ROOT_DIR = os.path.dirname(__file__)
  11. # Compiler flags.
  12. CXX_FLAGS = ["-g", "-O2", "-std=c++17"]
  13. # TODO(woosuk): Should we use -O3?
  14. NVCC_FLAGS = ["-O2", "-std=c++17"]
  15. ABI = 1 if torch._C._GLIBCXX_USE_CXX11_ABI else 0
  16. CXX_FLAGS += [f"-D_GLIBCXX_USE_CXX11_ABI={ABI}"]
  17. NVCC_FLAGS += [f"-D_GLIBCXX_USE_CXX11_ABI={ABI}"]
  18. if CUDA_HOME is None:
  19. raise RuntimeError(
  20. f"Cannot find CUDA_HOME. CUDA must be available in order to build the package.")
  21. def get_nvcc_cuda_version(cuda_dir: str) -> Version:
  22. """Get the CUDA version from nvcc.
  23. Adapted from https://github.com/NVIDIA/apex/blob/8b7a1ff183741dd8f9b87e7bafd04cfde99cea28/setup.py
  24. """
  25. nvcc_output = subprocess.check_output([cuda_dir + "/bin/nvcc", "-V"],
  26. universal_newlines=True)
  27. output = nvcc_output.split()
  28. release_idx = output.index("release") + 1
  29. nvcc_cuda_version = parse(output[release_idx].split(",")[0])
  30. return nvcc_cuda_version
  31. # Collect the compute capabilities of all available GPUs.
  32. device_count = torch.cuda.device_count()
  33. compute_capabilities: Set[int] = set()
  34. for i in range(device_count):
  35. major, minor = torch.cuda.get_device_capability(i)
  36. if major < 7:
  37. raise RuntimeError(
  38. "GPUs with compute capability less than 7.0 are not supported.")
  39. compute_capabilities.add(major * 10 + minor)
  40. # If no GPU is available, add all supported compute capabilities.
  41. if not compute_capabilities:
  42. compute_capabilities = {70, 75, 80, 86, 90}
  43. # Add target compute capabilities to NVCC flags.
  44. for capability in compute_capabilities:
  45. NVCC_FLAGS += ["-gencode", f"arch=compute_{capability},code=sm_{capability}"]
  46. # Validate the NVCC CUDA version.
  47. nvcc_cuda_version = get_nvcc_cuda_version(CUDA_HOME)
  48. if nvcc_cuda_version < Version("11.0"):
  49. raise RuntimeError("CUDA 11.0 or higher is required to build the package.")
  50. if 86 in compute_capabilities and nvcc_cuda_version < Version("11.1"):
  51. raise RuntimeError(
  52. "CUDA 11.1 or higher is required for GPUs with compute capability 8.6.")
  53. if 90 in compute_capabilities and nvcc_cuda_version < Version("11.8"):
  54. raise RuntimeError(
  55. "CUDA 11.8 or higher is required for GPUs with compute capability 9.0.")
  56. # Use NVCC threads to parallelize the build.
  57. if nvcc_cuda_version >= Version("11.2"):
  58. num_threads = min(os.cpu_count(), 8)
  59. NVCC_FLAGS += ["--threads", str(num_threads)]
  60. ext_modules = []
  61. # Cache operations
  62. cache_extension = CUDAExtension(
  63. name="aphrodite.cache_ops",
  64. sources=["kernels/cache.cpp", "kernels/cache_kernels.cu"],
  65. extra_compile_args={"cxx": CXX_FLAGS, "nvcc": NVCC_FLAGS},
  66. )
  67. ext_modules.append(cache_extension)
  68. # Attention operations
  69. attention_extension = CUDAExtension(
  70. name="aphrodite.attention_ops",
  71. sources=["kernels/attention.cpp", "kernels/attention/attention_kernels.cu"],
  72. extra_compile_args={"cxx": CXX_FLAGS, "nvcc": NVCC_FLAGS},
  73. )
  74. ext_modules.append(attention_extension)
  75. # Positional encoding operations
  76. positional_encoding_extension = CUDAExtension(
  77. name="aphrodite.pos_encoding_ops",
  78. sources=["kernels/pos_encoding.cpp", "kernels/pos_encoding_kernels.cu"],
  79. extra_compile_args={"cxx": CXX_FLAGS, "nvcc": NVCC_FLAGS},
  80. )
  81. ext_modules.append(positional_encoding_extension)
  82. # Layer normalization operations
  83. layernorm_extension = CUDAExtension(
  84. name="aphrodite.layernorm_ops",
  85. sources=["kernels/layernorm.cpp", "kernels/layernorm_kernels.cu"],
  86. extra_compile_args={"cxx": CXX_FLAGS, "nvcc": NVCC_FLAGS},
  87. )
  88. ext_modules.append(layernorm_extension)
  89. # Activation operations
  90. activation_extension = CUDAExtension(
  91. name="aphrodite.activation_ops",
  92. sources=["kernels/activation.cpp", "kernels/activation_kernels.cu"],
  93. extra_compile_args={"cxx": CXX_FLAGS, "nvcc": NVCC_FLAGS},
  94. )
  95. ext_modules.append(activation_extension)
  96. def get_path(*filepath) -> str:
  97. return os.path.join(ROOT_DIR, *filepath)
  98. def find_version(filepath: str):
  99. """Extract version information from the given filepath.
  100. Adapted from https://github.com/ray-project/ray/blob/0b190ee1160eeca9796bc091e07eaebf4c85b511/python/setup.py
  101. """
  102. with open(filepath) as fp:
  103. version_match = re.search(
  104. r"^__version__ = ['\"]([^'\"]*)['\"]", fp.read(), re.M)
  105. if version_match:
  106. return version_match.group(1)
  107. raise RuntimeError("Unable to find version string.")
  108. def read_readme() -> str:
  109. """Read the README file."""
  110. return io.open(get_path("README.md"), "r", encoding="utf-8").read()
  111. def get_requirements() -> List[str]:
  112. """Get Python package dependencies from requirements.txt."""
  113. with open(get_path("requirements.txt")) as f:
  114. requirements = f.read().strip().split("\n")
  115. return requirements
  116. setuptools.setup(
  117. name="aphrodite-engine",
  118. version=find_version(get_path("aphrodite", "__init__.py")),
  119. author="PygmalionAI",
  120. license="AGPL 3.0",
  121. description="The inference engine for PygmalionAI models",
  122. long_description=read_readme(),
  123. long_description_content_type="text/markdown",
  124. url="https://github.com/PygmalionAI/aphrodite-engine",
  125. project_urls={
  126. "Homepage": "https://pygmalion.chat",
  127. "Documentation": "https://docs.pygmalion.chat",
  128. "GitHub": "https://github.com/PygmalionAI",
  129. "Huggingface": "https://huggingface.co/PygmalionAI",
  130. },
  131. classifiers=[
  132. "Programming Language :: Python :: 3.8",
  133. "Programming Language :: Python :: 3.9",
  134. "Programming Language :: Python :: 3.10",
  135. "License :: OSI Approved :: GNU Affero General Public License",
  136. "Topic :: Scientific/Engineering :: Artificial Intelligence",
  137. ],
  138. packages=setuptools.find_packages(
  139. exclude=("assets", "kernels","examples")),
  140. python_requires=">=3.8",
  141. install_requires=get_requirements(),
  142. ext_modules=ext_modules,
  143. cmdclass={"build_ext": BuildExtension},
  144. )