HIPHooksInterface.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
  2. #pragma once
  3. #include <c10/core/Allocator.h>
  4. #include <c10/util/Exception.h>
  5. #include <c10/util/Registry.h>
  6. #include <ATen/detail/AcceleratorHooksInterface.h>
  7. // NB: Class must live in `at` due to limitations of Registry.h.
  8. namespace at {
  9. // The HIPHooksInterface is an omnibus interface for any HIP functionality
  10. // which we may want to call into from CPU code (and thus must be dynamically
  11. // dispatched, to allow for separate compilation of HIP code). See
  12. // CUDAHooksInterface for more detailed motivation.
  13. struct TORCH_API HIPHooksInterface : AcceleratorHooksInterface {
  14. // This should never actually be implemented, but it is used to
  15. // squelch -Werror=non-virtual-dtor
  16. ~HIPHooksInterface() override = default;
  17. void init() const override {
  18. TORCH_CHECK(false, "Cannot initialize HIP without ATen_hip library.");
  19. }
  20. const Generator& getDefaultGenerator(
  21. [[maybe_unused]] DeviceIndex device_index = -1) const override {
  22. TORCH_CHECK(false, "Cannot initialize HIP without ATen_hip library.");
  23. }
  24. virtual bool hasHIP() const {
  25. return false;
  26. }
  27. virtual c10::DeviceIndex current_device() const {
  28. return -1;
  29. }
  30. bool isPinnedPtr(const void* /*data*/ ) const override {
  31. return false;
  32. }
  33. Allocator* getPinnedMemoryAllocator() const override {
  34. TORCH_CHECK(false, "Pinned memory requires HIP.");
  35. }
  36. virtual int getNumGPUs() const {
  37. return 0;
  38. }
  39. bool hasPrimaryContext(DeviceIndex /*device_index*/ ) const override {
  40. TORCH_CHECK(false, "Cannot check primary context without ATen_hip library.");
  41. }
  42. };
  43. // NB: dummy argument to suppress "ISO C++11 requires at least one argument
  44. // for the "..." in a variadic macro"
  45. struct TORCH_API HIPHooksArgs {};
  46. TORCH_DECLARE_REGISTRY(HIPHooksRegistry, HIPHooksInterface, HIPHooksArgs);
  47. #define REGISTER_HIP_HOOKS(clsname) \
  48. C10_REGISTER_CLASS(HIPHooksRegistry, clsname, clsname)
  49. namespace detail {
  50. TORCH_API const HIPHooksInterface& getHIPHooks();
  51. } // namespace detail
  52. } // namespace at
  53. #else
  54. #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
  55. #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)