[enco] Support Div for tflite frontend (#2779)
author박천교/On-Device Lab(SR)/Engineer/삼성전자 <ch.bahk@samsung.com>
Thu, 10 Jan 2019 09:26:33 +0000 (18:26 +0900)
committer박종현/On-Device Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Thu, 10 Jan 2019 09:26:33 +0000 (18:26 +0900)
* [enco] Support Div for tflite frontend

This commit extends enco tflite frontend including Div operation

Signed-off-by: Cheongyo Bahk <ch.bahk@samsung.com>
* Rename ifm, ofm indices to 'num', 'denom' and 'out'

* Add activation assertion

* Input shapes may differ in case of broadcasting

* Remove redundant variable d

* Add TODO notice about object type generalization

contrib/enco/frontend/tflite/src/GraphBuilderRegistry.h
contrib/enco/frontend/tflite/src/Op/Div.cpp [new file with mode: 0644]
contrib/enco/frontend/tflite/src/Op/Div.h [new file with mode: 0644]

index d07e0c1..1ae882e 100644 (file)
@@ -26,6 +26,7 @@
 #include "Op/ReLU6.h"
 #include "Op/Reshape.h"
 #include "Op/Sub.h"
+#include "Op/Div.h"
 
 #include <schema_generated.h>
 #include <stdex/Memory.h>
@@ -75,6 +76,7 @@ private:
     _builder_map[tflite::BuiltinOperator_RELU6] = make_unique<ReLU6GraphBuilder>();
     _builder_map[tflite::BuiltinOperator_RESHAPE] = make_unique<ReshapeGraphBuilder>();
     _builder_map[tflite::BuiltinOperator_SUB] = make_unique<SubGraphBuilder>();
+    _builder_map[tflite::BuiltinOperator_DIV] = make_unique<DivGraphBuilder>();
   }
 
 private:
diff --git a/contrib/enco/frontend/tflite/src/Op/Div.cpp b/contrib/enco/frontend/tflite/src/Op/Div.cpp
new file mode 100644 (file)
index 0000000..bffcaca
--- /dev/null
@@ -0,0 +1,116 @@
+/*
+ * 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 "Div.h"
+
+#include "Convert.h"
+#include "IRBuilder.h"
+#include "GraphBuilder.h"
+#include "Padding.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>
+
+#include <cassert>
+
+using namespace nncc::core::ADT;
+using namespace morph::tflite;
+
+namespace tflimport
+{
+
+void DivGraphBuilder::build(const tflite::Operator *op, GraphBuilderContext *context) const
+{
+  assert(context != nullptr);
+
+  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 : numerator
+  // input index 1 : denominator
+  // output index 0 : result
+  assert(opinputs.size() == 2);
+  assert(opoutputs.size() == 1);
+
+  tflite::ActivationFunctionType activation;
+  if (auto *options = op->builtin_options_as_DivOptions())
+  {
+    activation = options->fused_activation_function();
+  }
+  else
+  {
+    activation = tflite::ActivationFunctionType_NONE;
+  }
+
+  // TODO activation, e.g. ReLU
+  assert(activation == tflite::ActivationFunctionType_NONE);
+
+  auto num_idx = opinputs.at(0);
+  auto denom_idx = opinputs.at(1);
+  auto out_idx = opoutputs.at(0);
+
+  const tensor::Shape &num_shape = tensor_context.shape(num_idx);
+  const tensor::Shape &denom_shape = tensor_context.shape(denom_idx);
+  const tensor::Shape &out_shape = tensor_context.shape(out_idx);
+
+  // TODO Now input/output assumes Feature map, but Div should support generic object type
+  // Create an object for an input
+  auto *num_obj = m->entity()->object()->create<coco::FeatureObject>();
+  auto *num_bag = bags.bag(num_idx);
+  num_obj->bag(num_bag);
+  num_obj->layout(coco::FeatureLayouts::BHWC::create(as_feature_shape(num_shape)));
+
+  auto *denom_obj = m->entity()->object()->create<coco::FeatureObject>();
+  auto *denom_bag = bags.bag(denom_idx);
+  denom_obj->bag(denom_bag);
+  denom_obj->layout(coco::FeatureLayouts::BHWC::create(as_feature_shape(denom_shape)));
+
+  // Create an object for an output
+  auto *out_obj = m->entity()->object()->create<coco::FeatureObject>();
+  auto *out_bag = bags.bag(out_idx);
+  out_obj->bag(out_bag);
+  out_obj->layout(coco::FeatureLayouts::BHWC::create(as_feature_shape(out_shape)));
+
+  // Create a Load ops for each input
+  auto coco_load_num = op_builder(m).load(num_obj).pop();
+  auto coco_load_denom = op_builder(m).load(denom_obj).pop();
+
+  // Create a Div op
+  auto coco_div = m->entity()->op()->create<coco::Div>();
+
+  // Link ops
+  coco_div->left(coco_load_num);
+  coco_div->right(coco_load_denom);
+
+  // Create an Eval instruction
+  auto eval_ins = instr_builder(m).eval(out_obj, coco_div);
+
+  // Append the instruction to the block
+  blk->instr()->append(eval_ins);
+}
+
+} // namespace tflimport
diff --git a/contrib/enco/frontend/tflite/src/Op/Div.h b/contrib/enco/frontend/tflite/src/Op/Div.h
new file mode 100644 (file)
index 0000000..053d1a4
--- /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_DIV_H__
+#define __OP_DIV_H__
+
+#include "GraphBuilder.h"
+
+#include <schema_generated.h>
+
+namespace tflimport
+{
+
+/**
+ * @brief GraphBuilder for Div operator
+ */
+class DivGraphBuilder : public GraphBuilder
+{
+public:
+  void build(const tflite::Operator *op, GraphBuilderContext *) const override;
+};
+
+} // namespace tflimport
+
+#endif // __OP_DIV_H__