56{
57 assert(context != nullptr);
58
59
63 TensorContext &tensor_context = context->tensor();
64 TensorBags &bags = context->bags();
65 TflBufferContext &buffer_context = context->buffer();
66 const tflite::SubGraph *
graph = context->graph();
69
70
71
72
73
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);
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);
91 assert(ifm_shape.
dim(3) == ofm_shape.
dim(3));
92 assert(ofm_shape.
dim(3) == ker_shape.
dim(3));
93
94
96 auto *ifm_bag = bags.
bag(ifm_idx);
97 ifm_obj->bag(ifm_bag);
99
100
102 auto *ofm_bag = bags.
bag(ofm_idx);
103 ofm_obj->bag(ofm_bag);
105
106
108 auto *ker_bag = bags.
bag(ker_idx);
109 ker_obj->bag(ker_bag);
110
111
112
115
116
117
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
125
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() +
142 h * new_shape.width() * new_shape.count() +
w * new_shape.count() + tfl_c];
143 }
144 }
145 }
146 }
147
148
150
151
152 auto coco_dconv2d =
m->entity()->op()->create<
coco::Conv2D>();
153
154
155 coco_dconv2d->
ker(ker_obj);
156
157
158 auto dconv_params = op->builtin_options_as_DepthwiseConv2DOptions();
159
160 assert(dconv_params->depth_multiplier() == 1);
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
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
174 coco_dconv2d->arg(load);
175
176
179 dconv2d_obj->bag(dconv2d_bag);
181
182
184
185
187
188
190
191 if (hasBias)
192 {
193
194
197 btmp_obj->bag(btmp_bag);
199
200 int bias_idx = opinputs.at(2);
201
202
204 coco::Bag *bias_bag = bags.bag(bias_idx);
205 bias_obj->bag(bias_bag);
207
208
210
211
213
214
216
217
218 last_obj = btmp_obj;
219 }
220
221
224
225
228}
OpBuilder op_builder(coco::Module *m)
InstrBuilder instr_builder(coco::Module *m)
coco::Copy * copy(coco::Object *into, coco::Object *from) const
Create "Copy" instruction with given two "Object".
coco::Eval * eval(coco::Object *out, coco::Op *op) const
Create "Eval" instruction with a given "Object" and "Op".
OpBuilder & load(coco::Object *obj)
Create "Load" op and push it onto the internal stack.
coco::Op * pop(void)
Pop op from the internal stack.
OpBuilder & add(void)
Create "Add" op and push it onto the internal stack.
A collection of (abstracted) elements of the same type.
A unit of (grouped) instructions.
2D Convolution over 3D Feature Map with 4D kernel
KernelObject * ker(void) const
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.
coco::Bag * bag(void) const
uint32_t left(void) const
uint32_t right(void) const
uint32_t bottom(void) const
uint32_t & dim(uint32_t axis)
uint32_t rank(void) const
nncc::core::ADT::feature::Shape as_feature_shape(const nncc::core::ADT::tensor::Shape &)
uint32_t num_elements(const Shape &shape)
The number of elements of a feature map of a given shape.
TensorSignatures load(const char *info_path)
Function to create TensorSignatures defined in info file.
std::vector< int32_t > IndexVector
coco::Padding2D depthwiseConv2D_padding(const tflite::DepthwiseConv2DOptions *options, const tensor::Shape &ifm_shape, const tensor::Shape &kernel_shape)
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.
Core coco entity for constant weights.
virtual PlainWeightContext< float > * f32(void)=0