async_io.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. """
  2. Copyright 2020 The Microsoft DeepSpeed Team
  3. """
  4. import distutils.spawn
  5. import subprocess
  6. from .builder import OpBuilder
  7. class AsyncIOBuilder(OpBuilder):
  8. BUILD_VAR = "DS_BUILD_AIO"
  9. NAME = "async_io"
  10. def __init__(self):
  11. super().__init__(name=self.NAME)
  12. def absolute_name(self):
  13. return f'deepspeed.ops.aio.{self.NAME}_op'
  14. def sources(self):
  15. return [
  16. 'csrc/aio/py_lib/deepspeed_py_copy.cpp',
  17. 'csrc/aio/py_lib/py_ds_aio.cpp',
  18. 'csrc/aio/py_lib/deepspeed_py_aio.cpp',
  19. 'csrc/aio/py_lib/deepspeed_py_aio_handle.cpp',
  20. 'csrc/aio/py_lib/deepspeed_aio_thread.cpp',
  21. 'csrc/aio/common/deepspeed_aio_utils.cpp',
  22. 'csrc/aio/common/deepspeed_aio_common.cpp',
  23. 'csrc/aio/common/deepspeed_aio_types.cpp'
  24. ]
  25. def include_paths(self):
  26. return ['csrc/aio/py_lib', 'csrc/aio/common']
  27. def cxx_args(self):
  28. # -O0 for improved debugging, since performance is bound by I/O
  29. CPU_ARCH = self.cpu_arch()
  30. SIMD_WIDTH = self.simd_width()
  31. return [
  32. '-g',
  33. '-Wall',
  34. '-O0',
  35. '-std=c++14',
  36. '-shared',
  37. '-fPIC',
  38. '-Wno-reorder',
  39. CPU_ARCH,
  40. '-fopenmp',
  41. SIMD_WIDTH,
  42. '-laio',
  43. ]
  44. def extra_ldflags(self):
  45. return ['-laio']
  46. def check_for_libaio_pkg(self):
  47. libs = dict(
  48. dpkg=["-l",
  49. "libaio-dev",
  50. "apt"],
  51. pacman=["-Q",
  52. "libaio",
  53. "pacman"],
  54. rpm=["-q",
  55. "libaio-devel",
  56. "yum"],
  57. )
  58. found = False
  59. for pkgmgr, data in libs.items():
  60. flag, lib, tool = data
  61. path = distutils.spawn.find_executable(pkgmgr)
  62. if path is not None:
  63. cmd = f"{pkgmgr} {flag} {lib}"
  64. result = subprocess.Popen(cmd,
  65. stdout=subprocess.PIPE,
  66. stderr=subprocess.PIPE,
  67. shell=True)
  68. if result.wait() == 0:
  69. found = True
  70. else:
  71. self.warning(
  72. f"{self.NAME}: please install the {lib} package with {tool}")
  73. break
  74. return found
  75. def is_compatible(self, verbose=True):
  76. # Check for the existence of libaio by using distutils
  77. # to compile and link a test program that calls io_submit,
  78. # which is a function provided by libaio that is used in the async_io op.
  79. # If needed, one can define -I and -L entries in CFLAGS and LDFLAGS
  80. # respectively to specify the directories for libaio.h and libaio.so.
  81. aio_compatible = self.has_function('io_submit', ('aio', ))
  82. if verbose and not aio_compatible:
  83. self.warning(
  84. f"{self.NAME} requires the dev libaio .so object and headers but these were not found."
  85. )
  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