MPSDevice.h 2.1 KB

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