| 463 | } |
| 464 | |
| 465 | TfLiteStatus BenchmarkTfLiteModel::Init() { |
| 466 | std::string graph = params_.Get<std::string>("graph"); |
| 467 | model_ = tflite::FlatBufferModel::BuildFromFile(graph.c_str()); |
| 468 | if (!model_) { |
| 469 | TFLITE_LOG(ERROR) << "Failed to mmap model " << graph; |
| 470 | return kTfLiteError; |
| 471 | } |
| 472 | TFLITE_LOG(INFO) << "Loaded model " << graph; |
| 473 | model_->error_reporter(); |
| 474 | TFLITE_LOG(INFO) << "resolved reporter"; |
| 475 | |
| 476 | auto resolver = GetOpResolver(); |
| 477 | |
| 478 | const int32_t num_threads = params_.Get<int32_t>("num_threads"); |
| 479 | tflite::InterpreterBuilder(*model_, *resolver)(&interpreter_, num_threads); |
| 480 | if (!interpreter_) { |
| 481 | TFLITE_LOG(ERROR) << "Failed to construct interpreter"; |
| 482 | return kTfLiteError; |
| 483 | } |
| 484 | |
| 485 | interpreter_->UseNNAPI(params_.Get<bool>("use_legacy_nnapi")); |
| 486 | |
| 487 | delegates_ = GetDelegates(); |
| 488 | for (const auto& delegate : delegates_) { |
| 489 | if (interpreter_->ModifyGraphWithDelegate(delegate.second.get()) != |
| 490 | kTfLiteOk) { |
| 491 | TFLITE_LOG(ERROR) << "Failed to apply " << delegate.first << " delegate."; |
| 492 | return kTfLiteError; |
| 493 | } else { |
| 494 | if (params_.Get<bool>("require_full_delegation")) { |
| 495 | bool fully_delegated = true; |
| 496 | if (interpreter_->execution_plan().size() != 1) { |
| 497 | fully_delegated = false; |
| 498 | } else { |
| 499 | int first_node_id = interpreter_->execution_plan()[0]; |
| 500 | const TfLiteNode first_node = |
| 501 | interpreter_->node_and_registration(first_node_id)->first; |
| 502 | if (delegate.second.get() != first_node.delegate) { |
| 503 | fully_delegated = false; |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | if (!fully_delegated) { |
| 508 | TFLITE_LOG(ERROR) << "Disallowed CPU fallback detected."; |
| 509 | return kTfLiteError; |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | TFLITE_LOG(INFO) << "Applied " << delegate.first << " delegate."; |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | interpreter_->SetAllowFp16PrecisionForFp32(params_.Get<bool>("allow_fp16")); |
| 518 | |
| 519 | auto interpreter_inputs = interpreter_->inputs(); |
| 520 | |
| 521 | if (!inputs_.empty()) { |
| 522 | TFLITE_BENCHMARK_CHECK_EQ(inputs_.size(), interpreter_inputs.size()) |