Compute Library
 22.11
MLGOHeuristics.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021 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  */
27 #include "tests/framework/Macros.h"
28 
29 using namespace arm_compute::mlgo;
30 
31 namespace arm_compute
32 {
33 namespace test
34 {
35 namespace validation
36 {
37 TEST_SUITE(CL)
38 TEST_SUITE(UNIT)
40 TEST_CASE(CorrectDotMLGOShouldLoadCorrectly, framework::DatasetMode::ALL)
41 {
42  std::string mlgo_str = R"_(
43 
44  <header>
45 
46  gemm-version, [1,2,1]
47  ip-type,gpu
48  </header>
49  <heuristics-table>
50  0, g76 , 8, f32, best-performance, static, gemm-type, [m,n,k,n]
51 
52  1, g71 , 8, f16, best-performance, static, gemm-config-reshaped-only-rhs, [m,n,k,n]
53  2, g76 , 8, f16, best-performance, static, gemm-config-reshaped, [m,n,k,n]
54  </heuristics-table>
55  <heuristic, 0>
56  b , 0, var, m, ==, num, 10., 1, 2
57  l , 1, gemm-type, reshaped
58  b , 2, var, r_mn, >=, num, 2., 3, 6
59 
60  b , 3, var, n, >=, num, 200., 4, 5
61  l, 4, gemm-type, reshaped-only-rhs
62  l , 5, gemm-type, reshaped
63  l , 6, gemm-type, reshaped-only-rhs
64  </heuristic>
65  <heuristic, 1>
66  b ,0,var, n, >, num, 100., 1, 4
67  b ,1,var, r_mnk, <=, num, 20., 2, 3
68 
69 
70  l ,2,gemm-config-reshaped-only-rhs, [4, 4,4,2,1,0,1]
71  l ,3,gemm-config-reshaped-only-rhs,[ 2, 2,4,2,1,1, 1 ]
72  b ,4,var, n, >=, num, 199.12, 5, 6
73  l ,5,gemm-config-reshaped-only-rhs, [1, 4,3,4,0,0,0]
74  l ,6,gemm-config-reshaped-only-rhs, [5, 4,4,5,1,1,0]
75  </heuristic>
76 
77  <heuristic, 2>
78  l ,0,gemm-config-reshaped,[4,2,4,2,8,1,0,1,0]
79 
80  </heuristic>
81 
82  )_";
83  std::stringstream ss(mlgo_str);
85  heuristics.reload_from_stream(ss);
86 
87  ARM_COMPUTE_EXPECT(heuristics.query_gemm_type(Query{ "g76", DataType::F32, 10, 1024, 20, 1 }).second == GEMMType::RESHAPED, framework::LogLevel::ERRORS);
88  ARM_COMPUTE_EXPECT(heuristics.query_gemm_type(Query{ "g76", DataType::F32, 400, 201, 5, 1 }).second == GEMMType::RESHAPED_ONLY_RHS, framework::LogLevel::ERRORS);
89  ARM_COMPUTE_EXPECT(heuristics.query_gemm_type(Query{ "g76", DataType::F32, 400, 200, 199, 16 }).second == GEMMType::RESHAPED_ONLY_RHS, framework::LogLevel::ERRORS);
90  ARM_COMPUTE_EXPECT(heuristics.query_gemm_type(Query{ "g76", DataType::F32, 400, 199, 512, 4 }).second == GEMMType::RESHAPED, framework::LogLevel::ERRORS);
91 
92  ARM_COMPUTE_EXPECT((heuristics.query_gemm_config_reshaped_only_rhs(Query{ "g71", DataType::F16, 100, 1024, 20, 32 }).second == GEMMConfigReshapedOnlyRHS{ 4, 4, 4, 2, true, false, true }),
93  framework::LogLevel::ERRORS);
94  ARM_COMPUTE_EXPECT((heuristics.query_gemm_config_reshaped_only_rhs(Query{ "g71", DataType::F16, 100, 1024, 20, 32 }).second == GEMMConfigReshapedOnlyRHS{ 4, 4, 4, 2, true, false, true }),
95  framework::LogLevel::ERRORS);
96  ARM_COMPUTE_EXPECT((heuristics.query_gemm_config_reshaped_only_rhs(Query{ "g71", DataType::F16, 128, 101, 20, 1 }).second == GEMMConfigReshapedOnlyRHS{ 2, 2, 4, 2, true, true, true }),
97  framework::LogLevel::ERRORS);
98  ARM_COMPUTE_EXPECT((heuristics.query_gemm_config_reshaped_only_rhs(Query{ "g71", DataType::F16, 400, 100, 512, 1 }).second == GEMMConfigReshapedOnlyRHS{ 5, 4, 4, 5, true, true, false }),
99  framework::LogLevel::ERRORS);
100  ARM_COMPUTE_EXPECT((heuristics.query_gemm_config_reshaped_only_rhs(Query{ "g71", DataType::F16, 400, 100, 512, 1 }).second == GEMMConfigReshapedOnlyRHS{ 5, 4, 4, 5, true, true, false }),
101  framework::LogLevel::ERRORS);
102 
103  ARM_COMPUTE_EXPECT((heuristics.query_gemm_config_reshaped(Query{ "g76", DataType::F16, 100, 100, 20, 32 }).second == GEMMConfigReshaped{ 4, 2, 4, 2, 8, true, false, true, false }),
104  framework::LogLevel::ERRORS);
105  ARM_COMPUTE_EXPECT((heuristics.query_gemm_config_reshaped(Query{ "g76", DataType::F16, 128, 512, 1024, 1 }).second == GEMMConfigReshaped{ 4, 2, 4, 2, 8, true, false, true, false }),
106  framework::LogLevel::ERRORS);
107 }
108 
109 TEST_CASE(InvalidDotmlgoSyntaxShouldReturnInvalidStatus, framework::DatasetMode::ALL)
110 {
111  std::string mlgo_str = R"_(
112  <header>
113  gemm-version, [1,2,1]
114  ip-type,pu
115  </header>
116  <heuristics-table>
117  0, g76 , 8, f32, best-performance, static, gemm-config-reshaped, [m,n,k,n]
118 
119  </heurist
120  <heuristic, 0>
121  l ,0,gemm-config-reshaped,[4,2,4,2,8,1,0,1,0]
122  </heuristic>
123  )_";
124  std::stringstream ss(mlgo_str);
126  ARM_COMPUTE_EXPECT(!heuristics.reload_from_stream(ss), framework::LogLevel::ERRORS);
127 }
128 
129 TEST_SUITE(InvalidDotmlgoSemanticsShouldReturnInvalidStatus)
130 // If the semantics errors are local to some trees instead of the entire heuristics, an alternative is to simply
131 // ignore/remove those invalid trees. However the reason why we choose to throw, thus invalidating the entire
132 // heuristics is that if there are some invalid trees, the quality of the dotmlgo is called into question even if
133 // the rest of the trees are semantically valid, and they could severely degrade the performance of GEMM. Therefore
134 // this "all or nothing" approach when it comes to dotmlgo correctness is safer and more defensive.
135 
136 // Also note that the semantic error of the tree only refers to those that obstruct its evaluation and thus query,
137 // (e.g. invalid tree structure, unsupported features etc.) instead of those affecting the desired outcome
138 // (usually in terms of final GEMM performance, e.g. the effectiveness of the decision tree)
139 
140 // In the future we might want to check the content of the exceptions as well. But right now it suffices to only
141 // know that it throws exactly when it needs to.
142 TEST_CASE(MismatchesBetweenHeuristicsTableEntriesAndHeuristicTrees, framework::DatasetMode::ALL)
143 {
144  {
145  // Mismatching number of entries 1
146  std::string mlgo_str = R"_(
147  <header>
148  gemm-version, [1,2,1]
149  ip-type,gpu
150  </header>
151  <heuristics-table>
152 
153  0, g76 , 8, f32, best-performance, static, gemm-config-reshaped, [m,n,k,n]
154 
155  </heuristics-table>
156  )_";
157  std::stringstream ss(mlgo_str);
159  // NOTE: This case might throw an internal error as the tree inserted by the heuristics-table cannot not be checked
160  ARM_COMPUTE_EXPECT(!heuristics.reload_from_stream(ss), framework::LogLevel::ERRORS);
161  }
162 
163  {
164  // Mismatching number of entries 2
165  std::string mlgo_str = R"_(
166  <header>
167  gemm-version, [1,2,1]
168  ip-type,gpu
169  </header>
170  <heuristics-table>
171  </heuristics-table>
172  <heuristic, 1>
173  l ,0,gemm-config-reshaped,[4,2,4,2,8,1,0,1,0]
174  </heuristic>
175  )_";
176  std::stringstream ss(mlgo_str);
178  ARM_COMPUTE_EXPECT(!heuristics.reload_from_stream(ss), framework::LogLevel::ERRORS);
179  }
180 
181  {
182  // Mismatching info
183  std::string mlgo_str = R"_(
184  <header>
185  gemm-version, [1,2,1]
186  ip-type,gpu
187  </header>
188  <heuristics-table>
189  0, g76 , 8, f32, best-performance, static, gemm-type, [m,n,k,n]
190  </heuristics-table>
191  <heuristic, 0>
192  l ,0,gemm-config-reshaped,[4,2,4,2,8,1,0,1,0]
193  </heuristic>
194  )_";
195  std::stringstream ss(mlgo_str);
197  ARM_COMPUTE_EXPECT(!heuristics.reload_from_stream(ss), framework::LogLevel::ERRORS);
198  }
199 }
200 
201 TEST_CASE(RepeatedHeuristicsTableEntriesId, framework::DatasetMode::ALL)
202 {
203  std::string mlgo_str = R"_(
204  <header>
205  gemm-version, [1,2,1]
206  ip-type,gpu
207  </header>
208  <heuristics-table>
209  0, g76 , 8, f32, best-performance, static, gemm-config-reshaped, [m,n,k,n]
210  0, g71 , 8, f32, best-performance, static, gemm-config-reshaped, [m,n,k,n]
211  </heuristics-table>
212  <heuristic, 0>
213  l ,0,gemm-config-reshaped,[4,2,4,2,8,1,0,1,0]
214  </heuristic>
215  <heuristic, 1>
216  l ,0,gemm-config-reshaped,[4,2,4,2,8,1,0,1,0]
217  </heuristic>
218  )_";
219  std::stringstream ss(mlgo_str);
221  ARM_COMPUTE_EXPECT(!heuristics.reload_from_stream(ss), framework::LogLevel::ERRORS);
222 }
223 
224 TEST_CASE(RepeatedHeuristicsTableEntriesIndex, framework::DatasetMode::ALL)
225 {
226  std::string mlgo_str = R"_(
227  <header>
228  gemm-version, [1,2,1]
229  ip-type,gpu
230  </header>
231  <heuristics-table>
232  0, g76 , 8, f32, best-performance, static, gemm-config-reshaped, [m,n,k,n]
233  1, g76 , 8, f32, best-performance, static, gemm-config-reshaped, [m,n,k,n]
234  </heuristics-table>
235  <heuristic, 0>
236  l ,0,gemm-config-reshaped,[4,2,4,2,8,1,0,1,0]
237  </heuristic>
238  <heuristic, 1>
239  l ,0,gemm-config-reshaped,[4,2,4,2,8,1,0,1,0]
240  </heuristic>
241  )_";
242  std::stringstream ss(mlgo_str);
244  ARM_COMPUTE_EXPECT(!heuristics.reload_from_stream(ss), framework::LogLevel::ERRORS);
245 }
246 
247 TEST_CASE(RepeatedHeuristicTreesId, framework::DatasetMode::ALL)
248 {
249  std::string mlgo_str = R"_(
250  <header>
251  gemm-version, [1,2,1]
252  ip-type,gpu
253  </header>
254  <heuristics-table>
255  0, g76 , 8, f32, best-performance, static, gemm-config-reshaped, [m,n,k,n]
256  1, g71 , 8, f32, best-performance, static, gemm-config-reshaped, [m,n,k,n]
257  </heuristics-table>
258  <heuristic, 0>
259  l ,0,gemm-config-reshaped,[4,2,4,2,8,1,0,1,0]
260  </heuristic>
261  <heuristic, 0>
262  l ,0,gemm-config-reshaped,[4,2,4,2,8,1,0,1,0]
263  </heuristic>
264  )_";
265  std::stringstream ss(mlgo_str);
267  ARM_COMPUTE_EXPECT(!heuristics.reload_from_stream(ss), framework::LogLevel::ERRORS);
268 }
269 TEST_CASE(EmptyTree, framework::DatasetMode::ALL)
270 {
271  std::string mlgo_str = R"_(
272  <header>
273  gemm-version, [1,2,1]
274  ip-type,gpu
275  </header>
276  <heuristics-table>
277  0, g76 , 8, f32, best-performance, static, gemm-config-reshaped, [m,n,k,n]
278  </heuristics-table>
279  <heuristic, 0>
280  </heuristic>
281  )_";
282  std::stringstream ss(mlgo_str);
284  ARM_COMPUTE_EXPECT(!heuristics.reload_from_stream(ss), framework::LogLevel::ERRORS);
285 }
286 
287 TEST_CASE(InvalidTreeMissingRoot, framework::DatasetMode::ALL)
288 {
289  std::string mlgo_str = R"_(
290  <header>
291  gemm-version, [1,2,1]
292  ip-type,gpu
293  </header>
294  <heuristics-table>
295  0, g76 , 8, f32, best-performance, static, gemm-config-reshaped, [m,n,k,n]
296  </heuristics-table>
297  <heuristic, 0>
298  b ,2, var, m, ==, num, 10., 3, 4
299  l ,3,gemm-config-reshaped,[4,2,4,2,8,1,0,1,0]
300  l ,4,gemm-config-reshaped,[4,2,4,2,8,1,0,1,0]
301  </heuristic>
302  )_";
303  std::stringstream ss(mlgo_str);
305  ARM_COMPUTE_EXPECT(!heuristics.reload_from_stream(ss), framework::LogLevel::ERRORS);
306 }
307 TEST_CASE(InvalidTreeMissingNodes, framework::DatasetMode::ALL)
308 {
309  std::string mlgo_str = R"_(
310  <header>
311  gemm-version, [1,2,1]
312  ip-type,gpu
313  </header>
314  <heuristics-table>
315  0, g76 , 8, f32, best-performance, static, gemm-config-reshaped, [m,n,k,n]
316  </heuristics-table>
317  <heuristic, 0>
318  b ,0, var, m, ==, num, 10., 1, 2
319  l ,1,gemm-config-reshaped,[4,2,4,2,8,1,0,1,0]
320  </heuristic>
321  )_";
322  std::stringstream ss(mlgo_str);
324  ARM_COMPUTE_EXPECT(!heuristics.reload_from_stream(ss), framework::LogLevel::ERRORS);
325 }
326 TEST_CASE(InvalidTreeRepeatedNodeIds, framework::DatasetMode::ALL)
327 {
328  std::string mlgo_str = R"_(
329  <header>
330  gemm-version, [1,2,1]
331  ip-type,gpu
332  </header>
333  <heuristics-table>
334  0, g76 , 8, f32, best-performance, static, gemm-config-reshaped, [m,n,k,n]
335  </heuristics-table>
336  <heuristic, 0>
337  b ,0, var, m, ==, num, 10., 1, 2
338  l ,1,gemm-config-reshaped,[4,2,4,2,8,1,0,1,0]
339  l ,1,gemm-config-reshaped,[1,2,4,2,8,1,0,1,0]
340  l ,2,gemm-config-reshaped,[2,2,4,2,8,1,0,1,0]
341  </heuristic>
342  )_";
343  std::stringstream ss(mlgo_str);
345  ARM_COMPUTE_EXPECT(!heuristics.reload_from_stream(ss), framework::LogLevel::ERRORS);
346 }
347 TEST_CASE(InvalidTreeDisjointNodes, framework::DatasetMode::ALL)
348 {
349  std::string mlgo_str = R"_(
350  <header>
351  gemm-version, [1,2,1]
352  ip-type,gpu
353  </header>
354  <heuristics-table>
355  0, g76 , 8, f32, best-performance, static, gemm-config-reshaped, [m,n,k,n]
356  </heuristics-table>
357  <heuristic, 0>
358  b ,0, var, m, ==, num, 10., 1, 2
359  l ,1,gemm-config-reshaped,[4,2,4,2,8,1,0,1,0]
360  l ,2,gemm-config-reshaped,[2,2,4,2,8,1,0,1,0]
361 
362  b ,4, var, n, ==, num, 10., 5, 6
363  l ,5,gemm-config-reshaped,[4,2,4,2,8,1,0,1,0]
364  l ,6,gemm-config-reshaped,[2,2,4,2,8,1,0,1,0]
365 
366  l ,7,gemm-config-reshaped,[2,2,4,2,8,1,0,1,0]
367  </heuristic>
368  )_";
369  std::stringstream ss(mlgo_str);
371  ARM_COMPUTE_EXPECT(!heuristics.reload_from_stream(ss), framework::LogLevel::ERRORS);
372 }
373 TEST_CASE(InvalidTreeLoop, framework::DatasetMode::ALL)
374 {
375  std::string mlgo_str = R"_(
376  <header>
377  gemm-version, [1,2,1]
378  ip-type,gpu
379  </header>
380  <heuristics-table>
381  0, g76 , 8, f32, best-performance, static, gemm-config-reshaped, [m,n,k,n]
382  </heuristics-table>
383  <heuristic, 0>
384  b ,0, var, m, ==, num, 10., 0, 1
385  l ,1,gemm-config-reshaped,[4,2,4,2,8,1,0,1,0]
386  </heuristic>
387  )_";
388  std::stringstream ss(mlgo_str);
390  ARM_COMPUTE_EXPECT(!heuristics.reload_from_stream(ss), framework::LogLevel::ERRORS);
391 }
392 TEST_CASE(InvalidTreeCycle, framework::DatasetMode::ALL)
393 {
394  std::string mlgo_str = R"_(
395  <header>
396  gemm-version, [1,2,1]
397  ip-type,gpu
398  </header>
399  <heuristics-table>
400  0, g76 , 8, f32, best-performance, static, gemm-config-reshaped, [m,n,k,n]
401  </heuristics-table>
402  <heuristic, 0>
403  b ,0, var, m, ==, num, 10., 1, 5
404  b ,1, var, n, ==, num, 10., 2, 3
405  l ,2,gemm-config-reshaped,[4,2,4,2,8,1,0,1,0]
406  b ,3, var, k, ==, num, 10., 0, 4
407  l ,4,gemm-config-reshaped,[4,2,4,2,8,1,0,1,0]
408  l ,5,gemm-config-reshaped,[4,2,4,2,8,1,0,1,0]
409  </heuristic>
410  )_";
411  std::stringstream ss(mlgo_str);
413  ARM_COMPUTE_EXPECT(!heuristics.reload_from_stream(ss), framework::LogLevel::ERRORS);
414 }
415 TEST_CASE(InvalidTreeInvalidFeatures, framework::DatasetMode::ALL)
416 {
417  std::string mlgo_str = R"_(
418  <header>
419  gemm-version, [1,2,1]
420  ip-type,gpu
421  </header>
422  <heuristics-table>
423  0, g76 , 8, f32, best-performance, static, gemm-config-reshaped, [m,n,k,n]
424  </heuristics-table>
425  <heuristic, 0>
426  b ,0, var, magic_feature, ==, num, 10., 1, 2
427  l ,1,gemm-config-reshaped,[4,2,4,2,8,1,0,1,0]
428  l ,2,gemm-config-reshaped,[4,2,4,2,8,1,0,1,0]
429  </heuristic>
430  )_";
431  std::stringstream ss(mlgo_str);
433  ARM_COMPUTE_EXPECT(!heuristics.reload_from_stream(ss), framework::LogLevel::ERRORS);
434 }
435 TEST_SUITE_END() // InvalidDotmlgoSemanticsShouldReturnInvalidStatus
436 
437 TEST_CASE(InvalidUsageOfHeuristicsShouldReturnInvalidStatus, framework::DatasetMode::ALL)
438 {
439  std::string mlgo_str = R"_(
440  <header>
441  gemm-version, [1,2,1]
442  ip-type,gpu
443  </header>
444  <heuristics-table>
445  0, g76 , 8, f32, best-performance, static, gemm-type, [m,n,k,n]
446  </heuristics-table>
447  <heuristic, 0>
448  b , 0, var, m, ==, num, 10., 1, 2
449  l , 1, gemm-type, reshaped
450  b , 2, var, r_mn, >=, num, 2., 3, 6
451  b , 3, var, n, >=, num, 200., 4, 5
452  l , 4, gemm-type, reshaped-only-rhs
453  l , 5, gemm-type, reshaped
454  l , 6, gemm-type, reshaped-only-rhs
455  </heuristic>
456  )_";
457  std::stringstream ss(mlgo_str);
459  ARM_COMPUTE_EXPECT(heuristics.reload_from_stream(ss), framework::LogLevel::ERRORS);
460 
461  // Querying unavailable heuristic type should return invalid Status
462  ARM_COMPUTE_EXPECT(!heuristics.query_gemm_config_reshaped(Query{ "g76", DataType::F32, 1024, 1024, 100, 3 }).first, framework::LogLevel::ERRORS);
463  // Querying unavailable ip target should return invalid Status
464  ARM_COMPUTE_EXPECT(!heuristics.query_gemm_type(Query{ "g77", DataType::F32, 1024, 1024, 100, 3 }).first, framework::LogLevel::ERRORS);
465  // Querying unavailable data type should return invalid Status
466  ARM_COMPUTE_EXPECT(!heuristics.query_gemm_config_reshaped_only_rhs(Query{ "g76", DataType::QASYMM8, 1024, 1024, 100, 3 }).first, framework::LogLevel::ERRORS);
467 }
468 TEST_SUITE_END() // MLGOHeuristics
469 TEST_SUITE_END() // UNIT
470 TEST_SUITE_END() // CL
471 } // namespace validation
472 } // namespace test
473 } // namespace arm_compute
Query interface.
GEMM Configuration for Reshaped kernel.
Definition: Common.h:66
std::stringstream ss(mlgo_str)
Reshaped GEMM kernel where only the rhs matrix is reshaped.
Reshaped GEMM kernel where both lhs and rhs matrices are reshaped.
std::pair< bool, GEMMType > query_gemm_type(const Query &query) const
Query the gemm type.
Copyright (c) 2017-2022 Arm Limited.
#define TEST_SUITE(SUITE_NAME)
Definition: Macros.h:34
#define TEST_CASE(TEST_NAME, MODE)
Definition: Macros.h:180
DatasetMode
Possible dataset modes.
Definition: DatasetModes.h:40
GEMM Configuration for Reshaped Only RHS kernel.
Definition: Common.h:54
MLGOHeuristics for configuring GEMM kernels.
std::pair< bool, GEMMConfigReshapedOnlyRHS > query_gemm_config_reshaped_only_rhs(const Query &query) const
Query the gemm configuration for reshaped only rhs kernel.
#define ARM_COMPUTE_EXPECT(X, LEVEL)
Definition: Asserts.h:131
bool reload_from_stream(std::istream &istream)
(Re)Load the heuristics from reading an input stream
std::pair< bool, GEMMConfigReshaped > query_gemm_config_reshaped(const Query &query) const
Query the gemm configuration for reshaped kernel.
#define TEST_SUITE_END()
Definition: Macros.h:39