[enco] Add tflite frontend for Sub (#2710)
author윤지영/On-Device Lab(SR)/Engineer/삼성전자 <jy910.yun@samsung.com>
Tue, 18 Dec 2018 07:43:39 +0000 (16:43 +0900)
committer박종현/On-Device Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Tue, 18 Dec 2018 07:43:39 +0000 (16:43 +0900)
This commit will add the tflite frontend of enco for Sub.

Signed-off-by: Jiyoung Yun <jy910.yun@samsung.com>
contrib/enco/frontend/tflite/src/GraphBuilderRegistry.h
contrib/enco/frontend/tflite/src/Op/Sub.cpp [new file with mode: 0644]
contrib/enco/frontend/tflite/src/Op/Sub.h [new file with mode: 0644]

index e031b68..d07e0c1 100644 (file)
@@ -25,6 +25,7 @@
 #include "Op/ReLU.h"
 #include "Op/ReLU6.h"
 #include "Op/Reshape.h"
+#include "Op/Sub.h"
 
 #include <schema_generated.h>
 #include <stdex/Memory.h>
@@ -73,6 +74,7 @@ private:
     _builder_map[tflite::BuiltinOperator_RELU] = make_unique<ReLUGraphBuilder>();
     _builder_map[tflite::BuiltinOperator_RELU6] = make_unique<ReLU6GraphBuilder>();
     _builder_map[tflite::BuiltinOperator_RESHAPE] = make_unique<ReshapeGraphBuilder>();
+    _builder_map[tflite::BuiltinOperator_SUB] = make_unique<SubGraphBuilder>();
   }
 
 private:
diff --git a/contrib/enco/frontend/tflite/src/Op/Sub.cpp b/contrib/enco/frontend/tflite/src/Op/Sub.cpp
new file mode 100644 (file)
index 0000000..ea9e14c
--- /dev/null
@@ -0,0 +1,110 @@
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "Sub.h"
+
+#include "Convert.h"
+#include "IRBuilder.h"
+#include "GraphBuilder.h"
+#include "Activation.h"
+
+#include <morph/tflite.h>
+#include <coco/IR/Module.h>
+#include <coco/IR/FeatureLayouts.h>
+
+#include <nncc/core/ADT/tensor/Shape.h>
+#include <schema_generated.h>
+
+using namespace nncc::core::ADT;
+using namespace morph::tflite;
+
+namespace tflimport
+{
+
+void SubGraphBuilder::build(const tflite::Operator *op, GraphBuilderContext *context) const
+{
+  assert(context != nullptr); // check if init(..) is called
+
+  coco::Module *m = context->m();
+  coco::Block *blk = context->block();
+  TensorContext &tensor_context = context->tensor();
+  TensorBags &bags = context->bags();
+
+  IndexVector opinputs = as_index_vector(op->inputs());
+  IndexVector opoutputs = as_index_vector(op->outputs());
+
+  // these are fixed in tflite
+  // input index 0 : left input feature
+  // input index 1 : right input feature
+  // output index 0 : output feature
+  assert(opinputs.size() == 2);
+  assert(opoutputs.size() == 1);
+
+  // Default parameter values are referenced from schema_generated.h
+  auto *params = op->builtin_options_as_SubOptions();
+  tflite::ActivationFunctionType activation = tflite::ActivationFunctionType_NONE;
+
+  if (auto *params = op->builtin_options_as_SubOptions())
+  {
+    activation = params->fused_activation_function();
+  }
+  assert(activation == tflite::ActivationFunctionType_NONE);
+
+  // Construct a vector of input objects
+  std::vector<coco::FeatureObject *> input_objects;
+
+  for (auto &input_index : opinputs)
+  {
+    // Add objects for input feature map
+    const tensor::Shape &input_shape = tensor_context.shape(input_index);
+    coco::FeatureObject *input_obj = m->entity()->object()->create<coco::FeatureObject>();
+    coco::Bag *input_bag = bags.bag(input_index);
+    input_obj->bag(input_bag);
+    input_obj->layout(coco::FeatureLayouts::BHWC::create(as_feature_shape(input_shape)));
+
+    input_objects.emplace_back(input_obj);
+  }
+
+  // Create an object for an output feature map
+  int const output_index = opoutputs.at(0);
+  const tensor::Shape &output_shape = tensor_context.shape(output_index);
+  coco::FeatureObject *output_obj = m->entity()->object()->create<coco::FeatureObject>();
+  coco::Bag *output_bag = bags.bag(output_index);
+  output_obj->bag(output_bag);
+  output_obj->layout(coco::FeatureLayouts::BHWC::create(as_feature_shape(output_shape)));
+
+  // Create Load ops
+  auto left_load = op_builder(m).load(input_objects[0]).pop();
+  auto right_load = op_builder(m).load(input_objects[1]).pop();
+
+  // Create a Sub
+  auto coco_sub = m->entity()->op()->create<coco::Sub>();
+
+  coco_sub->left(left_load);
+  coco_sub->right(right_load);
+
+  // Create an Eval instruction
+  auto eval = instr_builder(m).eval(output_obj, coco_sub);
+
+  // Append the instruction to the block
+  blk->instr()->append(eval);
+
+  // TODO activation, e.g., relu
+  assert(params->fused_activation_function() ==
+         tflite::ActivationFunctionType::ActivationFunctionType_NONE);
+}
+
+} // namespace tflimport
diff --git a/contrib/enco/frontend/tflite/src/Op/Sub.h b/contrib/enco/frontend/tflite/src/Op/Sub.h
new file mode 100644 (file)
index 0000000..580d8ba
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __OP_SUB_H__
+#define __OP_SUB_H__
+
+#include "GraphBuilder.h"
+
+#include <schema_generated.h>
+
+namespace tflimport
+{
+
+/**
+ * @brief GraphBuilder for Sub operator
+ */
+class SubGraphBuilder : public GraphBuilder
+{
+public:
+  void build(const tflite::Operator *op, GraphBuilderContext *) const override;
+};
+
+} // namespace tflimport
+
+#endif // __OP_SUB_H__