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

GraphBuilder for AvgPool2D operator. More...

#include <MaxPool2D.h>

Collaboration diagram for tflimport::MaxPool2DGraphBuilder:

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 MaxPool2D.h.

Member Function Documentation

◆ build()

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

Implements tflimport::GraphBuilder.

Definition at line 52 of file MaxPool2D.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 coco::Op *coco_load = op_builder(m).load(ifm_obj).pop();
90
91 // Create a MaxPool2D
92 coco::MaxPool2D *coco_maxpool2d = m->entity()->op()->create<coco::MaxPool2D>();
93 const tflite::Pool2DOptions *params = op->builtin_options_as_Pool2DOptions();
94
95 coco_maxpool2d->window()->height(params->filter_height());
96 coco_maxpool2d->window()->width(params->filter_width());
97
98 coco_maxpool2d->stride()->vertical(params->stride_h());
99 coco_maxpool2d->stride()->horizontal(params->stride_w());
100
101 coco::Padding2D padding =
102 pool2D_padding(params, ifm_shape, params->filter_width(), params->filter_height());
103
104 coco_maxpool2d->pad()->top(padding.top());
105 coco_maxpool2d->pad()->bottom(padding.bottom());
106 coco_maxpool2d->pad()->left(padding.left());
107 coco_maxpool2d->pad()->right(padding.right());
108
109 // Link ops
110 coco_maxpool2d->arg(coco_load);
111
112 // Create an Eval instruction
113 coco::Eval *ins = instr_builder(m).eval(ofm_obj, coco_maxpool2d);
114
115 // Append the instruction to the block
116 blk->instr()->append(ins);
117
118 // TODO activation, e.g., relu
119 assert(params->fused_activation_function() ==
120 tflite::ActivationFunctionType::ActivationFunctionType_NONE);
121}
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)
Evaluate an Object from a given Op.
Definition Instrs.h:43
static std::unique_ptr< BHWC > create(const nncc::core::ADT::feature::Shape &shape)
FeatureMap values (used in CNN)
const FeatureLayout * layout(void) const
2D Max Pooling
Definition Ops.h:142
Stride2D * stride(void)
Definition Ops.h:159
Padding2D * pad(void)
Definition Ops.h:163
Window2D * window(void)
Definition Ops.h:155
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
uint32_t vertical(void) const
Definition Stride2D.h:40
uint32_t horizontal(void) const
Definition Stride2D.h:44
Op * arg(uint32_t n) const final
Return N-th argument.
uint32_t height(void) const
Definition Window2D.h:40
uint32_t width(void) const
Definition Window2D.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
Base interface on all supported NN operations.
Definition Op.h:45

References coco::DLinkedList< Child, Parent >::Head::append(), coco::UnaryOp::arg(), 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(), InstrBuilder::eval(), coco::Window2D::height(), coco::Stride2D::horizontal(), coco::Block::instr(), instr_builder(), coco::FeatureObject::layout(), coco::Padding2D::left(), OpBuilder::load(), tflimport::GraphBuilderContext::m(), m, op_builder(), coco::MaxPool2D::pad(), tflimport::pool2D_padding(), OpBuilder::pop(), coco::Padding2D::right(), tflimport::TensorContext::shape(), coco::MaxPool2D::stride(), tflimport::GraphBuilderContext::tensor(), coco::Padding2D::top(), coco::Stride2D::vertical(), coco::Window2D::width(), and coco::MaxPool2D::window().

◆ validate()

bool tflimport::MaxPool2DGraphBuilder::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 MaxPool2D.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: