1 /* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
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 #ifndef TENSORFLOW_LITE_EXPERIMENTAL_ACCELERATION_CONFIGURATION_NNAPI_PLUGIN_H_
16 #define TENSORFLOW_LITE_EXPERIMENTAL_ACCELERATION_CONFIGURATION_NNAPI_PLUGIN_H_
17 
18 // This file provides the NNApiPlugin class, which implements the
19 // TFLite Delegate Plugin for the NNAPI Delegate.
20 
21 #include <memory>
22 #include <string>
23 
24 #include "absl/memory/memory.h"
25 #include "tensorflow/lite/c/common.h"
26 #include "tensorflow/lite/delegates/nnapi/nnapi_delegate.h"
27 #include "tensorflow/lite/experimental/acceleration/configuration/c/delegate_plugin.h"
28 #include "tensorflow/lite/experimental/acceleration/configuration/configuration_generated.h"
29 #include "tensorflow/lite/experimental/acceleration/configuration/delegate_registry.h"
30 
31 namespace tflite {
32 namespace delegates {
33 
34 class NnapiPlugin : public DelegatePluginInterface {
35  public:
Create()36   TfLiteDelegatePtr Create() override {
37     auto nnapi_delegate =
38         absl::make_unique<tflite::StatefulNnApiDelegate>(options_);
39     return TfLiteDelegatePtr(
40         nnapi_delegate.release(), [](TfLiteDelegate* delegate) {
41           delete static_cast<tflite::StatefulNnApiDelegate*>(delegate);
42         });
43   }
GetDelegateErrno(TfLiteDelegate * from_delegate)44   int GetDelegateErrno(TfLiteDelegate* from_delegate) override {
45     auto nnapi_delegate =
46         static_cast<tflite::StatefulNnApiDelegate*>(from_delegate);
47     return nnapi_delegate->GetNnApiErrno();
48   }
New(const TFLiteSettings & tflite_settings)49   static std::unique_ptr<NnapiPlugin> New(
50       const TFLiteSettings& tflite_settings) {
51     return absl::make_unique<NnapiPlugin>(tflite_settings);
52   }
NnapiPlugin(const TFLiteSettings & tflite_settings)53   explicit NnapiPlugin(const TFLiteSettings& tflite_settings) {
54     const NNAPISettings* nnapi_settings = tflite_settings.nnapi_settings();
55     if (!nnapi_settings) return;
56     if (nnapi_settings->accelerator_name() &&
57         nnapi_settings->accelerator_name()->Length() != 0) {
58       accelerator_ = nnapi_settings->accelerator_name()->str();
59       options_.accelerator_name = accelerator_.c_str();
60     }
61     if (nnapi_settings->cache_directory() &&
62         nnapi_settings->cache_directory()->Length() != 0) {
63       cache_dir_ = nnapi_settings->cache_directory()->str();
64       options_.cache_dir = cache_dir_.c_str();
65     }
66     if (nnapi_settings->model_token() &&
67         nnapi_settings->model_token()->Length() != 0) {
68       model_token_ = nnapi_settings->model_token()->str();
69       options_.model_token = model_token_.c_str();
70     }
71     options_.execution_preference =
72         ConvertExecutionPrefence(nnapi_settings->execution_preference());
73     options_.disallow_nnapi_cpu =
74         !nnapi_settings->allow_nnapi_cpu_on_android_10_plus();
75     options_.execution_priority =
76         ConvertExecutionPriority(nnapi_settings->execution_priority());
77     options_.allow_fp16 = nnapi_settings->allow_fp16_precision_for_fp32();
78   }
Options()79   const tflite::StatefulNnApiDelegate::Options& Options() { return options_; }
80 
81  private:
82   static inline tflite::StatefulNnApiDelegate::Options::ExecutionPreference
ConvertExecutionPrefence(NNAPIExecutionPreference from_compatibility_preference)83   ConvertExecutionPrefence(
84       NNAPIExecutionPreference from_compatibility_preference) {
85     using TflitePreference =
86         tflite::StatefulNnApiDelegate::Options::ExecutionPreference;
87     switch (from_compatibility_preference) {
88       case NNAPIExecutionPreference_NNAPI_LOW_POWER:
89         return TflitePreference::kLowPower;
90       case NNAPIExecutionPreference_NNAPI_FAST_SINGLE_ANSWER:
91         return TflitePreference::kFastSingleAnswer;
92       case NNAPIExecutionPreference_NNAPI_SUSTAINED_SPEED:
93         return TflitePreference::kSustainedSpeed;
94       default:
95         return TflitePreference::kUndefined;
96     }
97   }
98 
ConvertExecutionPriority(NNAPIExecutionPriority from_compatibility_priority)99   static inline int ConvertExecutionPriority(
100       NNAPIExecutionPriority from_compatibility_priority) {
101     switch (from_compatibility_priority) {
102       case NNAPIExecutionPriority_NNAPI_PRIORITY_LOW:
103         return ANEURALNETWORKS_PRIORITY_LOW;
104       case NNAPIExecutionPriority_NNAPI_PRIORITY_MEDIUM:
105         return ANEURALNETWORKS_PRIORITY_MEDIUM;
106       case NNAPIExecutionPriority_NNAPI_PRIORITY_HIGH:
107         return ANEURALNETWORKS_PRIORITY_HIGH;
108       default:
109         return ANEURALNETWORKS_PRIORITY_DEFAULT;
110     }
111   }
112 
113   std::string accelerator_, cache_dir_, model_token_;
114   tflite::StatefulNnApiDelegate::Options options_;
115 };
116 
117 }  // namespace delegates
118 }  // namespace tflite
119 
120 #endif  // TENSORFLOW_LITE_EXPERIMENTAL_ACCELERATION_CONFIGURATION_NNAPI_PLUGIN_H_
121