1 /* Copyright 2018 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 
16 #include "tensorflow/lite/c/c_api_experimental.h"
17 
18 #include <stdint.h>
19 
20 #include <memory>
21 
22 #include "tensorflow/lite/builtin_ops.h"
23 #include "tensorflow/lite/c/c_api.h"
24 #include "tensorflow/lite/c/c_api_internal.h"
25 #include "tensorflow/lite/interpreter.h"
26 
27 extern "C" {
28 
TfLiteInterpreterResetVariableTensors(TfLiteInterpreter * interpreter)29 TfLiteStatus TfLiteInterpreterResetVariableTensors(
30     TfLiteInterpreter* interpreter) {
31   return interpreter->impl->ResetVariableTensors();
32 }
33 
TfLiteInterpreterOptionsAddBuiltinOp(TfLiteInterpreterOptions * options,TfLiteBuiltinOperator op,const TfLiteRegistration * registration,int32_t min_version,int32_t max_version)34 void TfLiteInterpreterOptionsAddBuiltinOp(
35     TfLiteInterpreterOptions* options, TfLiteBuiltinOperator op,
36     const TfLiteRegistration* registration, int32_t min_version,
37     int32_t max_version) {
38   options->mutable_op_resolver.AddBuiltin(
39       static_cast<tflite::BuiltinOperator>(op), registration, min_version,
40       max_version);
41 }
42 
TfLiteInterpreterCreateWithSelectedOps(const TfLiteModel * model,const TfLiteInterpreterOptions * optional_options)43 TfLiteInterpreter* TfLiteInterpreterCreateWithSelectedOps(
44     const TfLiteModel* model,
45     const TfLiteInterpreterOptions* optional_options) {
46   tflite::MutableOpResolver resolver;
47   return tflite::internal::InterpreterCreateWithOpResolver(
48       model, optional_options, &resolver);
49 }
50 
TfLiteInterpreterOptionsAddCustomOp(TfLiteInterpreterOptions * options,const char * name,const TfLiteRegistration * registration,int32_t min_version,int32_t max_version)51 void TfLiteInterpreterOptionsAddCustomOp(TfLiteInterpreterOptions* options,
52                                          const char* name,
53                                          const TfLiteRegistration* registration,
54                                          int32_t min_version,
55                                          int32_t max_version) {
56   options->mutable_op_resolver.AddCustom(name, registration, min_version,
57                                          max_version);
58 }
59 
TfLiteInterpreterOptionsSetOpResolver(TfLiteInterpreterOptions * options,const TfLiteRegistration * (* find_builtin_op)(void * user_data,TfLiteBuiltinOperator op,int version),const TfLiteRegistration * (* find_custom_op)(void * user_data,const char * op,int version),void * op_resolver_user_data)60 void TfLiteInterpreterOptionsSetOpResolver(
61     TfLiteInterpreterOptions* options,
62     const TfLiteRegistration* (*find_builtin_op)(void* user_data,
63                                                  TfLiteBuiltinOperator op,
64                                                  int version),
65     const TfLiteRegistration* (*find_custom_op)(void* user_data, const char* op,
66                                                 int version),
67     void* op_resolver_user_data) {
68   options->op_resolver_callbacks.find_builtin_op = find_builtin_op;
69   options->op_resolver_callbacks.find_custom_op = find_custom_op;
70   options->op_resolver_callbacks.user_data = op_resolver_user_data;
71 }
72 
TfLiteInterpreterOptionsSetUseNNAPI(TfLiteInterpreterOptions * options,bool enable)73 void TfLiteInterpreterOptionsSetUseNNAPI(TfLiteInterpreterOptions* options,
74                                          bool enable) {
75   options->use_nnapi = enable;
76 }
77 
TfLiteInterpreterOptionsSetEnableDelegateFallback(TfLiteInterpreterOptions * options,bool enable)78 void TfLiteInterpreterOptionsSetEnableDelegateFallback(
79     TfLiteInterpreterOptions* options, bool enable) {
80   options->enable_delegate_fallback = enable;
81 }
82 
83 }  // extern "C"
84