|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <c10/core/DeviceGuard.h> |
| 4 | +#include <c10/core/impl/DeviceGuardImplInterface.h> |
| 5 | +#include <c10/core/Stream.h> |
| 6 | +#include <c10/core/Event.h> |
| 7 | +#include <c10/core/DeviceType.h> |
| 8 | +#include <c10/util/Optional.h> |
| 9 | + |
| 10 | +namespace c10::extension_device::impl { |
| 11 | + |
| 12 | +struct ExtensionDeviceGuardImpl final : public c10::impl::DeviceGuardImplInterface { |
| 13 | + static constexpr DeviceType static_type = DeviceType::PrivateUse1; // ✅ your backend type |
| 14 | + |
| 15 | + ExtensionDeviceGuardImpl() = default; |
| 16 | + |
| 17 | + explicit ExtensionDeviceGuardImpl(DeviceType t) { |
| 18 | + TORCH_CHECK( |
| 19 | + t == static_type, |
| 20 | + "ExtensionDeviceGuardImpl initialized with non-extension_device DeviceType: ", |
| 21 | + t); |
| 22 | + } |
| 23 | + |
| 24 | + // -------------------------------------------------------------------------- |
| 25 | + // 기본적인 device guard (CPU처럼 동작) |
| 26 | + // -------------------------------------------------------------------------- |
| 27 | + DeviceType type() const override { |
| 28 | + return static_type; |
| 29 | + } |
| 30 | + |
| 31 | + Device exchangeDevice(Device d) const override { |
| 32 | + TORCH_CHECK(d.type() == static_type, "Expected extension_device but got ", d); |
| 33 | + return d; // nothing to exchange, CPU-like |
| 34 | + } |
| 35 | + |
| 36 | + Device getDevice() const override { |
| 37 | + return Device(static_type, 0); |
| 38 | + } |
| 39 | + |
| 40 | + void setDevice(Device d) const override { |
| 41 | + TORCH_CHECK(d.type() == static_type, "Expected extension_device but got ", d); |
| 42 | + } |
| 43 | + |
| 44 | + void uncheckedSetDevice(Device d) const noexcept override {} |
| 45 | + |
| 46 | + DeviceIndex deviceCount() const noexcept override { |
| 47 | + return 1; // pretend single device |
| 48 | + } |
| 49 | + |
| 50 | + // -------------------------------------------------------------------------- |
| 51 | + // Stream handling (동기식이므로 기본 stream만 사용) |
| 52 | + // -------------------------------------------------------------------------- |
| 53 | + Stream getStream(Device d) const override { |
| 54 | + return Stream(Stream::DEFAULT, d); |
| 55 | + } |
| 56 | + |
| 57 | + Stream getNewStream(Device d, int priority = 0) const override { |
| 58 | + return Stream(Stream::DEFAULT, d); |
| 59 | + } |
| 60 | + |
| 61 | + Stream getStreamFromGlobalPool(Device d, bool = false) const override { |
| 62 | + return Stream(Stream::DEFAULT, d); |
| 63 | + } |
| 64 | + |
| 65 | + Stream exchangeStream(Stream s) const override { |
| 66 | + return s; |
| 67 | + } |
| 68 | + |
| 69 | + bool queryStream(const Stream& stream) const override { |
| 70 | + (void)stream; |
| 71 | + return true; |
| 72 | + } |
| 73 | + |
| 74 | + void synchronizeStream(const Stream& stream) const override { |
| 75 | + (void)stream; |
| 76 | + } |
| 77 | + |
| 78 | + void synchronizeDevice(DeviceIndex device_index) const override { |
| 79 | + (void)device_index; |
| 80 | + } |
| 81 | + |
| 82 | + // -------------------------------------------------------------------------- |
| 83 | + // Event handling (전부 no-op) |
| 84 | + // -------------------------------------------------------------------------- |
| 85 | + void destroyEvent(void* event, const DeviceIndex device_index) const noexcept override { |
| 86 | + (void)event; |
| 87 | + (void)device_index; |
| 88 | + } |
| 89 | + |
| 90 | + void record(void** event, const Stream& stream, const DeviceIndex device_index, const EventFlag flag) const override { |
| 91 | + (void)event; |
| 92 | + (void)stream; |
| 93 | + (void)device_index; |
| 94 | + (void)flag; |
| 95 | + } |
| 96 | + |
| 97 | + void block(void* event, const Stream& stream) const override { |
| 98 | + (void)event; |
| 99 | + (void)stream; |
| 100 | + } |
| 101 | + |
| 102 | + bool queryEvent(void* event) const override { |
| 103 | + (void)event; |
| 104 | + return true; |
| 105 | + } |
| 106 | + |
| 107 | + void synchronizeEvent(void* event) const override { |
| 108 | + (void)event; |
| 109 | + } |
| 110 | + |
| 111 | + double elapsedTime(void* start_event, void* end_event, const DeviceIndex device_index) const override { |
| 112 | + (void)start_event; |
| 113 | + (void)end_event; |
| 114 | + (void)device_index; |
| 115 | + return 0.0; |
| 116 | + } |
| 117 | + |
| 118 | + // -------------------------------------------------------------------------- |
| 119 | + // Misc (allocator integration) |
| 120 | + // -------------------------------------------------------------------------- |
| 121 | + void recordDataPtrOnStream(const c10::DataPtr& data_ptr, const Stream& stream) const override { |
| 122 | + (void)data_ptr; |
| 123 | + (void)stream; |
| 124 | + } |
| 125 | +}; |
| 126 | + |
| 127 | +} // namespace c10::extension_device::impl |
0 commit comments