ONE - On-device Neural Engine
Loading...
Searching...
No Matches
TestHelper.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_HELPER_H__
18#define __TEST_HELPER_H__
19
20#include "Check.h"
21#include "ProgressReporter.h"
22#include "Passes.h"
23
24#include <logo/Pass.h>
25#include <logo/Phase.h>
26
27#include <loco.h>
28
29#include <memory>
30
31#include <gtest/gtest.h>
32
36#define EXO_TEST_ASSERT_NODE_COUNT(OUTPUTS, COUNT) \
37 { \
38 auto v = loco::postorder_traversal(OUTPUTS); \
39 ASSERT_EQ(v.size(), (COUNT)); \
40 }
41
42namespace exo
43{
44namespace test
45{
46
52{
53public:
55 {
56 // Type and Shape inference is prerequisite for run other test
57 _phase.emplace_back(std::make_unique<::exo::TypeInferencePass>());
58 _phase.emplace_back(std::make_unique<::exo::ShapeInferencePass>());
59 }
60
61 template <typename PassT> void add_pass() { _phase.emplace_back(std::make_unique<PassT>()); }
62
64 {
65 const auto restart = logo::PhaseStrategy::Restart;
66 logo::PhaseRunner<restart> phase_runner{g};
67
68 ::exo::ProgressReporter prog(g, restart);
69 phase_runner.attach(&prog);
70 phase_runner.run(_phase);
71 }
72
73private:
74 logo::Phase _phase;
75};
76
83template <typename LocoNodeT> inline LocoNodeT *get_only_succ(loco::Node *parent)
84{
85 auto succs = loco::succs(parent);
86 EXO_ASSERT(succs.size() == 1, "parent has more than 1 succs.");
87
88 return dynamic_cast<LocoNodeT *>(*succs.begin());
89}
90
91template <typename T> inline T *find_first_node_bytype(loco::Graph *g)
92{
93 T *first_node = nullptr;
94 loco::Graph::NodeContext *nodes = g->nodes();
95 uint32_t count = nodes->size();
96
97 for (uint32_t i = 0; i < count; ++i)
98 {
99 first_node = dynamic_cast<T *>(nodes->at(i));
100 if (first_node != nullptr)
101 break;
102 }
103
104 return first_node;
105}
106
107} // namespace test
108} // namespace exo
109
110#endif // __TEST_HELPER_H__
Phase for test, that is used to test pass. This phase initially adds TypeInferencePass and ShapeInfer...
Definition TestHelper.h:52
void run(loco::Graph *g)
Definition TestHelper.h:63
A neural network graph.
Definition Graph.h:161
Logical unit of computation.
Definition Node.h:54
T * at(uint32_t n) const
Access N-th object.
Definition ObjectPool.h:41
uint32_t size(void) const
Return the number of objects.
Definition ObjectPool.h:38
#define EXO_ASSERT(condition, msg)
Definition Check.h:28
LocoNodeT * get_only_succ(loco::Node *parent)
Get the only succ object of type LocoNodeT. (The name only succ comes from English word only child....
Definition TestHelper.h:83
T * find_first_node_bytype(loco::Graph *g)
Definition TestHelper.h:91
std::set< Node * > succs(const Node *node)
Enumerate all the successors of a given node.
Definition Node.cpp:46
std::vector< std::unique_ptr< Pass > > Phase
Definition Phase.h:31