HIPHooksInterface.h 2.0 KB

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