David Mansell | e39334c | 2018-07-06 17:53:35 +0100 | [diff] [blame] | 1 | /* |
Georgios Pinitas | 5aa1a0b | 2020-07-02 20:02:20 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2020 Arm Limited. |
David Mansell | e39334c | 2018-07-06 17:53:35 +0100 | [diff] [blame] | 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 | |
Georgios Pinitas | 5aa1a0b | 2020-07-02 20:02:20 +0100 | [diff] [blame] | 25 | #include "arm_gemm.hpp" |
Georgios Pinitas | 7cd26d4 | 2019-01-09 18:35:17 +0000 | [diff] [blame] | 26 | |
| 27 | #include <functional> |
David Mansell | e39334c | 2018-07-06 17:53:35 +0100 | [diff] [blame] | 28 | |
| 29 | namespace arm_gemm { |
| 30 | |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 31 | /* Structure describing an implementation. For each supported combination |
| 32 | * of types, a static list of these structures is built up to describe the |
| 33 | * implementations available. |
| 34 | */ |
| 35 | template<typename Top, typename Tret, class OutputStage = Nothing> |
Georgios Pinitas | 7cd26d4 | 2019-01-09 18:35:17 +0000 | [diff] [blame] | 36 | struct GemmImplementation { |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 37 | const GemmMethod method; |
| 38 | const char * name; |
| 39 | std::function<bool(const GemmArgs &, const OutputStage &)> is_supported; |
| 40 | std::function<bool(const GemmArgs &, const OutputStage &)> is_recommended; |
| 41 | std::function<GemmCommon<Top, Tret> *(const GemmArgs &, const OutputStage &)> instantiate; |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 42 | |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 43 | bool do_is_supported(const GemmArgs &args, const OutputStage &os) const { |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 44 | if (is_supported != nullptr) { |
| 45 | return is_supported(args, os); |
| 46 | } else { |
| 47 | return true; |
| 48 | } |
| 49 | } |
| 50 | |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 51 | bool do_is_recommended(const GemmArgs &args, const OutputStage &os) const { |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 52 | if (is_recommended != nullptr) { |
| 53 | return is_recommended(args, os); |
| 54 | } else { |
| 55 | return true; |
| 56 | } |
| 57 | } |
| 58 | |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 59 | GemmCommon<Top, Tret> *do_instantiate(const GemmArgs &args, const OutputStage &os) const { |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 60 | return instantiate(args, os); |
| 61 | } |
| 62 | }; |
| 63 | |
| 64 | /* Slightly different version of above for straightforward GEMMs with no |
| 65 | * output stage, so the std::functions there don't have to deal with the |
| 66 | * unnecessary second argument. */ |
| 67 | template<typename Top, typename Tret> |
| 68 | struct GemmImplementation<Top, Tret, Nothing> { |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 69 | const GemmMethod method; |
| 70 | const char * name; |
| 71 | std::function<bool(const GemmArgs &)> is_supported; |
| 72 | std::function<bool(const GemmArgs &)> is_recommended; |
| 73 | std::function<GemmCommon<Top, Tret> *(const GemmArgs &)> instantiate; |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 74 | |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 75 | bool do_is_supported(const GemmArgs &args, const Nothing &) const { |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 76 | if (is_supported != nullptr) { |
| 77 | return is_supported(args); |
| 78 | } else { |
| 79 | return true; |
| 80 | } |
| 81 | } |
| 82 | |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 83 | bool do_is_recommended(const GemmArgs &args, const Nothing &) const { |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 84 | if (is_recommended != nullptr) { |
| 85 | return is_recommended(args); |
| 86 | } else { |
| 87 | return true; |
| 88 | } |
| 89 | } |
| 90 | |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 91 | GemmCommon<Top, Tret> *do_instantiate(const GemmArgs &args, const Nothing &) const { |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 92 | return instantiate(args); |
| 93 | } |
David Mansell | e39334c | 2018-07-06 17:53:35 +0100 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | /* "Master" function implemented for each valid combination of types. |
| 97 | * Returns a list of GEMM implementation descriptors for processing by the |
Georgios Pinitas | 7cd26d4 | 2019-01-09 18:35:17 +0000 | [diff] [blame] | 98 | * other functions, terminated by an implementation with |
| 99 | * method==GemmMethod::DEFAULT. */ |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 100 | template<typename Top, typename Tret, class OutputStage = Nothing> |
| 101 | const GemmImplementation<Top, Tret, OutputStage> *gemm_implementation_list(); |
David Mansell | e39334c | 2018-07-06 17:53:35 +0100 | [diff] [blame] | 102 | |
Georgios Pinitas | 7cd26d4 | 2019-01-09 18:35:17 +0000 | [diff] [blame] | 103 | /* |
| 104 | * Select a GEMM implementation for the given arguments. |
| 105 | * |
| 106 | * The logic here returns the first method on the list which supports the |
| 107 | * requested problem parameters, matches the provided filters (method and/or |
| 108 | * name string match) and recommends itself. |
| 109 | * |
| 110 | * If there is no such method, it will return the first method which |
| 111 | * supports the requested parameters and passes the filters, regardless of |
| 112 | * recommendation. |
| 113 | * |
| 114 | * If no method supports the requested parameters and passes the filters, |
| 115 | * this function returns false and doesn't touch the provided pointer |
| 116 | * reference. |
| 117 | */ |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 118 | template<typename Top, typename Tret, class OutputStage> |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 119 | bool find_implementation(const GemmArgs &args, const OutputStage &os, const GemmImplementation<Top, Tret, OutputStage> * &impl) { |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 120 | auto gemms = gemm_implementation_list<Top, Tret, OutputStage>(); |
Georgios Pinitas | 7cd26d4 | 2019-01-09 18:35:17 +0000 | [diff] [blame] | 121 | const GemmConfig *cfg = args._cfg; |
David Mansell | e39334c | 2018-07-06 17:53:35 +0100 | [diff] [blame] | 122 | |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 123 | const GemmImplementation<Top, Tret, OutputStage> *saved_impl = nullptr; |
Georgios Pinitas | 7cd26d4 | 2019-01-09 18:35:17 +0000 | [diff] [blame] | 124 | |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 125 | for (const GemmImplementation<Top, Tret, OutputStage> *i = gemms; i->method != GemmMethod::DEFAULT; i++) { |
David Mansell | e39334c | 2018-07-06 17:53:35 +0100 | [diff] [blame] | 126 | /* Skip if this implementation doesn't support these args. */ |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 127 | if (!i->do_is_supported(args, os)) { |
David Mansell | e39334c | 2018-07-06 17:53:35 +0100 | [diff] [blame] | 128 | continue; |
| 129 | } |
| 130 | |
| 131 | /* Skip if a specific method is requested and this is a different one. */ |
| 132 | if (cfg && cfg->method != GemmMethod::DEFAULT && i->method != cfg->method) { |
| 133 | continue; |
| 134 | } |
| 135 | |
Georgios Pinitas | 7cd26d4 | 2019-01-09 18:35:17 +0000 | [diff] [blame] | 136 | /* Skip if a filter is to be applied and it doesn't match. */ |
| 137 | if (cfg && cfg->filter != "" && !strstr(i->name, cfg->filter.c_str())) { |
David Mansell | e39334c | 2018-07-06 17:53:35 +0100 | [diff] [blame] | 138 | continue; |
| 139 | } |
| 140 | |
Georgios Pinitas | 7cd26d4 | 2019-01-09 18:35:17 +0000 | [diff] [blame] | 141 | /* At this point, if we don't have a saved implementation, save this |
| 142 | * one. This is so that we always return something if a filter |
| 143 | * matches, even if it doesn't recommend itself. |
| 144 | */ |
| 145 | if (saved_impl == nullptr) { |
| 146 | saved_impl=i; |
| 147 | } |
| 148 | |
| 149 | /* Check that this method recommends itself. */ |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 150 | if (!i->do_is_recommended(args, os)) { |
Georgios Pinitas | 7cd26d4 | 2019-01-09 18:35:17 +0000 | [diff] [blame] | 151 | continue; |
| 152 | } |
| 153 | |
| 154 | impl=i; |
| 155 | |
| 156 | return true; |
David Mansell | e39334c | 2018-07-06 17:53:35 +0100 | [diff] [blame] | 157 | } |
| 158 | |
Georgios Pinitas | 7cd26d4 | 2019-01-09 18:35:17 +0000 | [diff] [blame] | 159 | /* We didn't find an option matching the filters that recommended |
| 160 | * itself. But if we found something earlier that matched the filters |
| 161 | * but wasn't recommended, return it here. */ |
| 162 | if (saved_impl != nullptr) { |
| 163 | impl = saved_impl; |
David Mansell | e39334c | 2018-07-06 17:53:35 +0100 | [diff] [blame] | 164 | return true; |
| 165 | } |
| 166 | |
| 167 | return false; |
| 168 | } |
| 169 | |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 170 | template<typename Top, typename Tret, class OutputStage> |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 171 | std::vector<KernelDescription> get_compatible_kernels(const GemmArgs &args, const OutputStage &os) { |
Georgios Pinitas | 1461383 | 2019-03-01 19:07:11 +0000 | [diff] [blame] | 172 | std::vector<KernelDescription> res; |
| 173 | |
| 174 | /* Find out what the default implementation in so we can set the flag accordingly later. */ |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 175 | const GemmImplementation<Top, Tret, OutputStage> *default_impl; |
| 176 | find_implementation(args, os, default_impl); |
Georgios Pinitas | 7cd26d4 | 2019-01-09 18:35:17 +0000 | [diff] [blame] | 177 | |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 178 | auto gemms = gemm_implementation_list<Top, Tret, OutputStage>(); |
Georgios Pinitas | 7cd26d4 | 2019-01-09 18:35:17 +0000 | [diff] [blame] | 179 | |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 180 | for (const GemmImplementation<Top, Tret, OutputStage> *i = gemms; i->method != GemmMethod::DEFAULT; i++) { |
Georgios Pinitas | 7cd26d4 | 2019-01-09 18:35:17 +0000 | [diff] [blame] | 181 | /* Check that this implementation supports the presented problem. */ |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 182 | if (!i->do_is_supported(args, os)) { |
Georgios Pinitas | 7cd26d4 | 2019-01-09 18:35:17 +0000 | [diff] [blame] | 183 | continue; |
| 184 | } |
| 185 | |
Georgios Pinitas | 1461383 | 2019-03-01 19:07:11 +0000 | [diff] [blame] | 186 | res.push_back(KernelDescription(i->method, i->name, i==default_impl)); |
Georgios Pinitas | 7cd26d4 | 2019-01-09 18:35:17 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | return res; |
| 190 | } |
| 191 | |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 192 | template<typename Top, typename Tret, class OutputStage> |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 193 | UniqueGemmCommon<Top, Tret> gemm(const GemmArgs &args, const OutputStage &os) { |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 194 | const GemmImplementation<Top, Tret, OutputStage> *impl; |
Georgios Pinitas | 7cd26d4 | 2019-01-09 18:35:17 +0000 | [diff] [blame] | 195 | |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 196 | if (find_implementation<Top, Tret, OutputStage>(args, os, impl)) { |
| 197 | return UniqueGemmCommon<Top, Tret>(impl->do_instantiate(args, os)); |
Georgios Pinitas | 7cd26d4 | 2019-01-09 18:35:17 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | return UniqueGemmCommon<Top, Tret>(nullptr); |
| 201 | } |
| 202 | |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 203 | template<typename Top, typename Tret, class OutputStage> |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 204 | KernelDescription get_gemm_method(const GemmArgs &args, const OutputStage &os) { |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 205 | const GemmImplementation<Top, Tret, OutputStage> *impl; |
Georgios Pinitas | 7cd26d4 | 2019-01-09 18:35:17 +0000 | [diff] [blame] | 206 | |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 207 | if (find_implementation<Top, Tret>(args, os, impl)) { |
Georgios Pinitas | 7cd26d4 | 2019-01-09 18:35:17 +0000 | [diff] [blame] | 208 | return KernelDescription(impl->method, impl->name); |
| 209 | } |
| 210 | |
| 211 | /* This shouldn't happen - there should always be at least one valid implementation. */ |
| 212 | return KernelDescription(); |
| 213 | } |
| 214 | |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 215 | } // namespace arm_gemm |