blob: 02e8111b6ebc53a7b30052af5f45425c5ef4fb99 [file] [log] [blame]
SiCong Li7061eb22021-01-08 15:16:02 +00001/*
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#ifndef SRC_RUNTIME_MLGO_MLGOHEURISTICS_H
25#define SRC_RUNTIME_MLGO_MLGOHEURISTICS_H
26
27#include "src/runtime/CL/mlgo/Common.h"
28#include "src/runtime/CL/mlgo/HeuristicTree.h"
29
30#include <iostream>
31#include <map>
32#include <string>
33#include <utility>
34namespace arm_compute
35{
36namespace mlgo
37{
38/** Query interface */
39struct Query
40{
41 std::string ip_target; /**< The name of the IP target */
42 DataType data_type; /**< Data type */
43 unsigned int m; /**< Number of rows for the lhs matrix. Lhs matrix NOT transposed */
44 unsigned int n; /**< Number of columns for the rhs matrix. Rhs matrix NOT transposed */
45 unsigned int k; /**< Number of rows for the rhs matrix. Rhs matrix NOT transposed */
46 unsigned int b; /**< Batch size */
47};
48
49bool operator==(const GEMMConfigReshapedOnlyRHS &lhs, const GEMMConfigReshapedOnlyRHS &rhs);
50bool operator==(const GEMMConfigReshaped &lhs, const GEMMConfigReshaped &rhs);
51
52/** MLGOHeuristics for configuring GEMM kernels */
53class MLGOHeuristics
54{
55public:
56 /** Constructor */
57 MLGOHeuristics();
58 /** Query the gemm type
59 *
60 * @param[in] query Query
61 *
62 * @return std::pair<bool, GEMMType> signals if the query succeeded or failed
63 */
64 std::pair<bool, GEMMType> query_gemm_type(Query) const;
65 /** Query the gemm configuration for native kernel
66 *
67 * @param[in] query Query
68 *
69 * @return std::pair<bool, GEMMConfigNative> bool signals if the query succeeded or failed
70 */
71 std::pair<bool, GEMMConfigNative> query_gemm_config_native(Query query) const;
72 /** Query the gemm configuration for reshaped only rhs kernel
73 *
74 * @param[in] query Query
75 *
76 * @return std::pair<bool, GEMMConfigReshapedOnlyRHS> bool signals if the query succeeded or failed
77 */
78 std::pair<bool, GEMMConfigReshapedOnlyRHS> query_gemm_config_reshaped_only_rhs(Query) const;
79 /** Query the gemm configuration for reshaped kernel
80 *
81 * @param[in] query Query
82 *
83 * @return std::pair<bool, GEMMConfigReshaped> bool signals if the query succeeded or failed
84 */
85 std::pair<bool, GEMMConfigReshaped> query_gemm_config_reshaped(Query) const;
86 /** (Re)Load the heuristics from reading a dotmlgo file
87 *
88 * @param[in] filename Path to the dotmlgo file
89 *
90 * @return bool Signals if the reload succeeded or failed
91 */
92 bool reload_from_file(const std::string &filename);
93 /** (Re)Load the heuristics from reading an input stream
94 *
95 * @param[in] istream Istream containing mlgo heuristics
96 *
97 * @return bool Signals if the reload succeeded or failed
98 */
99 bool reload_from_stream(std::istream &istream);
100
101 /** Get the heuristic tree from tree id
102 *
103 * @param[in] id Tree id.
104 *
105 * @return HeuristicTree&
106 */
107 std::pair<bool, HeuristicTree *> get_heuristic_tree(HeuristicTree::TreeID id);
108 /** Add a heuristic tree
109 * @param t Heuristic tree to be added
110 */
111 bool add_heuristic_tree(HeuristicTree &&t);
112
113 /** Check the validity of the heuristic tree.
114 *
115 * @param id ID of the tree to be checked
116 *
117 * @return bool
118 */
119 bool check_heuristic_tree(HeuristicTree::TreeID id);
120
121 /** Check the overall validity of the heuristics.
122 * @return bool
123 */
124 bool check_all() const;
125
126private:
127 static constexpr size_t _max_num_trees{ 100 }; /**< Max number of trees that can be added*/
128
129private:
130 // There exists a one-to-one mappipng between TreeID and Index, either can be used to identify a @ref HeuristicTree
131 std::map<HeuristicTree::TreeID, HeuristicTree::Index> _indices; /**< A mapping from TreeID to Index */
132 std::map<HeuristicTree::Index, HeuristicTree> _trees; /**< A mapping from Index to HeuristicTree */
133 std::map<HeuristicTree::TreeID, bool> _tree_valid; /**< Result cache of the tree validity checks */
134 bool _valid; /**< Overall validity */
135};
136
137} // namespace mlgo
138} // namespace arm_compute
139#endif //SRC_MLGO_MLGOHEURISTICS_H