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