| 259 | } |
| 260 | |
| 261 | TfLiteStatus DelegatePrepare(TfLiteContext* context, TfLiteDelegate* delegate) { |
| 262 | const TfLiteRegistration kRegistration = { |
| 263 | // .init |
| 264 | [](TfLiteContext* context, const char* buffer, size_t) -> void* { |
| 265 | const auto* params = |
| 266 | reinterpret_cast<const TfLiteDelegateParams*>(buffer); |
| 267 | auto* gpu_delegate = GetDelegate(params->delegate); |
| 268 | // Everything below should happen in prepare function call, but TFLite |
| 269 | // for whatever reason forbids that. |
| 270 | const auto status = gpu_delegate->Prepare(context, params); |
| 271 | if (!status.ok()) { |
| 272 | context->ReportError(context, "TfLiteGpuDelegate Init: %s", |
| 273 | status.error_message().c_str()); |
| 274 | return nullptr; |
| 275 | } |
| 276 | return gpu_delegate; |
| 277 | }, |
| 278 | // .free |
| 279 | [](TfLiteContext*, void* buffer) -> void {}, |
| 280 | // .prepare |
| 281 | [](TfLiteContext* context, TfLiteNode* node) -> TfLiteStatus { |
| 282 | if (!node->user_data) { |
| 283 | context->ReportError( |
| 284 | context, |
| 285 | "TfLiteGpuDelegate Prepare: delegate is not initialized"); |
| 286 | return kTfLiteError; |
| 287 | } |
| 288 | // TODO(akulik): tflite tensors are not allocated here either. It would |
| 289 | // be good to set inputs and outputs only once here instead of setting |
| 290 | // them every time in .invoke. |
| 291 | return kTfLiteOk; |
| 292 | }, |
| 293 | // .invoke |
| 294 | [](TfLiteContext* context, TfLiteNode* node) -> TfLiteStatus { |
| 295 | const auto status = GetDelegate(node)->Invoke(context); |
| 296 | if (!status.ok()) { |
| 297 | context->ReportError(context, "TfLiteGpuDelegate Invoke: %s", |
| 298 | status.error_message().c_str()); |
| 299 | return kTfLiteError; |
| 300 | } |
| 301 | return kTfLiteOk; |
| 302 | }, |
| 303 | nullptr, // .profiling_string |
| 304 | 0, // .builtin_code |
| 305 | "TfLiteGpuDelegate_New", // .custom_name |
| 306 | 1, // .version |
| 307 | }; |
| 308 | TfLiteIntArray* ops_to_replace = GetOpsToReplace(context); |
| 309 | const auto status = context->ReplaceNodeSubsetsWithDelegateKernels( |
| 310 | context, kRegistration, ops_to_replace, delegate); |
| 311 | TfLiteIntArrayFree(ops_to_replace); |
| 312 | return status; |
| 313 | } |
| 314 | |
| 315 | } // namespace |
| 316 | } // namespace cl |
nothing calls this directly
no test coverage detected