Skip to content

Commit bf6f488

Browse files
jdduketensorflower-gardener
authored andcommitted
Mark Interpreter:UseNNAPI(bool) deprecated
Prefer using the NnApiDelegate() API directly. The current API is unreliable and causes issues with the first inference execution. PiperOrigin-RevId: 325496322 Change-Id: I44c8fc04bcd08ce5b92e22cd170e075c0abbaecf
1 parent 874db4d commit bf6f488

File tree

6 files changed

+19
-4
lines changed

6 files changed

+19
-4
lines changed

RELEASE.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@
111111
string to be joined is empty.
112112
* `TFLiteConverter`:
113113
* Support optional flags `inference_input_type` and `inference_output_type` for full integer quantized models. This allows users to modify the model input and output type to integer types (`tf.int8`, `tf.uint8`) instead of defaulting to float type (`tf.float32`).
114+
* Deprecate `Interpreter::UseNNAPI(bool)` C++ API
115+
* Prefer using `NnApiDelegate()` and related delegate configuration methods directly.
114116
* <ADD RELEASE NOTES HERE>
115117
* `tf.random`:
116118
* <ADD RELEASE NOTES HERE>

tensorflow/lite/c/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ cc_library(
6262
"//tensorflow/lite:framework",
6363
"//tensorflow/lite:version",
6464
"//tensorflow/lite/core/api",
65+
"//tensorflow/lite/delegates/nnapi:nnapi_delegate",
6566
"//tensorflow/lite/kernels:builtin_ops",
6667
],
6768
alwayslink = 1,

tensorflow/lite/c/c_api.cc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
#include <memory>
1818

1919
#include "tensorflow/lite/c/c_api_internal.h"
20+
#include "tensorflow/lite/delegates/nnapi/nnapi_delegate.h"
2021
#include "tensorflow/lite/error_reporter.h"
2122
#include "tensorflow/lite/interpreter.h"
2223
#include "tensorflow/lite/kernels/register.h"
@@ -123,13 +124,18 @@ TfLiteInterpreter* TfLiteInterpreterCreate(
123124
}
124125

125126
if (optional_options) {
126-
interpreter->UseNNAPI(optional_options->use_nnapi);
127-
128127
if (optional_options->num_threads !=
129128
TfLiteInterpreterOptions::kDefaultNumThreads) {
130129
interpreter->SetNumThreads(optional_options->num_threads);
131130
}
132131

132+
if (optional_options->use_nnapi) {
133+
if (interpreter->ModifyGraphWithDelegate(tflite::NnApiDelegate()) !=
134+
kTfLiteOk) {
135+
return nullptr;
136+
}
137+
}
138+
133139
for (auto* delegate : optional_options->delegates) {
134140
if (interpreter->ModifyGraphWithDelegate(delegate) != kTfLiteOk) {
135141
return nullptr;

tensorflow/lite/delegates/gpu/common/testing/tflite_model_reader.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ absl::Status BuildFromFlatBuffer(const tflite::FlatBufferModel& flatbuffer,
7979
if (interpreter_builder(&interpreter) != kTfLiteOk || !interpreter) {
8080
return absl::InternalError("Unable to prepare TfLite interpreter.");
8181
}
82-
interpreter->UseNNAPI(false);
8382
TfLiteDelegate delegate;
8483
delegate.data_ = graph;
8584
delegate.flags = kTfLiteDelegateFlagsNone;

tensorflow/lite/interpreter.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,12 @@ TfLiteStatus Interpreter::SetExecutionPlan(const std::vector<int>& new_plan) {
300300
return primary_subgraph().SetExecutionPlan(new_plan);
301301
}
302302

303-
void Interpreter::UseNNAPI(bool enable) { primary_subgraph().UseNNAPI(enable); }
303+
void Interpreter::UseNNAPI(bool enable) {
304+
TFLITE_LOG_PROD_ONCE(TFLITE_LOG_INFO,
305+
"Interpreter::UseNNAPI() is deprecated. Use "
306+
"tflite::NnApiDelegate() directly instead.");
307+
primary_subgraph().UseNNAPI(enable);
308+
}
304309

305310
TfLiteStatus Interpreter::SetNumThreads(int num_threads) {
306311
if (num_threads < -1) {

tensorflow/lite/interpreter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,8 @@ class Interpreter {
365365
TfLiteStatus Invoke();
366366

367367
/// Enable or disable the NN API (true to enable)
368+
/// NOTE: This API is deprecated, prefer using the NNAPI delegate directly.
369+
/// This method will be removed in a future release.
368370
void UseNNAPI(bool enable);
369371

370372
/// Set the number of threads available to the interpreter.

0 commit comments

Comments
 (0)