|
| 1 | +// Copyright 2025 Google LLC. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "tensorflow/lite/experimental/litert/runtime/accelerators/dispatch/dispatch_accelerator.h" |
| 16 | + |
| 17 | +#include <memory> |
| 18 | +#include <string> |
| 19 | +#include <utility> |
| 20 | + |
| 21 | +#include "absl/strings/string_view.h" |
| 22 | +#include "tensorflow/lite/c/c_api_types.h" |
| 23 | +#include "tensorflow/lite/experimental/litert/c/litert_accelerator.h" |
| 24 | +#include "tensorflow/lite/experimental/litert/c/litert_accelerator_options.h" |
| 25 | +#include "tensorflow/lite/experimental/litert/c/litert_accelerator_registration.h" |
| 26 | +#include "tensorflow/lite/experimental/litert/c/litert_common.h" |
| 27 | +#include "tensorflow/lite/experimental/litert/c/litert_dispatch_delegate.h" |
| 28 | +#include "tensorflow/lite/experimental/litert/c/litert_environment.h" |
| 29 | +#include "tensorflow/lite/experimental/litert/cc/litert_any.h" |
| 30 | +#include "tensorflow/lite/experimental/litert/cc/litert_dispatch_delegate.h" |
| 31 | +#include "tensorflow/lite/experimental/litert/cc/litert_expected.h" |
| 32 | +#include "tensorflow/lite/experimental/litert/cc/litert_macros.h" |
| 33 | +#include "tensorflow/lite/experimental/litert/core/accelerator_model_compilation_data.h" |
| 34 | +#include "tensorflow/lite/experimental/litert/core/environment.h" |
| 35 | + |
| 36 | +namespace litert { |
| 37 | + |
| 38 | +class NpuAccelerator final { |
| 39 | + constexpr static const absl::string_view kName = "NpuAccelerator"; |
| 40 | + // Warning: this should be incremented every time the code of this accelerator |
| 41 | + // is updated according to semanting versioning. |
| 42 | + constexpr static const LiteRtApiVersion kVersion{1, 0, 0}; |
| 43 | + constexpr static const LiteRtHwAcceleratorSet kHwSupport = |
| 44 | + kLiteRtHwAcceleratorNpu; |
| 45 | + |
| 46 | + public: |
| 47 | + explicit NpuAccelerator(std::string library_folder) |
| 48 | + : library_folder_(std::move(library_folder)) {} |
| 49 | + |
| 50 | + struct Deleter { |
| 51 | + void operator()(NpuAccelerator* npu_accelerator) { delete npu_accelerator; } |
| 52 | + }; |
| 53 | + using Ptr = std::unique_ptr<NpuAccelerator, Deleter>; |
| 54 | + |
| 55 | + static Expected<Ptr> Create(std::string library_folder) { |
| 56 | + LITERT_RETURN_IF_ERROR( |
| 57 | + !library_folder.empty(), |
| 58 | + Error(kLiteRtStatusErrorInvalidArgument, |
| 59 | + "Dispatch API implementation library folder was not specified.")); |
| 60 | + return Ptr(new NpuAccelerator(std::move(library_folder))); |
| 61 | + } |
| 62 | + |
| 63 | + // C API |
| 64 | + |
| 65 | + // Deletes the accelerator data. |
| 66 | + static void Destroy(void* npu_accelerator) { |
| 67 | + Deleter()(reinterpret_cast<NpuAccelerator*>(npu_accelerator)); |
| 68 | + } |
| 69 | + |
| 70 | + // Stores the accelerator's name in `name`. |
| 71 | + static LiteRtStatus GetName(LiteRtAccelerator accelerator, |
| 72 | + const char** name) { |
| 73 | + LITERT_ENSURE(accelerator != nullptr, kLiteRtStatusErrorInvalidArgument, |
| 74 | + "Accelerator handle is invalid."); |
| 75 | + LITERT_ENSURE(name != nullptr, kLiteRtStatusErrorInvalidArgument, |
| 76 | + "Name pointer is null."); |
| 77 | + *name = kName.data(); |
| 78 | + return kLiteRtStatusOk; |
| 79 | + } |
| 80 | + |
| 81 | + // Stores the accelerator's version in `version`. |
| 82 | + static LiteRtStatus GetVersion(LiteRtAccelerator accelerator, |
| 83 | + LiteRtApiVersion* version) { |
| 84 | + LITERT_ENSURE(accelerator != nullptr, kLiteRtStatusErrorInvalidArgument, |
| 85 | + "Accelerator handle is invalid."); |
| 86 | + LITERT_ENSURE(version != nullptr, kLiteRtStatusErrorInvalidArgument, |
| 87 | + "Version pointer is null."); |
| 88 | + *version = kVersion; |
| 89 | + return kLiteRtStatusOk; |
| 90 | + } |
| 91 | + |
| 92 | + // Stores the accelerator's hardware support in `hw_set`. |
| 93 | + static LiteRtStatus GetHardwareSupport(LiteRtAccelerator accelerator, |
| 94 | + LiteRtHwAcceleratorSet* hw_set) { |
| 95 | + LITERT_ENSURE(accelerator != nullptr, kLiteRtStatusErrorInvalidArgument, |
| 96 | + "Accelerator handle is invalid."); |
| 97 | + LITERT_ENSURE(hw_set != nullptr, kLiteRtStatusErrorInvalidArgument, |
| 98 | + "Harware support pointer is null."); |
| 99 | + *hw_set = kHwSupport; |
| 100 | + return kLiteRtStatusOk; |
| 101 | + } |
| 102 | + |
| 103 | + // Goes through the options in the linked list and returns the model |
| 104 | + // compilation data if it exists. |
| 105 | + static Expected<const litert::ModelCompilationData*> GetModelCompilationData( |
| 106 | + LiteRtAcceleratorCompilationOptions options) { |
| 107 | + while (options) { |
| 108 | + if (options->identifier == litert::ModelCompilationData::kIdentifier) { |
| 109 | + return reinterpret_cast<litert::ModelCompilationData*>(options); |
| 110 | + } |
| 111 | + LiteRtGetNextAcceleratorCompilationOptions(&options); |
| 112 | + } |
| 113 | + return Unexpected(kLiteRtStatusErrorNotFound, |
| 114 | + "Could not retrieve mode compilation data."); |
| 115 | + } |
| 116 | + |
| 117 | + // Creates a Dispatch delegate instance. |
| 118 | + static LiteRtStatus CreateDelegate( |
| 119 | + LiteRtAccelerator accelerator, |
| 120 | + LiteRtAcceleratorCompilationOptions options, void** delegate) { |
| 121 | + LITERT_ENSURE(delegate != nullptr, kLiteRtStatusErrorInvalidArgument, |
| 122 | + "Delegate pointer is null."); |
| 123 | + LITERT_ENSURE(accelerator != nullptr, kLiteRtStatusErrorInvalidArgument, |
| 124 | + "Accelerator handle is invalid."); |
| 125 | + LITERT_ENSURE(accelerator->env != nullptr, |
| 126 | + kLiteRtStatusErrorInvalidArgument, |
| 127 | + "Accelerator is not registered to an environment."); |
| 128 | + |
| 129 | + LITERT_ASSIGN_OR_RETURN( |
| 130 | + const litert::ModelCompilationData* compilation_data, |
| 131 | + GetModelCompilationData(options)); |
| 132 | + const char* allocation_base = compilation_data->allocation_base; |
| 133 | + |
| 134 | + LITERT_ENSURE(allocation_base != nullptr, kLiteRtStatusErrorRuntimeFailure, |
| 135 | + "No model allocation was passed by the runtime."); |
| 136 | + |
| 137 | + auto dispatch_delegate_options = |
| 138 | + litert::CreateDispatchDelegateOptionsPtr(*accelerator->env); |
| 139 | + LITERT_ENSURE(dispatch_delegate_options != nullptr, |
| 140 | + kLiteRtStatusErrorRuntimeFailure, |
| 141 | + "Dispatch delegate options failed to be created."); |
| 142 | + |
| 143 | + LITERT_ENSURE( |
| 144 | + LiteRtDispatchDelegateAddAllocBaseOption( |
| 145 | + dispatch_delegate_options.get(), allocation_base) == kTfLiteOk, |
| 146 | + kLiteRtStatusErrorRuntimeFailure, |
| 147 | + "Could not add allocation base to dispatch delegate options."); |
| 148 | + |
| 149 | + auto dispatch_delegate = litert::CreateDispatchDelegatePtr( |
| 150 | + *accelerator->env, std::move(dispatch_delegate_options)); |
| 151 | + LITERT_ENSURE(dispatch_delegate != nullptr, |
| 152 | + kLiteRtStatusErrorRuntimeFailure, |
| 153 | + "Dispatch delegate failed to be created."); |
| 154 | + |
| 155 | + *delegate = dispatch_delegate.release(); |
| 156 | + return kLiteRtStatusOk; |
| 157 | + } |
| 158 | + |
| 159 | + // Destroys a Dispatch delegate instance. |
| 160 | + static void DestroyDelegate(void* delegate) { |
| 161 | + LiteRtDestroyDispatchDelegate( |
| 162 | + reinterpret_cast<TfLiteOpaqueDelegate*>(delegate)); |
| 163 | + } |
| 164 | + |
| 165 | + private: |
| 166 | + // Note: we do not directly use the option structure because we want to copy |
| 167 | + // and own all the option data. |
| 168 | + |
| 169 | + // Folder to the Dispatch API implementation shared library. |
| 170 | + std::string library_folder_; |
| 171 | +}; |
| 172 | + |
| 173 | +namespace { |
| 174 | + |
| 175 | +struct AcceleratorDestructor { |
| 176 | + void operator()(LiteRtAccelerator accelerator) { |
| 177 | + LiteRtDestroyAccelerator(accelerator); |
| 178 | + } |
| 179 | +}; |
| 180 | + |
| 181 | +using AcceleratorGuard = |
| 182 | + std::unique_ptr<std::pointer_traits<LiteRtAccelerator>::element_type, |
| 183 | + AcceleratorDestructor>; |
| 184 | + |
| 185 | +} // namespace |
| 186 | +} // namespace litert |
| 187 | + |
| 188 | +extern "C" { |
| 189 | + |
| 190 | +LiteRtStatus LiteRtRegisterNpuAccelerator( |
| 191 | + LiteRtEnvironmentT* environment, LiteRtNpuAcceleratorOptions* options) { |
| 192 | + LITERT_ENSURE(environment != nullptr, kLiteRtStatusErrorInvalidArgument, |
| 193 | + "accelerator handle is invalid"); |
| 194 | + LiteRtAccelerator accelerator_handle; |
| 195 | + LITERT_RETURN_IF_ERROR(LiteRtCreateAccelerator(&accelerator_handle)); |
| 196 | + litert::AcceleratorGuard accelerator(accelerator_handle); |
| 197 | + |
| 198 | + LiteRtSetAcceleratorGetName(accelerator.get(), |
| 199 | + litert::NpuAccelerator::GetName); |
| 200 | + LiteRtSetAcceleratorGetVersion(accelerator.get(), |
| 201 | + litert::NpuAccelerator::GetVersion); |
| 202 | + LiteRtSetAcceleratorGetHardwareSupport( |
| 203 | + accelerator.get(), litert::NpuAccelerator::GetHardwareSupport); |
| 204 | + |
| 205 | + LiteRtSetDelegateFunction(accelerator.get(), |
| 206 | + litert::NpuAccelerator::CreateDelegate, |
| 207 | + litert::NpuAccelerator::DestroyDelegate); |
| 208 | + |
| 209 | + std::string library_folder; |
| 210 | + if (options && options->library_folder) { |
| 211 | + library_folder = options->library_folder; |
| 212 | + } |
| 213 | + // Check the environment options if the library folder wasn't set in the |
| 214 | + // options. |
| 215 | + if (library_folder.empty()) { |
| 216 | + if (auto env_library_folder = |
| 217 | + environment->GetOption(kLiteRtEnvOptionTagDispatchLibraryDir); |
| 218 | + env_library_folder.has_value()) { |
| 219 | + LITERT_ASSIGN_OR_RETURN( |
| 220 | + library_folder, litert::Get<std::string>(env_library_folder.value())); |
| 221 | + } |
| 222 | + } |
| 223 | + |
| 224 | + LITERT_ASSIGN_OR_RETURN( |
| 225 | + auto accelerator_impl, |
| 226 | + litert::NpuAccelerator::Create(std::move(library_folder))); |
| 227 | + |
| 228 | + LITERT_RETURN_IF_ERROR(LiteRtRegisterAccelerator( |
| 229 | + environment, accelerator.release(), accelerator_impl.release(), |
| 230 | + litert::NpuAccelerator::Destroy)); |
| 231 | + return kLiteRtStatusOk; |
| 232 | +} |
| 233 | + |
| 234 | +} // extern "C" |
0 commit comments