async_io.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # Copyright (c) Microsoft Corporation.
  2. # SPDX-License-Identifier: Apache-2.0
  3. # DeepSpeed Team
  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', 'csrc/aio/py_lib/py_ds_aio.cpp',
  17. 'csrc/aio/py_lib/deepspeed_py_aio.cpp', 'csrc/aio/py_lib/deepspeed_py_aio_handle.cpp',
  18. 'csrc/aio/py_lib/deepspeed_aio_thread.cpp', 'csrc/aio/common/deepspeed_aio_utils.cpp',
  19. 'csrc/aio/common/deepspeed_aio_common.cpp', 'csrc/aio/common/deepspeed_aio_types.cpp',
  20. 'csrc/aio/py_lib/deepspeed_pin_tensor.cpp'
  21. ]
  22. def include_paths(self):
  23. return ['csrc/aio/py_lib', 'csrc/aio/common']
  24. def cxx_args(self):
  25. # -O0 for improved debugging, since performance is bound by I/O
  26. CPU_ARCH = self.cpu_arch()
  27. SIMD_WIDTH = self.simd_width()
  28. import torch # Keep this import here to avoid errors when building DeepSpeed wheel without torch installed
  29. TORCH_MAJOR, TORCH_MINOR = map(int, torch.__version__.split('.')[0:2])
  30. if TORCH_MAJOR >= 2 and TORCH_MINOR >= 1:
  31. CPP_STD = '-std=c++17'
  32. else:
  33. CPP_STD = '-std=c++14'
  34. return [
  35. '-g',
  36. '-Wall',
  37. '-O0',
  38. CPP_STD,
  39. '-shared',
  40. '-fPIC',
  41. '-Wno-reorder',
  42. CPU_ARCH,
  43. '-fopenmp',
  44. SIMD_WIDTH,
  45. '-laio',
  46. ]
  47. def extra_ldflags(self):
  48. return ['-laio']
  49. def check_for_libaio_pkg(self):
  50. libs = dict(
  51. dpkg=["-l", "libaio-dev", "apt"],
  52. pacman=["-Q", "libaio", "pacman"],
  53. rpm=["-q", "libaio-devel", "yum"],
  54. )
  55. found = False
  56. for pkgmgr, data in libs.items():
  57. flag, lib, tool = data
  58. path = distutils.spawn.find_executable(pkgmgr)
  59. if path is not None:
  60. cmd = f"{pkgmgr} {flag} {lib}"
  61. result = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
  62. if result.wait() == 0:
  63. found = True
  64. else:
  65. self.warning(f"{self.NAME}: please install the {lib} package with {tool}")
  66. break
  67. return found
  68. def is_compatible(self, verbose=True):
  69. # Check for the existence of libaio by using distutils
  70. # to compile and link a test program that calls io_submit,
  71. # which is a function provided by libaio that is used in the async_io op.
  72. # If needed, one can define -I and -L entries in CFLAGS and LDFLAGS
  73. # respectively to specify the directories for libaio.h and libaio.so.
  74. aio_compatible = self.has_function('io_pgetevents', ('aio', ))
  75. if verbose and not aio_compatible:
  76. self.warning(f"{self.NAME} requires the dev libaio .so object and headers but these were not found.")
  77. # Check for the libaio package via known package managers
  78. # to print suggestions on which package to install.
  79. self.check_for_libaio_pkg()
  80. self.warning(
  81. "If libaio is already installed (perhaps from source), try setting the CFLAGS and LDFLAGS environment variables to where it can be found."
  82. )
  83. return super().is_compatible(verbose) and aio_compatible