21#include "GraphBuilder.h"
32#include <schema_generated.h>
44 auto const options = op->builtin_options_as_DepthwiseConv2DOptions();
46 if ((options->stride_h() == 0) || (options->stride_w() == 0))
57 assert(context !=
nullptr);
66 const tflite::SubGraph *graph = context->
graph();
74 bool hasBias = (opinputs.size() == 3);
75 assert(opinputs.size() == 2 || hasBias);
76 assert(opoutputs.size() == 1);
78 int ifm_idx = opinputs.at(0);
79 int ker_idx = opinputs.at(1);
80 int ofm_idx = opoutputs.at(0);
86 assert(ifm_shape.
rank() == 4);
87 assert(ofm_shape.
rank() == 4);
88 assert(ker_shape.
rank() == 4);
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));
96 auto *ifm_bag = bags.
bag(ifm_idx);
97 ifm_obj->bag(ifm_bag);
102 auto *ofm_bag = bags.
bag(ofm_idx);
103 ofm_obj->bag(ofm_bag);
108 auto *ker_bag = bags.
bag(ker_idx);
109 ker_obj->bag(ker_bag);
118 d->
f32()->allocate(ker_bag);
122 auto ker_spn = d->
f32()->weight(ker_bag);
126 for (
auto n = 0; n < new_shape.count(); n++)
129 for (
auto h = 0; h < new_shape.height(); h++)
131 for (
auto w = 0; w < new_shape.width(); w++)
133 auto hw = new_shape.height() * new_shape.width();
134 for (
auto c = 0; c < new_shape.depth(); c++)
137 auto hwc = hw * new_shape.depth();
138 auto wc = new_shape.width() * new_shape.depth();
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];
152 auto coco_dconv2d =
m->entity()->op()->create<
coco::Conv2D>();
155 coco_dconv2d->
ker(ker_obj);
158 auto dconv_params = op->builtin_options_as_DepthwiseConv2DOptions();
160 assert(dconv_params->depth_multiplier() == 1);
162 coco_dconv2d->group(ifm_obj->asFeature()->shape().depth());
164 coco_dconv2d->stride()->vertical(dconv_params->stride_h());
165 coco_dconv2d->stride()->horizontal(dconv_params->stride_w());
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());
174 coco_dconv2d->arg(load);
178 auto *dconv2d_bag =
m->entity()->
bag()->create(num_elements(ofm_shape));
179 dconv2d_obj->bag(dconv2d_bag);
196 auto *btmp_bag =
m->entity()->
bag()->create(num_elements(ofm_shape));
197 btmp_obj->bag(btmp_bag);
200 int bias_idx = opinputs.at(2);
205 bias_obj->bag(bias_bag);
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
void build(const tflite::Operator *op, GraphBuilderContext *context) const override
bool validate(const tflite::Operator *op) const override
Class to store context to build IR from tflite.
TflBufferContext & buffer()
const tflite::SubGraph * graph()
Pre-creates coco:Bags for each operands(tensors)
coco::Bag * bag(int32_t tensor_id)
Extracts and holds operand(tensor) information such as name, shape, and type.
const tensor::Shape & shape(uint32_t tensor_id)
Class to read and provide buffer information of tflite.
TflBuffer< T > tensor_buffer(const tflite::SubGraph *graph, uint32_t tensor_idx) const
nncc::core::ADT::feature::Shape as_feature_shape(const nncc::core::ADT::tensor::Shape &)
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