Initialize the kernel (a NN model).
| 2597 | |
| 2598 | // Initialize the kernel (a NN model). |
| 2599 | TfLiteStatus NNAPIDelegateKernel::Init(TfLiteContext* context, |
| 2600 | const TfLiteDelegateParams* params) { |
| 2601 | for (auto node_index : TfLiteIntArrayView(params->nodes_to_replace)) { |
| 2602 | nodes_.push_back(node_index); |
| 2603 | } |
| 2604 | |
| 2605 | const auto delegate_options = |
| 2606 | StatefulNnApiDelegate::GetOptions(params->delegate); |
| 2607 | const char* device_name_ptr = delegate_options.accelerator_name; |
| 2608 | // user specified an acclelerator to use. |
| 2609 | if (nnapi_->android_sdk_version >= kMinSdkVersionForNNAPI12 && |
| 2610 | device_name_ptr != nullptr) { |
| 2611 | nnapi_device_ = GetDeviceHandle(context, device_name_ptr); |
| 2612 | if (nnapi_device_ == nullptr) { |
| 2613 | return kTfLiteError; |
| 2614 | } |
| 2615 | } |
| 2616 | |
| 2617 | // Mark the handle backed tensors. |
| 2618 | tensor_memory_map_ = |
| 2619 | &StatefulNnApiDelegate::GetTensorMemoryMap(params->delegate); |
| 2620 | |
| 2621 | if (!nn_model_) { |
| 2622 | ANeuralNetworksModel* model = nullptr; |
| 2623 | RETURN_TFLITE_ERROR_IF_NN_ERROR( |
| 2624 | context, nnapi_->ANeuralNetworksModel_create(&model)); |
| 2625 | nn_model_.reset(model); |
| 2626 | |
| 2627 | TF_LITE_ENSURE_STATUS( |
| 2628 | BuildGraph(context, params->input_tensors, params->output_tensors)); |
| 2629 | } |
| 2630 | |
| 2631 | if (!nn_compilation_) { |
| 2632 | ANeuralNetworksCompilation* compilation = nullptr; |
| 2633 | if (nnapi_device_ != nullptr) { |
| 2634 | // Compile for the selected accelerator. |
| 2635 | RETURN_TFLITE_ERROR_IF_NN_ERROR( |
| 2636 | context, nnapi_->ANeuralNetworksCompilation_createForDevices( |
| 2637 | nn_model_.get(), &nnapi_device_, 1, &compilation)); |
| 2638 | } else { |
| 2639 | RETURN_TFLITE_ERROR_IF_NN_ERROR( |
| 2640 | context, nnapi_->ANeuralNetworksCompilation_create(nn_model_.get(), |
| 2641 | &compilation)); |
| 2642 | } |
| 2643 | |
| 2644 | auto preference = delegate_options.execution_preference; |
| 2645 | if (preference != |
| 2646 | StatefulNnApiDelegate::Options::ExecutionPreference::kUndefined) { |
| 2647 | const int preference_result = |
| 2648 | nnapi_->ANeuralNetworksCompilation_setPreference(compilation, |
| 2649 | preference); |
| 2650 | if (preference_result != ANEURALNETWORKS_NO_ERROR) { |
| 2651 | nnapi_->ANeuralNetworksCompilation_free(compilation); |
| 2652 | compilation = nullptr; |
| 2653 | } |
| 2654 | RETURN_TFLITE_ERROR_IF_NN_ERROR(context, preference_result); |
| 2655 | } |
| 2656 |
no test coverage detected