MPSGuardImpl.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // Copyright © 2022 Apple Inc.
  2. #pragma once
  3. #include <ATen/Context.h>
  4. #include <ATen/mps/MPSEvent.h>
  5. #include <ATen/mps/MPSStream.h>
  6. #include <c10/core/impl/DeviceGuardImplInterface.h>
  7. #include <c10/macros/Macros.h>
  8. #include <c10/util/Exception.h>
  9. #ifdef __OBJC__
  10. #include <Foundation/Foundation.h>
  11. #include <Metal/Metal.h>
  12. #include <MetalPerformanceShaders/MetalPerformanceShaders.h>
  13. #endif
  14. #include <ATen/Tensor.h>
  15. #include <c10/core/MemoryFormat.h>
  16. #include <c10/core/Storage.h>
  17. #include <c10/core/TensorImpl.h>
  18. #include <c10/core/UndefinedTensorImpl.h>
  19. #include <c10/util/intrusive_ptr.h>
  20. #include <sys/_types/_size_t.h>
  21. #include <memory>
  22. namespace at::mps {
  23. typedef MPSEvent* mpsEvent_t;
  24. // TODO: Move the MPSGuardImpl to inherit from NoOpDeviceGuardImpl
  25. // https://github.com/pytorch/pytorch/issues/77170
  26. struct TORCH_API MPSGuardImpl final
  27. : public c10::impl::DeviceGuardImplInterface {
  28. static constexpr c10::DeviceType static_type = c10::DeviceType::MPS;
  29. // constructor
  30. MPSGuardImpl() {}
  31. explicit MPSGuardImpl(c10::DeviceType t) {
  32. TORCH_CHECK(
  33. t == DeviceType::MPS,
  34. "MPSGuardImpl initialized with non-MPS DeviceType: ",
  35. t);
  36. }
  37. // returns the type
  38. c10::DeviceType type() const override {
  39. return c10::DeviceType::MPS;
  40. }
  41. Device exchangeDevice(Device d) const override {
  42. return Device(c10::DeviceType::MPS, 0);
  43. }
  44. Device getDevice() const override {
  45. return Device(c10::DeviceType::MPS, 0);
  46. }
  47. std::optional<Device> uncheckedGetDevice() const noexcept {
  48. return Device(c10::DeviceType::MPS, 0);
  49. }
  50. void setDevice(Device d) const override {
  51. TORCH_CHECK(d.is_mps(), "Expected a MPS device, but got ", d);
  52. }
  53. void uncheckedSetDevice(Device d) const noexcept override {
  54. // TODO: Currently setting only device 0
  55. }
  56. Stream getStream(Device d) const override {
  57. return Stream(Stream::DEFAULT, Device(c10::DeviceType::MPS, 0));
  58. }
  59. Stream getNewStream(Device, int priority = 0) const override {
  60. (void)priority;
  61. return Stream(Stream::DEFAULT, Device(c10::DeviceType::MPS, 0));
  62. }
  63. Stream getDefaultStream(Device d) const override {
  64. return Stream(Stream::DEFAULT, Device(c10::DeviceType::MPS, 0));
  65. }
  66. // NB: These do NOT set the current device
  67. Stream exchangeStream(Stream s) const override {
  68. return Stream(Stream::DEFAULT, Device(c10::DeviceType::MPS, 0));
  69. }
  70. DeviceIndex deviceCount() const noexcept override {
  71. if (at::hasMPS()) {
  72. // TODO: extend it for multi-device case
  73. return 1;
  74. } else {
  75. return 0;
  76. }
  77. }
  78. // Event-related functions
  79. void createEvent(mpsEvent_t* event, const EventFlag flag) const;
  80. void destroyEvent(void* event, const DeviceIndex device_index)
  81. const noexcept override;
  82. void record(
  83. void** event,
  84. const Stream& stream,
  85. const DeviceIndex device_index,
  86. const EventFlag flag) const override;
  87. void block(void* event, const Stream& stream) const override;
  88. bool queryEvent(void* event) const override;
  89. void synchronizeEvent(void* event) const override;
  90. double elapsedTime(void* event1, void* event2, const DeviceIndex device_index)
  91. const override;
  92. void synchronizeDevice(const DeviceIndex device_index) const override;
  93. };
  94. /// A variant of OptionalDeviceGuard that is specialized for MPS.
  95. struct OptionalMPSGuard {
  96. explicit OptionalMPSGuard() : guard_() {}
  97. explicit OptionalMPSGuard(std::optional<Device> device_opt)
  98. : guard_(device_opt) {}
  99. /// Set the current MPS device to the passed device index, if it is not
  100. /// nullopt
  101. explicit OptionalMPSGuard(std::optional<DeviceIndex> device_index_opt)
  102. : guard_(device_index_opt) {}
  103. // Copy is not allowed
  104. OptionalMPSGuard(const OptionalMPSGuard&) = delete;
  105. OptionalMPSGuard& operator=(const OptionalMPSGuard&) = delete;
  106. OptionalMPSGuard(OptionalMPSGuard&& other) = delete;
  107. OptionalMPSGuard& operator=(OptionalMPSGuard&& other) = delete;
  108. /// Sets the MPS device to the given device, initializing the guard if it
  109. /// is not already initialized. Errors if the given device is not a MPS
  110. /// device.
  111. void set_device(Device device) {
  112. guard_.set_device(device);
  113. }
  114. /// Sets the MPS device to the given device, initializing the guard if it is
  115. /// not already initialized. Errors if the given device is not a MPS device.
  116. void reset_device(Device device) {
  117. guard_.reset_device(device);
  118. }
  119. /// Sets the MPS device to the given device index, initializing the guard if
  120. /// it is not already initialized.
  121. void set_index(DeviceIndex device_index) {
  122. guard_.set_index(device_index);
  123. }
  124. /// Returns the device that was set immediately prior to initialization of the
  125. /// guard, or nullopt if the guard is uninitialized.
  126. std::optional<Device> original_device() const {
  127. return guard_.original_device();
  128. }
  129. /// Returns the most recent device that was set using this device guard,
  130. /// either from construction, or via set_device, if the guard is initialized,
  131. /// or nullopt if the guard is uninitialized.
  132. std::optional<Device> current_device() const {
  133. return guard_.current_device();
  134. }
  135. /// Restore the original MPS device, resetting this guard to uninitialized
  136. /// state.
  137. void reset() {
  138. guard_.reset();
  139. }
  140. private:
  141. c10::impl::InlineOptionalDeviceGuard<MPSGuardImpl> guard_;
  142. };
  143. C10_REGISTER_GUARD_IMPL(MPS, MPSGuardImpl)
  144. } // namespace at::mps