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
17#ifndef __IR_BUILDER_H__
18#define __IR_BUILDER_H__
19
20#include "coco/IR/Module.h"
21
22#include <deque>
23
29{
30public:
31 OpBuilder(coco::Module *module) : _module{module}
32 {
33 // module SHOULD BE valid
34 assert(_module != nullptr);
35 }
36
37public:
41 bool empty(void) const { return _stack.empty(); }
42
46 coco::Op *top(void) const
47 {
48 assert(_stack.size() > 0);
49 return _stack.front();
50 }
51
59 {
60 _stack.push_front(op);
61 return (*this);
62 }
63
71 {
72 auto op = _module->entity()->op()->create<coco::Load>();
73 op->object(obj);
74 push(op);
75 return (*this);
76 }
77
84 OpBuilder &add(void) { return binary<coco::Add>(); }
85
92 OpBuilder &sub(void) { return binary<coco::Sub>(); }
93
100 OpBuilder &mul(void) { return binary<coco::Mul>(); }
101
108 OpBuilder &div(void) { return binary<coco::Div>(); }
109
117 {
118 assert(_stack.size() > 0);
119 auto op = _stack.front();
120 _stack.pop_front();
121 return op;
122 }
123
124private:
125 template <typename ConcreteOp> OpBuilder &binary()
126 {
127 assert(_stack.size() >= 2);
128 auto left = pop();
129 auto right = pop();
130
131 auto op = _module->entity()->op()->create<ConcreteOp>();
132 op->left(left);
133 op->right(right);
134 push(op);
135
136 return (*this);
137 }
138
139private:
140 coco::Module *_module;
141 std::deque<coco::Op *> _stack;
142};
143
145inline OpBuilder op_builder(const std::unique_ptr<coco::Module> &m) { return op_builder(m.get()); }
146
148{
149public:
150 InstrBuilder(coco::Module *module) : _module{module}
151 {
152 // NOTE _module SHOULD be valid
153 assert(_module != nullptr);
154 }
155
156public:
163 {
164 auto ins = _module->entity()->instr()->create<coco::Eval>();
165 ins->op(op);
166 ins->out(out);
167 return ins;
168 }
169
170private:
171 coco::Module *_module;
172};
173
175inline InstrBuilder instr_builder(const std::unique_ptr<coco::Module> &m)
176{
177 return instr_builder(m.get());
178}
179
180#endif // __IR_BUILDER_H__
OpBuilder op_builder(coco::Module *m)
Definition IRBuilder.h:144
InstrBuilder instr_builder(coco::Module *m)
Definition IRBuilder.h:174
InstrBuilder(coco::Module *module)
Definition IRBuilder.h:150
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
coco::Op * top(void) const
Return the operation at the top of the internal stack.
Definition IRBuilder.h:46
OpBuilder & mul(void)
Create "Mul" op and push it onto the internal stack.
Definition IRBuilder.h:100
OpBuilder & sub(void)
Create "Sub" op and push it onto the internal stack.
Definition IRBuilder.h:92
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
OpBuilder(coco::Module *module)
Definition IRBuilder.h:31
bool empty(void) const
Return true if the internal stack is empty.
Definition IRBuilder.h:41
OpBuilder & div(void)
Create "Div" op and push it onto the internal stack.
Definition IRBuilder.h:108
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