• 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_CUSTOM_OPERATOR_H_
16 #define TENSORFLOW_LITE_TOCO_TFLITE_CUSTOM_OPERATOR_H_
17 
18 #include "flatbuffers/flexbuffers.h"
19 #include "absl/memory/memory.h"
20 #include "tensorflow/lite/toco/tflite/operator.h"
21 
22 namespace toco {
23 
24 namespace tflite {
25 
26 // Custom operators have a generic byte buffer describing their options. This
27 // class provides the boilerplate code for populating those options using
28 // flexbuffers. Note that most of toco's operators will likely be supported
29 // as builtin operators in TF Lite.
30 //
31 // Template argument T must derive from ::toco::Operator.
32 template <typename T>
33 class CustomOperator : public BaseOperator {
34  public:
35   using TocoOperator = T;
36   using BaseOperator::BaseOperator;
37 
38   // Populate the given flexbuffer with options obtained from the tf.mini
39   // operator.
WriteOptions(const TocoOperator & op,flexbuffers::Builder * fbb)40   virtual void WriteOptions(const TocoOperator& op,
41                             flexbuffers::Builder* fbb) const {}
42 
43   // Set options in the given tf.mini operator using values from the flexbuffer
44   // map.
ReadOptions(const flexbuffers::Map & m,TocoOperator * op)45   virtual void ReadOptions(const flexbuffers::Map& m, TocoOperator* op) const {}
46 
Serialize(const Operator & op,flatbuffers::FlatBufferBuilder * builder)47   Options Serialize(const Operator& op,
48                     flatbuffers::FlatBufferBuilder* builder) const override {
49     flexbuffers::Builder fbb;
50     fbb.Map(
51         [&]() { WriteOptions(static_cast<const TocoOperator&>(op), &fbb); });
52     fbb.Finish();
53     return Options::Custom(builder->CreateVector(fbb.GetBuffer()));
54   }
55 
Deserialize(const BuiltinOptions * builtin_options,const CustomOptions * custom_options)56   std::unique_ptr<Operator> Deserialize(
57       const BuiltinOptions* builtin_options,
58       const CustomOptions* custom_options) const override {
59     auto op = absl::make_unique<TocoOperator>();
60     if (custom_options) {
61       auto flexbuffer_map =
62           flexbuffers::GetRoot(custom_options->data(), custom_options->size())
63               .AsMap();
64       ReadOptions(flexbuffer_map, op.get());
65     }
66     return std::unique_ptr<Operator>(op.release());
67   }
68 };
69 
70 }  // namespace tflite
71 
72 }  // namespace toco
73 
74 #endif  // TENSORFLOW_LITE_TOCO_TFLITE_CUSTOM_OPERATOR_H_
75