blob: 6962f0e6d1138bc2b588561ab9edf0b5252a8a69 [file] [log] [blame]
SiCong Lib63b1192022-01-28 18:24:39 +00001/*
2 * Copyright (c) 2022 Arm Limited.
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#ifndef ENABLE_EXPERIMENTAL_DYNAMIC_FUSION
25#error "This experimental feature must be enabled with -DENABLE_EXPERIMENTAL_DYNAMIC_FUSION"
26#endif /* ENABLE_EXPERIMENTAL_DYNAMIC_FUSION */
27#include "arm_compute/core/experimental/DependencyGraph.h"
28
29#include "tests/framework/Asserts.h"
30#include "tests/framework/Macros.h"
31
32using namespace arm_compute::experimental::dynamic_fusion;
33
34namespace arm_compute
35{
36namespace test
37{
38namespace validation
39{
40TEST_SUITE(CL)
41
42TEST_SUITE(UNIT)
43TEST_SUITE(DYNAMIC_FUSION)
44TEST_SUITE(DependencyGraph)
45
46TEST_CASE(Correct_Graph_Creation_Should_Pass, framework::DatasetMode::ALL)
47{
48 DependencyGraph graph{};
49 const auto t0 = graph.add_tensor();
50 const auto t1 = graph.add_tensor();
51 const auto t2 = graph.add_tensor();
52 const auto t3 = graph.add_tensor();
53 const auto t4 = graph.add_tensor();
54
55 const auto o0 = graph.add_operator({ t0, t1 }, { t2 }).second;
56 const auto o1 = graph.add_operator({ t3, t2 }, { t4 }).second;
57
58 ARM_COMPUTE_EXPECT_EQUAL(graph.number_of_ops(), 2U, framework::LogLevel::ERRORS);
59 ARM_COMPUTE_EXPECT_EQUAL(graph.number_of_tensors(), 5U, framework::LogLevel::ERRORS);
60
61 const DependencyGraph ref_graph
62 {
63 {
64 // src_tensors
65 { o0, { t0, t1 } },
66 { o1, { t3, t2 } },
67 },
68 {
69 // dst_tensors
70 { o0, { t2 } },
71 { o1, { t4 } },
72 },
73 {
74 // src_ops
75 { t0, {} },
76 { t1, {} },
77 { t2, { o0 } },
78 { t3, {} },
79 { t4, { o1 } },
80 },
81 {
82 // dst_ops
83 { t0, { o0 } },
84 { t1, { o0 } },
85 { t2, { o1 } },
86 { t3, { o1 } },
87 { t4, {} },
88 }
89
90 };
91 ARM_COMPUTE_EXPECT(graph == ref_graph, framework::LogLevel::ERRORS);
92}
93
94TEST_CASE(Correct_Merge_Points_Should_Enable_Graph_Expansion, framework::DatasetMode::ALL)
95{
96 // Merge points are a simple way to collapse "graph of graphs" into a single graph
97 // Suppose we have a top-level graph g0
98 DependencyGraph g0{};
99 const auto g0_t0 = g0.add_tensor();
100 const auto g0_t1 = g0.add_tensor();
101 const auto g0_t2 = g0.add_tensor();
102 const auto g0_t3 = g0.add_tensor();
103 const auto g0_t4 = g0.add_tensor();
104 g0.add_operator({ g0_t0, g0_t1 }, { g0_t2 }); // g0_o0
105 g0.add_operator({ g0_t3, g0_t2 }, { g0_t4 }); // g0_o1
106
107 // Then g0 expands into g1, with additional nodes added in-between "merge point tensors"
108 // Note that the expansion logic may be local to each operator node
109 DependencyGraph g1{};
110 // g0_o0 expands into g1_o0, g1_o1, g1_o2
111 const auto g1_t0 = g1.add_tensor(g0_t0);
112 const auto g1_t1 = g1.add_tensor(g0_t1);
113 const auto g1_t2 = g1.add_tensor();
114 const auto g1_t3 = g1.add_tensor();
115 const auto g1_t4 = g1.add_tensor(g0_t2);
116 const auto g1_o0 = g1.add_operator({ g1_t0 }, { g1_t2 }).second;
117 const auto g1_o1 = g1.add_operator({ g1_t1 }, { g1_t3 }).second;
118 const auto g1_o2 = g1.add_operator({ g1_t2, g1_t3 }, { g1_t4 }).second;
119
120 // g0_o1 expands into g1_o3
121 const auto g1_t5 = g1.add_tensor(g0_t3);
122 const auto g1_t6 = g1.add_tensor(g0_t2);
123 const auto g1_t7 = g1.add_tensor(g0_t4);
124 ARM_COMPUTE_EXPECT_EQUAL(g1_t4, g1_t6, framework::LogLevel::ERRORS); // both associate with the same merge point g0_t2, thus they should point to the same tensor in g1
125 const auto g1_o3 = g1.add_operator({ g1_t5, g1_t6 }, { g1_t7 }).second;
126
127 const DependencyGraph ref_graph
128 {
129 {
130 // src_tensors
131 { g1_o0, { g1_t0 } },
132 { g1_o1, { g1_t1 } },
133 { g1_o2, { g1_t2, g1_t3 } },
134 { g1_o3, { g1_t5, g1_t4 } },
135 },
136 {
137 // dst_tensors
138 { g1_o0, { g1_t2 } },
139 { g1_o1, { g1_t3 } },
140 { g1_o2, { g1_t4 } },
141 { g1_o3, { g1_t7 } },
142 },
143 {
144 // src_ops
145 { g1_t0, {} },
146 { g1_t1, {} },
147 { g1_t2, { g1_o0 } },
148 { g1_t3, { g1_o1 } },
149 { g1_t4, { g1_o2 } },
150 { g1_t5, {} },
151 { g1_t7, { g1_o3 } },
152 },
153 {
154 // dst_ops
155 { g1_t0, { g1_o0 } },
156 { g1_t1, { g1_o1 } },
157 { g1_t2, { g1_o2 } },
158 { g1_t3, { g1_o2 } },
159 { g1_t4, { g1_o3 } },
160 { g1_t5, { g1_o3 } },
161 { g1_t7, {} },
162 },
163 {
164 // merge points
165 { g0_t0, g1_t0 },
166 { g0_t1, g1_t1 },
167 { g0_t2, g1_t4 },
168 { g0_t3, g1_t5 },
169 { g0_t4, g1_t7 },
170 }
171 };
172 ARM_COMPUTE_EXPECT(g1 == ref_graph, framework::LogLevel::ERRORS);
173}
174
175TEST_CASE(Path_Existence_Check_0, framework::DatasetMode::ALL)
176{
177 DependencyGraph graph{};
178 const auto t0 = graph.add_tensor();
179 const auto t1 = graph.add_tensor();
180 const auto t2 = graph.add_tensor();
181 const auto t3 = graph.add_tensor();
182 const auto t4 = graph.add_tensor();
183 const auto t5 = graph.add_tensor();
184 const auto t6 = graph.add_tensor();
185 const auto t7 = graph.add_tensor();
186 const auto o0 = graph.add_operator({ t1 }, { t3, t4 }).second;
187 const auto o1 = graph.add_operator({ t3 }, { t5 }).second;
188 const auto o2 = graph.add_operator({ t5, t6 }, { t7 }).second;
189 const auto o3 = graph.add_operator({ t4 }, { t6 }).second;
190 const auto o4 = graph.add_operator({ t0, t5 }, { t2 }).second;
191
192 ARM_COMPUTE_UNUSED(o1, o3);
193
194 ARM_COMPUTE_EXPECT((graph.path_exists_from_tensor_to_op(t3, o2)), framework::LogLevel::ERRORS);
195 ARM_COMPUTE_EXPECT((graph.path_exists_from_tensor_to_op(t1, o4)), framework::LogLevel::ERRORS);
196 ARM_COMPUTE_EXPECT(!(graph.path_exists_from_tensor_to_op(t2, o4)), framework::LogLevel::ERRORS);
197 ARM_COMPUTE_EXPECT(!(graph.path_exists_from_tensor_to_op(t0, o2)), framework::LogLevel::ERRORS);
198
199 ARM_COMPUTE_EXPECT((graph.path_exists_from_op_to_op(o0, o2)), framework::LogLevel::ERRORS);
200 ARM_COMPUTE_EXPECT(!(graph.path_exists_from_op_to_op(o2, o0)), framework::LogLevel::ERRORS);
201
202 ARM_COMPUTE_EXPECT(!(graph.path_exists_from_op_to_op(o2, o4)), framework::LogLevel::ERRORS);
203}
204
205TEST_CASE(Correct_Topological_Sort_Should_Pass, framework::DatasetMode::ALL)
206{
207 DependencyGraph graph{};
208 const auto t0 = graph.add_tensor();
209 const auto t1 = graph.add_tensor();
210 const auto t2 = graph.add_tensor();
211 const auto t3 = graph.add_tensor();
212 const auto t4 = graph.add_tensor();
213 const auto t5 = graph.add_tensor();
214 const auto t6 = graph.add_tensor();
215 const auto t7 = graph.add_tensor();
216 const auto o0 = graph.add_operator({ t1 }, { t3, t4 }).second;
217 const auto o1 = graph.add_operator({ t3 }, { t5 }).second;
218 const auto o2 = graph.add_operator({ t5, t6 }, { t7 }).second;
219 const auto o3 = graph.add_operator({ t4 }, { t6 }).second;
220 const auto o4 = graph.add_operator({ t0, t5 }, { t2 }).second;
221
222 const auto res = graph.topological_sort();
223 ARM_COMPUTE_EXPECT(bool(res.first), framework::LogLevel::ERRORS);
224 std::vector<DependencyGraph::OpPack> ref_sorted_op_packs
225 {
226 { o0, { t1 }, { t3, t4 } },
227 { o1, { t3 }, { t5 } },
228 { o3, { t4 }, { t6 } },
229 { o4, { t0, t5 }, { t2 } },
230 { o2, { t5, t6 }, { t7 } },
231
232 };
233 ARM_COMPUTE_EXPECT((res.second == ref_sorted_op_packs), framework::LogLevel::ERRORS);
234}
235
236TEST_CASE(Cycles_Should_Fail, framework::DatasetMode::ALL)
237{
238 DependencyGraph graph{};
239 const auto t0 = graph.add_tensor();
240 const auto t1 = graph.add_tensor();
241 const auto t2 = graph.add_tensor();
242 const auto t3 = graph.add_tensor();
243
244 graph.add_operator({ t0, t1 }, { t2 });
245 graph.add_operator({ t2 }, { t1, t3 }); // Ideally error should occur here
246
247 const auto res = graph.topological_sort();
248 ARM_COMPUTE_EXPECT(!bool(res.first), framework::LogLevel::ERRORS);
249}
250TEST_CASE(Loops_Should_Fail, framework::DatasetMode::ALL)
251{
252 DependencyGraph graph{};
253 const auto t0 = graph.add_tensor();
254 const auto t1 = graph.add_tensor();
255 const auto t2 = graph.add_tensor();
256
257 ARM_COMPUTE_EXPECT_THROW(graph.add_operator({ t0, t2 }, { t1, t2 }).first, framework::LogLevel::ERRORS);
258 ARM_COMPUTE_UNUSED(t0, t1, t2);
259}
260TEST_SUITE_END() // DependencyGraph
261TEST_SUITE_END() // DYNAMIC_FUSION
262TEST_SUITE_END() // UNIT
263
264TEST_SUITE_END() // CL
265} // namespace validation
266} // namespace test
267} // namespace arm_compute