1
0

setup.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. import torch
  7. from torch.utils.cpp_extension import BuildExtension, CppExtension, CUDAExtension, CUDA_HOME
  8. from setuptools import setup, find_packages
  9. import subprocess
  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("--fast_layer_norm")
  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("dropout_layer_norm 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="dropout_layer_norm",
  96. sources=[
  97. "ln_api.cpp",
  98. "ln_fwd_256.cu",
  99. "ln_bwd_256.cu",
  100. "ln_fwd_512.cu",
  101. "ln_bwd_512.cu",
  102. "ln_fwd_768.cu",
  103. "ln_bwd_768.cu",
  104. "ln_fwd_1024.cu",
  105. "ln_bwd_1024.cu",
  106. "ln_fwd_1280.cu",
  107. "ln_bwd_1280.cu",
  108. "ln_fwd_1536.cu",
  109. "ln_bwd_1536.cu",
  110. "ln_fwd_2048.cu",
  111. "ln_bwd_2048.cu",
  112. "ln_fwd_2560.cu",
  113. "ln_bwd_2560.cu",
  114. "ln_fwd_3072.cu",
  115. "ln_bwd_3072.cu",
  116. "ln_fwd_4096.cu",
  117. "ln_bwd_4096.cu",
  118. "ln_fwd_5120.cu",
  119. "ln_bwd_5120.cu",
  120. "ln_fwd_6144.cu",
  121. "ln_bwd_6144.cu",
  122. "ln_fwd_7168.cu",
  123. "ln_bwd_7168.cu",
  124. "ln_fwd_8192.cu",
  125. "ln_bwd_8192.cu",
  126. "ln_parallel_fwd_256.cu",
  127. "ln_parallel_bwd_256.cu",
  128. "ln_parallel_fwd_512.cu",
  129. "ln_parallel_bwd_512.cu",
  130. "ln_parallel_fwd_768.cu",
  131. "ln_parallel_bwd_768.cu",
  132. "ln_parallel_fwd_1024.cu",
  133. "ln_parallel_bwd_1024.cu",
  134. "ln_parallel_fwd_1280.cu",
  135. "ln_parallel_bwd_1280.cu",
  136. "ln_parallel_fwd_1536.cu",
  137. "ln_parallel_bwd_1536.cu",
  138. "ln_parallel_fwd_2048.cu",
  139. "ln_parallel_bwd_2048.cu",
  140. "ln_parallel_fwd_2560.cu",
  141. "ln_parallel_bwd_2560.cu",
  142. "ln_parallel_fwd_3072.cu",
  143. "ln_parallel_bwd_3072.cu",
  144. "ln_parallel_fwd_4096.cu",
  145. "ln_parallel_bwd_4096.cu",
  146. "ln_parallel_fwd_5120.cu",
  147. "ln_parallel_bwd_5120.cu",
  148. "ln_parallel_fwd_6144.cu",
  149. "ln_parallel_bwd_6144.cu",
  150. "ln_parallel_fwd_7168.cu",
  151. "ln_parallel_bwd_7168.cu",
  152. "ln_parallel_fwd_8192.cu",
  153. "ln_parallel_bwd_8192.cu",
  154. ],
  155. extra_compile_args={
  156. "cxx": ["-O3"] + generator_flag,
  157. "nvcc": append_nvcc_threads(
  158. [
  159. "-O3",
  160. "-U__CUDA_NO_HALF_OPERATORS__",
  161. "-U__CUDA_NO_HALF_CONVERSIONS__",
  162. "-U__CUDA_NO_BFLOAT16_OPERATORS__",
  163. "-U__CUDA_NO_BFLOAT16_CONVERSIONS__",
  164. "-U__CUDA_NO_BFLOAT162_OPERATORS__",
  165. "-U__CUDA_NO_BFLOAT162_CONVERSIONS__",
  166. "--expt-relaxed-constexpr",
  167. "--expt-extended-lambda",
  168. "--use_fast_math",
  169. ]
  170. + generator_flag
  171. + cc_flag
  172. ),
  173. },
  174. include_dirs=[this_dir],
  175. )
  176. )
  177. setup(
  178. name="dropout_layer_norm",
  179. version="0.1",
  180. description="Fused dropout + add + layer norm",
  181. ext_modules=ext_modules,
  182. cmdclass={"build_ext": BuildExtension} if ext_modules else {},
  183. )