mps_accelerator.py 5.7 KB

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