ONE - On-device Neural Engine
Loading...
Searching...
No Matches
tflimport::AvgPool2DGraphBuilder Class Reference

GraphBuilder for AvgPool2D operator. More...

#include <AveragePool2D.h>

Collaboration diagram for tflimport::AvgPool2DGraphBuilder:

Public Member Functions

bool validate (const tflite::Operator *op) const override
 
void build (const tflite::Operator *op, GraphBuilderContext *) const override
 
- Public Member Functions inherited from tflimport::GraphBuilder
virtual ~GraphBuilder ()
 

Detailed Description

GraphBuilder for AvgPool2D operator.

Definition at line 30 of file AveragePool2D.h.

Member Function Documentation

◆ build()

void tflimport::AvgPool2DGraphBuilder::build ( const tflite::Operator *  op,
GraphBuilderContext context 
) const
overridevirtual

Implements tflimport::GraphBuilder.

Definition at line 52 of file AveragePool2D.cpp.

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}
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
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

References coco::DLinkedList< Child, Parent >::Head::append(), morph::tflite::as_feature_shape(), tflimport::as_index_vector(), tflimport::TensorBags::bag(), coco::Object::bag(), tflimport::GraphBuilderContext::bags(), tflimport::GraphBuilderContext::block(), coco::Padding2D::bottom(), coco::FeatureLayouts::BHWC::create(), coco::AvgPool2D::divisor(), InstrBuilder::eval(), coco::Block::instr(), instr_builder(), coco::FeatureObject::layout(), coco::Padding2D::left(), OpBuilder::load(), tflimport::GraphBuilderContext::m(), m, op_builder(), coco::AvgPool2D::PaddingExcluded, tflimport::pool2D_padding(), OpBuilder::pop(), coco::Padding2D::right(), tflimport::TensorContext::shape(), tflimport::GraphBuilderContext::tensor(), and coco::Padding2D::top().

◆ validate()

bool tflimport::AvgPool2DGraphBuilder::validate ( const tflite::Operator *  ) const
overridevirtual

TODO Declare "validate" method as a pure virtual method

Q: Is it possible to validate T/F Lite model only with this interface?

Reimplemented from tflimport::GraphBuilder.

Definition at line 40 of file AveragePool2D.cpp.

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}

The documentation for this class was generated from the following files: