|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +#include <executorch/backends/nvidia/tensorrt/runtime/TensorRTBackend.h> |
| 10 | +#include <executorch/backends/nvidia/tensorrt/runtime/TensorRTBlobHeader.h> |
| 11 | +#include <executorch/backends/nvidia/tensorrt/runtime/TensorRTExecutor.h> |
| 12 | +#include <executorch/runtime/backend/interface.h> |
| 13 | +#include <executorch/runtime/platform/log.h> |
| 14 | + |
| 15 | +namespace executorch { |
| 16 | +namespace backends { |
| 17 | +namespace tensorrt { |
| 18 | + |
| 19 | +using executorch::runtime::ArrayRef; |
| 20 | +using executorch::runtime::Backend; |
| 21 | +using executorch::runtime::BackendExecutionContext; |
| 22 | +using executorch::runtime::BackendInitContext; |
| 23 | +using executorch::runtime::CompileSpec; |
| 24 | +using executorch::runtime::DelegateHandle; |
| 25 | +using executorch::runtime::Error; |
| 26 | +using executorch::runtime::EValue; |
| 27 | +using executorch::runtime::FreeableBuffer; |
| 28 | +using executorch::runtime::MemoryAllocator; |
| 29 | +using executorch::runtime::register_backend; |
| 30 | +using executorch::runtime::Result; |
| 31 | +using executorch::runtime::Span; |
| 32 | + |
| 33 | +namespace { |
| 34 | + |
| 35 | +bool is_tensorrt_available() { |
| 36 | + return true; |
| 37 | +} |
| 38 | + |
| 39 | +} // namespace |
| 40 | + |
| 41 | +bool TensorRTBackend::is_available() const { |
| 42 | + return is_tensorrt_available(); |
| 43 | +} |
| 44 | + |
| 45 | +Result<DelegateHandle*> TensorRTBackend::init( |
| 46 | + BackendInitContext& context, |
| 47 | + FreeableBuffer* processed, |
| 48 | + ArrayRef<CompileSpec> compile_specs) const { |
| 49 | + (void)compile_specs; |
| 50 | + |
| 51 | + if (!is_available()) { |
| 52 | + ET_LOG(Error, "TensorRT backend is not available"); |
| 53 | + return Error::NotSupported; |
| 54 | + } |
| 55 | + |
| 56 | + if (processed == nullptr || processed->data() == nullptr) { |
| 57 | + ET_LOG(Error, "Invalid processed buffer"); |
| 58 | + return Error::InvalidArgument; |
| 59 | + } |
| 60 | + |
| 61 | + const void* blob_data = processed->data(); |
| 62 | + const size_t blob_size = processed->size(); |
| 63 | + |
| 64 | + TensorRTBlobHeader header{}; |
| 65 | + if (!parse_blob_header(blob_data, blob_size, header)) { |
| 66 | + ET_LOG(Error, "Failed to parse TensorRT blob header"); |
| 67 | + return Error::InvalidArgument; |
| 68 | + } |
| 69 | + |
| 70 | + MemoryAllocator* allocator = |
| 71 | + context.get_runtime_allocator(); |
| 72 | + if (allocator == nullptr) { |
| 73 | + ET_LOG(Error, "Failed to get runtime allocator"); |
| 74 | + return Error::InvalidState; |
| 75 | + } |
| 76 | + |
| 77 | + TensorRTExecutor* executor = |
| 78 | + allocator->allocateInstance<TensorRTExecutor>(); |
| 79 | + if (executor == nullptr) { |
| 80 | + ET_LOG(Error, "Failed to allocate TensorRT executor"); |
| 81 | + return Error::MemoryAllocationFailed; |
| 82 | + } |
| 83 | + |
| 84 | + new (executor) TensorRTExecutor(); |
| 85 | + |
| 86 | + Error err = executor->initialize(blob_data, blob_size); |
| 87 | + if (err != Error::Ok) { |
| 88 | + ET_LOG(Error, "Failed to initialize TensorRT executor"); |
| 89 | + executor->~TensorRTExecutor(); |
| 90 | + return err; |
| 91 | + } |
| 92 | + |
| 93 | + processed->Free(); |
| 94 | + |
| 95 | + return static_cast<DelegateHandle*>(executor); |
| 96 | +} |
| 97 | + |
| 98 | +Error TensorRTBackend::execute( |
| 99 | + BackendExecutionContext& context, |
| 100 | + DelegateHandle* handle, |
| 101 | + Span<EValue*> args) const { |
| 102 | + (void)context; |
| 103 | + |
| 104 | + if (handle == nullptr) { |
| 105 | + ET_LOG(Error, "Invalid delegate handle"); |
| 106 | + return Error::InvalidArgument; |
| 107 | + } |
| 108 | + |
| 109 | + auto* executor = static_cast<TensorRTExecutor*>(handle); |
| 110 | + |
| 111 | + if (!executor->is_initialized()) { |
| 112 | + ET_LOG(Error, "Executor not initialized"); |
| 113 | + return Error::InvalidState; |
| 114 | + } |
| 115 | + |
| 116 | + size_t num_inputs = executor->get_num_inputs(); |
| 117 | + size_t num_outputs = executor->get_num_outputs(); |
| 118 | + |
| 119 | + if (num_inputs + num_outputs == 0) { |
| 120 | + ET_LOG(Error, "No inputs or outputs found"); |
| 121 | + return Error::InvalidState; |
| 122 | + } |
| 123 | + |
| 124 | + std::vector<void*> input_buffers; |
| 125 | + std::vector<void*> output_buffers; |
| 126 | + input_buffers.reserve(num_inputs); |
| 127 | + output_buffers.reserve(num_outputs); |
| 128 | + |
| 129 | + size_t tensor_idx = 0; |
| 130 | + for (size_t i = 0; i < args.size(); ++i) { |
| 131 | + EValue* arg = args[i]; |
| 132 | + if (arg == nullptr || !arg->isTensor()) { |
| 133 | + continue; |
| 134 | + } |
| 135 | + |
| 136 | + ::executorch::aten::Tensor tensor = arg->toTensor(); |
| 137 | + void* data_ptr = tensor.mutable_data_ptr(); |
| 138 | + |
| 139 | + if (tensor_idx < num_inputs) { |
| 140 | + input_buffers.push_back(data_ptr); |
| 141 | + } else { |
| 142 | + output_buffers.push_back(data_ptr); |
| 143 | + } |
| 144 | + ++tensor_idx; |
| 145 | + } |
| 146 | + |
| 147 | + if (input_buffers.size() != num_inputs) { |
| 148 | + ET_LOG( |
| 149 | + Error, |
| 150 | + "Input buffer count mismatch: expected %zu, got %zu", |
| 151 | + num_inputs, |
| 152 | + input_buffers.size()); |
| 153 | + return Error::InvalidArgument; |
| 154 | + } |
| 155 | + |
| 156 | + if (output_buffers.size() != num_outputs) { |
| 157 | + ET_LOG( |
| 158 | + Error, |
| 159 | + "Output buffer count mismatch: expected %zu, got %zu", |
| 160 | + num_outputs, |
| 161 | + output_buffers.size()); |
| 162 | + return Error::InvalidArgument; |
| 163 | + } |
| 164 | + |
| 165 | + return executor->execute( |
| 166 | + input_buffers.data(), |
| 167 | + input_buffers.size(), |
| 168 | + output_buffers.data(), |
| 169 | + output_buffers.size()); |
| 170 | +} |
| 171 | + |
| 172 | +void TensorRTBackend::destroy(DelegateHandle* handle) const { |
| 173 | + if (handle != nullptr) { |
| 174 | + auto* executor = static_cast<TensorRTExecutor*>(handle); |
| 175 | + executor->~TensorRTExecutor(); |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +} // namespace tensorrt |
| 180 | +} // namespace backends |
| 181 | +} // namespace executorch |
| 182 | + |
| 183 | +namespace { |
| 184 | +executorch::backends::tensorrt::TensorRTBackend& get_backend() { |
| 185 | + static executorch::backends::tensorrt::TensorRTBackend backend; |
| 186 | + return backend; |
| 187 | +} |
| 188 | +const executorch::runtime::Backend backend_id{"TensorRTBackend", &get_backend()}; |
| 189 | +const auto registered = executorch::runtime::register_backend(backend_id); |
| 190 | +} // namespace |
0 commit comments