ONE - On-device Neural Engine
Loading...
Searching...
No Matches
AveragePool2D.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 "AveragePool2D.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
40bool AvgPool2DGraphBuilder::validate(const tflite::Operator *op) const
41{
42 auto const options = op->builtin_options_as_Pool2DOptions();
43
44 if ((options->stride_h() == 0) || (options->stride_w() == 0))
45 {
46 return false;
47 }
48
49 return true;
50}
51
52void AvgPool2DGraphBuilder::build(const tflite::Operator *op, GraphBuilderContext *context) const
53{
54 assert(context != nullptr); // check if init(..) is called
55
56 coco::Module *m = context->m();
57 coco::Block *blk = context->block();
58 TensorContext &tensor_context = context->tensor();
59 TensorBags &bags = context->bags();
60
61 IndexVector opinputs = as_index_vector(op->inputs());
62 IndexVector opoutputs = as_index_vector(op->outputs());
63
64 // these are fixed in tflite
65 // input index 0 : input feature
66 // output index 0 : output feature
67 assert(opinputs.size() == 1);
68 assert(opoutputs.size() == 1);
69
70 int ifm_idx = opinputs.at(0);
71 int ofm_idx = opoutputs.at(0);
72
73 const tensor::Shape &ifm_shape = tensor_context.shape(ifm_idx);
74 const tensor::Shape &ofm_shape = tensor_context.shape(ofm_idx);
75
76 // Create an object for an input feature map
77 coco::FeatureObject *ifm_obj = m->entity()->object()->create<coco::FeatureObject>();
78 coco::Bag *ifm_bag = bags.bag(ifm_idx);
79 ifm_obj->bag(ifm_bag);
81
82 // Create an object for an output feature map
83 coco::FeatureObject *ofm_obj = m->entity()->object()->create<coco::FeatureObject>();
84 coco::Bag *ofm_bag = bags.bag(ofm_idx);
85 ofm_obj->bag(ofm_bag);
87
88 // Create a Load op
89 auto coco_load = op_builder(m).load(ifm_obj).pop();
90
91 // Create a AvgPool2D
92 auto coco_avgpool2d = m->entity()->op()->create<coco::AvgPool2D>();
93 auto *params = op->builtin_options_as_Pool2DOptions();
94
95 // NOTE For Tensorflow lite, PaddingExcluded is needed
97
98 coco_avgpool2d->window()->height(params->filter_height());
99 coco_avgpool2d->window()->width(params->filter_width());
100
101 coco_avgpool2d->stride()->vertical(params->stride_h());
102 coco_avgpool2d->stride()->horizontal(params->stride_w());
103
104 coco::Padding2D padding =
105 pool2D_padding(params, ifm_shape, params->filter_width(), params->filter_height());
106
107 coco_avgpool2d->pad()->top(padding.top());
108 coco_avgpool2d->pad()->bottom(padding.bottom());
109 coco_avgpool2d->pad()->left(padding.left());
110 coco_avgpool2d->pad()->right(padding.right());
111
112 // Link ops
113 coco_avgpool2d->arg(coco_load);
114
115 // Create an Eval instruction
116 auto ins = instr_builder(m).eval(ofm_obj, coco_avgpool2d);
117
118 // Append the instruction to the block
119 blk->instr()->append(ins);
120
121 // TODO activation, e.g., relu
122 assert(params->fused_activation_function() ==
123 tflite::ActivationFunctionType::ActivationFunctionType_NONE);
124}
125
126} // 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
2D Average Pooling
Definition Ops.h:176
Divisor divisor(void) const
Definition Ops.h:199
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
uint32_t left(void) const
Definition Padding2D.h:49
uint32_t right(void) const
Definition Padding2D.h:53
uint32_t top(void) const
Definition Padding2D.h:41
uint32_t bottom(void) const
Definition Padding2D.h:45
void build(const tflite::Operator *op, GraphBuilderContext *) const override
bool validate(const tflite::Operator *op) const override
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
coco::Padding2D pool2D_padding(const tflite::Pool2DOptions *options, const tensor::Shape &ifm_shape, const int filter_w, const int filter_h)
Definition Padding.cpp:81
IndexVector as_index_vector(const flatbuffers::Vector< int32_t > *array)
Converts flatbuffers::Vector to IndexVector.
Definition Convert.cpp:28