Michele Di Giorgio | 5b6904b | 2018-01-29 12:24:14 +0000 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017 Arm Limited. |
Michele Di Giorgio | 5b6904b | 2018-01-29 12:24:14 +0000 | [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 | |
Michele Di Giorgio | 5b6904b | 2018-01-29 12:24:14 +0000 | [diff] [blame] | 25 | #pragma once |
| 26 | |
Georgios Pinitas | c0b6f76 | 2020-11-02 01:37:17 +0000 | [diff] [blame] | 27 | #include "arm_gemm.hpp" |
| 28 | |
Georgios Pinitas | 7cd26d4 | 2019-01-09 18:35:17 +0000 | [diff] [blame] | 29 | #include <cstddef> |
Georgios Pinitas | 421405b | 2018-10-26 19:05:32 +0100 | [diff] [blame] | 30 | |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 31 | // Macro for unreachable code (e.g. impossible default cases on switch) |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 32 | #define UNREACHABLE(why) __builtin_unreachable() |
Michele Di Giorgio | 5b6904b | 2018-01-29 12:24:14 +0000 | [diff] [blame] | 33 | |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 34 | // Paranoid option for the above with assert |
| 35 | // #define UNREACHABLE(why) assert(0 && why) |
Michele Di Giorgio | 5b6904b | 2018-01-29 12:24:14 +0000 | [diff] [blame] | 36 | |
Georgios Pinitas | c0b6f76 | 2020-11-02 01:37:17 +0000 | [diff] [blame] | 37 | namespace arm_gemm { |
| 38 | |
Georgios Pinitas | 1d48065 | 2019-01-23 11:24:50 +0000 | [diff] [blame] | 39 | template<typename T> |
| 40 | inline T iceildiv(const T a, const T b) { |
Georgios Pinitas | 421405b | 2018-10-26 19:05:32 +0100 | [diff] [blame] | 41 | return (a + b - 1) / b; |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 42 | } |
Michele Di Giorgio | 5b6904b | 2018-01-29 12:24:14 +0000 | [diff] [blame] | 43 | |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 44 | template <typename T> |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 45 | inline T roundup(const T a, const T b) { |
Georgios Pinitas | 421405b | 2018-10-26 19:05:32 +0100 | [diff] [blame] | 46 | T rem = a % b; |
Michele Di Giorgio | 5b6904b | 2018-01-29 12:24:14 +0000 | [diff] [blame] | 47 | |
Georgios Pinitas | 421405b | 2018-10-26 19:05:32 +0100 | [diff] [blame] | 48 | if (rem) { |
| 49 | return a + b - rem; |
| 50 | } else { |
| 51 | return a; |
| 52 | } |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 53 | } |
David Mansell | d93991e | 2018-07-06 14:52:52 +0100 | [diff] [blame] | 54 | |
Georgios Pinitas | c0b6f76 | 2020-11-02 01:37:17 +0000 | [diff] [blame] | 55 | enum class VLType { |
| 56 | None, |
| 57 | SVE, |
| 58 | }; |
| 59 | |
| 60 | template<typename T> |
| 61 | struct IndirectOutputArg { |
| 62 | struct { |
| 63 | T *base; |
| 64 | size_t stride; |
| 65 | } direct = {}; |
| 66 | struct { |
| 67 | T * const *ptr; |
| 68 | size_t offset; |
| 69 | } indirect = {}; |
| 70 | bool is_indirect; |
| 71 | |
| 72 | // Direct |
| 73 | IndirectOutputArg(T *base, size_t stride) : is_indirect(false) { |
| 74 | direct.base = base; |
| 75 | direct.stride = stride; |
| 76 | } |
| 77 | |
| 78 | // Indirect |
| 79 | IndirectOutputArg(T * const * ptr, size_t offset) : is_indirect(true) { |
| 80 | indirect.ptr = ptr; |
| 81 | indirect.offset = offset; |
| 82 | } |
| 83 | |
| 84 | IndirectOutputArg() : is_indirect(false) { |
| 85 | direct.base = nullptr; |
| 86 | direct.stride = 0; |
| 87 | } |
| 88 | }; |
| 89 | |
| 90 | // Check that the provided Requantize32 doesn't have a left shift. |
| 91 | inline bool quant_no_left_shift(const Requantize32 &qp) { |
| 92 | if (qp.per_channel_requant) { |
| 93 | return (qp.per_channel_left_shifts == nullptr); |
| 94 | } else { |
| 95 | return (qp.per_layer_left_shift == 0); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // Check that the provided Requantize32 is compatible with the "symmetric" hybrid kernels. These don't include row |
| 100 | // sums, so the 'b_offset' has to be zero. |
| 101 | inline bool quant_hybrid_symmetric(const Requantize32 &qp) { |
| 102 | return quant_no_left_shift(qp) && qp.b_offset == 0; |
| 103 | } |
| 104 | |
| 105 | // Check that the provided Requantize32 is compatible with the "asymmetric" hybrid kernels. These don't support per |
| 106 | // channel quantization. Technically b_offset==0 cases would work, but it is a waste to sum and then multiply by 0... |
| 107 | inline bool quant_hybrid_asymmetric(const Requantize32 &qp) { |
| 108 | return quant_no_left_shift(qp) /* && qp.b_offset != 0 */ && qp.per_channel_requant==false; |
| 109 | } |
| 110 | |
| 111 | template<typename T> |
| 112 | struct IndirectInputArg { |
| 113 | struct { |
| 114 | const T *base; |
| 115 | size_t stride; |
| 116 | } direct = {}; |
| 117 | struct { |
| 118 | const T * const * const * ptr; |
| 119 | unsigned int start_row; |
| 120 | unsigned int start_col; |
| 121 | } indirect = {}; |
| 122 | bool is_indirect; |
| 123 | |
| 124 | // Direct |
| 125 | IndirectInputArg(const T *base, size_t stride) : is_indirect(false) { |
| 126 | direct.base = base; |
| 127 | direct.stride = stride; |
| 128 | } |
| 129 | |
| 130 | // Indirect |
| 131 | IndirectInputArg(const T * const * const *ptr, unsigned int start_row, unsigned int start_col) : is_indirect(true) { |
| 132 | indirect.ptr = ptr; |
| 133 | indirect.start_row = start_row; |
| 134 | indirect.start_col = start_col; |
| 135 | } |
| 136 | |
| 137 | IndirectInputArg() : is_indirect(false) { |
| 138 | direct.base = nullptr; |
| 139 | direct.stride = 0; |
| 140 | } |
| 141 | }; |
| 142 | |
Georgios Pinitas | 7cd26d4 | 2019-01-09 18:35:17 +0000 | [diff] [blame] | 143 | namespace utils { |
| 144 | namespace { |
| 145 | |
| 146 | #ifdef __ARM_FEATURE_SVE |
| 147 | template<size_t sz> |
| 148 | inline unsigned long get_vector_length_sz() { |
| 149 | unsigned long v; |
| 150 | |
| 151 | __asm ( |
| 152 | "cntb %0" |
| 153 | : "=r" (v) |
| 154 | ); |
| 155 | |
| 156 | return v / sz; |
| 157 | } |
| 158 | |
| 159 | #define VEC_LEN_SPEC(sz, opcode) template <> inline unsigned long get_vector_length_sz<sz>() { unsigned long v; __asm ( opcode " %0" : "=r" (v)); return v; } |
| 160 | |
| 161 | VEC_LEN_SPEC(8, "cntd") |
| 162 | VEC_LEN_SPEC(4, "cntw") |
| 163 | VEC_LEN_SPEC(2, "cnth") |
| 164 | VEC_LEN_SPEC(1, "cntb") |
| 165 | #endif |
| 166 | |
| 167 | } // anonymous namespace |
| 168 | |
David Mansell | d93991e | 2018-07-06 14:52:52 +0100 | [diff] [blame] | 169 | template <typename T> |
| 170 | inline unsigned long get_vector_length() { |
Georgios Pinitas | 421405b | 2018-10-26 19:05:32 +0100 | [diff] [blame] | 171 | #ifdef __ARM_FEATURE_SVE |
Georgios Pinitas | 7cd26d4 | 2019-01-09 18:35:17 +0000 | [diff] [blame] | 172 | return get_vector_length_sz<sizeof(T)>(); |
Georgios Pinitas | 421405b | 2018-10-26 19:05:32 +0100 | [diff] [blame] | 173 | #else |
Georgios Pinitas | 7cd26d4 | 2019-01-09 18:35:17 +0000 | [diff] [blame] | 174 | return 16 / sizeof(T); |
Georgios Pinitas | 421405b | 2018-10-26 19:05:32 +0100 | [diff] [blame] | 175 | #endif |
Georgios Pinitas | 7cd26d4 | 2019-01-09 18:35:17 +0000 | [diff] [blame] | 176 | } |
David Mansell | d93991e | 2018-07-06 14:52:52 +0100 | [diff] [blame] | 177 | |
Georgios Pinitas | 7cd26d4 | 2019-01-09 18:35:17 +0000 | [diff] [blame] | 178 | } // utils namespace |
| 179 | } // arm_gemm namespace |
| 180 | |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 181 | using namespace arm_gemm::utils; |