setup.py 5.7 KB

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