MPSHooks.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
  2. // Copyright © 2022 Apple Inc.
  3. #pragma once
  4. #include <ATen/Generator.h>
  5. #include <ATen/detail/MPSHooksInterface.h>
  6. #include <ATen/mps/MPSEvent.h>
  7. #include <optional>
  8. namespace at::mps {
  9. // The real implementation of MPSHooksInterface
  10. struct MPSHooks : public at::MPSHooksInterface {
  11. MPSHooks(at::MPSHooksArgs) {}
  12. void init() const override;
  13. // MPSDevice interface
  14. bool hasMPS() const override;
  15. bool isOnMacOSorNewer(unsigned major, unsigned minor) const override;
  16. Device getDeviceFromPtr(void* data) const override;
  17. // MPSGeneratorImpl interface
  18. const Generator& getDefaultGenerator(
  19. DeviceIndex device_index = -1) const override;
  20. Generator getNewGenerator(DeviceIndex device_index = -1) const override;
  21. // MPSStream interface
  22. void deviceSynchronize() const override;
  23. void commitStream() const override;
  24. void* getCommandBuffer() const override;
  25. void* getDispatchQueue() const override;
  26. // MPSAllocator interface
  27. Allocator* getMPSDeviceAllocator() const override;
  28. void emptyCache() const override;
  29. size_t getCurrentAllocatedMemory() const override;
  30. size_t getDriverAllocatedMemory() const override;
  31. size_t getRecommendedMaxMemory() const override;
  32. void setMemoryFraction(double ratio) const override;
  33. bool isPinnedPtr(const void* data) const override;
  34. Allocator* getPinnedMemoryAllocator() const override;
  35. // MPSProfiler interface
  36. void profilerStartTrace(const std::string& mode, bool waitUntilCompleted)
  37. const override;
  38. void profilerStopTrace() const override;
  39. // MPSEvent interface
  40. uint32_t acquireEvent(bool enable_timing) const override;
  41. void releaseEvent(uint32_t event_id) const override;
  42. void recordEvent(uint32_t event_id) const override;
  43. void waitForEvent(uint32_t event_id) const override;
  44. void synchronizeEvent(uint32_t event_id) const override;
  45. bool queryEvent(uint32_t event_id) const override;
  46. double elapsedTimeOfEvents(uint32_t start_event_id, uint32_t end_event_id)
  47. const override;
  48. bool isBuilt() const override {
  49. return true;
  50. }
  51. bool isAvailable() const override {
  52. return hasMPS();
  53. }
  54. bool hasPrimaryContext(DeviceIndex device_index) const override {
  55. // When MPS is available, it is always in use for the one device.
  56. return true;
  57. }
  58. };
  59. } // namespace at::mps
  60. #else
  61. #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
  62. #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)