33{
37 std::map<std::string, tensor::Shape> &shape_ctx = context->shape_ctx();
38 std::map<std::string, coco::Bag *> &bag_ctx = context->bag_ctx();
39 WeightContext &weight_ctx = context->weight_ctx();
40
41
42 assert(layer.bottom().size() == 1);
43 assert(layer.top().size() == 1);
44
45 assert(layer.has_scale_param());
46 const auto ¶m = layer.scale_param();
47
48 assert(param.axis() == 1);
49 assert(!param.has_num_axes());
50
51 assert(weight_ctx.blob_count(layer.name()) >= 1);
52
53
54
55
56 auto shape = shape_ctx.at(layer.bottom(0));
57
58 coco::Bag *last_bag = bag_ctx.at(layer.bottom(0));
59
60
61 {
62 auto in_bag = last_bag;
63 auto in_obj = module->entity()->object()->create<coco::FeatureObject>();
64
65 in_obj->bag(in_bag);
67
68 auto factor_bag = module->entity()->bag()->create(num_elements(shape));
69 auto factor_obj = module->entity()->object()->create<coco::FeatureObject>();
70
71 factor_obj->bag(factor_bag);
73
74 auto out_bag = module->entity()->bag()->create(num_elements(shape));
75 auto out_obj = module->entity()->object()->create<coco::FeatureObject>();
76
77 out_obj->bag(out_bag);
79
82
84
85
86 {
87 data->f32()->allocate(factor_bag);
88
89 auto span =
data->f32()->weight(factor_bag);
90 auto blob = weight_ctx.blob_get(layer.name(), 0);
91
92 for (uint32_t ch = 0; ch < factor_obj->shape().depth(); ++ch)
93 {
94 span[ch] = blob->data(ch);
95 }
96 }
97
98
99 last_bag = out_bag;
100 }
101
102 assert(last_bag != nullptr);
103
104
105 if (param.bias_term())
106 {
107 assert(weight_ctx.blob_count(layer.name()) >= 2);
108
109 auto in_bag = last_bag;
110 auto in_obj = module->entity()->object()->create<coco::FeatureObject>();
111
112 in_obj->bag(in_bag);
114
115 auto bias_bag = module->entity()->bag()->create(num_elements(shape));
116 auto bias_obj = module->entity()->object()->create<coco::FeatureObject>();
117
118 bias_obj->bag(bias_bag);
120
121 auto out_bag = module->entity()->bag()->create(num_elements(shape));
122 auto out_obj = module->entity()->object()->create<coco::FeatureObject>();
123
124 out_obj->bag(out_bag);
126
129
131
132
133 {
134 data->f32()->allocate(bias_bag);
135
136 auto bias_span =
data->f32()->weight(bias_bag);
137 auto bias_blob = weight_ctx.blob_get(layer.name(), 1);
138
139 for (uint32_t ch = 0; ch < bias_obj->shape().depth(); ++ch)
140 {
141 bias_span[ch] = bias_blob->data(ch);
142 }
143 }
144
145
146 last_bag = out_bag;
147 }
148
149
150 {
151 const auto &out_name = layer.top(0);
152 const auto &out_bag = last_bag;
153 const auto &out_shape = shape;
154
155 bag_ctx[out_name] = out_bag;
156 shape_ctx[out_name] = out_shape;
157 }
158}
OpBuilder op_builder(coco::Module *m)
InstrBuilder instr_builder(coco::Module *m)
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.
OpBuilder & mul(void)
Create "Mul" 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.
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.
nncc::core::ADT::feature::Shape as_feature_shape(const nncc::core::ADT::tensor::Shape &)
Core coco entity for constant weights.