setup.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. # Adapted from https://github.com/NVIDIA/apex/blob/master/setup.py
  2. import sys
  3. import warnings
  4. import os
  5. from packaging.version import parse, Version
  6. from setuptools import setup, find_packages
  7. import subprocess
  8. import torch
  9. from torch.utils.cpp_extension import BuildExtension, CppExtension, CUDAExtension, CUDA_HOME
  10. # ninja build does not work unless include_dirs are abs path
  11. this_dir = os.path.dirname(os.path.abspath(__file__))
  12. def get_cuda_bare_metal_version(cuda_dir):
  13. raw_output = subprocess.check_output([cuda_dir + "/bin/nvcc", "-V"], universal_newlines=True)
  14. output = raw_output.split()
  15. release_idx = output.index("release") + 1
  16. bare_metal_version = parse(output[release_idx].split(",")[0])
  17. return raw_output, bare_metal_version
  18. def check_cuda_torch_binary_vs_bare_metal(cuda_dir):
  19. raw_output, bare_metal_version = get_cuda_bare_metal_version(cuda_dir)
  20. torch_binary_version = parse(torch.version.cuda)
  21. print("\nCompiling cuda extensions with")
  22. print(raw_output + "from " + cuda_dir + "/bin\n")
  23. if (bare_metal_version != torch_binary_version):
  24. raise RuntimeError(
  25. "Cuda extensions are being compiled with a version of Cuda that does "
  26. "not match the version used to compile Pytorch binaries. "
  27. "Pytorch binaries were compiled with Cuda {}.\n".format(torch.version.cuda)
  28. + "In some cases, a minor-version mismatch will not cause later errors: "
  29. "https://github.com/NVIDIA/apex/pull/323#discussion_r287021798. "
  30. "You can try commenting out this check (at your own risk)."
  31. )
  32. def raise_if_cuda_home_none(global_option: str) -> None:
  33. if CUDA_HOME is not None:
  34. return
  35. raise RuntimeError(
  36. f"{global_option} was requested, but nvcc was not found. Are you sure your environment has nvcc available? "
  37. "If you're installing within a container from https://hub.docker.com/r/pytorch/pytorch, "
  38. "only images whose names contain 'devel' will provide nvcc."
  39. )
  40. def append_nvcc_threads(nvcc_extra_args):
  41. _, bare_metal_version = get_cuda_bare_metal_version(CUDA_HOME)
  42. if bare_metal_version >= Version("11.2"):
  43. nvcc_threads = os.getenv("NVCC_THREADS") or "4"
  44. return nvcc_extra_args + ["--threads", nvcc_threads]
  45. return nvcc_extra_args
  46. if not torch.cuda.is_available():
  47. # https://github.com/NVIDIA/apex/issues/486
  48. # Extension builds after https://github.com/pytorch/pytorch/pull/23408 attempt to query torch.cuda.get_device_capability(),
  49. # which will fail if you are compiling in an environment without visible GPUs (e.g. during an nvidia-docker build command).
  50. print(
  51. "\nWarning: Torch did not find available GPUs on this system.\n",
  52. "If your intention is to cross-compile, this is not an error.\n"
  53. "By default, Apex will cross-compile for Pascal (compute capabilities 6.0, 6.1, 6.2),\n"
  54. "Volta (compute capability 7.0), Turing (compute capability 7.5),\n"
  55. "and, if the CUDA version is >= 11.0, Ampere (compute capability 8.0).\n"
  56. "If you wish to cross-compile for a single specific architecture,\n"
  57. 'export TORCH_CUDA_ARCH_LIST="compute capability" before running setup.py.\n',
  58. )
  59. if os.environ.get("TORCH_CUDA_ARCH_LIST", None) is None and CUDA_HOME is not None:
  60. _, bare_metal_version = get_cuda_bare_metal_version(CUDA_HOME)
  61. if bare_metal_version >= Version("11.8"):
  62. os.environ["TORCH_CUDA_ARCH_LIST"] = "6.0;6.1;6.2;7.0;7.5;8.0;8.6;9.0"
  63. elif bare_metal_version >= Version("11.1"):
  64. os.environ["TORCH_CUDA_ARCH_LIST"] = "6.0;6.1;6.2;7.0;7.5;8.0;8.6"
  65. elif bare_metal_version == Version("11.0"):
  66. os.environ["TORCH_CUDA_ARCH_LIST"] = "6.0;6.1;6.2;7.0;7.5;8.0"
  67. else:
  68. os.environ["TORCH_CUDA_ARCH_LIST"] = "6.0;6.1;6.2;7.0;7.5"
  69. print("\n\ntorch.__version__ = {}\n\n".format(torch.__version__))
  70. TORCH_MAJOR = int(torch.__version__.split(".")[0])
  71. TORCH_MINOR = int(torch.__version__.split(".")[1])
  72. cmdclass = {}
  73. ext_modules = []
  74. # Check, if ATen/CUDAGeneratorImpl.h is found, otherwise use ATen/cuda/CUDAGeneratorImpl.h
  75. # See https://github.com/pytorch/pytorch/pull/70650
  76. generator_flag = []
  77. torch_dir = torch.__path__[0]
  78. if os.path.exists(os.path.join(torch_dir, "include", "ATen", "CUDAGeneratorImpl.h")):
  79. generator_flag = ["-DOLD_GENERATOR_PATH"]
  80. raise_if_cuda_home_none("--ft_attention")
  81. # Check, if CUDA11 is installed for compute capability 8.0
  82. cc_flag = []
  83. _, bare_metal_version = get_cuda_bare_metal_version(CUDA_HOME)
  84. if bare_metal_version < Version("11.0"):
  85. raise RuntimeError("ft_attention is only supported on CUDA 11 and above")
  86. cc_flag.append("-gencode")
  87. cc_flag.append("arch=compute_70,code=sm_70")
  88. cc_flag.append("-gencode")
  89. cc_flag.append("arch=compute_80,code=sm_80")
  90. if bare_metal_version >= Version("11.8"):
  91. cc_flag.append("-gencode")
  92. cc_flag.append("arch=compute_90,code=sm_90")
  93. ext_modules.append(
  94. CUDAExtension(
  95. name="ft_attention",
  96. sources=[
  97. "ft_attention.cpp",
  98. "decoder_masked_multihead_attention.cu",
  99. ],
  100. extra_compile_args={
  101. "cxx": ["-O3", "-DENABLE_BF16"] + generator_flag,
  102. "nvcc": append_nvcc_threads(
  103. [
  104. "-DENABLE_BF16", # TODO
  105. "-O3",
  106. "-U__CUDA_NO_HALF_OPERATORS__",
  107. "-U__CUDA_NO_HALF_CONVERSIONS__",
  108. "-U__CUDA_NO_BFLOAT16_OPERATORS__",
  109. "-U__CUDA_NO_BFLOAT16_CONVERSIONS__",
  110. "-U__CUDA_NO_BFLOAT162_OPERATORS__",
  111. "-U__CUDA_NO_BFLOAT162_CONVERSIONS__",
  112. "--expt-relaxed-constexpr",
  113. "--expt-extended-lambda",
  114. "--use_fast_math",
  115. ]
  116. + generator_flag
  117. + cc_flag
  118. ),
  119. },
  120. include_dirs=[this_dir],
  121. )
  122. )
  123. setup(
  124. name="ft_attention",
  125. version="0.1",
  126. description="Attention for single query from FasterTransformer",
  127. ext_modules=ext_modules,
  128. cmdclass={"build_ext": BuildExtension} if ext_modules else {},
  129. )