DeviceAccelerator.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
  2. #pragma once
  3. #include <c10/core/CachingDeviceAllocator.h>
  4. #include <c10/core/DeviceCapability.h>
  5. #include <c10/core/DeviceType.h>
  6. #include <c10/macros/Macros.h>
  7. #include <ATen/detail/MTIAHooksInterface.h>
  8. #include <optional>
  9. namespace at::accelerator {
  10. // Note [Accelerator Concept]
  11. // This file defines the top level Accelerator concept for PyTorch.
  12. // A device is an accelerator per the definition here if:
  13. // - It is mutually exclusive with all other accelerators
  14. // - It performs asynchronous compute via a Stream/Event system
  15. // - It provides a set of common APIs as defined by AcceleratorHooksInterface
  16. //
  17. // As of today, accelerator devices are (in no particular order):
  18. // CUDA, MTIA, XPU, HIP, MPS, PrivateUse1
  19. // Ensures that only one accelerator is available (at
  20. // compile time if possible) and return it.
  21. // When checked is true, the returned optional always has a value.
  22. TORCH_API std::optional<c10::DeviceType> getAccelerator(bool checked = false);
  23. // Check if the given device type is an accelerator.
  24. TORCH_API bool isAccelerator(c10::DeviceType device_type);
  25. // Check if the given device type is an accelerator, not the excluded ones.
  26. template <
  27. typename... T,
  28. typename = std::enable_if_t<(std::is_same_v<T, c10::DeviceType> && ...)>>
  29. inline bool isAcceleratorExcluded(
  30. c10::DeviceType device_type,
  31. c10::DeviceType first_excluded,
  32. T... rest_excluded) {
  33. if constexpr (sizeof...(rest_excluded) > 0) {
  34. return device_type != first_excluded &&
  35. isAcceleratorExcluded(device_type, rest_excluded...);
  36. } else {
  37. return device_type != first_excluded && isAccelerator(device_type);
  38. }
  39. }
  40. // Return the number of the device available. Note that this is *REQUIRED* to
  41. // not raise any exception.
  42. TORCH_API c10::DeviceIndex deviceCount();
  43. // Set the current device index to the given device index.
  44. TORCH_API void setDeviceIndex(c10::DeviceIndex device_index);
  45. // Get the current device index.
  46. TORCH_API c10::DeviceIndex getDeviceIndex();
  47. // Set the current stream to a given stream. Note that this API doesn't change
  48. // the current device index.
  49. TORCH_API void setCurrentStream(c10::Stream stream);
  50. // Get the current stream of the given device index.
  51. TORCH_API c10::Stream getCurrentStream(c10::DeviceIndex device_index);
  52. // Wait (by blocking the calling thread) until all the work previously enqueued
  53. // on the given device index has been completed.
  54. TORCH_API void synchronizeDevice(c10::DeviceIndex device_index);
  55. // Set the current device index to the given device_index and return the
  56. // original device index that was active before the change.
  57. TORCH_API c10::DeviceIndex exchangeDevice(c10::DeviceIndex device_index);
  58. // Set the current device index to the given device_index. Avoid creating a new
  59. // context if the context for device_index is not initialized. Return the
  60. // original device index that was active before the change.
  61. TORCH_API c10::DeviceIndex maybeExchangeDevice(c10::DeviceIndex device_index);
  62. // Get the device capability of the given device index.
  63. TORCH_API c10::DeviceCapability getDeviceCapability(
  64. c10::DeviceIndex device_index);
  65. TORCH_API inline void emptyCache() {
  66. const auto device_type = getAccelerator(true).value();
  67. at::getDeviceAllocator(device_type)->emptyCache();
  68. }
  69. TORCH_API inline at::CachingDeviceAllocator::DeviceStats getDeviceStats(
  70. c10::DeviceIndex device_index) {
  71. const auto device_type = getAccelerator(true).value();
  72. return at::getDeviceAllocator(device_type)->getDeviceStats(device_index);
  73. }
  74. TORCH_API inline void resetAccumulatedStats(c10::DeviceIndex device_index) {
  75. const auto device_type = getAccelerator(true).value();
  76. at::getDeviceAllocator(device_type)->resetAccumulatedStats(device_index);
  77. }
  78. TORCH_API inline void resetPeakStats(c10::DeviceIndex device_index) {
  79. const auto device_type = getAccelerator(true).value();
  80. at::getDeviceAllocator(device_type)->resetPeakStats(device_index);
  81. }
  82. TORCH_API inline std::pair<size_t, size_t> getMemoryInfo(
  83. c10::DeviceIndex device_index) {
  84. const auto device_type = getAccelerator(true).value();
  85. return at::getDeviceAllocator(device_type)->getMemoryInfo(device_index);
  86. }
  87. } // namespace at::accelerator
  88. namespace at {
  89. // Keep BC only
  90. using at::accelerator::getAccelerator;
  91. using at::accelerator::isAccelerator;
  92. } // namespace at
  93. #else
  94. #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
  95. #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)