Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 1 | /* |
Georgios Pinitas | 7cd26d4 | 2019-01-09 18:35:17 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2019 ARM Limited. |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +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 | */ |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 24 | #pragma once |
| 25 | |
| 26 | #include <memory> |
Georgios Pinitas | 7cd26d4 | 2019-01-09 18:35:17 +0000 | [diff] [blame] | 27 | #include <cstring> |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 28 | |
| 29 | #include "arm_gemm_local.hpp" |
| 30 | #include "gemm_common.hpp" |
| 31 | |
| 32 | namespace arm_gemm { |
| 33 | |
David Mansell | e39334c | 2018-07-06 17:53:35 +0100 | [diff] [blame] | 34 | enum class GemmMethod |
| 35 | { |
| 36 | DEFAULT, |
| 37 | GEMV_BATCHED, |
| 38 | GEMV_PRETRANSPOSED, |
| 39 | GEMV_NATIVE_TRANSPOSED, |
| 40 | GEMM_NATIVE, |
Georgios Pinitas | 7cd26d4 | 2019-01-09 18:35:17 +0000 | [diff] [blame] | 41 | GEMM_HYBRID, |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 42 | GEMM_INTERLEAVED, |
| 43 | QUANTIZE_WRAPPER, |
| 44 | GEMM_HYBRID_QUANTIZED |
Georgios Pinitas | 7cd26d4 | 2019-01-09 18:35:17 +0000 | [diff] [blame] | 45 | }; |
| 46 | |
Georgios Pinitas | 7cd26d4 | 2019-01-09 18:35:17 +0000 | [diff] [blame] | 47 | struct KernelDescription |
| 48 | { |
Georgios Pinitas | 1461383 | 2019-03-01 19:07:11 +0000 | [diff] [blame] | 49 | GemmMethod method = GemmMethod::DEFAULT; |
| 50 | std::string name = ""; |
| 51 | bool is_default = false; |
Georgios Pinitas | 7cd26d4 | 2019-01-09 18:35:17 +0000 | [diff] [blame] | 52 | |
Georgios Pinitas | 1461383 | 2019-03-01 19:07:11 +0000 | [diff] [blame] | 53 | KernelDescription(GemmMethod m, std::string n, bool d=false) : method(m), name(n), is_default(d) { } |
Michalis Spyrou | 5cb49dc | 2019-12-03 13:42:25 +0000 | [diff] [blame] | 54 | KernelDescription() noexcept { } |
David Mansell | e39334c | 2018-07-06 17:53:35 +0100 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | struct GemmConfig |
| 58 | { |
Georgios Pinitas | 7cd26d4 | 2019-01-09 18:35:17 +0000 | [diff] [blame] | 59 | GemmMethod method = GemmMethod::DEFAULT; |
| 60 | std::string filter = ""; |
David Mansell | e39334c | 2018-07-06 17:53:35 +0100 | [diff] [blame] | 61 | unsigned int inner_block_size = 0; |
| 62 | unsigned int outer_block_size = 0; |
| 63 | |
| 64 | GemmConfig(GemmMethod method) : method(method) { } |
Georgios Pinitas | 7cd26d4 | 2019-01-09 18:35:17 +0000 | [diff] [blame] | 65 | GemmConfig() { } |
David Mansell | e39334c | 2018-07-06 17:53:35 +0100 | [diff] [blame] | 66 | }; |
| 67 | |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 68 | struct Activation |
| 69 | { |
| 70 | enum class Type { |
| 71 | None, |
| 72 | ReLU, |
| 73 | BoundedReLU |
| 74 | }; |
| 75 | |
| 76 | Type type; |
| 77 | float param1; |
| 78 | float param2; |
| 79 | |
| 80 | Activation(Type type=Type::None, float p1=0.0f, float p2=0.0f) : type(type), param1(p1), param2(p2) { } |
| 81 | }; |
| 82 | |
David Mansell | e39334c | 2018-07-06 17:53:35 +0100 | [diff] [blame] | 83 | struct GemmArgs |
| 84 | { |
| 85 | public: |
Georgios Pinitas | 7cd26d4 | 2019-01-09 18:35:17 +0000 | [diff] [blame] | 86 | const CPUInfo *_ci; |
| 87 | unsigned int _Msize; |
| 88 | unsigned int _Nsize; |
| 89 | unsigned int _Ksize; |
| 90 | unsigned int _nbatches; |
| 91 | unsigned int _nmulti; |
| 92 | bool _trA; |
| 93 | bool _trB; |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 94 | Activation _act; |
Georgios Pinitas | 7cd26d4 | 2019-01-09 18:35:17 +0000 | [diff] [blame] | 95 | int _maxthreads; |
| 96 | bool _pretransposed_hint; |
| 97 | const GemmConfig *_cfg; |
David Mansell | e39334c | 2018-07-06 17:53:35 +0100 | [diff] [blame] | 98 | |
| 99 | GemmArgs(const CPUInfo *ci, const unsigned int M, const unsigned int N, |
| 100 | const unsigned int K, const unsigned int nbatches, |
| 101 | const unsigned int nmulti, const bool trA, const bool trB, |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 102 | Activation act, const int maxthreads, |
Georgios Pinitas | 7cd26d4 | 2019-01-09 18:35:17 +0000 | [diff] [blame] | 103 | const bool pretransposed_hint, const GemmConfig *cfg=nullptr ) : |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 104 | _ci(ci), _Msize(M), _Nsize(N), _Ksize(K), _nbatches(nbatches), _nmulti(nmulti), |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 105 | _trA(trA), _trB(trB), _act(act), _maxthreads(maxthreads), |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 106 | _pretransposed_hint(pretransposed_hint), _cfg(cfg) |
David Mansell | e39334c | 2018-07-06 17:53:35 +0100 | [diff] [blame] | 107 | { |
| 108 | } |
| 109 | }; |
| 110 | |
Michalis Spyrou | 71ac903 | 2019-11-14 14:31:44 +0000 | [diff] [blame] | 111 | struct Requantize32 |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 112 | { |
| 113 | public: |
Michalis Spyrou | 71ac903 | 2019-11-14 14:31:44 +0000 | [diff] [blame] | 114 | const int32_t *bias = nullptr; |
| 115 | size_t bias_multi_stride = 0; |
| 116 | int32_t a_offset = 0; |
| 117 | int32_t b_offset = 0; |
| 118 | int32_t c_offset = 0; |
| 119 | bool per_channel_requant = false; |
| 120 | int32_t per_layer_shift = 0; |
| 121 | int32_t per_layer_mul = 0; |
| 122 | const int32_t *per_channel_shifts = nullptr; |
| 123 | const int32_t *per_channel_muls = nullptr; |
| 124 | int32_t minval = 0; |
| 125 | int32_t maxval = 0; |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 126 | |
Michalis Spyrou | 71ac903 | 2019-11-14 14:31:44 +0000 | [diff] [blame] | 127 | Requantize32() = default; |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 128 | |
Michalis Spyrou | 71ac903 | 2019-11-14 14:31:44 +0000 | [diff] [blame] | 129 | // Constructor for per-tensor quantization |
| 130 | Requantize32(const int32_t *bias, size_t bias_multi_stride, |
| 131 | int32_t a_offset, int32_t b_offset, int32_t c_offset, |
| 132 | int32_t requant_shift, int32_t requant_mul, |
| 133 | int32_t minv, int32_t maxv) : |
| 134 | bias(bias), bias_multi_stride(bias_multi_stride), |
| 135 | a_offset(a_offset), b_offset(b_offset), c_offset(c_offset), |
| 136 | per_channel_requant(false), per_layer_shift(requant_shift), per_layer_mul(requant_mul), |
| 137 | minval(minv), maxval(maxv) |
| 138 | { |
| 139 | } |
| 140 | |
| 141 | // Constructor for per-channel quantization |
| 142 | Requantize32(const int32_t *bias, size_t bias_multi_stride, |
| 143 | int32_t a_offset, int32_t b_offset, int32_t c_offset, |
| 144 | const int32_t *requant_shifts, const int32_t *requant_muls, |
| 145 | int32_t minv, int32_t maxv) : |
| 146 | bias(bias), bias_multi_stride(bias_multi_stride), |
| 147 | a_offset(a_offset), b_offset(b_offset), c_offset(c_offset), |
| 148 | per_channel_requant(true), per_channel_shifts(requant_shifts), per_channel_muls(requant_muls), |
| 149 | minval(minv), maxval(maxv) |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 150 | { |
| 151 | } |
| 152 | }; |
| 153 | |
| 154 | struct Nothing |
| 155 | { |
| 156 | }; |
| 157 | |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 158 | template<typename Top, typename Tret> |
| 159 | using UniqueGemmCommon = std::unique_ptr<GemmCommon<Top, Tret> >; |
| 160 | |
David Mansell | e39334c | 2018-07-06 17:53:35 +0100 | [diff] [blame] | 161 | /* Low level API calls. |
| 162 | * These are implemented as 'GemmArgs' versions, or with the arguments explicitly listed. */ |
| 163 | |
David Mansell | e39334c | 2018-07-06 17:53:35 +0100 | [diff] [blame] | 164 | /* get_gemm_method(): Given the templated types and provided parameters, |
| 165 | * which is the preferred method to implement this GEMM? */ |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 166 | template<typename Top, typename Tret, class OutputStage = Nothing> |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 167 | KernelDescription get_gemm_method(const GemmArgs &args, const OutputStage & ={}); |
David Mansell | e39334c | 2018-07-06 17:53:35 +0100 | [diff] [blame] | 168 | |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 169 | template<typename Top, typename Tret, class OutputStage = Nothing> |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 170 | UniqueGemmCommon<Top, Tret> gemm(const GemmArgs &args, const OutputStage & ={}); |
David Mansell | e39334c | 2018-07-06 17:53:35 +0100 | [diff] [blame] | 171 | |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 172 | template<typename Top, typename Tret, class OutputStage = Nothing> |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 173 | std::vector<KernelDescription> get_compatible_kernels(const GemmArgs &args, const OutputStage & ={}); |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 174 | |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 175 | } // namespace arm_gemm |