MPSDevice.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
  2. // Copyright © 2022 Apple Inc.
  3. #pragma once
  4. #include <ATen/Device.h>
  5. #include <c10/core/Allocator.h>
  6. #include <c10/macros/Macros.h>
  7. #include <c10/util/Exception.h>
  8. #ifdef __OBJC__
  9. #include <Foundation/Foundation.h>
  10. #include <Metal/Metal.h>
  11. typedef id<MTLDevice> MTLDevice_t;
  12. #else
  13. typedef void* MTLDevice_t;
  14. #endif
  15. namespace at::mps {
  16. // Helper enum to check if a MPSGraph op is supported in a given macOS version
  17. enum class MacOSVersion : uint32_t {
  18. MACOS_VER_14_4_PLUS = 0,
  19. MACOS_VER_15_0_PLUS,
  20. MACOS_VER_15_1_PLUS,
  21. MACOS_VER_15_2_PLUS,
  22. MACOS_VER_26_0_PLUS,
  23. };
  24. //-----------------------------------------------------------------
  25. // MPSDevice
  26. //
  27. // MPSDevice is a singleton class that returns the default device
  28. //-----------------------------------------------------------------
  29. class TORCH_API MPSDevice {
  30. public:
  31. /**
  32. * MPSDevice should not be cloneable.
  33. */
  34. MPSDevice(MPSDevice& other) = delete;
  35. /**
  36. * MPSDevice should not be assignable.
  37. */
  38. void operator=(const MPSDevice&) = delete;
  39. /**
  40. * Gets single instance of the Device.
  41. */
  42. static MPSDevice* getInstance();
  43. /**
  44. * Returns the single device.
  45. */
  46. MTLDevice_t device() {
  47. return _mtl_device;
  48. }
  49. /**
  50. * Returns whether running on Ventura or newer
  51. */
  52. bool isMacOS13Plus(MacOSVersion version) const;
  53. /**
  54. * Returns device name
  55. */
  56. std::string getName() const;
  57. /**
  58. * Returns number of GPU cores.
  59. * 1 Core = 16 ExecutionUnit x 8 ALU x 24 threads
  60. */
  61. unsigned getCoreCount() const;
  62. ~MPSDevice();
  63. private:
  64. static MPSDevice* _device;
  65. MTLDevice_t _mtl_device;
  66. MPSDevice();
  67. };
  68. TORCH_API bool is_available();
  69. TORCH_API bool is_macos_13_or_newer(MacOSVersion version);
  70. TORCH_API at::Allocator* GetMPSAllocator(bool useSharedAllocator = false);
  71. inline Device getDeviceFromPtr(void* ptr) {
  72. return {c10::DeviceType::MPS, 0};
  73. }
  74. } // namespace at::mps
  75. #else
  76. #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
  77. #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)