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

GraphBuilder for DepthwiseConv2D operator. More...

#include <DepthwiseConv2D.h>

Collaboration diagram for tflimport::DepthwiseConv2DGraphBuilder:

Public Member Functions

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

Detailed Description

GraphBuilder for DepthwiseConv2D operator.

Definition at line 30 of file DepthwiseConv2D.h.

Member Function Documentation

◆ build()

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

Implements tflimport::GraphBuilder.

Definition at line 54 of file DepthwiseConv2D.cpp.

56{
57 assert(context != nullptr);
58
59 // preparation
60 coco::Module *m = context->m();
61 coco::Data *d = context->d();
62 coco::Block *blk = context->block();
63 TensorContext &tensor_context = context->tensor();
64 TensorBags &bags = context->bags();
65 TflBufferContext &buffer_context = context->buffer();
66 const tflite::SubGraph *graph = context->graph();
67 IndexVector opinputs = as_index_vector(op->inputs());
68 IndexVector opoutputs = as_index_vector(op->outputs());
69
70 // these are fixed in tflite
71 // input index 0 : input feature
72 // input index 1 : kernel
73 // input index 2 : bias (optional)
74 bool hasBias = (opinputs.size() == 3);
75 assert(opinputs.size() == 2 || hasBias);
76 assert(opoutputs.size() == 1);
77
78 int ifm_idx = opinputs.at(0);
79 int ker_idx = opinputs.at(1);
80 int ofm_idx = opoutputs.at(0);
81
82 const tensor::Shape &ifm_shape = tensor_context.shape(ifm_idx);
83 const tensor::Shape &ofm_shape = tensor_context.shape(ofm_idx);
84 tensor::Shape &ker_shape = const_cast<tensor::Shape &>(tensor_context.shape(ker_idx));
85
86 assert(ifm_shape.rank() == 4);
87 assert(ofm_shape.rank() == 4);
88 assert(ker_shape.rank() == 4);
89
90 assert(ker_shape.dim(0) == 1); // value > 1 was not tested. This value seems 1 in DepthwiseConv2D
91 assert(ifm_shape.dim(3) == ofm_shape.dim(3));
92 assert(ofm_shape.dim(3) == ker_shape.dim(3));
93
94 // Create an input feature map object
95 auto *ifm_obj = m->entity()->object()->create<coco::FeatureObject>();
96 auto *ifm_bag = bags.bag(ifm_idx);
97 ifm_obj->bag(ifm_bag);
98 ifm_obj->layout(coco::FeatureLayouts::BHWC::create(as_feature_shape(ifm_shape)));
99
100 // Create an an output feature map object
101 auto *ofm_obj = m->entity()->object()->create<coco::FeatureObject>();
102 auto *ofm_bag = bags.bag(ofm_idx);
103 ofm_obj->bag(ofm_bag);
104 ofm_obj->layout(coco::FeatureLayouts::BHWC::create(as_feature_shape(ofm_shape)));
105
106 // Create an kernel object
107 auto *ker_obj = m->entity()->object()->create<coco::KernelObject>();
108 auto *ker_bag = bags.bag(ker_idx);
109 ker_obj->bag(ker_bag);
110
111 // Adjust tflite kernel shape [1, h, w, channel_out] for coco::Kernel.
112 // coco::Kernel will have kernel.count = channel_out, kernel.depth = 1 ( == ker_shape.dim(0))
113 kernel::Shape new_shape{ker_shape.dim(3), 1, ker_shape.dim(1), ker_shape.dim(2)};
114 ker_obj->layout(coco::KernelLayouts::NHWC::create(new_shape));
115
116 // Create a kernel overlay for the kernel object
117 // TODO : support for other types
118 d->f32()->allocate(ker_bag);
119
120 TflBufferContext::TflBuffer<float> buffer = buffer_context.tensor_buffer<float>(graph, ker_idx);
121
122 auto ker_spn = d->f32()->weight(ker_bag);
123
124 // Copy data from tflBuffer of [1, h, w, channel_out] shape to coco::Data, which will be accessed
125 // by coco::KernelLayouts::NHWC
126 for (auto n = 0; n < new_shape.count(); n++)
127 {
128 auto tfl_c = n;
129 for (auto h = 0; h < new_shape.height(); h++)
130 {
131 for (auto w = 0; w < new_shape.width(); w++)
132 {
133 auto hw = new_shape.height() * new_shape.width();
134 for (auto c = 0; c < new_shape.depth(); c++)
135 {
136 auto tfl_n = c;
137 auto hwc = hw * new_shape.depth();
138 auto wc = new_shape.width() * new_shape.depth();
139
140 ker_spn[n * hwc + h * wc + w * new_shape.depth() + c] =
141 buffer.ptr[tfl_n * hw * new_shape.count() + /* new_shape.count() is old c */
142 h * new_shape.width() * new_shape.count() + w * new_shape.count() + tfl_c];
143 }
144 }
145 }
146 }
147
148 // Create a Load op
149 auto load = op_builder(m).load(ifm_obj).pop();
150
151 // Create a coco::Conv2D op for DepthwiseConv2D
152 auto coco_dconv2d = m->entity()->op()->create<coco::Conv2D>();
153
154 // populating objects and options such as stride and padding for DepthwiseConv2D
155 coco_dconv2d->ker(ker_obj);
156
157 // setting params passed from TFLITE DepthwiseConv2DOptions
158 auto dconv_params = op->builtin_options_as_DepthwiseConv2DOptions();
159
160 assert(dconv_params->depth_multiplier() == 1); // other depth_multiplier was not tested
161
162 coco_dconv2d->group(ifm_obj->asFeature()->shape().depth());
163
164 coco_dconv2d->stride()->vertical(dconv_params->stride_h());
165 coco_dconv2d->stride()->horizontal(dconv_params->stride_w());
166
167 coco::Padding2D padding = depthwiseConv2D_padding(dconv_params, ifm_shape, ker_shape);
168 coco_dconv2d->pad()->top(padding.top());
169 coco_dconv2d->pad()->bottom(padding.bottom());
170 coco_dconv2d->pad()->left(padding.left());
171 coco_dconv2d->pad()->right(padding.right());
172
173 // Link ops
174 coco_dconv2d->arg(load);
175
176 // Object to store output for DepthwiseConv2D
177 auto *dconv2d_obj = m->entity()->object()->create<coco::FeatureObject>();
178 auto *dconv2d_bag = m->entity()->bag()->create(num_elements(ofm_shape));
179 dconv2d_obj->bag(dconv2d_bag);
180 dconv2d_obj->layout(coco::FeatureLayouts::BHWC::create(as_feature_shape(ofm_shape)));
181
182 // Create an Eval instruction for DepthwiseConv2D
183 auto dconv2d_ins = instr_builder(m).eval(dconv2d_obj, coco_dconv2d);
184
185 // Append the instruction to the block
186 blk->instr()->append(dconv2d_ins);
187
188 // Last Object to make a copy to Output Object
189 coco::FeatureObject *last_obj = dconv2d_obj;
190
191 if (hasBias)
192 {
193 // When there is a bias, use btmp_obj as bias add output
194 // Bias is adding last_obj with bias weight values
195 auto *btmp_obj = m->entity()->object()->create<coco::FeatureObject>();
196 auto *btmp_bag = m->entity()->bag()->create(num_elements(ofm_shape));
197 btmp_obj->bag(btmp_bag);
198 btmp_obj->layout(coco::FeatureLayouts::BHWC::create(ofm_obj->shape()));
199
200 int bias_idx = opinputs.at(2);
201
202 // Create an object for bias
203 auto bias_obj = m->entity()->object()->create<coco::FeatureObject>();
204 coco::Bag *bias_bag = bags.bag(bias_idx);
205 bias_obj->bag(bias_bag);
206 bias_obj->layout(coco::FeatureLayouts::BC::create(ofm_obj->shape()));
207
208 // Create Op of conv2d output (last_obj) + bias values(bias_obj)
209 auto bias_add = op_builder(m).load(last_obj).load(bias_obj).add().pop();
210
211 // Create Instr as bias add result write to btmp_obj
212 auto bias_add_ins = instr_builder(m).eval(btmp_obj, bias_add);
213
214 // Append the instruction
215 blk->instr()->append(bias_add_ins);
216
217 // Update last_obj to btmp_obj
218 last_obj = btmp_obj;
219 }
220
221 // fused activation
222 coco::FeatureObject *act_output =
223 build_activation(dconv_params->fused_activation_function(), blk, last_obj);
224
225 // Create Copy Instr of last_obj to Output Object
226 auto copy_ins = instr_builder(m).copy(ofm_obj, act_output);
227 blk->instr()->append(copy_ins);
228}
OpBuilder op_builder(coco::Module *m)
Definition IRBuilder.h:144
InstrBuilder instr_builder(coco::Module *m)
Definition IRBuilder.h:174
coco::Copy * copy(coco::Object *into, coco::Object *from) const
Create "Copy" instruction with given two "Object".
Definition IRBuilder.h:161
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
OpBuilder & add(void)
Create "Add" op and push it onto the internal stack.
Definition IRBuilder.h:84
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
2D Convolution over 3D Feature Map with 4D kernel
Definition Ops.h:74
KernelObject * ker(void) const
Definition Conv2D.cpp:64
void append(Child *child)
static std::unique_ptr< BC > create(const nncc::core::ADT::feature::Shape &shape)
static std::unique_ptr< BHWC > create(const nncc::core::ADT::feature::Shape &shape)
FeatureMap values (used in CNN)
static std::unique_ptr< NHWC > create(const nncc::core::ADT::kernel::Shape &shape)
Convolution Kernel (in CNN) values.
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 & dim(uint32_t axis)
Definition Shape.cpp:42
uint32_t rank(void) const
Definition Shape.cpp:35
nncc::core::ADT::feature::Shape as_feature_shape(const nncc::core::ADT::tensor::Shape &)
Definition tflite.cpp:54
uint32_t num_elements(const Shape &shape)
The number of elements of a feature map of a given shape.
Definition Shape.h:59
TensorSignatures load(const char *info_path)
Function to create TensorSignatures defined in info file.
std::vector< int32_t > IndexVector
Definition Convert.h:29
coco::Padding2D depthwiseConv2D_padding(const tflite::DepthwiseConv2DOptions *options, const tensor::Shape &ifm_shape, const tensor::Shape &kernel_shape)
Definition Padding.cpp:96
coco::FeatureObject * build_activation(tflite::ActivationFunctionType act, coco::Block *block, coco::FeatureObject *ifm)
Add coco::Eval for fused activation. This method creates an ofm object, appends Eval(ofm object,...
IndexVector as_index_vector(const flatbuffers::Vector< int32_t > *array)
Converts flatbuffers::Vector to IndexVector.
Definition Convert.cpp:28
Core coco entity for constant weights.
Definition Data.h:31
virtual PlainWeightContext< float > * f32(void)=0

References OpBuilder::add(), 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(), tflimport::GraphBuilderContext::buffer(), tflimport::build_activation(), InstrBuilder::copy(), coco::FeatureLayouts::BHWC::create(), coco::FeatureLayouts::BC::create(), coco::KernelLayouts::NHWC::create(), tflimport::GraphBuilderContext::d(), tflimport::depthwiseConv2D_padding(), nncc::core::ADT::tensor::Shape::dim(), InstrBuilder::eval(), coco::Data::f32(), tflimport::GraphBuilderContext::graph(), coco::Block::instr(), instr_builder(), coco::Conv2D::ker(), coco::Padding2D::left(), OpBuilder::load(), tflimport::GraphBuilderContext::m(), m, op_builder(), OpBuilder::pop(), tflimport::TflBufferContext::TflBuffer< T >::ptr, nncc::core::ADT::tensor::Shape::rank(), coco::Padding2D::right(), tflimport::TensorContext::shape(), tflimport::GraphBuilderContext::tensor(), tflimport::TflBufferContext::tensor_buffer(), and coco::Padding2D::top().

◆ validate()

bool tflimport::DepthwiseConv2DGraphBuilder::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 42 of file DepthwiseConv2D.cpp.

43{
44 auto const options = op->builtin_options_as_DepthwiseConv2DOptions();
45
46 if ((options->stride_h() == 0) || (options->stride_w() == 0))
47 {
48 return false;
49 }
50
51 return true;
52}

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