__init__.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # coding=utf-8
  2. # Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import os
  16. import pathlib
  17. import subprocess
  18. from torch.utils import cpp_extension
  19. # Setting this param to a list has a problem of generating different
  20. # compilation commands (with diferent order of architectures) and
  21. # leading to recompilation of fused kernels. Set it to empty string
  22. # to avoid recompilation and assign arch flags explicity in
  23. # extra_cuda_cflags below
  24. os.environ["TORCH_CUDA_ARCH_LIST"] = ""
  25. def load(args):
  26. # Check if cuda 11 is installed for compute capability 8.0
  27. cc_flag = []
  28. _, bare_metal_major, _ = _get_cuda_bare_metal_version(
  29. cpp_extension.CUDA_HOME)
  30. if int(bare_metal_major) >= 11:
  31. cc_flag.append('-gencode')
  32. cc_flag.append('arch=compute_80,code=sm_80')
  33. # Build path
  34. srcpath = pathlib.Path(__file__).parent.absolute()
  35. buildpath = srcpath / 'build'
  36. _create_build_dir(buildpath)
  37. # Helper function to build the kernels.
  38. def _cpp_extention_load_helper(name, sources, extra_cuda_flags):
  39. return cpp_extension.load(
  40. name=name,
  41. sources=sources,
  42. build_directory=buildpath,
  43. extra_cflags=['-O3',],
  44. extra_cuda_cflags=['-O3',
  45. '-gencode', 'arch=compute_70,code=sm_70',
  46. '--use_fast_math'] + extra_cuda_flags + cc_flag,
  47. verbose=(args.rank == 0)
  48. )
  49. # ==============
  50. # Fused softmax.
  51. # ==============
  52. if args.masked_softmax_fusion:
  53. extra_cuda_flags = ['-U__CUDA_NO_HALF_OPERATORS__',
  54. '-U__CUDA_NO_HALF_CONVERSIONS__',
  55. '--expt-relaxed-constexpr',
  56. '--expt-extended-lambda']
  57. # Upper triangular softmax.
  58. sources=[srcpath / 'scaled_upper_triang_masked_softmax.cpp',
  59. srcpath / 'scaled_upper_triang_masked_softmax_cuda.cu']
  60. scaled_upper_triang_masked_softmax_cuda = _cpp_extention_load_helper(
  61. "scaled_upper_triang_masked_softmax_cuda",
  62. sources, extra_cuda_flags)
  63. # Masked softmax.
  64. sources=[srcpath / 'scaled_masked_softmax.cpp',
  65. srcpath / 'scaled_masked_softmax_cuda.cu']
  66. scaled_masked_softmax_cuda = _cpp_extention_load_helper(
  67. "scaled_masked_softmax_cuda", sources, extra_cuda_flags)
  68. # =================================
  69. # Mixed precision fused layer norm.
  70. # =================================
  71. extra_cuda_flags = ['-maxrregcount=50']
  72. sources=[srcpath / 'layer_norm_cuda.cpp',
  73. srcpath / 'layer_norm_cuda_kernel.cu']
  74. fused_mix_prec_layer_norm_cuda = _cpp_extention_load_helper(
  75. "fused_mix_prec_layer_norm_cuda", sources, extra_cuda_flags)
  76. def _get_cuda_bare_metal_version(cuda_dir):
  77. raw_output = subprocess.check_output([cuda_dir + "/bin/nvcc", "-V"],
  78. universal_newlines=True)
  79. output = raw_output.split()
  80. release_idx = output.index("release") + 1
  81. release = output[release_idx].split(".")
  82. bare_metal_major = release[0]
  83. bare_metal_minor = release[1][0]
  84. return raw_output, bare_metal_major, bare_metal_minor
  85. def _create_build_dir(buildpath):
  86. try:
  87. os.mkdir(buildpath)
  88. except OSError:
  89. if not os.path.isdir(buildpath):
  90. print(f"Creation of the build directory {buildpath} failed")