• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
24 #include "src/runtime/CL/mlgo/MLGOParser.h"
25 #include "arm_compute/core/Log.h"
26 #include "src/runtime/CL/mlgo/Utils.h"
27 
28 #include <sstream>
29 
30 #define CHECK(parser_expr, valid_var) \
31     (parser_expr);                    \
32     if(!valid_var)                    \
33         return;
34 
35 #define CHECK_DEFAULT(parser_expr, valid_var, default_val) \
36     (parser_expr);                                         \
37     if(!valid_var)                                         \
38         return default_val;
39 
40 #ifdef ARM_COMPUTE_LOGGING_ENABLED
41 
42 #define FAIL_WITH_MSG(valid_var, pos, msg)           \
43     std::stringstream ss;                            \
44     ss << "MLGOParser Error: " << pos << " " << msg; \
45     ARM_COMPUTE_LOG_INFO_MSG_CORE(ss.str().c_str()); \
46     valid_var = false;                               \
47     return;
48 
49 #define FAIL_WITH_MSG_DEFAULT(valid_var, default_val, pos, msg) \
50     std::stringstream ss;                                       \
51     ss << "MLGOParser Error: " << pos << " " << msg;            \
52     ARM_COMPUTE_LOG_INFO_MSG_CORE(ss.str().c_str());            \
53     valid_var = false;                                          \
54     return default_val;
55 
56 #define LOG_TOKEN_POS(tokens, pos_var) \
57     const auto pos_var = tokens.current_pos();
58 
59 #else // ARM_COMPUTE_LOGGING_ENABLED
60 
61 #define FAIL_WITH_MSG(valid_var, pos, msg) \
62     valid_var = false;                     \
63     return;
64 
65 #define FAIL_WITH_MSG_DEFAULT(valid_var, default_val, pos, msg) \
66     valid_var = false;                                          \
67     return default_val;
68 
69 #define LOG_TOKEN_POS(tokens, pos_var)
70 
71 #endif // ARM_COMPUTE_LOGGING_ENABLED
72 namespace
73 {
ltrim(std::string & str)74 void ltrim(std::string &str)
75 {
76     str.erase(str.begin(), std::find_if(str.begin(), str.end(), [](char ch)
77     {
78         return !std::isspace(ch);
79     }));
80 }
81 
rtrim(std::string & str)82 void rtrim(std::string &str)
83 {
84     str.erase(std::find_if(str.rbegin(), str.rend(), [](char ch)
85     {
86         return !std::isspace(ch);
87     }).base(),
88     str.end());
89 }
90 
trim(std::string & str)91 void trim(std::string &str)
92 {
93     ltrim(str);
94     rtrim(str);
95 }
96 } // namespace
97 
98 namespace arm_compute
99 {
100 namespace mlgo
101 {
102 namespace parser
103 {
104 enum class ComparatorType
105 {
106     Enum,
107     Num,
108     Var
109 };
110 
TokenStream(std::istream & s,const std::string & delims)111 TokenStream::TokenStream(std::istream &s, const std::string &delims)
112     : _delims{ delims }, _istream{ s }, _tokens{}, _lookahead_pos{}
113 {
114     read();
115 }
116 
operator bool() const117 TokenStream::operator bool() const
118 {
119     ARM_COMPUTE_ERROR_ON_MSG(_tokens.empty(), "TokenStream can never be empty");
120     return !reached_end();
121 }
122 
take()123 Token TokenStream::take()
124 {
125     ARM_COMPUTE_ERROR_ON_MSG(_tokens.empty(), "TokenStream can never be empty");
126     Token t = _tokens.front();
127     _tokens.pop_front();
128     if(_tokens.empty())
129     {
130         read();
131     }
132     return t;
133 }
peek(size_t i)134 Token TokenStream::peek(size_t i)
135 {
136     ARM_COMPUTE_ERROR_ON_MSG(_tokens.empty(), "TokenStream can never be empty");
137     ARM_COMPUTE_ERROR_ON_MSG(i >= max_look_ahead, "TokenStream: Exceeding max look ahead");
138     // NOTE: If i exceeds the stream (_istream.eof()), read() automatically appends a End token at the end
139     while(_istream && _tokens.size() <= i)
140     {
141         read();
142     }
143     size_t ind = std::min(i, _tokens.size() - 1);
144     return _tokens[ind];
145 }
146 
advance(CharPosition & pos,char ch)147 void advance(CharPosition &pos, char ch)
148 {
149     if(ch == '\n')
150     {
151         pos.ln += 1;
152         pos.col = 0;
153     }
154     else
155     {
156         pos.col += 1;
157     }
158 }
rewind(CharPosition & pos)159 void rewind(CharPosition &pos)
160 {
161     pos.col -= 1;
162 }
read()163 void TokenStream::read()
164 {
165     char ch;
166     // Skip any leading space and delim characters
167     do
168     {
169         // Reached eof
170         if(!_istream.get(ch))
171         {
172             if(!reached_end())
173             {
174                 _tokens.emplace_back(TokenType::End, "", _lookahead_pos);
175             }
176             return;
177         }
178         advance(_lookahead_pos, ch);
179     }
180     while(std::isspace(ch) || is_delim(ch));
181     // Read chars until we hit a delim or eof
182     auto orig_pos = _lookahead_pos;
183     auto tok      = recognize_tok(ch);
184     rewind(orig_pos);
185     tok.pos = orig_pos;
186     // Trim leading and trailing white spaces
187     trim(tok.value);
188     _tokens.push_back(tok);
189 }
190 
recognize_tok(char ch)191 Token TokenStream::recognize_tok(char ch)
192 {
193     if(ch == '[')
194     {
195         return Token{ TokenType::L_List, "", _lookahead_pos };
196     }
197     else if(ch == ']')
198     {
199         return Token{ TokenType::R_List, "", _lookahead_pos };
200     }
201     else if(ch == '.')
202     {
203         return float_after_dp_st(std::string{ ch });
204     }
205     else if(std::isdigit(ch))
206     {
207         return num_st(std::string{ ch });
208     }
209     else
210     {
211         return text_st(std::string{ ch });
212     }
213 }
214 
num_st(std::string value)215 Token TokenStream::num_st(std::string value)
216 {
217     char ch{};
218     while(_istream.get(ch))
219     {
220         advance(_lookahead_pos, ch);
221         if(ch == '.')
222         {
223             return float_after_dp_st(value + ch);
224         }
225         else if(!std::isdigit(ch))
226         {
227             if(!is_delim(ch) && !std::isspace(ch))
228             {
229                 rewind(_lookahead_pos);
230                 _istream.unget();
231             }
232             break;
233         }
234         value += ch;
235     }
236     return Token{ TokenType::Int, value, _lookahead_pos };
237 }
238 
float_after_dp_st(std::string value)239 Token TokenStream::float_after_dp_st(std::string value)
240 {
241     char ch{};
242     while(_istream.get(ch))
243     {
244         advance(_lookahead_pos, ch);
245         if(!std::isdigit(ch))
246         {
247             if(!is_delim(ch) && !std::isspace(ch))
248             {
249                 rewind(_lookahead_pos);
250                 _istream.unget();
251             }
252             break;
253         }
254         value += ch;
255     }
256     return Token{ TokenType::Float, value, _lookahead_pos };
257 }
258 
text_st(std::string value)259 Token TokenStream::text_st(std::string value)
260 {
261     char ch{};
262     while(_istream.get(ch))
263     {
264         advance(_lookahead_pos, ch);
265         if(is_delim(ch))
266         {
267             break;
268         }
269         if(ch == '[' || ch == ']')
270         {
271             rewind(_lookahead_pos);
272             _istream.unget();
273             break;
274         }
275         value += ch;
276     }
277     return Token{ TokenType::Text, value, _lookahead_pos };
278 }
279 
reached_end() const280 bool TokenStream::reached_end() const
281 {
282     return _tokens.size() == 1 && _tokens.front().type == TokenType::End;
283 }
284 
is_delim(char ch) const285 bool TokenStream::is_delim(char ch) const
286 {
287     return _delims.find(ch) != std::string::npos;
288 }
289 
end(TokenStream & in,bool & valid)290 void end(TokenStream &in, bool &valid)
291 {
292     LOG_TOKEN_POS(in, pos);
293     auto tok = in.take();
294     if(tok.type != TokenType::End)
295     {
296         FAIL_WITH_MSG(valid, pos, "Unexpected token at the end of stream");
297     }
298 }
299 
bool_val(TokenStream & in,bool & valid)300 bool bool_val(TokenStream &in, bool &valid)
301 {
302     LOG_TOKEN_POS(in, pos);
303     auto tok = in.take();
304     if(tok.type != TokenType::Int)
305     {
306         FAIL_WITH_MSG_DEFAULT(valid, false, pos, "Expect bool or int token");
307     }
308     bool val{};
309     std::stringstream(tok.value) >> val;
310     return val;
311 }
312 
int_val(TokenStream & in,bool & valid)313 int int_val(TokenStream &in, bool &valid)
314 {
315     LOG_TOKEN_POS(in, pos);
316     auto tok = in.take();
317     if(tok.type != TokenType::Int)
318     {
319         FAIL_WITH_MSG_DEFAULT(valid, -1, pos, "Expect int token");
320     }
321     int val{};
322     std::stringstream(tok.value) >> val;
323     return val;
324 }
325 
uint_val(TokenStream & in,bool & valid)326 unsigned int uint_val(TokenStream &in, bool &valid)
327 {
328     LOG_TOKEN_POS(in, pos);
329     int val = CHECK_DEFAULT(int_val(in, valid), valid, 0);
330     if(val < 0)
331     {
332         FAIL_WITH_MSG_DEFAULT(valid, 0, pos, "Expect unsigned int token");
333     }
334     return static_cast<unsigned int>(val);
335 }
336 
float_val(TokenStream & in,bool & valid)337 float float_val(TokenStream &in, bool &valid)
338 {
339     LOG_TOKEN_POS(in, pos);
340     auto tok = in.take();
341     if(tok.type != TokenType::Float)
342     {
343         FAIL_WITH_MSG_DEFAULT(valid, 0.f, pos, "Expect float token");
344     }
345     float val{};
346     std::stringstream(tok.value) >> val;
347     return val;
348 }
349 
text_val(TokenStream & in,bool & valid)350 std::string text_val(TokenStream &in, bool &valid)
351 {
352     LOG_TOKEN_POS(in, pos);
353     auto tok = in.take();
354     if(tok.type != TokenType::Text || tok.value.empty())
355     {
356         FAIL_WITH_MSG_DEFAULT(valid, "", pos, "Expect a non-empty text token");
357     }
358     return tok.value;
359 }
360 
accept_text(TokenStream & in,const std::string & c_str,bool take=true)361 bool accept_text(TokenStream &in, const std::string &c_str, bool take = true)
362 {
363     auto tok = in.peek();
364     if(tok.type == TokenType::Text && tok.value == c_str)
365     {
366         if(take)
367         {
368             in.take();
369         }
370         return true;
371     }
372     return false;
373 }
374 
expect_text(TokenStream & in,const std::string & str,bool & valid)375 void expect_text(TokenStream &in, const std::string &str, bool &valid)
376 {
377     LOG_TOKEN_POS(in, pos);
378     if(!accept_text(in, str))
379     {
380         FAIL_WITH_MSG(valid, pos, std::string("Expect text token: ") + str);
381     }
382 }
383 
accept_l_list(TokenStream & in)384 bool accept_l_list(TokenStream &in)
385 {
386     auto tok = in.peek();
387     if(tok.type == TokenType::L_List)
388     {
389         in.take();
390         return true;
391     }
392     return false;
393 }
394 
expect_l_list(TokenStream & in,bool & valid)395 void expect_l_list(TokenStream &in, bool &valid)
396 {
397     LOG_TOKEN_POS(in, pos);
398     if(!accept_l_list(in))
399     {
400         FAIL_WITH_MSG(valid, pos, "Expect '['");
401     }
402 }
403 
accept_r_list(TokenStream & in)404 bool accept_r_list(TokenStream &in)
405 {
406     auto tok = in.peek();
407     if(tok.type == TokenType::R_List)
408     {
409         in.take();
410         return true;
411     }
412     return false;
413 }
414 
expect_r_list(TokenStream & in,bool & valid)415 void expect_r_list(TokenStream &in, bool &valid)
416 {
417     LOG_TOKEN_POS(in, pos);
418     if(!accept_r_list(in))
419     {
420         FAIL_WITH_MSG(valid, pos, "Expect ']'");
421     }
422 }
423 
conditional_op(TokenStream & in,bool & valid)424 ConditionalOp conditional_op(TokenStream &in, bool &valid)
425 {
426     LOG_TOKEN_POS(in, pos);
427     if(accept_text(in, "<="))
428     {
429         return ConditionalOp::LE;
430     }
431     else if(accept_text(in, ">="))
432     {
433         return ConditionalOp::GE;
434     }
435     else if(accept_text(in, "=="))
436     {
437         return ConditionalOp::EQ;
438     }
439     else if(accept_text(in, "<"))
440     {
441         return ConditionalOp::LT;
442     }
443     else if(accept_text(in, ">"))
444     {
445         return ConditionalOp::GT;
446     }
447     else
448     {
449         FAIL_WITH_MSG_DEFAULT(valid, ConditionalOp::EQ, pos, "Expect conditional op");
450     }
451 }
452 
gemm_version(TokenStream & in,bool & valid)453 void gemm_version(TokenStream &in, bool &valid)
454 {
455     CHECK(expect_text(in, "gemm-version", valid), valid);
456     CHECK(expect_l_list(in, valid), valid);
457     CHECK(uint_val(in, valid), valid);
458     CHECK(uint_val(in, valid), valid);
459     CHECK(uint_val(in, valid), valid);
460     CHECK(expect_r_list(in, valid), valid);
461 }
462 
ip_type(TokenStream & in,bool & valid)463 void ip_type(TokenStream &in, bool &valid)
464 {
465     CHECK(expect_text(in, "ip-type", valid), valid);
466     LOG_TOKEN_POS(in, pos);
467     if(accept_text(in, "gpu"))
468     {
469         ;
470     }
471     else if(accept_text(in, "cpu"))
472     {
473         ;
474     }
475     else
476     {
477         FAIL_WITH_MSG(valid, pos, "Expect ip type");
478     }
479 }
480 
header(TokenStream & in,bool & valid)481 void header(TokenStream &in, bool &valid)
482 {
483     CHECK(expect_text(in, "<header>", valid), valid);
484     CHECK(gemm_version(in, valid), valid);
485     CHECK(ip_type(in, valid), valid);
486     CHECK(expect_text(in, "</header>", valid), valid);
487 }
488 
data_type(TokenStream & in,bool & valid)489 DataType data_type(TokenStream &in, bool &valid)
490 {
491     LOG_TOKEN_POS(in, pos);
492     if(accept_text(in, "f16"))
493     {
494         return DataType::F16;
495     }
496     else if(accept_text(in, "f32"))
497     {
498         return DataType::F32;
499     }
500     else if(accept_text(in, "qasymm8"))
501     {
502         return DataType::QASYMM8;
503     }
504     else
505     {
506         FAIL_WITH_MSG_DEFAULT(valid, DataType::QASYMM8, pos, "Expect data type");
507     }
508 }
509 
comparator_type(TokenStream & in,bool & valid)510 ComparatorType comparator_type(TokenStream &in, bool &valid)
511 {
512     LOG_TOKEN_POS(in, pos);
513     if(accept_text(in, "var"))
514     {
515         return ComparatorType::Var;
516     }
517     else if(accept_text(in, "num"))
518     {
519         return ComparatorType::Num;
520     }
521     else if(accept_text(in, "enum"))
522     {
523         return ComparatorType::Enum;
524     }
525     else
526     {
527         FAIL_WITH_MSG_DEFAULT(valid, ComparatorType::Num, pos, "Expect comparator type");
528     }
529 }
530 
heuristic_type(TokenStream & in,bool & valid,bool take=true)531 HeuristicType heuristic_type(TokenStream &in, bool &valid, bool take = true)
532 {
533     LOG_TOKEN_POS(in, pos);
534     if(accept_text(in, "gemm-type", take))
535     {
536         return HeuristicType::GEMM_Type;
537     }
538     else if(accept_text(in, "gemm-config-native", take))
539     {
540         return HeuristicType::GEMM_Config_Native;
541     }
542     else if(accept_text(in, "gemm-config-reshaped-only-rhs", take))
543     {
544         return HeuristicType::GEMM_Config_Reshaped_Only_RHS;
545     }
546     else if(accept_text(in, "gemm-config-reshaped", take))
547     {
548         return HeuristicType::GEMM_Config_Reshaped;
549     }
550     else
551     {
552         FAIL_WITH_MSG_DEFAULT(valid, HeuristicType::GEMM_Config_Reshaped, pos, "Expect heuristic type");
553     }
554 }
555 
expect_heuristic_type(TokenStream & in,HeuristicType expected_ht,bool & valid)556 void expect_heuristic_type(TokenStream &in, HeuristicType expected_ht, bool &valid)
557 {
558     LOG_TOKEN_POS(in, pos);
559     auto ht = CHECK(heuristic_type(in, valid, false), valid);
560     if(ht != expected_ht)
561     {
562         FAIL_WITH_MSG(valid, pos, "Unexpected heuristic type");
563     }
564     CHECK(heuristic_type(in, valid, true), valid);
565 }
566 
gemm_type(TokenStream & in,bool & valid)567 GEMMType gemm_type(TokenStream &in, bool &valid)
568 {
569     LOG_TOKEN_POS(in, pos);
570     if(accept_text(in, "native"))
571     {
572         return GEMMType::NATIVE;
573     }
574     else if(accept_text(in, "reshaped-only-rhs"))
575     {
576         return GEMMType::RESHAPED_ONLY_RHS;
577     }
578     else if(accept_text(in, "reshaped"))
579     {
580         return GEMMType::RESHAPED;
581     }
582     else
583     {
584         FAIL_WITH_MSG_DEFAULT(valid, GEMMType::RESHAPED_ONLY_RHS, pos, "Expect gemm type");
585     }
586 }
587 
gemm_config_native(TokenStream & in,bool & valid)588 GEMMConfigNative gemm_config_native(TokenStream &in, bool &valid)
589 {
590     const auto invalid_val = GEMMConfigNative{};
591     CHECK_DEFAULT(expect_l_list(in, valid), valid, invalid_val);
592     const auto m0 = CHECK_DEFAULT(uint_val(in, valid), valid, invalid_val);
593     const auto n0 = CHECK_DEFAULT(uint_val(in, valid), valid, invalid_val);
594     const auto k0 = CHECK_DEFAULT(uint_val(in, valid), valid, invalid_val);
595     CHECK_DEFAULT(expect_r_list(in, valid), valid, invalid_val);
596     return GEMMConfigNative{ m0, n0, k0 };
597 }
598 
gemm_config_reshaped_only_rhs(TokenStream & in,bool & valid)599 GEMMConfigReshapedOnlyRHS gemm_config_reshaped_only_rhs(TokenStream &in, bool &valid)
600 {
601     const auto invalid_val = GEMMConfigReshapedOnlyRHS{};
602     CHECK_DEFAULT(expect_l_list(in, valid), valid, invalid_val);
603     const auto m0 = CHECK_DEFAULT(uint_val(in, valid), valid, invalid_val);
604     const auto n0 = CHECK_DEFAULT(uint_val(in, valid), valid, invalid_val);
605     const auto k0 = CHECK_DEFAULT(uint_val(in, valid), valid, invalid_val);
606     const auto h0 = CHECK_DEFAULT(uint_val(in, valid), valid, invalid_val);
607     const auto ir = CHECK_DEFAULT(bool_val(in, valid), valid, invalid_val);
608     const auto tr = CHECK_DEFAULT(bool_val(in, valid), valid, invalid_val);
609     const auto ex = CHECK_DEFAULT(bool_val(in, valid), valid, invalid_val);
610     CHECK_DEFAULT(expect_r_list(in, valid), valid, invalid_val);
611     return GEMMConfigReshapedOnlyRHS{ m0, n0, k0, h0, ir, tr, ex };
612 }
613 
gemm_config_reshaped(TokenStream & in,bool & valid)614 GEMMConfigReshaped gemm_config_reshaped(TokenStream &in, bool &valid)
615 {
616     const auto invalid_val = GEMMConfigReshaped{};
617     CHECK_DEFAULT(expect_l_list(in, valid), valid, invalid_val);
618     const auto m0 = CHECK_DEFAULT(uint_val(in, valid), valid, invalid_val);
619     const auto n0 = CHECK_DEFAULT(uint_val(in, valid), valid, invalid_val);
620     const auto k0 = CHECK_DEFAULT(uint_val(in, valid), valid, invalid_val);
621     const auto v0 = CHECK_DEFAULT(uint_val(in, valid), valid, invalid_val);
622     const auto h0 = CHECK_DEFAULT(uint_val(in, valid), valid, invalid_val);
623     const auto il = CHECK_DEFAULT(bool_val(in, valid), valid, invalid_val);
624     const auto ir = CHECK_DEFAULT(bool_val(in, valid), valid, invalid_val);
625     const auto tr = CHECK_DEFAULT(bool_val(in, valid), valid, invalid_val);
626     const auto ex = CHECK_DEFAULT(bool_val(in, valid), valid, invalid_val);
627     CHECK_DEFAULT(expect_r_list(in, valid), valid, invalid_val);
628     return GEMMConfigReshaped{ m0, n0, k0, v0, h0, il, ir, tr, ex };
629 }
630 
gpu_priority(TokenStream & in,bool & valid)631 void gpu_priority(TokenStream &in, bool &valid)
632 {
633     LOG_TOKEN_POS(in, pos);
634     if(accept_text(in, "best-performance"))
635     {
636         ;
637     }
638     else if(accept_text(in, "best-memory-usage"))
639     {
640         ;
641     }
642     else
643     {
644         FAIL_WITH_MSG(valid, pos, "Expect gpu priority");
645     }
646 }
647 
gpu_behavior(TokenStream & in,bool & valid)648 void gpu_behavior(TokenStream &in, bool &valid)
649 {
650     LOG_TOKEN_POS(in, pos);
651     if(accept_text(in, "static"))
652     {
653         ;
654     }
655     else if(accept_text(in, "dynamic"))
656     {
657         ;
658     }
659     else
660     {
661         FAIL_WITH_MSG(valid, pos, "Expect ip type");
662     }
663 }
664 
free_vars(TokenStream & in,bool & valid)665 void free_vars(TokenStream &in, bool &valid)
666 {
667     CHECK(expect_l_list(in, valid), valid);
668     while(!accept_r_list(in))
669     {
670         CHECK(text_val(in, valid), valid);
671     }
672 }
673 
heuristics_table_entry(TokenStream & in,MLGOHeuristics & h,bool & valid)674 void heuristics_table_entry(TokenStream &in, MLGOHeuristics &h, bool &valid)
675 {
676     const auto id = CHECK(uint_val(in, valid), valid);
677     const auto ip = CHECK(text_val(in, valid), valid);
678     CHECK(uint_val(in, valid), valid); // Num cores
679     const auto dt = CHECK(data_type(in, valid), valid);
680     CHECK(gpu_priority(in, valid), valid);
681     CHECK(gpu_behavior(in, valid), valid);
682     const auto ht = CHECK(heuristic_type(in, valid), valid);
683     CHECK(free_vars(in, valid), valid);
684     HeuristicTree t(id, ht, ip, dt);
685     valid = CHECK(h.add_heuristic_tree(std::move(t)), valid);
686 }
687 
heuristics_table(TokenStream & in,MLGOHeuristics & h,bool & valid)688 void heuristics_table(TokenStream &in, MLGOHeuristics &h, bool &valid)
689 {
690     CHECK(expect_text(in, "<heuristics-table>", valid), valid);
691     while(!accept_text(in, "</heuristics-table>"))
692     {
693         CHECK(heuristics_table_entry(in, h, valid), valid);
694     }
695 }
696 
condition(TokenStream & in,bool & valid)697 Condition condition(TokenStream &in, bool &valid)
698 {
699     LOG_TOKEN_POS(in, pos);
700     // NOTE: Only simplified Conditions are accepted, which means the lhs comparator type is fixed to Var and that of
701     // the rhs is fixed to Num (float)
702     const auto invalid_val = Condition{};
703     const auto l_t         = CHECK_DEFAULT(comparator_type(in, valid), valid, invalid_val);
704     const auto l_v         = CHECK_DEFAULT(text_val(in, valid), valid, invalid_val);
705     const auto c_o         = CHECK_DEFAULT(conditional_op(in, valid), valid, invalid_val);
706     const auto r_t         = CHECK_DEFAULT(comparator_type(in, valid), valid, invalid_val);
707     const auto r_v         = CHECK_DEFAULT(float_val(in, valid), valid, invalid_val);
708     if(l_t != ComparatorType::Var || r_t != ComparatorType::Num)
709     {
710         FAIL_WITH_MSG_DEFAULT(valid, invalid_val, pos, "Only accept LHS type to be Var (string) and RHS type to be Num (float)");
711     }
712     return Condition{ l_v, c_o, r_v };
713 }
714 
heuristic_tree(TokenStream & in,MLGOHeuristics & h,bool & valid)715 void heuristic_tree(TokenStream &in, MLGOHeuristics &h, bool &valid)
716 {
717     CHECK(expect_text(in, "<heuristic", valid), valid);
718     const auto tree_id = CHECK(uint_val(in, valid), valid);
719     CHECK(expect_text(in, ">", valid), valid);
720     HeuristicTree *t = nullptr;
721     std::tie(valid, t) = CHECK(h.get_heuristic_tree(tree_id), valid);
722     const HeuristicType t_heuristic_type = std::get<0>(t->index());
723     while(!accept_text(in, "</heuristic>"))
724     {
725         LOG_TOKEN_POS(in, pos);
726         if(accept_text(in, "b"))
727         {
728             // Branch node
729             const auto id   = CHECK(uint_val(in, valid), valid);
730             const auto cond = CHECK(condition(in, valid), valid);
731             const auto t_id = CHECK(uint_val(in, valid), valid);
732             const auto f_id = CHECK(uint_val(in, valid), valid);
733             valid           = CHECK(t->add_branch(id, cond, t_id, f_id), valid);
734         }
735         else if(accept_text(in, "l"))
736         {
737             // Leaf node
738             const auto id = CHECK(uint_val(in, valid), valid);
739             // NOTE: Heuristic type within each tree appears to be redundant (same information can be obtained from the
740             // heuristic table). For now it remains as a step for validation.
741             LOG_TOKEN_POS(in, pos);
742             CHECK(expect_heuristic_type(in, t_heuristic_type, valid), valid);
743             switch(t_heuristic_type)
744             {
745                 case HeuristicType::GEMM_Type:
746                 {
747                     const auto g_type = CHECK(gemm_type(in, valid), valid);
748                     valid             = CHECK(t->add_leaf(id, g_type), valid);
749                     break;
750                 }
751                 case HeuristicType::GEMM_Config_Native:
752                 {
753                     const auto g_c = CHECK(gemm_config_native(in, valid), valid);
754                     valid          = CHECK(t->add_leaf(id, g_c), valid);
755                     break;
756                 }
757                 case HeuristicType::GEMM_Config_Reshaped_Only_RHS:
758                 {
759                     const auto g_c = CHECK(gemm_config_reshaped_only_rhs(in, valid), valid);
760                     valid          = CHECK(t->add_leaf(id, g_c), valid);
761                     break;
762                 }
763                 case HeuristicType::GEMM_Config_Reshaped:
764                 {
765                     const auto g_c = CHECK(gemm_config_reshaped(in, valid), valid);
766                     valid          = CHECK(t->add_leaf(id, g_c), valid);
767                     break;
768                 }
769                 default:
770                 {
771                     FAIL_WITH_MSG(valid, pos, "Unexpected heuristic type");
772                 }
773             }
774         }
775         else
776         {
777             FAIL_WITH_MSG(valid, pos, "Expect tree node type");
778         }
779     }
780     // Perform semantic checks in the middle of parsing so that it can fail fast should there be any invalidities
781     valid = CHECK(h.check_heuristic_tree(tree_id), valid);
782 }
783 
mlgo(TokenStream & in,bool & valid)784 MLGOHeuristics mlgo(TokenStream &in, bool &valid)
785 {
786     MLGOHeuristics h;
787     CHECK_DEFAULT(header(in, valid), valid, h);
788     CHECK_DEFAULT(heuristics_table(in, h, valid), valid, h);
789     while(accept_text(in, "<heuristic", false))
790     {
791         CHECK_DEFAULT(heuristic_tree(in, h, valid), valid, h);
792     }
793     CHECK_DEFAULT(end(in, valid), valid, h);
794     valid = CHECK_DEFAULT(h.check_all(), valid, h);
795     return h;
796 }
797 
parse_mlgo(std::istream & in)798 std::pair<bool, MLGOHeuristics> parse_mlgo(std::istream &in)
799 {
800     auto tokens = TokenStream(in);
801     bool valid  = true;
802     auto h      = mlgo(tokens, valid);
803     return std::make_pair(std::move(valid), std::move(h));
804 }
805 } // namespace parser
806 } // namespace mlgo
807 } // namespace arm_compute
808 
809 #undef CHECK
810 #undef CHECK_DEFAULT
811 #undef FAIL_WITH_MSG
812 #undef FAIL_WITH_MSG_DEFAULT