ONE - On-device Neural Engine
Loading...
Searching...
No Matches
IRBuilder.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
21#ifndef __IR_BUILDER_H__
22#define __IR_BUILDER_H__
23
24#include "coco/IR/Module.h"
25
26#include <deque>
27
28using namespace nncc::core::ADT;
29
30class OpBuilder
31{
32public:
33 OpBuilder(coco::Module *module) : _module{module}
34 {
35 // module SHOULD BE valid
36 assert(_module != nullptr);
37 }
38
39public:
43 bool empty(void) const { return _stack.empty(); }
44
48 coco::Op *top(void) const
49 {
50 assert(_stack.size() > 0);
51 return _stack.front();
52 }
53
61 {
62 _stack.push_front(op);
63 return (*this);
64 }
65
73 {
74 auto op = _module->entity()->op()->create<coco::Load>();
75 op->object(obj);
76 push(op);
77 return (*this);
78 }
79
86 OpBuilder &add(void) { return binary<coco::Add>(); }
87
94 OpBuilder &mul(void) { return binary<coco::Mul>(); }
95
103 {
104 assert(_stack.size() > 0);
105 auto op = _stack.front();
106 _stack.pop_front();
107 return op;
108 }
109
110private:
111 template <typename ConcreteOp> OpBuilder &binary()
112 {
113 assert(_stack.size() >= 2);
114 auto left = pop();
115 auto right = pop();
116
117 auto op = _module->entity()->op()->create<ConcreteOp>();
118 op->left(left);
119 op->right(right);
120 push(op);
121
122 return (*this);
123 }
124
125private:
126 coco::Module *_module;
127 std::deque<coco::Op *> _stack;
128};
129
131inline OpBuilder op_builder(const std::unique_ptr<coco::Module> &m) { return op_builder(m.get()); }
132
133class InstrBuilder
134{
135public:
136 InstrBuilder(coco::Module *module) : _module{module}
137 {
138 // NOTE _module SHOULD be valid
139 assert(_module != nullptr);
140 }
141
142public:
149 {
150 auto ins = _module->entity()->instr()->create<coco::Eval>();
151 ins->op(op);
152 ins->out(out);
153 return ins;
154 }
155
162 {
163 auto ins = _module->entity()->instr()->create<coco::Copy>();
164 ins->from(from);
165 ins->into(into);
166 return ins;
167 }
168
169private:
170 coco::Module *_module;
171};
172
173using ModuleHandle = std::unique_ptr<coco::Module>;
174
176inline InstrBuilder instr_builder(const ModuleHandle &m) { return instr_builder(m.get()); }
177
178#endif // __IR_BUILDER_H__
coco::Copy * copy(coco::Object *into, coco::Object *from) const
Create "Copy" instruction with given two "Object".
Definition IRBuilder.h:161
InstrBuilder(coco::Module *module)
Definition IRBuilder.h:136
coco::Eval * eval(coco::Object *out, coco::Op *op) const
Create "Eval" instruction with a given "Object" and "Op".
Definition IRBuilder.h:148
OpBuilder & load(coco::Object *obj)
Create "Load" op and push it onto the internal stack.
Definition IRBuilder.h:72
coco::Op * top(void) const
Return the operation at the top of the internal stack.
Definition IRBuilder.h:48
OpBuilder & mul(void)
Create "Mul" op and push it onto the internal stack.
Definition IRBuilder.h:94
OpBuilder & push(coco::Op *op)
Push op onto the internal stack.
Definition IRBuilder.h:60
coco::Op * pop(void)
Pop op from the internal stack.
Definition IRBuilder.h:102
OpBuilder & add(void)
Create "Add" op and push it onto the internal stack.
Definition IRBuilder.h:86
OpBuilder(coco::Module *module)
Definition IRBuilder.h:33
bool empty(void) const
Return true if the internal stack is empty.
Definition IRBuilder.h:43
Index-wise element transfer between two objects.
Definition Instrs.h:85
Object * from(void) const
Definition Instrs.h:100
Evaluate an Object from a given Op.
Definition Instrs.h:43
Op * op(void) const
Definition Instrs.h:59
Ins * create(void)
Load an Object.
Definition Ops.h:38
void object(Object *o)
Definition Ops.h:60
Top-level element of coco IR which represents a neural network.
Definition Module.h:34
virtual EntityManager * entity(void)=0
Base interface on all typed NN values.
Definition Object.h:38
T * create(void)
virtual OpManager * op(void)=0
virtual InstrManager * instr(void)=0
Base interface on all supported NN operations.
Definition Op.h:45
std::unique_ptr< coco::Module > ModuleHandle
Definition IRBuilder.h:173
OpBuilder op_builder(coco::Module *m)
Definition IRBuilder.h:130
InstrBuilder instr_builder(coco::Module *m)
Definition IRBuilder.h:175