ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Div.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019 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 "Div.h"
18
19#include "Convert.h"
20#include "IRBuilder.h"
21#include "GraphBuilder.h"
22#include "Padding.h"
23#include "Activation.h"
24
25#include <morph/tflite.h>
26#include <coco/IR/Module.h>
28
30#include <schema_generated.h>
31
32#include <cassert>
33
34using namespace nncc::core::ADT;
35using namespace morph::tflite;
36
37namespace tflimport
38{
39
40void DivGraphBuilder::build(const tflite::Operator *op, GraphBuilderContext *context) const
41{
42 assert(context != nullptr);
43
44 coco::Module *m = context->m();
45 coco::Block *blk = context->block();
46 TensorContext &tensor_context = context->tensor();
47 TensorBags &bags = context->bags();
48
49 IndexVector opinputs = as_index_vector(op->inputs());
50 IndexVector opoutputs = as_index_vector(op->outputs());
51
52 // these are fixed in tflite
53 // input index 0 : numerator
54 // input index 1 : denominator
55 // output index 0 : result
56 assert(opinputs.size() == 2);
57 assert(opoutputs.size() == 1);
58
59 tflite::ActivationFunctionType activation;
60 if (auto *options = op->builtin_options_as_DivOptions())
61 {
62 activation = options->fused_activation_function();
63 }
64 else
65 {
66 activation = tflite::ActivationFunctionType_NONE;
67 }
68
69 // TODO activation, e.g. ReLU
70 assert(activation == tflite::ActivationFunctionType_NONE);
71
72 auto num_idx = opinputs.at(0);
73 auto denom_idx = opinputs.at(1);
74 auto out_idx = opoutputs.at(0);
75
76 const tensor::Shape &num_shape = tensor_context.shape(num_idx);
77 const tensor::Shape &denom_shape = tensor_context.shape(denom_idx);
78 const tensor::Shape &out_shape = tensor_context.shape(out_idx);
79
80 // TODO Now input/output assumes Feature map, but Div should support generic object type
81 // Create an object for an input
82 auto *num_obj = m->entity()->object()->create<coco::FeatureObject>();
83 auto *num_bag = bags.bag(num_idx);
84 num_obj->bag(num_bag);
85 num_obj->layout(coco::FeatureLayouts::BHWC::create(as_feature_shape(num_shape)));
86
87 auto *denom_obj = m->entity()->object()->create<coco::FeatureObject>();
88 auto *denom_bag = bags.bag(denom_idx);
89 denom_obj->bag(denom_bag);
90 denom_obj->layout(coco::FeatureLayouts::BHWC::create(as_feature_shape(denom_shape)));
91
92 // Create an object for an output
93 auto *out_obj = m->entity()->object()->create<coco::FeatureObject>();
94 auto *out_bag = bags.bag(out_idx);
95 out_obj->bag(out_bag);
96 out_obj->layout(coco::FeatureLayouts::BHWC::create(as_feature_shape(out_shape)));
97
98 // Create a Load ops for each input
99 auto coco_load_num = op_builder(m).load(num_obj).pop();
100 auto coco_load_denom = op_builder(m).load(denom_obj).pop();
101
102 // Create a Div op
103 auto coco_div = m->entity()->op()->create<coco::Div>();
104
105 // Link ops
106 coco_div->left(coco_load_num);
107 coco_div->right(coco_load_denom);
108
109 // Create an Eval instruction
110 auto eval_ins = instr_builder(m).eval(out_obj, coco_div);
111
112 // Append the instruction to the block
113 blk->instr()->append(eval_ins);
114}
115
116} // 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
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)
Element-wise division.
Definition Ops.h:344
static std::unique_ptr< BHWC > create(const nncc::core::ADT::feature::Shape &shape)
FeatureMap values (used in CNN)
Top-level element of coco IR which represents a neural network.
Definition Module.h:34
void build(const tflite::Operator *op, GraphBuilderContext *) const override
Definition Div.cpp:40
Class to store context to build IR from tflite.
Definition Context.h:133
TensorContext & tensor()
Definition Context.h:152
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
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