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_MICRO_MICRO_OP_RESOLVER_H_
16 #define TENSORFLOW_LITE_MICRO_MICRO_OP_RESOLVER_H_
17 
18 #include "tensorflow/lite/c/common.h"
19 #include "tensorflow/lite/core/api/error_reporter.h"
20 #include "tensorflow/lite/core/api/flatbuffer_conversions.h"
21 #include "tensorflow/lite/core/api/op_resolver.h"
22 #include "tensorflow/lite/schema/schema_generated.h"
23 
24 namespace tflite {
25 
26 // This is an interface for the OpResolver for TFLiteMicro. The differences from
27 // the TFLite OpResolver base class are to:
28 //  * explicitly remove support for Op versions
29 //  * allow for finer grained registration of the Builtin Ops to reduce code
30 //    size for TFLiteMicro.
31 //
32 // We need an interface class instead of directly using MicroMutableOpResolver
33 // because MicroMutableOpResolver is a class template with the number of
34 // registered Ops as the template parameter.
35 class MicroOpResolver : public OpResolver {
36  public:
37   typedef TfLiteStatus (*BuiltinParseFunction)(const Operator* op,
38                                                ErrorReporter* error_reporter,
39                                                BuiltinDataAllocator* allocator,
40                                                void** builtin_data);
41 
42   // Returns the Op registration struct corresponding to the enum code from the
43   // flatbuffer schema. Returns nullptr if the op is not found or if op ==
44   // BuiltinOperator_CUSTOM.
45   virtual const TfLiteRegistration* FindOp(BuiltinOperator op) const = 0;
46 
47   // Returns the Op registration struct corresponding to the custom operator by
48   // name.
49   virtual const TfLiteRegistration* FindOp(const char* op) const = 0;
50 
51   // This implementation exists for compatibility with the OpResolver base class
52   // and disregards the version parameter.
FindOp(BuiltinOperator op,int version)53   const TfLiteRegistration* FindOp(BuiltinOperator op,
54                                    int version) const final {
55     return FindOp(op);
56   }
57 
58   // This implementation exists for compatibility with the OpResolver base class
59   // and disregards the version parameter.
FindOp(const char * op,int version)60   const TfLiteRegistration* FindOp(const char* op, int version) const final {
61     return FindOp(op);
62   }
63 
64   // Returns the operator specific parsing function for the OpData for a
65   // BuiltinOperator (if registered), else nullptr.
66   virtual BuiltinParseFunction GetOpDataParser(BuiltinOperator op) const = 0;
67 
~MicroOpResolver()68   ~MicroOpResolver() override {}
69 };
70 
71 }  // namespace tflite
72 
73 #endif  // TENSORFLOW_LITE_MICRO_MICRO_OP_RESOLVER_H_
74