• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2017 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_TOCO_TFLITE_BUILTIN_OPERATOR_H_
16 #define TENSORFLOW_LITE_TOCO_TFLITE_BUILTIN_OPERATOR_H_
17 
18 #include "absl/memory/memory.h"
19 #include "tensorflow/lite/toco/tflite/operator.h"
20 
21 namespace toco {
22 
23 namespace tflite {
24 
25 // Builtin operators have special TF Lite objects describing their options.
26 // This class has the boilerplate code for creating those.
27 //
28 // Template arguments:
29 //   - T1 must derive from ::toco::Operator.
30 //   - T2 must be one of TF Lite's objects defining Builtin Options, such as
31 //     ::tflite::Conv2DOptions.
32 template <typename T1, typename T2, ::tflite::BuiltinOptions TfLiteEnum>
33 class BuiltinOperator : public BaseOperator {
34  public:
35   using TocoOperator = T1;
36   using TfLiteOptions = T2;
37 
BuiltinOperator(::tflite::BuiltinOperator op,OperatorType type)38   BuiltinOperator(::tflite::BuiltinOperator op, OperatorType type)
39       : BaseOperator(::tflite::EnumNameBuiltinOperator(op), type),
40         builtin_op_(op) {}
41 
42   // Build the configuration object in the given flatbuffer builder. Return
43   // its offset.
44   virtual flatbuffers::Offset<TfLiteOptions> WriteOptions(
45       const TocoOperator& op,
46       flatbuffers::FlatBufferBuilder* builder) const = 0;
47 
48   // Read options from the TF Lite object and set the corresponding values in
49   // the tf.mini operator.
50   virtual void ReadOptions(const TfLiteOptions& opt,
51                            TocoOperator* op) const = 0;
52 
Serialize(const Operator & op,flatbuffers::FlatBufferBuilder * builder)53   Options Serialize(const Operator& op,
54                     flatbuffers::FlatBufferBuilder* builder) const override {
55     auto options = WriteOptions(static_cast<const TocoOperator&>(op), builder);
56     return Options::Builtin(TfLiteEnum, options.Union());
57   }
58 
Deserialize(const BuiltinOptions * builtin_options,const CustomOptions * custom_options)59   std::unique_ptr<Operator> Deserialize(
60       const BuiltinOptions* builtin_options,
61       const CustomOptions* custom_options) const override {
62     auto op = absl::make_unique<TocoOperator>();
63     auto* options = static_cast<const TfLiteOptions*>(builtin_options);
64     if (options) {
65       ReadOptions(*options, op.get());
66     }
67     return std::unique_ptr<Operator>(op.release());
68   }
69 
GetVersion(const OperatorSignature & op_signature)70   int GetVersion(const OperatorSignature& op_signature) const override {
71     return ::tflite::GetBuiltinOperatorVersion(
72         GetVersioningOpSig(builtin_op_, op_signature));
73   }
74 
builtin_op()75   ::tflite::BuiltinOperator builtin_op() const { return builtin_op_; }
76 
77  private:
78   const ::tflite::BuiltinOperator builtin_op_;
79 };
80 
81 }  // namespace tflite
82 
83 }  // namespace toco
84 
85 #endif  // TENSORFLOW_LITE_TOCO_TFLITE_BUILTIN_OPERATOR_H_
86