Home | History | Annotate | Download | only in tflite
      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_CONTRIB_LITE_TOCO_TFLITE_BUILTIN_OPERATOR_H_
     16 #define TENSORFLOW_CONTRIB_LITE_TOCO_TFLITE_BUILTIN_OPERATOR_H_
     17 
     18 #include "absl/memory/memory.h"
     19 #include "tensorflow/contrib/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 
     38   BuiltinOperator(::tflite::BuiltinOperator op, OperatorType type)
     39       : BaseOperator(::tflite::EnumNameBuiltinOperator(op), type) {}
     40 
     41   // Build the configuration object in the given flatbuffer builder. Return
     42   // its offset.
     43   virtual flatbuffers::Offset<TfLiteOptions> WriteOptions(
     44       const TocoOperator& op,
     45       flatbuffers::FlatBufferBuilder* builder) const = 0;
     46 
     47   // Read options from the TF Lite object and set the corresponding values in
     48   // the tf.mini operator.
     49   virtual void ReadOptions(const TfLiteOptions& opt,
     50                            TocoOperator* op) const = 0;
     51 
     52   Options Serialize(const Operator& op,
     53                     flatbuffers::FlatBufferBuilder* builder) const override {
     54     auto options = WriteOptions(static_cast<const TocoOperator&>(op), builder);
     55     return Options::Builtin(TfLiteEnum, options.Union());
     56   }
     57 
     58   std::unique_ptr<Operator> Deserialize(
     59       const BuiltinOptions* builtin_options,
     60       const CustomOptions* custom_options) const override {
     61     auto op = absl::make_unique<TocoOperator>();
     62     auto* options = static_cast<const TfLiteOptions*>(builtin_options);
     63     if (options) {
     64       ReadOptions(*options, 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_CONTRIB_LITE_TOCO_TFLITE_BUILTIN_OPERATOR_H_
     75