npu_accelerator.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. # Copyright (c) Microsoft Corporation.
  2. # SPDX-License-Identifier: Apache-2.0
  3. # DeepSpeed Team
  4. from .abstract_accelerator import DeepSpeedAccelerator
  5. # During setup stage torch may not be installed, pass on no torch will
  6. # allow op builder related API to be executed.
  7. try:
  8. import torch.npu
  9. except ImportError:
  10. pass
  11. class NPU_Accelerator(DeepSpeedAccelerator):
  12. def __init__(self):
  13. self._name = 'npu'
  14. self._communication_backend_name = 'hccl'
  15. def is_synchronized_device(self):
  16. return False
  17. # Device APIs
  18. def device_name(self, device_index=None):
  19. if device_index == None:
  20. return 'npu'
  21. return 'npu:{}'.format(device_index)
  22. def device(self, device_index=None):
  23. return torch.npu.device(device_index)
  24. def set_device(self, device_index):
  25. torch.npu.set_device(device_index)
  26. def current_device(self):
  27. return torch.npu.current_device()
  28. def current_device_name(self):
  29. return 'npu:{}'.format(torch.npu.current_device())
  30. def device_count(self):
  31. return torch.npu.device_count()
  32. def synchronize(self, device_index=None):
  33. return torch.npu.synchronize(device_index)
  34. # RNG APIs
  35. def random(self):
  36. return torch.random
  37. def set_rng_state(self, new_state, device_index=None):
  38. if device_index is None:
  39. return torch.npu.set_rng_state(new_state)
  40. return torch.npu.set_rng_state(new_state, device_index)
  41. def get_rng_state(self, device_index=None):
  42. if device_index is None:
  43. return torch.npu.get_rng_state()
  44. return torch.npu.get_rng_state(device_index)
  45. def manual_seed(self, seed):
  46. return torch.npu.manual_seed(seed)
  47. def manual_seed_all(self, seed):
  48. return torch.npu.manual_seed_all(seed)
  49. def initial_seed(self, seed):
  50. return torch.npu.initial_seed(seed)
  51. def default_generator(self, device_index):
  52. return torch.npu.default_generators[device_index]
  53. # Streams/Events
  54. @property
  55. def Stream(self):
  56. return torch.npu.Stream
  57. def stream(self, stream):
  58. return torch.npu.stream(stream)
  59. def current_stream(self, device_index=None):
  60. return torch.npu.current_stream(device_index)
  61. def default_stream(self, device_index=None):
  62. return torch.npu.default_stream(device_index)
  63. @property
  64. def Event(self):
  65. return torch.npu.Event
  66. # Memory management
  67. def empty_cache(self):
  68. return torch.npu.empty_cache()
  69. def memory_allocated(self, device_index=None):
  70. return torch.npu.memory_allocated(device_index)
  71. def max_memory_allocated(self, device_index=None):
  72. return torch.npu.max_memory_allocated(device_index)
  73. def reset_max_memory_allocated(self, device_index=None):
  74. return torch.npu.reset_max_memory_allocated(device_index)
  75. def memory_cached(self, device_index=None):
  76. return torch.npu.memory_cached(device_index)
  77. def max_memory_cached(self, device_index=None):
  78. return torch.npu.max_memory_cached(device_index)
  79. def reset_max_memory_cached(self, device_index=None):
  80. return torch.npu.reset_max_memory_cached(device_index)
  81. def memory_stats(self, device_index=None):
  82. if hasattr(torch.npu, 'memory_stats'):
  83. return torch.npu.memory_stats(device_index)
  84. def reset_peak_memory_stats(self, device_index=None):
  85. if hasattr(torch.npu, 'reset_peak_memory_stats'):
  86. return torch.npu.reset_peak_memory_stats(device_index)
  87. def memory_reserved(self, device_index=None):
  88. if hasattr(torch.npu, 'memory_reserved'):
  89. return torch.npu.memory_reserved(device_index)
  90. def max_memory_reserved(self, device_index=None):
  91. if hasattr(torch.npu, 'max_memory_reserved'):
  92. return torch.npu.max_memory_reserved(device_index)
  93. def total_memory(self, device_index=None):
  94. return torch.npu.get_device_properties(device_index).total_memory
  95. # Data types
  96. def is_bf16_supported(self):
  97. return torch.npu.is_bf16_supported()
  98. def is_fp16_supported(self):
  99. return True
  100. # Misc
  101. def amp(self):
  102. if hasattr(torch.npu, 'amp'):
  103. return torch.npu.amp
  104. return None
  105. def is_available(self):
  106. return torch.npu.is_available()
  107. def range_push(self, msg):
  108. return
  109. def range_pop(self):
  110. return
  111. def lazy_call(self, callback):
  112. return torch.npu._lazy_call(callback)
  113. def communication_backend_name(self):
  114. return self._communication_backend_name
  115. # Tensor operations
  116. @property
  117. def BFloat16Tensor(self):
  118. return torch.npu.BFloat16Tensor
  119. @property
  120. def ByteTensor(self):
  121. return torch.npu.ByteTensor
  122. @property
  123. def DoubleTensor(self):
  124. return torch.npu.DoubleTensor
  125. @property
  126. def FloatTensor(self):
  127. return torch.npu.FloatTensor
  128. @property
  129. def HalfTensor(self):
  130. return torch.npu.HalfTensor
  131. @property
  132. def IntTensor(self):
  133. return torch.npu.IntTensor
  134. @property
  135. def LongTensor(self):
  136. return torch.npu.LongTensor
  137. def pin_memory(self, tensor):
  138. return tensor.pin_memory()
  139. def on_accelerator(self, tensor):
  140. device_str = str(tensor.device)
  141. if device_str.startswith('npu:'):
  142. return True
  143. else:
  144. return False
  145. def op_builder_dir(self):
  146. try:
  147. # is op_builder from deepspeed or a 3p version? this should only succeed if it's deepspeed
  148. # if successful this also means we're doing a local install and not JIT compile path
  149. from op_builder import __deepspeed__ # noqa: F401
  150. return "op_builder.npu"
  151. except ImportError:
  152. return "deepspeed.ops.op_builder.npu"
  153. # dict that holds class name <--> class type mapping i.e.
  154. # 'AsyncIOBuilder': <class 'op_builder.async_io.AsyncIOBuilder'>
  155. # this dict will be filled at init stage
  156. class_dict = None
  157. def _lazy_init_class_dict(self):
  158. if self.class_dict != None:
  159. return
  160. else:
  161. self.class_dict = {}
  162. # create an instance of op builder and return, name specified by class_name
  163. def create_op_builder(self, class_name):
  164. self._lazy_init_class_dict()
  165. if class_name in self.class_dict:
  166. return self.class_dict[class_name]()
  167. else:
  168. return None
  169. # return an op builder class, name specified by class_name
  170. def get_op_builder(self, class_name):
  171. self._lazy_init_class_dict()
  172. if class_name in self.class_dict:
  173. return self.class_dict[class_name]
  174. else:
  175. return None
  176. def build_extension(self):
  177. from torch.utils.cpp_extension import BuildExtension
  178. return BuildExtension