ONE - On-device Neural Engine
Loading...
Searching...
No Matches
ReLU6.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 "ReLU6.h"
18
19#include "IRBuilder.h"
20#include "GraphBuilder.h"
21
22#include <morph/tflite.h>
23#include <coco/IR/Module.h>
25
27#include <schema_generated.h>
28
29#include <cassert>
30
31using namespace nncc::core::ADT;
32using namespace morph::tflite;
33
34namespace tflimport
35{
36
37void ReLU6GraphBuilder::build(const tflite::Operator *op, GraphBuilderContext *context) const
38{
39 assert(context != nullptr); // check if init(..) is called
40
41 coco::Module *m = context->m();
42 coco::Block *blk = context->block();
43 TensorContext &tensor_context = context->tensor();
44 TensorBags &bags = context->bags();
45
46 IndexVector opinputs = as_index_vector(op->inputs());
47 IndexVector opoutputs = as_index_vector(op->outputs());
48
49 // these are fixed in tflite
50 // input index 0 : input feature
51 // output index 0 : output feature
52 assert(opinputs.size() == 1);
53 assert(opoutputs.size() == 1);
54
55 int ifm_idx = opinputs.at(0);
56 int ofm_idx = opoutputs.at(0);
57
58 const tensor::Shape &ifm_shape = tensor_context.shape(ifm_idx);
59 const tensor::Shape &ofm_shape = tensor_context.shape(ofm_idx);
60
61 // Create an object for an input feature map
62 coco::FeatureObject *ifm_obj = m->entity()->object()->create<coco::FeatureObject>();
63 coco::Bag *ifm_bag = bags.bag(ifm_idx);
64 ifm_obj->bag(ifm_bag);
66
67 // Create an object for an output feature map
68 coco::FeatureObject *ofm_obj = m->entity()->object()->create<coco::FeatureObject>();
69 coco::Bag *ofm_bag = bags.bag(ofm_idx);
70 ofm_obj->bag(ofm_bag);
72
73 // Create a Load op
74 auto coco_load = op_builder(m).load(ifm_obj).pop();
75
76 // Create a ReLU6
77 auto coco_relu6 = m->entity()->op()->create<coco::ReLU6>();
78
79 // Link ops
80 coco_relu6->arg(coco_load);
81
82 // Create an Eval instruction
83 auto eval_ins = instr_builder(m).eval(ofm_obj, coco_relu6);
84
85 // Append the instruction to the block
86 blk->instr()->append(eval_ins);
87}
88
89} // 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
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
Apply ReLU6 over elements.
Definition Ops.h:268
Op * arg(uint32_t n) const final
Return N-th argument.
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 ReLU6.cpp:37
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