MPSDevice.h 1.8 KB

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