ONE - On-device Neural Engine
Loading...
Searching...
No Matches
TestGraph.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019 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 __TEST_GRAPH_H__
18#define __TEST_GRAPH_H__
19
20#include "Dialect/IR/TFLNodes.h"
21#include "GraphBlock.h"
22#include "TestHelper.h"
23
24#include <loco.h>
25
26#include <cassert>
27
28namespace exo
29{
30namespace test
31{
32
34{
35public:
36 std::unique_ptr<loco::Graph> g;
39
40 TestGraph() // creates Pull and Push
41 {
43
44 pull = g->nodes()->create<loco::Pull>();
45
46 push = g->nodes()->create<loco::Push>();
47
48 auto input = g->inputs()->create();
49 {
50 input->name("input");
51 loco::link(input, pull);
52 }
53 auto output = g->outputs()->create();
54 {
55 output->name("output");
56 loco::link(output, push);
57 }
58
59 _next_input = pull;
60 }
61
62 loco::Graph *graph() { return g.get(); }
63
65 template <class T> T *append()
66 {
67 auto node = g->nodes()->create<T>();
68 _next_input = node;
69
70 return node;
71 }
72
74 template <class T> T *append(loco::Node *arg1)
75 {
76 auto node = g->nodes()->create<T>();
77 setInput(node, arg1);
78 _next_input = node;
79
80 return node;
81 }
82
84 template <class T> T *append(loco::Node *arg1, loco::Node *arg2)
85 {
86 auto node = g->nodes()->create<T>();
87 setInput(node, arg1, arg2);
88 _next_input = node;
89
90 return node;
91 }
92
94 template <class T> T *append(loco::Node *arg1, loco::Node *arg2, loco::Node *arg3)
95 {
96 auto node = g->nodes()->create<T>();
97 setInput(node, arg1, arg2, arg3);
98 _next_input = node;
99
100 return node;
101 }
102
103 // push will get the last appended node
104 void complete() { push->from(_next_input); }
105
106 void complete(loco::Node *last_node) { push->from(last_node); }
107
108private:
109 // arity 1
110 void setInput(loco::Node *node, loco::Node *) { assert(false && "NYI"); };
111
112 void setInput(loco::AvgPool2D *node, loco::Node *input) { node->ifm(input); }
113 void setInput(loco::BiasDecode *node, loco::Node *input) { node->input(input); };
114 void setInput(loco::BiasEncode *node, loco::Node *input) { node->input(input); };
115 void setInput(loco::FeatureDecode *node, loco::Node *input) { node->input(input); };
116 void setInput(loco::FeatureEncode *node, loco::Node *input) { node->input(input); };
117 void setInput(loco::MaxPool2D *node, loco::Node *input) { node->ifm(input); }
118 void setInput(loco::Push *node, loco::Node *input) { node->from(input); };
119 void setInput(loco::ReLU *node, loco::Node *input) { node->input(input); };
120 void setInput(loco::ReLU6 *node, loco::Node *input) { node->input(input); };
121 void setInput(loco::Tanh *node, loco::Node *input) { node->input(input); };
122 void setInput(loco::TensorTranspose *node, loco::Node *input) { node->input(input); };
123
124 void setInput(locoex::TFLAveragePool2D *node, loco::Node *input) { node->value(input); };
125 void setInput(locoex::TFLMaxPool2D *node, loco::Node *input) { node->value(input); };
126 void setInput(locoex::TFLRelu *node, loco::Node *input) { node->features(input); };
127 void setInput(locoex::TFLRelu6 *node, loco::Node *input) { node->features(input); };
128
129 // arity 2
130 void setInput(loco::Node *node, loco::Node *, loco::Node *) { assert(false && "NYI"); };
131
132 void setInput(loco::Conv2D *node, loco::Node *input, loco::Node *filter)
133 {
134 node->ifm(input);
135 node->ker(filter);
136 }
137
138 void setInput(loco::EltwiseAdd *node, loco::Node *arg1, loco::Node *arg2)
139 {
140 node->lhs(arg1);
141 node->rhs(arg2);
142 };
143
144 void setInput(loco::FeatureBiasAdd *node, loco::Node *arg1, loco::Node *arg2)
145 {
146 node->value(arg1);
147 node->bias(arg2);
148 };
149
150 void setInput(locoex::TFLAdd *node, loco::Node *arg1, loco::Node *arg2)
151 {
152 node->x(arg1);
153 node->y(arg2);
154 };
155
156 void setInput(locoex::TFLMul *node, loco::Node *arg1, loco::Node *arg2)
157 {
158 node->x(arg1);
159 node->y(arg2);
160 };
161
162 void setInput(locoex::TFLSub *node, loco::Node *arg1, loco::Node *arg2)
163 {
164 node->x(arg1);
165 node->y(arg2);
166 };
167
168 void setInput(locoex::TFLTranspose *node, loco::Node *arg1, loco::Node *arg2)
169 {
170 node->a(arg1);
171 node->perm(arg2);
172 };
173
174 // arity 3
175 void setInput(loco::Node *node, loco::Node *, loco::Node *, loco::Node *)
176 {
177 assert(false && "NYI");
178 };
179
180 void setInput(locoex::TFLConv2D *node, loco::Node *input, loco::Node *filter, loco::Node *bias)
181 {
182 node->input(input);
183 node->filter(filter);
184 node->bias(bias);
185 }
186
187private:
188 loco::Node *_next_input;
189};
190
200
201template <ExampleGraphType T> class ExampleGraph;
202
211{
212public:
213 loco::FeatureEncode *fea_enc = nullptr;
214 loco::ConstGen *constgen = nullptr;
215 loco::BiasEncode *bias_enc = nullptr;
216 loco::FeatureBiasAdd *fea_bias_add = nullptr;
217 loco::FeatureDecode *fea_dec = nullptr;
218
219public:
221 {
222 fea_enc = exo::make_feature_encode<exo::FeatureLayout::NHWC>(pull);
223 constgen = append<loco::ConstGen>();
224 bias_enc = append<loco::BiasEncode>(constgen);
225 fea_bias_add = append<loco::FeatureBiasAdd>(fea_enc, bias_enc);
226 fea_dec = exo::make_feature_decode<exo::FeatureLayout::NHWC>(fea_bias_add);
227 complete(fea_dec);
228 }
229};
230
237{
238public:
239 loco::ConstGen *constgen = nullptr;
240 loco::ReLU *relu = nullptr;
241
242public:
244 {
245 constgen = append<loco::ConstGen>();
246 relu = append<loco::ReLU>(constgen);
247 complete(relu);
248 }
249};
250
257{
258public:
259 loco::TensorTranspose *transpose = nullptr;
260
261public:
263 {
264 transpose = append<loco::TensorTranspose>(pull);
265 complete(transpose);
266 }
267};
268
275{
276public:
277 loco::FilterEncode *filterEncode = nullptr;
278 loco::FilterDecode *filterDecode = nullptr;
279
280public:
282 {
283 filterEncode = exo::make_filter_encode<exo::FilterLayout::HWIO>(pull); // from Tensorflow
284 filterDecode =
285 exo::make_filter_decode<exo::FilterLayout::OHWI>(filterEncode); // to Tensorflow Lite
286 complete(filterDecode);
287 }
288};
289
296{
297public:
298 loco::ConstGen *const_perm = nullptr;
299 locoex::TFLTranspose *tfl_transpose = nullptr;
300
301public:
303 {
304 const_perm = append<loco::ConstGen>();
305 tfl_transpose = append<locoex::TFLTranspose>(pull, const_perm);
306 complete(tfl_transpose);
307 }
308};
309
310} // namespace test
311} // namespace exo
312
313#endif // __TEST_GRAPH_H__
T * append(loco::Node *arg1, loco::Node *arg2, loco::Node *arg3)
Creates op T (arity=3) with arg1, arg2, arg3 as inputs and appends it to graph.
Definition TestGraph.h:94
T * append(loco::Node *arg1)
Creates op T (arity=1) with arg1 as an input and appends it to graph.
Definition TestGraph.h:74
loco::Pull * pull
Definition TestGraph.h:37
void complete(loco::Node *last_node)
Definition TestGraph.h:106
loco::Graph * graph()
Definition TestGraph.h:62
T * append(loco::Node *arg1, loco::Node *arg2)
Creates op T (arity=2) with arg1, arg2 as inputs and appends it to graph.
Definition TestGraph.h:84
loco::Push * push
Definition TestGraph.h:38
std::unique_ptr< loco::Graph > g
Definition TestGraph.h:36
T * append()
Creates node with NO arg and appends it to graph.
Definition TestGraph.h:65
2D Average Pooling
Definition Nodes.h:337
Node * ifm(void) const
Definition Nodes.h:349
Add Feature and Bias along "depth" axis.
Definition Nodes.h:817
Node * value(void) const
Definition Nodes.h:822
Node * bias(void) const
Definition Nodes.h:825
Create a "Tensor" from a "Bias".
Definition Nodes.h:743
Node * input(void) const
Definition Nodes.h:748
Create a "Bias" from a "Tensor".
Definition Nodes.h:758
Node * input(void) const
Definition Nodes.h:763
Create a value from constant byte array.
Definition Nodes.h:218
2D Spatial Convolution
Definition Nodes.h:554
Node * ker(void) const
Definition Nodes.h:559
Node * ifm(void) const
Definition Nodes.h:556
Elementwise Add lhs and rhs.
Definition Nodes.h:872
Node * rhs(void) const
Definition Nodes.h:880
Node * lhs(void) const
Definition Nodes.h:877
Create a tensor from a feature map.
Definition Nodes.h:399
Node * input(void) const
Definition Nodes.h:401
Create a feature map from a tensor.
Definition Nodes.h:380
Node * input(void) const
Definition Nodes.h:382
Create a tensor from a filter.
Definition Nodes.h:437
Create a filter from a tensor.
Definition Nodes.h:418
A neural network graph.
Definition Graph.h:161
2D Max Pooling
Definition Nodes.h:305
Node * ifm(void) const
Definition Nodes.h:307
Logical unit of computation.
Definition Node.h:54
Create a value from user data.
Definition Nodes.h:96
Make a value visible to user.
Definition Nodes.h:53
Node * from(void) const
Definition Nodes.h:58
Create a new value that rectifies its input capping the units at 6.
Definition Nodes.h:172
Node * input(void) const
Definition Nodes.h:177
Create a new value that rectifies its input.
Definition Nodes.h:159
Node * input(void) const
Definition Nodes.h:164
Create a new value that rectifies its input by tanh.
Definition Nodes.h:185
Node * input(void) const
Definition Nodes.h:190
Permute an input.
Definition Nodes.h:1090
Node * input(void) const
Definition Nodes.h:1095
ADD in TensorFlow Lite.
Definition TFLNodes.h:116
loco::Node * y(void) const
Definition TFLNodes.h:121
loco::Node * x(void) const
Definition TFLNodes.h:118
AVERAGE_POOL_2D in TensorFlow Lite.
Definition TFLNodes.h:130
loco::Node * value(void) const
Definition TFLNodes.h:137
CONV_2D in TensorFlow Lite.
Definition TFLNodes.h:218
loco::Node * filter(void) const
Definition TFLNodes.h:223
loco::Node * bias(void) const override
Definition TFLNodes.h:226
loco::Node * input(void) const
Definition TFLNodes.h:220
MAX_POOL_2D in TensorFlow Lite.
Definition TFLNodes.h:328
loco::Node * value(void) const
Definition TFLNodes.h:335
MUL in TensorFlow Lite.
Definition TFLNodes.h:375
loco::Node * y(void) const
Definition TFLNodes.h:380
loco::Node * x(void) const
Definition TFLNodes.h:377
loco::Node * features(void) const
Definition TFLNodes.h:400
loco::Node * features(void) const
Definition TFLNodes.h:390
SUB in TensorFlow Lite.
Definition TFLNodes.h:488
loco::Node * y(void) const
Definition TFLNodes.h:496
loco::Node * x(void) const
Definition TFLNodes.h:493
TRANSPOSE in TensorFlow Lite.
Definition TFLNodes.h:506
loco::Node * perm(void) const
Definition TFLNodes.h:517
loco::Node * a(void) const
Get the input node to transpose.
Definition TFLNodes.h:512
void link(GraphOutput *, Push *push)
Definition Nodes.cpp:65
std::unique_ptr< Graph > make_graph(void)
Definition Graph.cpp:131