async_io.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # Copyright (c) Microsoft Corporation.
  2. # SPDX-License-Identifier: Apache-2.0
  3. # DeepSpeed Team
  4. import os
  5. import distutils.spawn
  6. import subprocess
  7. from .builder import TorchCPUOpBuilder
  8. class AsyncIOBuilder(TorchCPUOpBuilder):
  9. BUILD_VAR = "DS_BUILD_AIO"
  10. NAME = "async_io"
  11. def __init__(self):
  12. super().__init__(name=self.NAME)
  13. def absolute_name(self):
  14. return f'deepspeed.ops.aio.{self.NAME}_op'
  15. def lib_sources(self):
  16. src_list = [
  17. 'csrc/aio/py_lib/deepspeed_py_io_handle.cpp', 'csrc/aio/py_lib/deepspeed_py_aio.cpp',
  18. 'csrc/aio/py_lib/deepspeed_py_aio_handle.cpp', 'csrc/aio/py_lib/deepspeed_aio_thread.cpp',
  19. 'csrc/aio/common/deepspeed_aio_utils.cpp', 'csrc/aio/common/deepspeed_aio_common.cpp',
  20. 'csrc/aio/common/deepspeed_aio_types.cpp', 'csrc/aio/py_lib/deepspeed_cpu_op.cpp',
  21. 'csrc/aio/py_lib/deepspeed_aio_op_desc.cpp', 'csrc/aio/py_lib/deepspeed_py_copy.cpp',
  22. 'csrc/aio/py_lib/deepspeed_pin_tensor.cpp'
  23. ]
  24. return src_list
  25. def sources(self):
  26. return self.lib_sources() + ['csrc/aio/py_lib/py_ds_aio.cpp']
  27. def include_paths(self):
  28. import torch
  29. if self.build_for_cpu:
  30. CUDA_INCLUDE = []
  31. elif not self.is_rocm_pytorch():
  32. CUDA_INCLUDE = [os.path.join(torch.utils.cpp_extension.CUDA_HOME, "include")]
  33. else:
  34. CUDA_INCLUDE = [
  35. os.path.join(torch.utils.cpp_extension.ROCM_HOME, "include"),
  36. os.path.join(torch.utils.cpp_extension.ROCM_HOME, "include", "rocrand"),
  37. os.path.join(torch.utils.cpp_extension.ROCM_HOME, "include", "hiprand"),
  38. ]
  39. return ['csrc/aio/py_lib', 'csrc/aio/common'] + CUDA_INCLUDE
  40. def cxx_args(self):
  41. # -O0 for improved debugging, since performance is bound by I/O
  42. args = super().cxx_args()
  43. import torch
  44. TORCH_MAJOR, TORCH_MINOR = map(int, torch.__version__.split('.')[0:2])
  45. if not (TORCH_MAJOR >= 2 and TORCH_MINOR >= 1):
  46. args.remove('-std=c++17')
  47. args.append('-std=c++14')
  48. args += ['-Wall', '-O0', '-shared', '-fPIC', '-Wno-reorder']
  49. return args
  50. def extra_ldflags(self):
  51. if self.build_for_cpu:
  52. return ['-fopenmp']
  53. import torch.utils.cpp_extension
  54. CUDA_HOME = torch.utils.cpp_extension.CUDA_HOME
  55. CUDA_LIB64 = os.path.join(CUDA_HOME, "lib64")
  56. ldflags = [f'-L{CUDA_HOME}', f'-L{CUDA_LIB64}', '-laio', '-lcuda', '-lcudart']
  57. return ldflags
  58. def check_for_libaio_pkg(self):
  59. libs = dict(
  60. dpkg=["-l", "libaio-dev", "apt"],
  61. pacman=["-Q", "libaio", "pacman"],
  62. rpm=["-q", "libaio-devel", "yum"],
  63. )
  64. found = False
  65. for pkgmgr, data in libs.items():
  66. flag, lib, tool = data
  67. path = distutils.spawn.find_executable(pkgmgr)
  68. if path is not None:
  69. cmd = [pkgmgr, flag, lib]
  70. result = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  71. if result.wait() == 0:
  72. found = True
  73. else:
  74. self.warning(f"{self.NAME}: please install the {lib} package with {tool}")
  75. break
  76. return found
  77. def is_compatible(self, verbose=False):
  78. # Check for the existence of libaio by using distutils
  79. # to compile and link a test program that calls io_submit,
  80. # which is a function provided by libaio that is used in the async_io op.
  81. # If needed, one can define -I and -L entries in CFLAGS and LDFLAGS
  82. # respectively to specify the directories for libaio.h and libaio.so.
  83. aio_compatible = self.has_function('io_submit', ('aio', ))
  84. if verbose and not aio_compatible:
  85. self.warning(f"{self.NAME} requires the dev libaio .so object and headers but these were not found.")
  86. # Check for the libaio package via known package managers
  87. # to print suggestions on which package to install.
  88. self.check_for_libaio_pkg()
  89. self.warning(
  90. "If libaio is already installed (perhaps from source), try setting the CFLAGS and LDFLAGS environment variables to where it can be found."
  91. )
  92. return super().is_compatible(verbose) and aio_compatible