From 2126297e45a505d13398e0b71b88f4bef4ba430c Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EC=9C=A4=EC=A7=80=EC=98=81/On-Device=20Lab=28SR=29/Enginee?= =?utf8?q?r/=EC=82=BC=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Tue, 18 Dec 2018 16:43:39 +0900 Subject: [PATCH] [enco] Add tflite frontend for Sub (#2710) This commit will add the tflite frontend of enco for Sub. Signed-off-by: Jiyoung Yun --- .../frontend/tflite/src/GraphBuilderRegistry.h | 2 + contrib/enco/frontend/tflite/src/Op/Sub.cpp | 110 +++++++++++++++++++++ contrib/enco/frontend/tflite/src/Op/Sub.h | 38 +++++++ 3 files changed, 150 insertions(+) create mode 100644 contrib/enco/frontend/tflite/src/Op/Sub.cpp create mode 100644 contrib/enco/frontend/tflite/src/Op/Sub.h diff --git a/contrib/enco/frontend/tflite/src/GraphBuilderRegistry.h b/contrib/enco/frontend/tflite/src/GraphBuilderRegistry.h index e031b68..d07e0c1 100644 --- a/contrib/enco/frontend/tflite/src/GraphBuilderRegistry.h +++ b/contrib/enco/frontend/tflite/src/GraphBuilderRegistry.h @@ -25,6 +25,7 @@ #include "Op/ReLU.h" #include "Op/ReLU6.h" #include "Op/Reshape.h" +#include "Op/Sub.h" #include #include @@ -73,6 +74,7 @@ private: _builder_map[tflite::BuiltinOperator_RELU] = make_unique(); _builder_map[tflite::BuiltinOperator_RELU6] = make_unique(); _builder_map[tflite::BuiltinOperator_RESHAPE] = make_unique(); + _builder_map[tflite::BuiltinOperator_SUB] = make_unique(); } 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 index 0000000..ea9e14c --- /dev/null +++ b/contrib/enco/frontend/tflite/src/Op/Sub.cpp @@ -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 +#include +#include + +#include +#include + +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 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::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::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->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 index 0000000..580d8ba --- /dev/null +++ b/contrib/enco/frontend/tflite/src/Op/Sub.h @@ -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 + +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__ -- 2.7.4