builder.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # Copyright (c) Microsoft Corporation.
  2. # SPDX-License-Identifier: Apache-2.0
  3. # DeepSpeed Team
  4. import re
  5. import os
  6. try:
  7. import torch_npu
  8. except ImportError as e:
  9. pass
  10. try:
  11. # is op_builder from deepspeed or a 3p version? this should only succeed if it's deepspeed
  12. # if successful this also means we're doing a local install and not JIT compile path
  13. from op_builder import __deepspeed__ # noqa: F401 # type: ignore
  14. from op_builder.builder import OpBuilder
  15. except ImportError:
  16. from deepspeed.ops.op_builder.builder import OpBuilder
  17. class NPUOpBuilder(OpBuilder):
  18. _ascend_path = None
  19. _torch_npu_path = None
  20. _cann_version = None
  21. def __init__(self, name):
  22. super().__init__(name)
  23. self._ascend_path = self.installed_cann_path()
  24. self._torch_npu_path = os.path.join(os.path.dirname(os.path.abspath(torch_npu.__file__)))
  25. try:
  26. self._cann_version = self.installed_cann_version(self.name)
  27. except BaseException:
  28. print(f"{self.name} ascend_cann is missing, npu ops cannot be compiled!")
  29. def cann_defs(self):
  30. if self._cann_version:
  31. return '-D__ENABLE_CANN__'
  32. return '-D__DISABLE_CANN__'
  33. def installed_cann_path(self):
  34. if "ASCEND_HOME_PATH" in os.environ or os.path.exists(os.environ["ASCEND_HOME_PATH"]):
  35. return os.environ["ASCEND_HOME_PATH"]
  36. return None
  37. def installed_cann_version(self, name=""):
  38. ascend_path = self.installed_cann_path()
  39. assert ascend_path is not None, "CANN_HOME does not exist, unable to compile NPU op(s)"
  40. cann_version = ""
  41. for dirpath, _, filenames in os.walk(os.path.realpath(ascend_path)):
  42. if cann_version:
  43. break
  44. install_files = [file for file in filenames if re.match(r"ascend_.*_install\.info", file)]
  45. if install_files:
  46. filepath = os.path.join(dirpath, install_files[0])
  47. with open(filepath, "r") as f:
  48. for line in f:
  49. if line.find("version") != -1:
  50. cann_version = line.strip().split("=")[-1]
  51. break
  52. return cann_version
  53. def include_paths(self):
  54. paths = super().include_paths()
  55. paths += [os.path.join(self._ascend_path, 'include'), os.path.join(self._torch_npu_path, 'include')]
  56. return paths
  57. def cxx_args(self):
  58. args = super().cxx_args()
  59. args += ['-O3', '-std=c++17', '-g', '-Wno-reorder', '-fopenmp']
  60. args += ['-fstack-protector-all', '-Wl,-z,relro,-z,now,-z,noexecstack', '-Wl,--disable-new-dtags,--rpath']
  61. args += [
  62. self.cann_defs(),
  63. self.cpu_arch(),
  64. self.simd_width(), '-L' + os.path.join(self._ascend_path, 'lib64'),
  65. '-L' + os.path.join(self._torch_npu_path, 'lib')
  66. ]
  67. return args
  68. def extra_ldflags(self):
  69. flags = super().extra_ldflags()
  70. flags += [
  71. '-L' + os.path.join(self._ascend_path, 'lib64'), '-lascendcl',
  72. '-L' + os.path.join(self._torch_npu_path, 'lib'), '-ltorch_npu'
  73. ]
  74. return flags