ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Sub.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "Sub.h"
18
19#include "Convert.h"
20#include "IRBuilder.h"
21#include "GraphBuilder.h"
22#include "Activation.h"
23
24#include <morph/tflite.h>
25#include <coco/IR/Module.h>
27
29#include <schema_generated.h>
30
31#include <cassert>
32
33using namespace nncc::core::ADT;
34using namespace morph::tflite;
35
36namespace tflimport
37{
38
39void SubGraphBuilder::build(const tflite::Operator *op, GraphBuilderContext *context) const
40{
41 assert(context != nullptr); // check if init(..) is called
42
43 coco::Module *m = context->m();
44 coco::Block *blk = context->block();
45 TensorContext &tensor_context = context->tensor();
46 TensorBags &bags = context->bags();
47
48 IndexVector opinputs = as_index_vector(op->inputs());
49 IndexVector opoutputs = as_index_vector(op->outputs());
50
51 // these are fixed in tflite
52 // input index 0 : left input feature
53 // input index 1 : right input feature
54 // output index 0 : output feature
55 assert(opinputs.size() == 2);
56 assert(opoutputs.size() == 1);
57
58 // Default parameter values are referenced from schema_generated.h
59 auto *params = op->builtin_options_as_SubOptions();
60 tflite::ActivationFunctionType activation = tflite::ActivationFunctionType_NONE;
61
62 if (auto *params = op->builtin_options_as_SubOptions())
63 {
64 activation = params->fused_activation_function();
65 }
66 assert(activation == tflite::ActivationFunctionType_NONE);
67
68 // Construct a vector of input objects
69 std::vector<coco::FeatureObject *> input_objects;
70
71 for (auto &input_index : opinputs)
72 {
73 // Add objects for input feature map
74 const tensor::Shape &input_shape = tensor_context.shape(input_index);
75 coco::FeatureObject *input_obj = m->entity()->object()->create<coco::FeatureObject>();
76 coco::Bag *input_bag = bags.bag(input_index);
77 input_obj->bag(input_bag);
79
80 input_objects.emplace_back(input_obj);
81 }
82
83 // Create an object for an output feature map
84 int const output_index = opoutputs.at(0);
85 const tensor::Shape &output_shape = tensor_context.shape(output_index);
86 coco::FeatureObject *output_obj = m->entity()->object()->create<coco::FeatureObject>();
87 coco::Bag *output_bag = bags.bag(output_index);
88 output_obj->bag(output_bag);
90
91 // Create Load ops
92 auto left_load = op_builder(m).load(input_objects[0]).pop();
93 auto right_load = op_builder(m).load(input_objects[1]).pop();
94
95 // Create a Sub
96 auto coco_sub = m->entity()->op()->create<coco::Sub>();
97
98 coco_sub->left(left_load);
99 coco_sub->right(right_load);
100
101 // Create an Eval instruction
102 auto eval = instr_builder(m).eval(output_obj, coco_sub);
103
104 // Append the instruction to the block
105 blk->instr()->append(eval);
106
107 // TODO activation, e.g., relu
108 assert(params->fused_activation_function() ==
109 tflite::ActivationFunctionType::ActivationFunctionType_NONE);
110}
111
112} // namespace tflimport
OpBuilder op_builder(coco::Module *m)
Definition IRBuilder.h:144
InstrBuilder instr_builder(coco::Module *m)
Definition IRBuilder.h:174
coco::Eval * eval(coco::Object *out, coco::Op *op) const
Create "Eval" instruction with a given "Object" and "Op".
Definition IRBuilder.h:162
OpBuilder & load(coco::Object *obj)
Create "Load" op and push it onto the internal stack.
Definition IRBuilder.h:70
coco::Op * pop(void)
Pop op from the internal stack.
Definition IRBuilder.h:116
A collection of (abstracted) elements of the same type.
Definition Bag.h:48
Op * left(void) const
Definition Op.h:232
A unit of (grouped) instructions.
Definition Block.h:40
InstrList * instr(void)
Definition Block.h:65
void append(Child *child)
static std::unique_ptr< BHWC > create(const nncc::core::ADT::feature::Shape &shape)
FeatureMap values (used in CNN)
const FeatureLayout * layout(void) const
Top-level element of coco IR which represents a neural network.
Definition Module.h:34
coco::Bag * bag(void) const
Definition Object.h:74
Element-wise subtraction.
Definition Ops.h:306
Class to store context to build IR from tflite.
Definition Context.h:133
TensorContext & tensor()
Definition Context.h:152
void build(const tflite::Operator *op, GraphBuilderContext *) const override
Definition Sub.cpp:39
Pre-creates coco:Bags for each operands(tensors)
Definition TensorBags.h:38
coco::Bag * bag(int32_t tensor_id)
Definition TensorBags.h:52
Extracts and holds operand(tensor) information such as name, shape, and type.
Definition Context.h:39
const tensor::Shape & shape(uint32_t tensor_id)
Definition Context.h:44
const luci_interpreter::RuntimeShape output_shape
nncc::core::ADT::feature::Shape as_feature_shape(const nncc::core::ADT::tensor::Shape &)
Definition tflite.cpp:54
std::vector< int32_t > IndexVector
Definition Convert.h:29
IndexVector as_index_vector(const flatbuffers::Vector< int32_t > *array)
Converts flatbuffers::Vector to IndexVector.
Definition Convert.cpp:28
virtual ElemID at(uint32_t b, uint32_t ch, uint32_t row, uint32_t col) const =0