deepspeed
99 строк · 3.6 Кб
1# Copyright (c) Microsoft Corporation.
2# SPDX-License-Identifier: Apache-2.0
3
4# DeepSpeed Team
5
6import distutils.spawn7import subprocess8
9from .builder import OpBuilder10
11
12class AsyncIOBuilder(OpBuilder):13BUILD_VAR = "DS_BUILD_AIO"14NAME = "async_io"15
16def __init__(self):17super().__init__(name=self.NAME)18
19def absolute_name(self):20return f'deepspeed.ops.aio.{self.NAME}_op'21
22def sources(self):23return [24'csrc/aio/py_lib/deepspeed_py_copy.cpp', 'csrc/aio/py_lib/py_ds_aio.cpp',25'csrc/aio/py_lib/deepspeed_py_aio.cpp', 'csrc/aio/py_lib/deepspeed_py_aio_handle.cpp',26'csrc/aio/py_lib/deepspeed_aio_thread.cpp', 'csrc/aio/common/deepspeed_aio_utils.cpp',27'csrc/aio/common/deepspeed_aio_common.cpp', 'csrc/aio/common/deepspeed_aio_types.cpp',28'csrc/aio/py_lib/deepspeed_pin_tensor.cpp'29]30
31def include_paths(self):32return ['csrc/aio/py_lib', 'csrc/aio/common']33
34def cxx_args(self):35# -O0 for improved debugging, since performance is bound by I/O36CPU_ARCH = self.cpu_arch()37SIMD_WIDTH = self.simd_width()38import torch # Keep this import here to avoid errors when building DeepSpeed wheel without torch installed39TORCH_MAJOR, TORCH_MINOR = map(int, torch.__version__.split('.')[0:2])40if TORCH_MAJOR >= 2 and TORCH_MINOR >= 1:41CPP_STD = '-std=c++17'42else:43CPP_STD = '-std=c++14'44return [45'-g',46'-Wall',47'-O0',48CPP_STD,49'-shared',50'-fPIC',51'-Wno-reorder',52CPU_ARCH,53'-fopenmp',54SIMD_WIDTH,55'-laio',56]57
58def extra_ldflags(self):59return ['-laio']60
61def check_for_libaio_pkg(self):62libs = dict(63dpkg=["-l", "libaio-dev", "apt"],64pacman=["-Q", "libaio", "pacman"],65rpm=["-q", "libaio-devel", "yum"],66)67
68found = False69for pkgmgr, data in libs.items():70flag, lib, tool = data71path = distutils.spawn.find_executable(pkgmgr)72if path is not None:73cmd = f"{pkgmgr} {flag} {lib}"74result = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)75if result.wait() == 0:76found = True77else:78self.warning(f"{self.NAME}: please install the {lib} package with {tool}")79break80return found81
82def is_compatible(self, verbose=True):83# Check for the existence of libaio by using distutils84# to compile and link a test program that calls io_submit,85# which is a function provided by libaio that is used in the async_io op.86# If needed, one can define -I and -L entries in CFLAGS and LDFLAGS87# respectively to specify the directories for libaio.h and libaio.so.88aio_compatible = self.has_function('io_pgetevents', ('aio', ))89if verbose and not aio_compatible:90self.warning(f"{self.NAME} requires the dev libaio .so object and headers but these were not found.")91
92# Check for the libaio package via known package managers93# to print suggestions on which package to install.94self.check_for_libaio_pkg()95
96self.warning(97"If libaio is already installed (perhaps from source), try setting the CFLAGS and LDFLAGS environment variables to where it can be found."98)99return super().is_compatible(verbose) and aio_compatible100