39{
43 std::map<std::string, tensor::Shape> &shape_ctx = context->shape_ctx();
44 std::map<std::string, coco::Bag *> &bag_ctx = context->bag_ctx();
45 WeightContext &weight_ctx = context->weight_ctx();
46
47 assert(layer.bottom().size() == 1);
48 assert(layer.top().size() == 1);
49
50 assert(layer.has_convolution_param());
51 const auto ¶m = layer.convolution_param();
52
54 {
55 const auto ifm_name = layer.bottom(0);
56 const auto ifm_shape = shape_ctx.at(ifm_name);
58 }
59
60
61
62 assert(spec.num_batch_axes() == 1);
63 assert(spec.num_spatial_axes() == 2);
64
65
66 const auto ifm_name = layer.bottom(0);
67 const auto ifm_shape = shape_ctx.at(ifm_name);
68 auto ifm_bag = bag_ctx.at(ifm_name);
69 auto ifm_obj = module->entity()->object()->create<coco::FeatureObject>();
70
71 ifm_obj->bag(ifm_bag);
73
74
75 const auto ofm_name = layer.top(0);
76 const auto ofm_shape = spec.ofm_shape();
77 auto ofm_bag = module->entity()->bag()->create(num_elements(ofm_shape));
78 auto ofm_obj = module->entity()->object()->create<coco::FeatureObject>();
79
80 ofm_obj->bag(ofm_bag);
82
83
85
86 const auto ker_shape = spec.ker_shape();
87 auto ker_bag = module->entity()->bag()->create(num_elements(ker_shape));
88 auto ker_obj = module->entity()->object()->create<coco::KernelObject>();
89
90 ker_obj->bag(ker_bag);
92
93
94 data->f32()->allocate(ker_bag);
95
96
97 assert(weight_ctx.blob_count(layer.name()) >= 1);
98 auto ker_blob = weight_ctx.blob_get(layer.name(), 0);
99
101
102 auto ker_dst =
data->f32()->access(ker_obj);
104 ker_obj->shape(), ker_blob->mutable_data()->begin());
105
106 for (uint32_t n = 0; n < ker_obj->shape().count(); ++n)
107 {
108 for (uint32_t ch = 0; ch < ker_obj->shape().depth(); ++ch)
109 {
110 for (uint32_t row = 0; row < ker_obj->shape().height(); ++row)
111 {
112 for (uint32_t col = 0; col < ker_obj->shape().width(); ++col)
113 {
114 ker_dst->at(n, ch, row, col) = ker_src.at(n, ch, row, col);
115 }
116 }
117 }
118 }
119
120
122
123
124 auto op = module->entity()->op()->create<coco::Conv2D>();
125
126 op->group(spec.group());
127
128 op->ker(ker_obj);
129 op->stride()->vertical(spec.stride(0));
130 op->stride()->horizontal(spec.stride(1));
131
132 op->pad()->top(spec.pad(0));
133 op->pad()->bottom(spec.pad(0));
134 op->pad()->left(spec.pad(1));
135 op->pad()->right(spec.pad(1));
136
138
139
141
142
144
145
146
147
148
149
150
151 if (param.bias_term())
152 {
153 assert(weight_ctx.blob_count(layer.name()) >= 2);
154
155
156 auto bias_bag = module->entity()->bag()->create(ker_shape.dim(0));
157 auto bias_obj = module->entity()->object()->create<coco::FeatureObject>();
158
159 bias_obj->bag(bias_bag);
161
162 auto added_bag = module->entity()->bag()->create(num_elements(ofm_shape));
163 auto added_obj = module->entity()->object()->create<coco::FeatureObject>();
164
165 added_obj->bag(added_bag);
167
168
170
171
173
174
176
177
178 data->f32()->allocate(bias_bag);
179
180 auto bias_span =
data->f32()->weight(bias_bag);
181 auto bias_blob = weight_ctx.blob_get(layer.name(), 1);
182
183 for (uint32_t ch = 0; ch < ker_obj->shape().count(); ++ch)
184 {
185 bias_span[ch] = bias_blob->data(ch);
186 }
187
188
189 ofm_bag = added_bag;
190 }
191
192
193 bag_ctx[ofm_name] = ofm_bag;
194 shape_ctx[ofm_name] = ofm_shape;
195}
OpBuilder op_builder(coco::Module *m)
InstrBuilder instr_builder(coco::Module *m)
const nncc::core::ADT::tensor::Shape & ifm_shape(void) const
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 unit of (grouped) instructions.
void append(Child *child)
static std::unique_ptr< BCHW > create(const nncc::core::ADT::feature::Shape &shape)
static std::unique_ptr< BC > create(const nncc::core::ADT::feature::Shape &shape)
Top-level element of coco IR which represents a neural network.
tensor::Shape as_tensor_shape(const ::caffe::BlobShape &blob_shape)
nncc::core::ADT::feature::Shape as_feature_shape(const nncc::core::ADT::tensor::Shape &)
nncc::core::ADT::kernel::Shape as_kernel_shape(const nncc::core::ADT::tensor::Shape &)
TensorSignatures load(const char *info_path)
Function to create TensorSignatures defined in info file.
Core coco entity for constant weights.
virtual Op * arg(uint32_t n) const =0
Return N-th argument.
static Overlay< T, InputIt > make(const Shape &shape, InputIt it)