|
15 | 15 | #include <executorch/devtools/etdump/etdump_schema_flatcc_builder.h> |
16 | 16 | #include <executorch/devtools/etdump/etdump_schema_flatcc_reader.h> |
17 | 17 | #include <executorch/devtools/etdump/utils.h> |
| 18 | +#include <executorch/runtime/core/device_allocator.h> |
18 | 19 | #include <executorch/runtime/core/error.h> |
19 | 20 | #include <executorch/runtime/core/exec_aten/exec_aten.h> |
20 | 21 | #include <executorch/runtime/core/exec_aten/util/scalar_type_util.h> |
21 | 22 | #include <executorch/runtime/platform/assert.h> |
22 | 23 |
|
| 24 | +#ifdef USE_ATEN_LIB |
| 25 | +#include <executorch/extension/aten_util/aten_bridge.h> |
| 26 | +#endif |
| 27 | + |
23 | 28 | #include <flatcc/flatcc_types.h> |
24 | 29 |
|
25 | 30 | using ::executorch::aten::Tensor; |
@@ -105,6 +110,75 @@ Result<etdump_Tensor_ref_t> add_tensor_entry( |
105 | 110 | return etdump_Tensor_end(builder_); |
106 | 111 | } |
107 | 112 |
|
| 113 | +// The allocator registry is keyed on the runtime's own device type. In ATen |
| 114 | +// mode a tensor carries the ATen enum instead, which names many more devices |
| 115 | +// than ExecuTorch can register an allocator for. |
| 116 | +Result<::executorch::runtime::etensor::DeviceType> to_runtime_device_type( |
| 117 | + ::executorch::aten::DeviceType type) { |
| 118 | +#ifdef USE_ATEN_LIB |
| 119 | + std::optional<::executorch::runtime::etensor::Device> device = |
| 120 | + ::executorch::extension::torch_to_executorch_device(c10::Device(type)); |
| 121 | + if (!device.has_value()) { |
| 122 | + return Error::NotSupported; |
| 123 | + } |
| 124 | + return device->type(); |
| 125 | +#else |
| 126 | + return type; |
| 127 | +#endif |
| 128 | +} |
| 129 | + |
| 130 | +// Stages a tensor that lives on an accelerator through host memory before |
| 131 | +// handing it to the data sink, which can only read host pointers. A data sink |
| 132 | +// stores the bytes with a plain host read, so handing it the accelerator |
| 133 | +// pointer straight would crash the process. |
| 134 | +Result<long> write_device_tensor(DataSinkBase* data_sink, Tensor tensor) { |
| 135 | + Result<::executorch::runtime::etensor::DeviceType> device_type = |
| 136 | + to_runtime_device_type(tensor.device().type()); |
| 137 | + if (!device_type.ok()) { |
| 138 | + ET_LOG( |
| 139 | + Error, |
| 140 | + "ETDump cannot read a tensor on device type %d", |
| 141 | + static_cast<int>(tensor.device().type())); |
| 142 | + return device_type.error(); |
| 143 | + } |
| 144 | + |
| 145 | + ::executorch::runtime::DeviceAllocator* allocator = |
| 146 | + ::executorch::runtime::get_device_allocator(device_type.get()); |
| 147 | + if (allocator == nullptr) { |
| 148 | + ET_LOG( |
| 149 | + Error, |
| 150 | + "No device allocator registered for device type %d, so a tensor on that device cannot be copied back to host memory", |
| 151 | + static_cast<int>(device_type.get())); |
| 152 | + return Error::NotFound; |
| 153 | + } |
| 154 | + |
| 155 | + const size_t nbytes = tensor.nbytes(); |
| 156 | + void* staging = malloc(nbytes); |
| 157 | + if (staging == nullptr) { |
| 158 | + ET_LOG( |
| 159 | + Error, "Failed to allocate %zu bytes to stage a device tensor", nbytes); |
| 160 | + return Error::MemoryAllocationFailed; |
| 161 | + } |
| 162 | + |
| 163 | + Error copy_error = allocator->copy_device_to_host( |
| 164 | + staging, |
| 165 | + tensor.const_data_ptr(), |
| 166 | + nbytes, |
| 167 | + static_cast<::executorch::runtime::etensor::DeviceIndex>( |
| 168 | + tensor.device().index())); |
| 169 | + if (copy_error != Error::Ok) { |
| 170 | + free(staging); |
| 171 | + return copy_error; |
| 172 | + } |
| 173 | + |
| 174 | + Result<size_t> ret = data_sink->write(staging, nbytes); |
| 175 | + free(staging); |
| 176 | + if (!ret.ok()) { |
| 177 | + return ret.error(); |
| 178 | + } |
| 179 | + return static_cast<long>(ret.get()); |
| 180 | +} |
| 181 | + |
108 | 182 | } // namespace |
109 | 183 |
|
110 | 184 | // Constructor implementation |
@@ -725,6 +799,11 @@ Result<long> ETDumpGen::write_tensor_or_return_error(Tensor tensor) { |
725 | 799 | if (!data_sink_) { |
726 | 800 | return Error::InvalidArgument; |
727 | 801 | } |
| 802 | + |
| 803 | + if (!tensor.device().is_cpu()) { |
| 804 | + return write_device_tensor(data_sink_, tensor); |
| 805 | + } |
| 806 | + |
728 | 807 | Result<size_t> ret = |
729 | 808 | data_sink_->write(tensor.const_data_ptr(), tensor.nbytes()); |
730 | 809 | if (!ret.ok()) { |
|
0 commit comments