ONE - On-device Neural Engine
Loading...
Searching...
No Matches
caffeimport::EltwiseBuilder Class Referencefinal

#include <Eltwise.h>

Collaboration diagram for caffeimport::EltwiseBuilder:

Public Member Functions

void build (const ::caffe::LayerParameter &layer, GraphBuilderContext *context) const override
 
- Public Member Functions inherited from caffeimport::GraphBuilder
virtual ~GraphBuilder ()
 

Detailed Description

Definition at line 27 of file Eltwise.h.

Member Function Documentation

◆ build()

void caffeimport::EltwiseBuilder::build ( const ::caffe::LayerParameter &  layer,
GraphBuilderContext context 
) const
overridevirtual

Implements caffeimport::GraphBuilder.

Definition at line 33 of file Eltwise.cpp.

34{
35 coco::Module *module = context->module();
36 coco::Block *blk = context->block();
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
41
42 assert(layer.bottom().size() > 1);
43 assert(layer.top().size() == 1);
44
45 assert(layer.has_eltwise_param());
46 const auto &param = layer.eltwise_param();
47
48 using ::caffe::EltwiseParameter_EltwiseOp;
49 using ::caffe::EltwiseParameter_EltwiseOp_SUM;
50 using ::caffe::EltwiseParameter_EltwiseOp_PROD;
51
52 using Reducer = std::function<coco::Op *(coco::Op * lhs, coco::Op * rhs)>;
53 using ReducerRegistry = std::map<EltwiseParameter_EltwiseOp, Reducer>;
54
55 ReducerRegistry registry;
56
57 // MAX are not supported, yet
58 registry[EltwiseParameter_EltwiseOp_SUM] = [](coco::Op *lhs, coco::Op *rhs) -> coco::Op * {
59 if (lhs == nullptr)
60 {
61 assert(rhs != nullptr);
62 return rhs;
63 }
64
65 assert(lhs != nullptr && rhs != nullptr);
66 assert(lhs->module() == rhs->module());
67 assert(lhs->module() != nullptr);
68
69 auto m = lhs->module();
70 return op_builder(m).push(rhs).push(lhs).add().pop();
71 };
72
73 registry[EltwiseParameter_EltwiseOp_PROD] = [](coco::Op *lhs, coco::Op *rhs) -> coco::Op * {
74 if (lhs == nullptr)
75 {
76 assert(rhs != nullptr);
77 return rhs;
78 }
79
80 assert(lhs != nullptr && rhs != nullptr);
81 assert(lhs->module() == rhs->module());
82 assert(lhs->module() != nullptr);
83
84 auto m = lhs->module();
85 return op_builder(m).push(rhs).push(lhs).mul().pop();
86 };
87
88 // coeff is not supported, yet
89 assert(!param.coeff().size());
90
91 // Decide appropriate reduce function
92 auto reduce = registry.at(param.operation());
93
94 coco::Op *op = nullptr;
95
96 for (const auto &ifm_name : layer.bottom())
97 {
98 auto ifm_shape = shape_ctx.at(ifm_name);
99
100 // NOTE The current implementation does not work in general
101 auto ifm_bag = bag_ctx.at(ifm_name);
102 auto ifm_obj = module->entity()->object()->create<coco::FeatureObject>();
103
104 ifm_obj->bag(ifm_bag);
105 ifm_obj->layout(BCHW::create(as_feature_shape(ifm_shape)));
106
107 auto load = op_builder(module).load(ifm_obj).pop();
108
109 op = reduce(op, load);
110 }
111
112 assert(op != nullptr);
113
114 const auto ofm_name = layer.top(0);
115 const auto ofm_shape = shape_ctx.at(layer.bottom(0));
116
117 auto ofm_bag = module->entity()->bag()->create(num_elements(ofm_shape));
118 auto ofm_obj = module->entity()->object()->create<coco::FeatureObject>();
119
120 ofm_obj->bag(ofm_bag);
121 ofm_obj->layout(BCHW::create(as_feature_shape(ofm_shape)));
122
123 // Create "Eval" instruction
124 auto eval = instr_builder(module).eval(ofm_obj, op);
125
126 // Append the instruction to the block
127 blk->instr()->append(eval);
128
129 // Update bag and shape context
130 bag_ctx[ofm_name] = ofm_bag;
131 shape_ctx[ofm_name] = ofm_shape;
132}
OpBuilder op_builder(coco::Module *m)
Definition IRBuilder.h:144
InstrBuilder instr_builder(coco::Module *m)
Definition IRBuilder.h:174
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
OpBuilder & mul(void)
Create "Mul" op and push it onto the internal stack.
Definition IRBuilder.h:100
OpBuilder & push(coco::Op *op)
Push op onto the internal stack.
Definition IRBuilder.h:58
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 unit of (grouped) instructions.
Definition Block.h:40
Module * module(void) const
Definition Entity.h:39
BCHW Feature Layout.
Top-level element of coco IR which represents a neural network.
Definition Module.h:34
nncc::core::ADT::feature::Shape as_feature_shape(const nncc::core::ADT::tensor::Shape &)
Definition caffe.cpp:54
TensorSignatures load(const char *info_path)
Function to create TensorSignatures defined in info file.
Base interface on all supported NN operations.
Definition Op.h:45

References OpBuilder::add(), morph::caffe::as_feature_shape(), caffeimport::GraphBuilderContext::bag_ctx(), caffeimport::GraphBuilderContext::block(), InstrBuilder::eval(), instr_builder(), OpBuilder::load(), m, OpBuilder::mul(), op_builder(), OpBuilder::pop(), OpBuilder::push(), and caffeimport::GraphBuilderContext::shape_ctx().


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