Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 1 | /* |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 ARM Limited. |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +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 | */ |
Gian Marco Iodice | 43a129e | 2019-05-14 10:14:08 +0100 | [diff] [blame] | 24 | #include "gemm_helpers.h" |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 25 | #include "helpers_asymm.h" |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 26 | #include "repeat.h" |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 27 | |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 28 | #if defined(DATA_TYPE) && defined(ACC_DATA_TYPE) |
| 29 | |
Georgios Pinitas | daa3855 | 2018-08-28 17:43:18 +0100 | [diff] [blame] | 30 | #if defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8) |
| 31 | #if defined(ARM_COMPUTE_OPENCL_DOT8_ACC_ENABLED) && defined(cl_arm_integer_dot_product_accumulate_int8) |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 32 | #define ARM_DOT(x, y, val) val = arm_dot_acc((x), (y), (val)); |
Georgios Pinitas | daa3855 | 2018-08-28 17:43:18 +0100 | [diff] [blame] | 33 | #else // defined(ARM_COMPUTE_OPENCL_DOT8_ACC_ENABLED) && defined(cl_arm_integer_dot_product_accumulate_int8) |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 34 | #define ARM_DOT(x, y, val) val += arm_dot((x), (y)); |
Georgios Pinitas | daa3855 | 2018-08-28 17:43:18 +0100 | [diff] [blame] | 35 | #endif // defined(ARM_COMPUTE_OPENCL_DOT8_ACC_ENABLED) && defined(cl_arm_integer_dot_product_accumulate_int8) |
| 36 | #endif // defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8) |
Giorgio Arena | c50da38 | 2018-07-26 15:50:09 +0100 | [diff] [blame] | 37 | |
Gian Marco Iodice | 43a129e | 2019-05-14 10:14:08 +0100 | [diff] [blame] | 38 | #if defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8) |
| 39 | |
| 40 | /** Specialized macros to perform the dot product instruction between two vectors of size N [1,16]. These macros use the dot8 instruction */ |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 41 | #define ARM_DOT1(a, b, c) \ |
| 42 | ({ \ |
| 43 | ARM_DOT((VEC_DATA_TYPE(DATA_TYPE, 4))(a, (VEC_DATA_TYPE(DATA_TYPE, 3))0), (VEC_DATA_TYPE(DATA_TYPE, 4))(b, (VEC_DATA_TYPE(DATA_TYPE, 3))0), c); \ |
Gian Marco Iodice | 43a129e | 2019-05-14 10:14:08 +0100 | [diff] [blame] | 44 | }) |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 45 | #define ARM_DOT2(a, b, c) \ |
| 46 | ({ \ |
| 47 | ARM_DOT((VEC_DATA_TYPE(DATA_TYPE, 4))(a, (VEC_DATA_TYPE(DATA_TYPE, 2))0), (VEC_DATA_TYPE(DATA_TYPE, 4))(b, (VEC_DATA_TYPE(DATA_TYPE, 2))0), c); \ |
Gian Marco Iodice | 43a129e | 2019-05-14 10:14:08 +0100 | [diff] [blame] | 48 | }) |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 49 | #define ARM_DOT3(a, b, c) \ |
| 50 | ({ \ |
| 51 | ARM_DOT((VEC_DATA_TYPE(DATA_TYPE, 4))(a, (DATA_TYPE)0), (VEC_DATA_TYPE(DATA_TYPE, 4))(b, (DATA_TYPE)0), c); \ |
Gian Marco Iodice | 43a129e | 2019-05-14 10:14:08 +0100 | [diff] [blame] | 52 | }) |
| 53 | #define ARM_DOT4(a, b, c) \ |
| 54 | ({ \ |
| 55 | ARM_DOT(a, b, c); \ |
| 56 | }) |
| 57 | #define ARM_DOT8(a, b, c) \ |
| 58 | ({ \ |
| 59 | ARM_DOT4((a.lo), (b.lo), c); \ |
| 60 | ARM_DOT4((a.hi), (b.hi), c); \ |
| 61 | }) |
| 62 | #define ARM_DOT16(a, b, c) \ |
| 63 | ({ \ |
| 64 | ARM_DOT8((a.lo), (b.lo), c); \ |
| 65 | ARM_DOT8((a.hi), (b.hi), c); \ |
| 66 | }) |
| 67 | |
| 68 | #else // defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8) |
| 69 | |
| 70 | /** Specialized macros to perform the dot product instruction between two vectors of size K0 [1,16] without using the dot8 instruction. */ |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 71 | #define ARM_DOT1(a, b, c) \ |
| 72 | ({ \ |
| 73 | c += (ACC_DATA_TYPE)a * b; \ |
Gian Marco Iodice | 43a129e | 2019-05-14 10:14:08 +0100 | [diff] [blame] | 74 | }) |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 75 | #define ARM_DOT2(a, b, c) \ |
| 76 | ({ \ |
| 77 | c += (ACC_DATA_TYPE)a.s0 * b.s0; \ |
| 78 | c += (ACC_DATA_TYPE)a.s1 * b.s1; \ |
Gian Marco Iodice | 43a129e | 2019-05-14 10:14:08 +0100 | [diff] [blame] | 79 | }) |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 80 | #define ARM_DOT3(a, b, c) \ |
| 81 | ({ \ |
| 82 | ARM_DOT2(a, b, c); \ |
| 83 | c += (ACC_DATA_TYPE)a.s2 * b.s2; \ |
Gian Marco Iodice | 43a129e | 2019-05-14 10:14:08 +0100 | [diff] [blame] | 84 | }) |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 85 | #define ARM_DOT4(a, b, c) \ |
| 86 | ({ \ |
| 87 | ARM_DOT3(a, b, c); \ |
| 88 | c += (ACC_DATA_TYPE)a.s3 * b.s3; \ |
Gian Marco Iodice | 43a129e | 2019-05-14 10:14:08 +0100 | [diff] [blame] | 89 | }) |
| 90 | #define ARM_DOT8(a, b, c) \ |
| 91 | ({ \ |
| 92 | ARM_DOT4((a.lo), (b.lo), c); \ |
| 93 | ARM_DOT4((a.hi), (b.hi), c); \ |
| 94 | }) |
| 95 | #define ARM_DOT16(a, b, c) \ |
| 96 | ({ \ |
| 97 | ARM_DOT8((a.lo), (b.lo), c); \ |
| 98 | ARM_DOT8((a.hi), (b.hi), c); \ |
| 99 | }) |
| 100 | #endif // defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8) |
| 101 | |
| 102 | /** Specialized macros to perform a broadcast dot product operation between one vector "a" and N0 vectors "b" of size K0 [1,16] */ |
| 103 | #define ARM_DOT_K0X2(k0, a, b, c) \ |
| 104 | ({ \ |
| 105 | ARM_DOT_K0(k0, (a), (b##0), (c.s0)); \ |
| 106 | ARM_DOT_K0(k0, (a), (b##1), (c.s1)); \ |
| 107 | }) |
| 108 | #define ARM_DOT_K0X3(k0, a, b, c) \ |
| 109 | ({ \ |
| 110 | ARM_DOT_K0X2(k0, a, b, c); \ |
| 111 | ARM_DOT_K0(k0, (a), (b##2), (c.s2)); \ |
| 112 | }) |
| 113 | #define ARM_DOT_K0X4(k0, a, b, c) \ |
| 114 | ({ \ |
| 115 | ARM_DOT_K0X3(k0, a, b, c); \ |
| 116 | ARM_DOT_K0(k0, (a), (b##3), (c.s3)); \ |
| 117 | }) |
| 118 | #define ARM_DOT_K0X8(k0, a, b, c) \ |
| 119 | ({ \ |
| 120 | ARM_DOT_K0X4(k0, a, b, c); \ |
| 121 | ARM_DOT_K0(k0, (a), (b##4), (c.s4)); \ |
| 122 | ARM_DOT_K0(k0, (a), (b##5), (c.s5)); \ |
| 123 | ARM_DOT_K0(k0, (a), (b##6), (c.s6)); \ |
| 124 | ARM_DOT_K0(k0, (a), (b##7), (c.s7)); \ |
| 125 | }) |
| 126 | #define ARM_DOT_K0X16(k0, a, b, c) \ |
| 127 | ({ \ |
| 128 | ARM_DOT_K0X8(k0, a, b, c); \ |
| 129 | ARM_DOT_K0(k0, (a), (b##8), (c.s8)); \ |
| 130 | ARM_DOT_K0(k0, (a), (b##9), (c.s9)); \ |
| 131 | ARM_DOT_K0(k0, (a), (b##A), (c.sA)); \ |
| 132 | ARM_DOT_K0(k0, (a), (b##B), (c.sB)); \ |
| 133 | ARM_DOT_K0(k0, (a), (b##C), (c.sC)); \ |
| 134 | ARM_DOT_K0(k0, (a), (b##D), (c.sD)); \ |
| 135 | ARM_DOT_K0(k0, (a), (b##E), (c.sE)); \ |
| 136 | ARM_DOT_K0(k0, (a), (b##F), (c.sF)); \ |
| 137 | }) |
| 138 | |
Georgios Pinitas | 705fd3d | 2019-06-17 17:23:22 +0100 | [diff] [blame] | 139 | /** Specialized macros to perform a a partial matrix multiplication with dimensions M0,N0,K0 */ |
Gian Marco Iodice | 43a129e | 2019-05-14 10:14:08 +0100 | [diff] [blame] | 140 | #define ARM_MM_K0XN0X1(n0, k0, a, b, c) \ |
| 141 | ({ \ |
| 142 | ARM_DOT_K0XN0(n0, k0, (a##0), b, (c##0)); \ |
| 143 | }) |
| 144 | #define ARM_MM_K0XN0X2(n0, k0, a, b, c) \ |
| 145 | ({ \ |
| 146 | ARM_MM_K0XN0X1(n0, k0, a, b, c); \ |
| 147 | ARM_DOT_K0XN0(n0, k0, (a##1), b, (c##1)); \ |
| 148 | }) |
| 149 | #define ARM_MM_K0XN0X3(n0, k0, a, b, c) \ |
| 150 | ({ \ |
| 151 | ARM_MM_K0XN0X2(n0, k0, a, b, c); \ |
| 152 | ARM_DOT_K0XN0(n0, k0, (a##2), b, (c##2)); \ |
| 153 | }) |
| 154 | #define ARM_MM_K0XN0X4(n0, k0, a, b, c) \ |
| 155 | ({ \ |
| 156 | ARM_MM_K0XN0X3(n0, k0, a, b, c); \ |
| 157 | ARM_DOT_K0XN0(n0, k0, (a##3), b, (c##3)); \ |
| 158 | }) |
| 159 | #define ARM_MM_K0XN0X5(n0, k0, a, b, c) \ |
| 160 | ({ \ |
| 161 | ARM_MM_K0XN0X4(n0, k0, a, b, c); \ |
| 162 | ARM_DOT_K0XN0(n0, k0, (a##4), b, (c##4)); \ |
| 163 | }) |
| 164 | #define ARM_MM_K0XN0X6(n0, k0, a, b, c) \ |
| 165 | ({ \ |
| 166 | ARM_MM_K0XN0X5(n0, k0, a, b, c); \ |
| 167 | ARM_DOT_K0XN0(n0, k0, (a##5), b, (c##5)); \ |
| 168 | }) |
| 169 | #define ARM_MM_K0XN0X7(n0, k0, a, b, c) \ |
| 170 | ({ \ |
| 171 | ARM_MM_K0XN0X6(n0, k0, a, b, c); \ |
| 172 | ARM_DOT_K0XN0(n0, k0, (a##6), b, (c##6)); \ |
| 173 | }) |
| 174 | #define ARM_MM_K0XN0X8(n0, k0, a, b, c) \ |
| 175 | ({ \ |
| 176 | ARM_MM_K0XN0X7(n0, k0, a, b, c); \ |
| 177 | ARM_DOT_K0XN0(n0, k0, (a##7), b, (c##7)); \ |
| 178 | }) |
| 179 | |
| 180 | #define ARM_DOT_K0(k0, a, b, c) \ |
| 181 | ({ \ |
| 182 | CONCAT(ARM_DOT, k0) \ |
| 183 | ((a), (b), (c)); \ |
| 184 | }) |
| 185 | |
| 186 | #define ARM_DOT_K0XN0(n0, k0, a, b, c) \ |
| 187 | ({ \ |
| 188 | CONCAT(ARM_DOT_K0X, n0) \ |
| 189 | (k0, (a), b, (c)); \ |
| 190 | }) |
| 191 | |
| 192 | #define ARM_MM_K0XN0XM0(m0, n0, k0, a, b, c) \ |
| 193 | ({ \ |
| 194 | CONCAT(ARM_MM_K0XN0X, m0) \ |
| 195 | (n0, k0, a, b, c); \ |
| 196 | }) |
| 197 | |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 198 | #if defined(NUM_ELEMS_PROCESSED_PER_THREAD_X) && defined(NUM_ELEMS_PROCESSED_PER_THREAD_Y) && defined(COLS_A) |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 199 | #define VECTOR_TYPE VEC_DATA_TYPE(DATA_TYPE, NUM_ELEMS_PROCESSED_PER_THREAD_X) |
| 200 | #define VECTOR_ACC_TYPE VEC_DATA_TYPE(ACC_DATA_TYPE, NUM_ELEMS_PROCESSED_PER_THREAD_X) |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 201 | #define VECTOR_INT VEC_DATA_TYPE(int, NUM_ELEMS_PROCESSED_PER_THREAD_X) |
| 202 | /** This OpenCL kernel computes the matrix multiplication between matrix A (src0) and matrix B (src1) in case both matrices have not beed reshaped |
| 203 | * |
| 204 | * @attention The number of matrix A columns needs to be passed at compile time using -DCOLS_A |
| 205 | * |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 206 | * @note The input data type must be passed at compile time using -DDATA_TYPE (i.e. -DDATA_TYPE=uchar) |
| 207 | * @note The accumulator data type must be passed at compile time using -DACC_DATA_TYPE (i.e. -DACC_DATA_TYPE=uint) |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 208 | * @note In case the input or output have to be reinterpreted as a 3D tensor, the following information must be passed at compile time: |
| 209 | * -# REINTERPRET_INPUT_AS_3D: To reinterpret the input as 3D |
| 210 | * -# REINTERPRET_OUTPUT_AS_3D: To reinterpret the output as 3D |
| 211 | * -# HEIGHT_GEMM3D: The height of the output in case it has to be reinterpreted as a 3D tensor. |
| 212 | * -# DEPTH_GEMM3D: The depth of the output in case it has to be reinterpreted as a 3D tensor |
| 213 | * (HEIGHT_GEMM3D * DEPTH_GEMM3D) = columns matrix A NOT reshaped |
| 214 | * |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 215 | * @param[in] src0_ptr Pointer to the source matrix. Supported data type: QASYMM8 |
| 216 | * @param[in] src0_stride_x Stride of the source matrix in X dimension (in bytes) |
| 217 | * @param[in] src0_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 218 | * @param[in] src0_stride_y Stride of the source matrix in Y dimension (in bytes) |
| 219 | * @param[in] src0_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 220 | * @param[in] src0_offset_first_element_in_bytes The offset of the first element in the source matrix |
| 221 | * @param[in] src1_ptr Pointer to the source matrix. Supported data type: same as @p src0_ptr |
| 222 | * @param[in] src1_stride_x Stride of the source matrix in X dimension (in bytes) |
| 223 | * @param[in] src1_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 224 | * @param[in] src1_stride_y Stride of the source matrix in Y dimension (in bytes) |
| 225 | * @param[in] src1_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 226 | * @param[in] src1_offset_first_element_in_bytes The offset of the first element in the source matrix |
| 227 | * @param[out] dst_ptr Pointer to the destination matrix Supported data type: S32 |
| 228 | * @param[in] dst_stride_x Stride of the destination matrix in X dimension (in bytes) |
| 229 | * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes) |
| 230 | * @param[in] dst_stride_y Stride of the destination matrix in Y dimension (in bytes) |
| 231 | * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes) |
| 232 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination matrix |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 233 | * @param[in] src0_stride_z Stride of the source matrix in Z dimension (in bytes) |
| 234 | * @param[in] src1_stride_z Stride of the source matrix in Z dimension (in bytes) |
| 235 | * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes) |
| 236 | * @param[in] src_cross_plane_pad (Optional) Bottom paddings in unit of elements for the input tensor (only if defined REINTERPRET_INPUT_AS_3D) |
| 237 | * @param[in] dst_cross_plane_pad (Optional) Bottom paddings in unit of elements for the output tensor (only if defined REINTERPRET_OUTPUT_AS_3D) |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 238 | */ |
Gian Marco | 7b4d547 | 2018-01-10 15:56:30 +0000 | [diff] [blame] | 239 | __kernel void gemmlowp_mm_midgard(IMAGE_DECLARATION(src0), |
| 240 | IMAGE_DECLARATION(src1), |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 241 | IMAGE_DECLARATION(dst), |
| 242 | uint src0_stride_z, |
| 243 | uint src1_stride_z, |
| 244 | uint dst_stride_z |
| 245 | #if defined(REINTERPRET_INPUT_AS_3D) |
| 246 | , |
| 247 | uint src_cross_plane_pad |
| 248 | #endif // REINTERPRET_INPUT_AS_3D |
| 249 | #if defined(REINTERPRET_OUTPUT_AS_3D) |
| 250 | , |
| 251 | uint dst_cross_plane_pad |
| 252 | #endif // REINTERPRET_OUTPUT_AS_3D |
| 253 | ) |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 254 | { |
| 255 | int idx = get_global_id(0) * NUM_ELEMS_PROCESSED_PER_THREAD_X; |
| 256 | |
| 257 | // Compute starting address for matrix A and Matrix B |
| 258 | int2 src_addr = ((int2)(src0_offset_first_element_in_bytes, src1_offset_first_element_in_bytes)); |
| 259 | |
| 260 | // Update address for the matrix A |
| 261 | src_addr.s0 += get_global_id(1) * src0_stride_y * NUM_ELEMS_PROCESSED_PER_THREAD_Y; |
| 262 | |
| 263 | // Update address for the matrix B |
| 264 | src_addr.s1 += idx; |
| 265 | |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 266 | #if defined(REINTERPRET_INPUT_AS_3D) |
| 267 | // Since we load a 2D input tile from a 3D tensor, we need to check when the plane changes across the z dimension |
| 268 | // in order to take into account the presence of possible cross plane paddings |
| 269 | // |
| 270 | // | | |
| 271 | // | plane0 | |
| 272 | // | | |
| 273 | // |__________________| |
| 274 | // |******************| |
| 275 | // | cross_plane_pad | |
| 276 | // |******************| |
| 277 | // | | |
| 278 | // | plane1 | |
| 279 | // | | |
| 280 | // |__________________| |
| 281 | |
| 282 | // The plane (zin) is calculated dividing M (get_global_id(1) * NUM_ELEMS_PROCESSED_PER_THREAD_Y) by HEIGHT_GEMM3D |
| 283 | uint4 zin = ((uint4)(0, 1, 2, 3) + (uint4)(get_global_id(1) * NUM_ELEMS_PROCESSED_PER_THREAD_Y)) / (uint4)HEIGHT_GEMM3D; |
| 284 | zin = min(DEPTH_GEMM3D - 1, zin); |
| 285 | |
| 286 | // Add offset due to the cross plane paddings |
| 287 | zin *= (src_cross_plane_pad * src0_stride_y); |
| 288 | |
| 289 | // Add offset for batched GEMM. The batches will be in the fourth dimension and for this reason we |
| 290 | // multiply src0_stride_z by DEPTH_GEMM3D |
| 291 | src_addr.s0 += get_global_id(2) * src0_stride_z * DEPTH_GEMM3D; |
| 292 | |
| 293 | #else // defined(REINTERPRET_INPUT_AS_3D) |
| 294 | |
| 295 | // Add offset for batched GEMM |
| 296 | src_addr.s0 += get_global_id(2) * src0_stride_z; |
| 297 | |
| 298 | #endif // defined(REINTERPRET_INPUT_AS_3D) |
| 299 | |
| 300 | #if defined(MATRIX_B_DEPTH) |
| 301 | // Do not slide matrix B if the matrix B has 3 dimensions and matrix A more than 3 |
| 302 | src_addr.s1 += (get_global_id(2) % MATRIX_B_DEPTH) * src1_stride_z; |
| 303 | #else // defined(MATRIX_B_DEPTH) |
| 304 | src_addr.s1 += get_global_id(2) * src1_stride_z; |
| 305 | #endif // defined(MATRIX_B_DEPTH) |
| 306 | |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 307 | int end_row_vec_a = src_addr.s0 + COLS_A; |
| 308 | |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 309 | VECTOR_ACC_TYPE acc0 = 0; |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 310 | #if NUM_ELEMS_PROCESSED_PER_THREAD_Y > 1 |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 311 | VECTOR_ACC_TYPE acc1 = 0; |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 312 | #endif // NUM_ELEMS_PROCESSED_PER_THREAD_Y > 1 |
| 313 | #if NUM_ELEMS_PROCESSED_PER_THREAD_Y > 2 |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 314 | VECTOR_ACC_TYPE acc2 = 0; |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 315 | #endif // NUM_ELEMS_PROCESSED_PER_THREAD_Y > 2 |
| 316 | #if NUM_ELEMS_PROCESSED_PER_THREAD_Y > 3 |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 317 | VECTOR_ACC_TYPE acc3 = 0; |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 318 | #endif // NUM_ELEMS_PROCESSED_PER_THREAD_Y > 3 |
Gian Marco | 7b4d547 | 2018-01-10 15:56:30 +0000 | [diff] [blame] | 319 | #if NUM_ELEMS_PROCESSED_PER_THREAD_Y > 4 |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 320 | VECTOR_ACC_TYPE acc4 = 0; |
Gian Marco | 7b4d547 | 2018-01-10 15:56:30 +0000 | [diff] [blame] | 321 | #endif // NUM_ELEMS_PROCESSED_PER_THREAD_Y > 4 |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 322 | |
| 323 | for(; src_addr.s0 <= (end_row_vec_a - 2); src_addr += (int2)(2, 2 * src1_stride_y)) |
| 324 | { |
| 325 | // Load values from matrix A |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 326 | VEC_DATA_TYPE(DATA_TYPE, 2) |
| 327 | a0 = vload2(0, (__global DATA_TYPE *)(src0_ptr + src_addr.s0 + 0 * src0_stride_y)); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 328 | #if NUM_ELEMS_PROCESSED_PER_THREAD_Y > 1 |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 329 | VEC_DATA_TYPE(DATA_TYPE, 2) |
| 330 | a1 = vload2(0, (__global DATA_TYPE *)(src0_ptr + src_addr.s0 + 1 * src0_stride_y)); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 331 | #endif // NUM_ELEMS_PROCESSED_PER_THREAD_Y > 1 |
| 332 | #if NUM_ELEMS_PROCESSED_PER_THREAD_Y > 2 |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 333 | VEC_DATA_TYPE(DATA_TYPE, 2) |
| 334 | a2 = vload2(0, (__global DATA_TYPE *)(src0_ptr + src_addr.s0 + 2 * src0_stride_y)); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 335 | #endif // NUM_ELEMS_PROCESSED_PER_THREAD_Y > 2 |
| 336 | #if NUM_ELEMS_PROCESSED_PER_THREAD_Y > 3 |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 337 | VEC_DATA_TYPE(DATA_TYPE, 2) |
| 338 | a3 = vload2(0, (__global DATA_TYPE *)(src0_ptr + src_addr.s0 + 3 * src0_stride_y)); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 339 | #endif // NUM_ELEMS_PROCESSED_PER_THREAD_Y > 3 |
Gian Marco | 7b4d547 | 2018-01-10 15:56:30 +0000 | [diff] [blame] | 340 | #if NUM_ELEMS_PROCESSED_PER_THREAD_Y > 4 |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 341 | VEC_DATA_TYPE(DATA_TYPE, 2) |
| 342 | a4 = vload2(0, (__global DATA_TYPE *)(src0_ptr + src_addr.s0 + 4 * src0_stride_y)); |
Gian Marco | 7b4d547 | 2018-01-10 15:56:30 +0000 | [diff] [blame] | 343 | #endif // NUM_ELEMS_PROCESSED_PER_THREAD_Y > 4 |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 344 | // Load values from matrix B |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 345 | VECTOR_TYPE b0 = VLOAD(NUM_ELEMS_PROCESSED_PER_THREAD_X)(0, (__global DATA_TYPE *)(src1_ptr + src_addr.s1)); |
| 346 | VECTOR_TYPE b1 = VLOAD(NUM_ELEMS_PROCESSED_PER_THREAD_X)(0, (__global DATA_TYPE *)(src1_ptr + src_addr.s1 + src1_stride_y)); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 347 | |
| 348 | // Accumulate |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 349 | acc0 += CONVERT(b0, VECTOR_ACC_TYPE) * (VECTOR_ACC_TYPE)a0.s0; |
| 350 | acc0 += CONVERT(b1, VECTOR_ACC_TYPE) * (VECTOR_ACC_TYPE)a0.s1; |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 351 | #if NUM_ELEMS_PROCESSED_PER_THREAD_Y > 1 |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 352 | acc1 += CONVERT(b0, VECTOR_ACC_TYPE) * (VECTOR_ACC_TYPE)a1.s0; |
| 353 | acc1 += CONVERT(b1, VECTOR_ACC_TYPE) * (VECTOR_ACC_TYPE)a1.s1; |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 354 | #endif // NUM_ELEMS_PROCESSED_PER_THREAD_Y > 1 |
| 355 | #if NUM_ELEMS_PROCESSED_PER_THREAD_Y > 2 |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 356 | acc2 += CONVERT(b0, VECTOR_ACC_TYPE) * (VECTOR_ACC_TYPE)a2.s0; |
| 357 | acc2 += CONVERT(b1, VECTOR_ACC_TYPE) * (VECTOR_ACC_TYPE)a2.s1; |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 358 | #endif // NUM_ELEMS_PROCESSED_PER_THREAD_Y > 2 |
| 359 | #if NUM_ELEMS_PROCESSED_PER_THREAD_Y > 3 |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 360 | acc3 += CONVERT(b0, VECTOR_ACC_TYPE) * (VECTOR_ACC_TYPE)a3.s0; |
| 361 | acc3 += CONVERT(b1, VECTOR_ACC_TYPE) * (VECTOR_ACC_TYPE)a3.s1; |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 362 | #endif // NUM_ELEMS_PROCESSED_PER_THREAD_Y > 3 |
Gian Marco | 7b4d547 | 2018-01-10 15:56:30 +0000 | [diff] [blame] | 363 | #if NUM_ELEMS_PROCESSED_PER_THREAD_Y > 4 |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 364 | acc4 += CONVERT(b0, VECTOR_ACC_TYPE) * (VECTOR_ACC_TYPE)a4.s0; |
| 365 | acc4 += CONVERT(b1, VECTOR_ACC_TYPE) * (VECTOR_ACC_TYPE)a4.s1; |
Gian Marco | 7b4d547 | 2018-01-10 15:56:30 +0000 | [diff] [blame] | 366 | #endif // NUM_ELEMS_PROCESSED_PER_THREAD_Y > 4 |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | for(; src_addr.s0 < end_row_vec_a; src_addr += (int2)(1, src1_stride_y)) |
| 370 | { |
| 371 | // Load values from matrix A |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 372 | DATA_TYPE a0 = *((__global DATA_TYPE *)(src0_ptr + src_addr.s0 + 0 * src0_stride_y)); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 373 | #if NUM_ELEMS_PROCESSED_PER_THREAD_Y > 1 |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 374 | DATA_TYPE a1 = *((__global DATA_TYPE *)(src0_ptr + src_addr.s0 + 1 * src0_stride_y)); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 375 | #endif // NUM_ELEMS_PROCESSED_PER_THREAD_Y > 1 |
| 376 | #if NUM_ELEMS_PROCESSED_PER_THREAD_Y > 2 |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 377 | DATA_TYPE a2 = *((__global DATA_TYPE *)(src0_ptr + src_addr.s0 + 2 * src0_stride_y)); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 378 | #endif // NUM_ELEMS_PROCESSED_PER_THREAD_Y > 2 |
| 379 | #if NUM_ELEMS_PROCESSED_PER_THREAD_Y > 3 |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 380 | DATA_TYPE a3 = *((__global DATA_TYPE *)(src0_ptr + src_addr.s0 + 3 * src0_stride_y)); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 381 | #endif // NUM_ELEMS_PROCESSED_PER_THREAD_Y > 3 |
Gian Marco | 7b4d547 | 2018-01-10 15:56:30 +0000 | [diff] [blame] | 382 | #if NUM_ELEMS_PROCESSED_PER_THREAD_Y > 4 |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 383 | DATA_TYPE a4 = *((__global DATA_TYPE *)(src0_ptr + src_addr.s0 + 4 * src0_stride_y)); |
Gian Marco | 7b4d547 | 2018-01-10 15:56:30 +0000 | [diff] [blame] | 384 | #endif // NUM_ELEMS_PROCESSED_PER_THREAD_Y > 4 |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 385 | // Load values from matrix B |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 386 | VECTOR_TYPE b0 = VLOAD(NUM_ELEMS_PROCESSED_PER_THREAD_X)(0, (__global DATA_TYPE *)(src1_ptr + src_addr.s1)); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 387 | |
| 388 | // Accumulate |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 389 | acc0 += CONVERT(b0, VECTOR_ACC_TYPE) * (VECTOR_ACC_TYPE)a0; |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 390 | #if NUM_ELEMS_PROCESSED_PER_THREAD_Y > 1 |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 391 | acc1 += CONVERT(b0, VECTOR_ACC_TYPE) * (VECTOR_ACC_TYPE)a1; |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 392 | #endif // NUM_ELEMS_PROCESSED_PER_THREAD_Y > 1 |
| 393 | #if NUM_ELEMS_PROCESSED_PER_THREAD_Y > 2 |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 394 | acc2 += CONVERT(b0, VECTOR_ACC_TYPE) * (VECTOR_ACC_TYPE)a2; |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 395 | #endif // NUM_ELEMS_PROCESSED_PER_THREAD_Y > 2 |
| 396 | #if NUM_ELEMS_PROCESSED_PER_THREAD_Y > 3 |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 397 | acc3 += CONVERT(b0, VECTOR_ACC_TYPE) * (VECTOR_ACC_TYPE)a3; |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 398 | #endif // NUM_ELEMS_PROCESSED_PER_THREAD_Y > 3 |
Gian Marco | 7b4d547 | 2018-01-10 15:56:30 +0000 | [diff] [blame] | 399 | #if NUM_ELEMS_PROCESSED_PER_THREAD_Y > 4 |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 400 | acc4 += CONVERT(b0, VECTOR_ACC_TYPE) * (VECTOR_ACC_TYPE)a4; |
Gian Marco | 7b4d547 | 2018-01-10 15:56:30 +0000 | [diff] [blame] | 401 | #endif // NUM_ELEMS_PROCESSED_PER_THREAD_Y > 4 |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 404 | const int z = get_global_id(2); |
| 405 | |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 406 | // Compute destination address |
| 407 | Image dst = CONVERT_TO_IMAGE_STRUCT(dst); |
| 408 | |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 409 | #if defined(REINTERPRET_OUTPUT_AS_3D) |
| 410 | // Since we store a 2D output tile in a 3D tensor, we need to check when the plane changes across the z dimension |
| 411 | // in order to take into account the presence of possible cross plane paddings |
| 412 | // |
| 413 | // | | |
| 414 | // | plane0 | |
| 415 | // | | |
| 416 | // |__________________| |
| 417 | // |******************| |
| 418 | // | cross_plane_pad | |
| 419 | // |******************| |
| 420 | // | | |
| 421 | // | plane1 | |
| 422 | // | | |
| 423 | // |__________________| |
| 424 | |
| 425 | // The plane (zout) is calculated dividing M (get_global_id(1) * NUM_ELEMS_PROCESSED_PER_THREAD_Y) by HEIGHT_GEMM3D |
| 426 | uint8 zout = ((uint8)(0, 1, 2, 3, 4, 5, 6, 7) + (uint8)(get_global_id(1) * NUM_ELEMS_PROCESSED_PER_THREAD_Y)) / (uint8)HEIGHT_GEMM3D; |
| 427 | zout = min(DEPTH_GEMM3D - 1, zout); |
| 428 | |
| 429 | // Add offset due to the cross plane paddings |
| 430 | zout *= (dst_cross_plane_pad * dst_stride_y); |
| 431 | |
| 432 | // Add offset for batched GEMM. The batches will be in the fourth dimension and for this reason we |
| 433 | // multiply dst_stride_z by DEPTH_GEMM3D |
| 434 | dst.ptr += z * dst_stride_z * DEPTH_GEMM3D; |
| 435 | |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 436 | // Store the result |
| 437 | VSTORE(NUM_ELEMS_PROCESSED_PER_THREAD_X) |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 438 | (CONVERT(acc0, VECTOR_INT), 0, (__global int *)(dst.ptr + 0 * dst_stride_y + zout.s0)); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 439 | #if NUM_ELEMS_PROCESSED_PER_THREAD_Y > 1 |
| 440 | VSTORE(NUM_ELEMS_PROCESSED_PER_THREAD_X) |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 441 | (CONVERT(acc1, VECTOR_INT), 0, (__global int *)(dst.ptr + 1 * dst_stride_y + zout.s1)); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 442 | #endif // NUM_ELEMS_PROCESSED_PER_THREAD_Y > 1 |
| 443 | #if NUM_ELEMS_PROCESSED_PER_THREAD_Y > 2 |
| 444 | VSTORE(NUM_ELEMS_PROCESSED_PER_THREAD_X) |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 445 | (CONVERT(acc2, VECTOR_INT), 0, (__global int *)(dst.ptr + 2 * dst_stride_y + zout.s2)); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 446 | #endif // NUM_ELEMS_PROCESSED_PER_THREAD_Y > 2 |
| 447 | #if NUM_ELEMS_PROCESSED_PER_THREAD_Y > 3 |
| 448 | VSTORE(NUM_ELEMS_PROCESSED_PER_THREAD_X) |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 449 | (CONVERT(acc3, VECTOR_INT), 0, (__global int *)(dst.ptr + 3 * dst_stride_y + zout.s3)); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 450 | #endif // NUM_ELEMS_PROCESSED_PER_THREAD_Y > 3 |
Gian Marco | 7b4d547 | 2018-01-10 15:56:30 +0000 | [diff] [blame] | 451 | #if NUM_ELEMS_PROCESSED_PER_THREAD_Y > 4 |
| 452 | VSTORE(NUM_ELEMS_PROCESSED_PER_THREAD_X) |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 453 | (CONVERT(acc4, VECTOR_INT), 0, (__global int *)(dst.ptr + 4 * dst_stride_y + zout.s4)); |
Gian Marco | 7b4d547 | 2018-01-10 15:56:30 +0000 | [diff] [blame] | 454 | #endif // NUM_ELEMS_PROCESSED_PER_THREAD_Y > 4 |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 455 | |
| 456 | #else // defined(REINTERPRET_OUTPUT_AS_3D) |
| 457 | // Add offset for batched GEMM |
| 458 | dst.ptr += z * dst_stride_z; |
| 459 | |
| 460 | // Store the result |
| 461 | VSTORE(NUM_ELEMS_PROCESSED_PER_THREAD_X) |
| 462 | (CONVERT(acc0, VECTOR_INT), 0, (__global int *)(dst.ptr + 0 * dst_stride_y)); |
| 463 | #if NUM_ELEMS_PROCESSED_PER_THREAD_Y > 1 |
| 464 | VSTORE(NUM_ELEMS_PROCESSED_PER_THREAD_X) |
| 465 | (CONVERT(acc1, VECTOR_INT), 0, (__global int *)(dst.ptr + 1 * dst_stride_y)); |
| 466 | #endif // NUM_ELEMS_PROCESSED_PER_THREAD_Y > 1 |
| 467 | #if NUM_ELEMS_PROCESSED_PER_THREAD_Y > 2 |
| 468 | VSTORE(NUM_ELEMS_PROCESSED_PER_THREAD_X) |
| 469 | (CONVERT(acc2, VECTOR_INT), 0, (__global int *)(dst.ptr + 2 * dst_stride_y)); |
| 470 | #endif // NUM_ELEMS_PROCESSED_PER_THREAD_Y > 2 |
| 471 | #if NUM_ELEMS_PROCESSED_PER_THREAD_Y > 3 |
| 472 | VSTORE(NUM_ELEMS_PROCESSED_PER_THREAD_X) |
| 473 | (CONVERT(acc3, VECTOR_INT), 0, (__global int *)(dst.ptr + 3 * dst_stride_y)); |
| 474 | #endif // NUM_ELEMS_PROCESSED_PER_THREAD_Y > 3 |
| 475 | #if NUM_ELEMS_PROCESSED_PER_THREAD_Y > 4 |
| 476 | VSTORE(NUM_ELEMS_PROCESSED_PER_THREAD_X) |
| 477 | (CONVERT(acc4, VECTOR_INT), 0, (__global int *)(dst.ptr + 4 * dst_stride_y)); |
| 478 | #endif // NUM_ELEMS_PROCESSED_PER_THREAD_Y > 4 |
| 479 | #endif // defined(REINTERPRET_OUTPUT_AS_3D) |
Gian Marco | 7b4d547 | 2018-01-10 15:56:30 +0000 | [diff] [blame] | 480 | } |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 481 | #endif // defined(NUM_ELEMS_PROCESSED_PER_THREAD_X) && defined(NUM_ELEMS_PROCESSED_PER_THREAD_Y) && defined(COLS_A) |
| 482 | |
Gian Marco Iodice | b0c5037 | 2019-03-15 10:13:05 +0000 | [diff] [blame] | 483 | #if defined(M0) && defined(N0) && defined(K0) && defined(V0) && defined(H0) && defined(M) && defined(N) |
Gian Marco Iodice | 43a129e | 2019-05-14 10:14:08 +0100 | [diff] [blame] | 484 | /** This OpenCL kernel computes the matrix multiplication between 2 matrices with QASYMM data type. |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 485 | * The LHS matrix must be reshaped with @ref CLGEMMReshapeLHSMatrixKernel and the M0xK0 must be NOT transposed |
| 486 | * The RHS matrix must be reshaped with @ref CLGEMMReshapeRHSMatrixKernel and the K0xN0 must be transposed |
| 487 | * |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 488 | * @note The input data type must be passed at compile time using -DDATA_TYPE (i.e. -DDATA_TYPE=uchar) |
| 489 | * @note The accumulator data type must be passed at compile time using -DACC_DATA_TYPE (i.e. -DACC_DATA_TYPE=uint) |
Gian Marco Iodice | b0c5037 | 2019-03-15 10:13:05 +0000 | [diff] [blame] | 490 | * @note If the first two dimensions of NDRange have been dispatched with "dummy_work_items" support, the option -DDUMMY_WORK_ITEMS must be passed at compile time. |
| 491 | * @note The GEMM's dimensions M and N must be passed at compile time using -DM and -DN (i.e. -DM=52 and -DN=90). |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 492 | * @note The block's dimensions used for reshaping the LHS matrix and the RHS matrix (M0, N0 and K0) must be passed at compile time using -DM0, -DN0 and -DK0 (i.e. -DM0=4, -DN0=8, -DK0=4). |
| 493 | * @note The number of M0xK0 vertical blocks stored on the same output row of the reshaped LHS matrix must be passed at compile time using -DV0 (i.e. -DV0=2) |
| 494 | * @note The number of K0xN0 horizontal blocks stored on the same output row of the reshaped RHS matrix must be passed at compile time using -DH0 (i.e. -DH0=2) |
| 495 | * @note If the M0xK0 blocks in the reshaped LHS matrix have been interleaved, the option -DLHS_INTERLEAVE must passed at compile time. |
| 496 | * @note If the K0xN0 blocks in the reshaped RHS matrix have been interleaved, the option -DRHS_INTERLEAVE must passed at compile time. |
| 497 | * @note Only the following configurations of M0, N0 and K0 are currently supported: |
| 498 | * - M0 = 2, 3, 4, 5, 6, 7, 8 |
| 499 | * - N0 = 2, 3, 4, 8, 16 |
| 500 | * - K0 = 2, 3, 4, 8, 16 |
Gian Marco Iodice | 62251f7 | 2019-03-11 16:07:12 +0000 | [diff] [blame] | 501 | * - V0 >= 1 |
| 502 | * - H0 >= 1 |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 503 | * |
| 504 | * @note In case the output has to be reinterpreted as a 3D tensor (i.e. output of convolution layer), the following information must be passed at compile time: |
| 505 | * -# REINTERPRET_OUTPUT_AS_3D: To reinterpret the output as 3D |
| 506 | * -# HEIGHT_GEMM3D: The height of the output in case it has to be reinterpreted as a 3D tensor. |
| 507 | * -# DEPTH_GEMM3D: The depth of the output in case it has to be reinterpreted as a 3D tensor |
| 508 | * (HEIGHT_GEMM3D * DEPTH_GEMM3D) = columns LHS matrix NOT reshaped |
| 509 | * |
| 510 | * @param[in] lhs_ptr Pointer to the LHS reshaped matrix. Supported data type: QASYMM8 |
| 511 | * @param[in] lhs_stride_x Stride of the LHS reshaped matrix in X dimension (in bytes) |
| 512 | * @param[in] lhs_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 513 | * @param[in] lhs_stride_y Stride of the LHS reshaped matrix in Y dimension (in bytes) |
| 514 | * @param[in] lhs_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 515 | * @param[in] lhs_offset_first_element_in_bytes The offset of the first element in the LHS reshaped matrix |
| 516 | * @param[in] rhs_ptr Pointer to the RHS reshaped matrix. Supported data type: same as @p lhs_ptr |
| 517 | * @param[in] rhs_stride_x Stride of the RHS reshaped matrix in X dimension (in bytes) |
| 518 | * @param[in] rhs_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 519 | * @param[in] rhs_stride_y Stride of the RHS reshaped matrix in Y dimension (in bytes) |
| 520 | * @param[in] rhs_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 521 | * @param[in] rhs_offset_first_element_in_bytes The offset of the first element in the RHS reshaped matrix |
| 522 | * @param[out] dst_ptr Pointer to the destination matrix Supported data type: same as @p lhs_ptr |
| 523 | * @param[in] dst_stride_x Stride of the destination matrix in X dimension (in bytes) |
| 524 | * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes) |
| 525 | * @param[in] dst_stride_y Stride of the destination matrix in Y dimension (in bytes) |
| 526 | * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes) |
| 527 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination matrix |
| 528 | * @param[in] k Number of columns in LHS matrix and rows in RHS matrix not reshaped. |
| 529 | * @param[in] lhs_stride_z Stride of the LHS reshaped matrix in Z dimension (in bytes) |
| 530 | * @param[in] rhs_stride_z Stride of the RHS reshaped matrix in Z dimension (in bytes) |
| 531 | * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes) |
| 532 | * @param[in] dst_cross_plane_pad (Optional) Bottom paddings in unit of elements (only if defined REINTERPRET_OUTPUT_AS_3D) |
| 533 | */ |
| 534 | __kernel void gemmlowp_mm_reshaped_lhs_nt_rhs_t(IMAGE_DECLARATION(lhs), |
| 535 | IMAGE_DECLARATION(rhs), |
| 536 | IMAGE_DECLARATION(dst), |
| 537 | uint k, |
| 538 | uint lhs_stride_z, |
| 539 | uint rhs_stride_z, |
| 540 | uint dst_stride_z |
| 541 | #if defined(REINTERPRET_OUTPUT_AS_3D) |
| 542 | , |
| 543 | uint dst_cross_plane_pad |
| 544 | #endif // REINTERPRET_OUTPUT_AS_3D |
| 545 | ) |
| 546 | { |
| 547 | // Block size |
| 548 | #define LHS_BLOCK_SIZE ((K0) * (M0)) |
| 549 | |
| 550 | #if defined(LHS_INTERLEAVE) |
| 551 | #define LHS_OFFSET_X (K0) |
| 552 | #define LHS_STEP_X ((K0) * (V0)) |
| 553 | #define LHS_STEP_LOOP (1) |
| 554 | #else // defined(INTERLEAVE) |
| 555 | #define LHS_OFFSET_X (LHS_BLOCK_SIZE) |
| 556 | #define LHS_STEP_X (K0) |
| 557 | #define LHS_STEP_LOOP (V0) |
| 558 | #endif // defined(INTERLEAVE) |
| 559 | |
| 560 | // Block size |
| 561 | #define RHS_BLOCK_SIZE ((K0) * (N0)) |
| 562 | |
| 563 | // RHS offset and step X |
| 564 | #if defined(RHS_INTERLEAVE) |
| 565 | #define RHS_OFFSET_X (K0) |
| 566 | #define RHS_STEP_X ((K0) * (H0)) |
| 567 | #define RHS_STEP_LOOP (1) |
| 568 | #else // defined(RHS_INTERLEAVE) |
| 569 | #define RHS_OFFSET_X (RHS_BLOCK_SIZE) |
| 570 | #define RHS_STEP_X (K0) |
| 571 | #define RHS_STEP_LOOP (H0) |
| 572 | #endif // defined(RHS_INTERLEAVE) |
| 573 | |
Gian Marco Iodice | 43a129e | 2019-05-14 10:14:08 +0100 | [diff] [blame] | 574 | uint x = get_global_id(0); |
| 575 | uint y = get_global_id(1); |
| 576 | uint z = get_global_id(2); |
| 577 | |
Gian Marco Iodice | b0c5037 | 2019-03-15 10:13:05 +0000 | [diff] [blame] | 578 | #if defined(DUMMY_WORK_ITEMS) |
Gian Marco Iodice | 43a129e | 2019-05-14 10:14:08 +0100 | [diff] [blame] | 579 | if((x * N0 >= N) || (y * M0 >= M)) |
Gian Marco Iodice | b0c5037 | 2019-03-15 10:13:05 +0000 | [diff] [blame] | 580 | { |
| 581 | return; |
| 582 | } |
| 583 | #endif // defined(DUMMY_WORK_ITEMS) |
| 584 | |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 585 | // Compute LHS matrix address |
Gian Marco Iodice | 43a129e | 2019-05-14 10:14:08 +0100 | [diff] [blame] | 586 | __global uchar *lhs_addr = lhs_ptr + lhs_offset_first_element_in_bytes + (y % V0) * (uint)LHS_OFFSET_X + (y / V0) * (uint)lhs_stride_y + (z * lhs_stride_z); |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 587 | |
| 588 | // Compute RHS matrix address |
Gian Marco Iodice | 43a129e | 2019-05-14 10:14:08 +0100 | [diff] [blame] | 589 | __global uchar *rhs_addr = rhs_ptr + rhs_offset_first_element_in_bytes + (x % H0) * (uint)RHS_OFFSET_X + (x / (uint)H0) * rhs_stride_y; |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 590 | |
| 591 | #if defined(MATRIX_B_DEPTH) |
| 592 | // Do not slide matrix B if the matrix B has 3 dimensions and matrix A more than 3 |
Gian Marco Iodice | 43a129e | 2019-05-14 10:14:08 +0100 | [diff] [blame] | 593 | rhs_addr += (z % MATRIX_B_DEPTH) * rhs_stride_z; |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 594 | #else // defined(MATRIX_B_DEPTH) |
Gian Marco Iodice | 43a129e | 2019-05-14 10:14:08 +0100 | [diff] [blame] | 595 | rhs_addr += z * rhs_stride_z; |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 596 | #endif // defined(MATRIX_B_DEPTH) |
| 597 | |
Gian Marco Iodice | 43a129e | 2019-05-14 10:14:08 +0100 | [diff] [blame] | 598 | REPEAT_VAR_INIT_TO_CONST(8, uint, zlhs, 0); //uint zout0=0,zout1=0,zout2=0,... zout7=0; |
| 599 | REPEAT_VAR_INIT_TO_CONST(16, uint, zrhs, 0); |
| 600 | |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 601 | // Initialize the accumulators |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 602 | REPEAT_VAR_INIT_TO_CONST(M0, VEC_DATA_TYPE(ACC_DATA_TYPE, N0), c, 0); //VEC_DATA_TYPE(ACC_DATA_TYPE, N0) c0=0,c1=0,c2=0,... c(M0-1)=0; |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 603 | |
| 604 | for(int i = 0; i < k; i += K0) |
| 605 | { |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 606 | // Load values from LHS matrix |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 607 | LOAD_BLOCK(M0, K0, DATA_TYPE, a, lhs_addr, 0, LHS_STEP_X, zlhs); |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 608 | |
| 609 | // Load values from RHS matrix |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 610 | LOAD_BLOCK(N0, K0, DATA_TYPE, b, rhs_addr, 0, RHS_STEP_X, zrhs); |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 611 | |
Gian Marco Iodice | 43a129e | 2019-05-14 10:14:08 +0100 | [diff] [blame] | 612 | // Partial matrix multiplication M0,N0,K0 |
| 613 | ARM_MM_K0XN0XM0(M0, N0, K0, a, b, c); |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 614 | |
Gian Marco Iodice | 43a129e | 2019-05-14 10:14:08 +0100 | [diff] [blame] | 615 | // Update address |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 616 | lhs_addr += (M0 * LHS_STEP_X * LHS_STEP_LOOP); |
| 617 | rhs_addr += (N0 * RHS_STEP_X * RHS_STEP_LOOP); |
| 618 | } |
| 619 | |
Gian Marco Iodice | 43a129e | 2019-05-14 10:14:08 +0100 | [diff] [blame] | 620 | __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + (x * (uint)N0 * sizeof(int)) + (y * (uint)M0 * dst_stride_y); |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 621 | |
| 622 | REPEAT_VAR_INIT_TO_CONST(8, uint, zout, 0); //uint zout0=0,zout1=0,zout2=0,... zout7=0; |
| 623 | |
| 624 | #if defined(REINTERPRET_OUTPUT_AS_3D) |
Gian Marco Iodice | 43a129e | 2019-05-14 10:14:08 +0100 | [diff] [blame] | 625 | // The plane (zout) is calculated dividing M (y * M0) by HEIGHT_GEMM3D |
| 626 | CALCULATE_Z_OFFSET(M0, uint, zout, y, HEIGHT_GEMM3D, DEPTH_GEMM3D, dst_cross_plane_pad, dst_stride_y); |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 627 | |
| 628 | // Add offset for batched GEMM. The batches will be in the fourth dimension and for this reason we |
| 629 | // multiply dst_stride_z by DEPTH_GEMM3D |
Gian Marco Iodice | 43a129e | 2019-05-14 10:14:08 +0100 | [diff] [blame] | 630 | dst_addr += z * dst_stride_z * DEPTH_GEMM3D; |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 631 | |
| 632 | #else // defined(REINTERPRET_OUTPUT_AS_3D) |
| 633 | |
| 634 | // Add offset for batched GEMM |
Gian Marco Iodice | 43a129e | 2019-05-14 10:14:08 +0100 | [diff] [blame] | 635 | dst_addr += z * dst_stride_z; |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 636 | |
| 637 | #endif // defined(REINTERPRET_OUTPUT_AS_3D) |
| 638 | |
Gian Marco Iodice | 43a129e | 2019-05-14 10:14:08 +0100 | [diff] [blame] | 639 | // Convert and store output block |
| 640 | CONVERT_STORE_BLOCK(M0, N0, int, c, dst_addr, dst_stride_y, zout); |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 641 | |
| 642 | #undef LHS_BLOCK_SIZE |
| 643 | #undef LHS_OFFSET_X |
| 644 | #undef LHS_STEP_X |
| 645 | #undef RHS_BLOCK_SIZE |
| 646 | #undef RHS_OFFSET_X |
| 647 | #undef RHS_STEP_X |
| 648 | } |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 649 | #endif // defined(M0) && defined(N0) && defined(K0) && defined(V0) && defined(H0) && defined(K) |
| 650 | |
Gian Marco Iodice | 62251f7 | 2019-03-11 16:07:12 +0000 | [diff] [blame] | 651 | #if defined(M0) && defined(N0) && defined(K0) && defined(H0) && defined(K) |
| 652 | |
Gian Marco Iodice | 62251f7 | 2019-03-11 16:07:12 +0000 | [diff] [blame] | 653 | /** This OpenCL kernel computes the matrix multiplication between 2 matrices. |
| 654 | * The LHS matrix is NOT reshaped |
Gian Marco Iodice | 43a129e | 2019-05-14 10:14:08 +0100 | [diff] [blame] | 655 | * The RHS matrix is reshaped with @ref CLGEMMReshapeRHSMatrixKernel and the block K0xN0 is transposed |
Gian Marco Iodice | 62251f7 | 2019-03-11 16:07:12 +0000 | [diff] [blame] | 656 | * |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 657 | * @note The input data type must be passed at compile time using -DDATA_TYPE (i.e. -DDATA_TYPE=uchar) |
| 658 | * @note The accumulator data type must be passed at compile time using -DACC_DATA_TYPE (i.e. -DACC_DATA_TYPE=uint) |
Gian Marco Iodice | 62251f7 | 2019-03-11 16:07:12 +0000 | [diff] [blame] | 659 | * @note The number of columns of LHS matrix must be passed at compile time using -DK (i.e. -DK=64) |
| 660 | * @note The block's dimensions used for reshaping the RHS matrix (N0 and K0) must be passed at compile time using -DN0 and -DK0 (i.e. -DN0=8, -DK0=4). |
| 661 | * @note The number of M0 rows to process must be passed at compile time using -DM0 (i.e. -DM0=2) |
| 662 | * @note The number of K0xN0 horizontal blocks stored on the same output row of the reshaped RHS matrix must be passed at compile time using -DH0 (i.e. -DH0=2) |
| 663 | * @note If the K0xN0 blocks in the reshaped RHS matrix have been interleaved, the option -DRHS_INTERLEAVE must passed at compile time. |
| 664 | * @note Only the following configurations of M0, N0 and K0 are currently supported: |
| 665 | * - M0 = 1, 2, 3, 4, 5, 6, 7, 8 |
| 666 | * - N0 = 2, 3, 4, 8, 16 |
| 667 | * - K0 = 2, 3, 4, 8, 16 |
| 668 | * - H0 >= 1 |
| 669 | * |
| 670 | * @note In case the input or output have to be reinterpreted as a 3D tensor, the following information must be passed at compile time: |
| 671 | * -# REINTERPRET_INPUT_AS_3D: To reinterpret the input as 3D |
| 672 | * -# REINTERPRET_OUTPUT_AS_3D: To reinterpret the output as 3D |
| 673 | * -# HEIGHT_GEMM3D: The height of the output in case it has to be reinterpreted as a 3D tensor. |
| 674 | * -# DEPTH_GEMM3D: The depth of the output in case it has to be reinterpreted as a 3D tensor |
| 675 | * (HEIGHT_GEMM3D * DEPTH_GEMM3D) = columns LHS matrix |
| 676 | * |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 677 | * @param[in] lhs_ptr Pointer to the LHS reshaped matrix. Supported data type: QASYMM8/QASYMM8_SIGNED |
Gian Marco Iodice | 62251f7 | 2019-03-11 16:07:12 +0000 | [diff] [blame] | 678 | * @param[in] lhs_stride_x Stride of the LHS reshaped matrix in X dimension (in bytes) |
| 679 | * @param[in] lhs_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 680 | * @param[in] lhs_stride_y Stride of the LHS reshaped matrix in Y dimension (in bytes) |
| 681 | * @param[in] lhs_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 682 | * @param[in] lhs_offset_first_element_in_bytes The offset of the first element in the LHS reshaped matrix |
| 683 | * @param[in] rhs_ptr Pointer to the RHS reshaped matrix. Supported data type: same as @p lhs_ptr |
| 684 | * @param[in] rhs_stride_x Stride of the RHS reshaped matrix in X dimension (in bytes) |
| 685 | * @param[in] rhs_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 686 | * @param[in] rhs_stride_y Stride of the RHS reshaped matrix in Y dimension (in bytes) |
| 687 | * @param[in] rhs_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 688 | * @param[in] rhs_offset_first_element_in_bytes The offset of the first element in the RHS reshaped matrix |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 689 | * @param[out] dst_ptr Pointer to the destination matrix Supported data type: S32 |
Gian Marco Iodice | 62251f7 | 2019-03-11 16:07:12 +0000 | [diff] [blame] | 690 | * @param[in] dst_stride_x Stride of the destination matrix in X dimension (in bytes) |
| 691 | * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes) |
| 692 | * @param[in] dst_stride_y Stride of the destination matrix in Y dimension (in bytes) |
| 693 | * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes) |
| 694 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination matrix |
| 695 | * @param[in] lhs_stride_z Stride of the LHS reshaped matrix in Z dimension (in bytes) |
| 696 | * @param[in] rhs_stride_z Stride of the RHS reshaped matrix in Z dimension (in bytes) |
| 697 | * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes) |
| 698 | * @param[in] lhs_cross_plane_pad (Optional) Bottom paddings for LHS matrix in unit of elements (only if defined REINTERPRET_INPUT_AS_3D) |
| 699 | * @param[in] dst_cross_plane_pad (Optional) Bottom paddings for the output matrix in unit of elements (only if defined REINTERPRET_OUTPUT_AS_3D) |
| 700 | */ |
| 701 | __kernel void gemmlowp_mm_reshaped_only_rhs_t(IMAGE_DECLARATION(lhs), |
| 702 | IMAGE_DECLARATION(rhs), |
| 703 | IMAGE_DECLARATION(dst), |
| 704 | uint lhs_stride_z, |
| 705 | uint rhs_stride_z, |
| 706 | uint dst_stride_z |
| 707 | #if defined(REINTERPRET_INPUT_AS_3D) |
| 708 | , |
| 709 | uint lhs_cross_plane_pad |
| 710 | #endif // REINTERPRET_INPUT_AS_3D |
| 711 | #if defined(REINTERPRET_OUTPUT_AS_3D) |
| 712 | , |
| 713 | uint dst_cross_plane_pad |
| 714 | #endif // REINTERPRET_OUTPUT_AS_3D |
| 715 | ) |
| 716 | { |
| 717 | // Block size |
| 718 | #define RHS_BLOCK_SIZE ((K0) * (N0)) |
| 719 | |
| 720 | // RHS offset and step X |
| 721 | #if defined(RHS_INTERLEAVE) |
| 722 | #define RHS_OFFSET_X (K0) |
| 723 | #define RHS_STEP_X ((K0) * (H0)) |
| 724 | #define RHS_STEP_LOOP (1) |
| 725 | #else // defined(RHS_INTERLEAVE) |
| 726 | #define RHS_OFFSET_X (RHS_BLOCK_SIZE) |
| 727 | #define RHS_STEP_X (K0) |
| 728 | #define RHS_STEP_LOOP (H0) |
| 729 | #endif // defined(RHS_INTERLEAVE) |
| 730 | |
| 731 | uint x = get_global_id(0); |
| 732 | uint y = get_global_id(1); |
| 733 | uint z = get_global_id(2); |
| 734 | |
Gian Marco Iodice | 86cfffe | 2019-04-02 11:02:20 +0100 | [diff] [blame] | 735 | #if defined(DUMMY_WORK_ITEMS) |
| 736 | if((x * N0 >= N) || (y * M0 >= M)) |
| 737 | { |
| 738 | return; |
| 739 | } |
| 740 | #endif // defined(DUMMY_WORK_ITEMS) |
| 741 | |
Gian Marco Iodice | 62251f7 | 2019-03-11 16:07:12 +0000 | [diff] [blame] | 742 | // Compute LHS matrix address |
| 743 | uint lhs_offset = lhs_offset_first_element_in_bytes + y * M0 * (uint)lhs_stride_y; |
| 744 | |
| 745 | // Compute RHS matrix address |
| 746 | uint rhs_offset = rhs_offset_first_element_in_bytes + (x % H0) * (uint)RHS_OFFSET_X + (x / (uint)H0) * rhs_stride_y; |
| 747 | |
| 748 | #if defined(MATRIX_B_DEPTH) |
| 749 | // Do not slide matrix B if the matrix B has 3 dimensions and matrix A more than 3 |
| 750 | rhs_offset += (z % MATRIX_B_DEPTH) * rhs_stride_z; |
| 751 | #else // defined(MATRIX_B_DEPTH) |
| 752 | rhs_offset += z * rhs_stride_z; |
| 753 | #endif // defined(MATRIX_B_DEPTH) |
| 754 | |
Gian Marco Iodice | 43a129e | 2019-05-14 10:14:08 +0100 | [diff] [blame] | 755 | REPEAT_VAR_INIT_TO_CONST(8, uint, zlhs, 0); //uint zout0=0,zout1=0,zout2=0,... zout7=0; |
| 756 | REPEAT_VAR_INIT_TO_CONST(16, uint, zrhs, 0); |
Gian Marco Iodice | 62251f7 | 2019-03-11 16:07:12 +0000 | [diff] [blame] | 757 | |
| 758 | #if defined(REINTERPRET_INPUT_AS_3D) |
Gian Marco Iodice | 43a129e | 2019-05-14 10:14:08 +0100 | [diff] [blame] | 759 | // The plane (zlhs) is calculated dividing M (y * M0) by HEIGHT_GEMM3D |
| 760 | CALCULATE_Z_OFFSET(M0, uint, zlhs, y, HEIGHT_GEMM3D, DEPTH_GEMM3D, lhs_cross_plane_pad, lhs_stride_y); |
Gian Marco Iodice | 62251f7 | 2019-03-11 16:07:12 +0000 | [diff] [blame] | 761 | |
| 762 | // Add offset for batched GEMM. The batches will be in the fourth dimension and for this reason we |
| 763 | // multiply lhs_stride_z by DEPTH_GEMM3D |
| 764 | lhs_offset += z * lhs_stride_z * DEPTH_GEMM3D; |
| 765 | |
| 766 | #else // defined(REINTERPRET_INPUT_AS_3D) |
| 767 | |
| 768 | // Add offset for batched GEMM |
| 769 | lhs_offset += z * lhs_stride_z; |
| 770 | |
| 771 | #endif // defined(REINTERPRET_INPUT_AS_3D) |
| 772 | |
| 773 | // Initialize the accumulators |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 774 | REPEAT_VAR_INIT_TO_CONST(M0, VEC_DATA_TYPE(ACC_DATA_TYPE, N0), c, 0); //VEC_DATA_TYPE(ACC_DATA_TYPE, N0) c0=0,c1=0,c2=0,... c(N0-1)=0; |
Gian Marco Iodice | 62251f7 | 2019-03-11 16:07:12 +0000 | [diff] [blame] | 775 | |
| 776 | for(int i = 0; i < K; i += K0) |
| 777 | { |
Gian Marco Iodice | 62251f7 | 2019-03-11 16:07:12 +0000 | [diff] [blame] | 778 | // Load values from LHS matrix |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 779 | LOAD_BLOCK(M0, K0, DATA_TYPE, a, lhs_ptr, lhs_offset, lhs_stride_y, zlhs); |
Gian Marco Iodice | 62251f7 | 2019-03-11 16:07:12 +0000 | [diff] [blame] | 780 | |
| 781 | // Load values from RHS matrix |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 782 | LOAD_BLOCK(N0, K0, DATA_TYPE, b, rhs_ptr, rhs_offset, RHS_STEP_X, zrhs); |
Gian Marco Iodice | 62251f7 | 2019-03-11 16:07:12 +0000 | [diff] [blame] | 783 | |
Gian Marco Iodice | 43a129e | 2019-05-14 10:14:08 +0100 | [diff] [blame] | 784 | // Partial matrix multiplication M0,N0,K0 |
| 785 | ARM_MM_K0XN0XM0(M0, N0, K0, a, b, c); |
Gian Marco Iodice | 62251f7 | 2019-03-11 16:07:12 +0000 | [diff] [blame] | 786 | |
| 787 | lhs_offset += K0; |
| 788 | rhs_offset += N0 * RHS_STEP_X * RHS_STEP_LOOP; |
| 789 | } |
| 790 | |
| 791 | __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + (x * (uint)N0) * sizeof(int) + (y * (uint)M0 * dst_stride_y); |
| 792 | |
| 793 | REPEAT_VAR_INIT_TO_CONST(8, uint, zout, 0); //uint zout0=0,zout1=0,zout2=0,... zout7=0; |
| 794 | |
| 795 | #if defined(REINTERPRET_OUTPUT_AS_3D) |
Gian Marco Iodice | 62251f7 | 2019-03-11 16:07:12 +0000 | [diff] [blame] | 796 | // The plane (zout) is calculated dividing M (y * M0) by HEIGHT_GEMM3D |
Gian Marco Iodice | 43a129e | 2019-05-14 10:14:08 +0100 | [diff] [blame] | 797 | CALCULATE_Z_OFFSET(M0, uint, zout, y, HEIGHT_GEMM3D, DEPTH_GEMM3D, dst_cross_plane_pad, dst_stride_y); |
Gian Marco Iodice | 62251f7 | 2019-03-11 16:07:12 +0000 | [diff] [blame] | 798 | |
| 799 | // Add offset for batched GEMM. The batches will be in the fourth dimension and for this reason we |
| 800 | // multiply dst_stride_z by DEPTH_GEMM3D |
| 801 | dst_addr += z * dst_stride_z * DEPTH_GEMM3D; |
| 802 | |
| 803 | #else // defined(REINTERPRET_OUTPUT_AS_3D) |
| 804 | |
| 805 | // Add offset for batched GEMM |
| 806 | dst_addr += z * dst_stride_z; |
| 807 | |
| 808 | #endif // defined(REINTERPRET_OUTPUT_AS_3D) |
| 809 | |
Gian Marco Iodice | 43a129e | 2019-05-14 10:14:08 +0100 | [diff] [blame] | 810 | // Convert and store output block |
| 811 | CONVERT_STORE_BLOCK(M0, N0, int, c, dst_addr, dst_stride_y, zout); |
Gian Marco Iodice | 62251f7 | 2019-03-11 16:07:12 +0000 | [diff] [blame] | 812 | |
| 813 | #undef RHS_BLOCK_SIZE |
| 814 | #undef RHS_OFFSET_X |
| 815 | #undef RHS_STEP_X |
| 816 | } |
| 817 | #endif // defined(M0) && defined(N0) && defined(K0) && defined(H0) && defined(DATA_TYPE) && defined(K) |
| 818 | |
Gian Marco Iodice | e751062 | 2019-06-03 17:28:17 +0100 | [diff] [blame] | 819 | #if defined(M0) && defined(N0) && defined(K0) && defined(K) |
| 820 | |
| 821 | /** This OpenCL kernel computes the matrix multiplication between 2 matrices. |
| 822 | * The LHS matrix is NOT reshaped |
| 823 | * The RHS matrix is NOT reshaped |
| 824 | * |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 825 | * @note The input data type must be passed at compile time using -DDATA_TYPE (i.e. -DDATA_TYPE=uchar) |
| 826 | * @note The accumulator data type must be passed at compile time using -DACC_DATA_TYPE (i.e. -DACC_DATA_TYPE=uint) |
Gian Marco Iodice | e751062 | 2019-06-03 17:28:17 +0100 | [diff] [blame] | 827 | * @note The number of columns of LHS matrix must be passed at compile time using -DK (i.e. -DK=64) |
| 828 | * @note The number of M0 rows to process must be passed at compile time using -DM0 (i.e. -DM0=2) |
| 829 | * @note The number of N0 columns to process must be passed at compile time using -DN0 (i.e. -DN0=2) |
| 830 | * @note The number of K0 partial accumulations must be passed at compile time using -DK0 (i.e., -DK0=2) |
| 831 | * @note Only the following configurations of M0, N0 and K0 are currently supported: |
| 832 | * - M0 = 1, 2, 3, 4, 5, 6, 7, 8 |
| 833 | * - N0 = 2, 3, 4, 8, 16 |
| 834 | * - K0 = 2, 3, 4, 8, 16 |
| 835 | * |
| 836 | * @note In case the input or output have to be reinterpreted as a 3D tensor, the following information must be passed at compile time: |
| 837 | * -# REINTERPRET_INPUT_AS_3D: To reinterpret the input as 3D |
| 838 | * -# REINTERPRET_OUTPUT_AS_3D: To reinterpret the output as 3D |
| 839 | * -# HEIGHT_GEMM3D: The height of the output in case it has to be reinterpreted as a 3D tensor. |
| 840 | * -# DEPTH_GEMM3D: The depth of the output in case it has to be reinterpreted as a 3D tensor |
| 841 | * (HEIGHT_GEMM3D * DEPTH_GEMM3D) = columns LHS matrix |
| 842 | * |
| 843 | * @param[in] lhs_ptr Pointer to the LHS reshaped matrix. Supported data type: F16/F32 |
| 844 | * @param[in] lhs_stride_x Stride of the LHS reshaped matrix in X dimension (in bytes) |
| 845 | * @param[in] lhs_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 846 | * @param[in] lhs_stride_y Stride of the LHS reshaped matrix in Y dimension (in bytes) |
| 847 | * @param[in] lhs_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 848 | * @param[in] lhs_offset_first_element_in_bytes The offset of the first element in the LHS reshaped matrix |
| 849 | * @param[in] rhs_ptr Pointer to the RHS reshaped matrix. Supported data type: same as @p lhs_ptr |
| 850 | * @param[in] rhs_stride_x Stride of the RHS reshaped matrix in X dimension (in bytes) |
| 851 | * @param[in] rhs_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 852 | * @param[in] rhs_stride_y Stride of the RHS reshaped matrix in Y dimension (in bytes) |
| 853 | * @param[in] rhs_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 854 | * @param[in] rhs_offset_first_element_in_bytes The offset of the first element in the RHS reshaped matrix |
| 855 | * @param[out] dst_ptr Pointer to the destination matrix Supported data type: same as @p lhs_ptr |
| 856 | * @param[in] dst_stride_x Stride of the destination matrix in X dimension (in bytes) |
| 857 | * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes) |
| 858 | * @param[in] dst_stride_y Stride of the destination matrix in Y dimension (in bytes) |
| 859 | * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes) |
| 860 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination matrix |
| 861 | * @param[in] lhs_stride_z Stride of the LHS reshaped matrix in Z dimension (in bytes) |
| 862 | * @param[in] rhs_stride_z Stride of the RHS reshaped matrix in Z dimension (in bytes) |
| 863 | * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes) |
| 864 | * @param[in] lhs_cross_plane_pad (Optional) Bottom paddings for LHS matrix in unit of elements (only if defined REINTERPRET_INPUT_AS_3D) |
| 865 | * @param[in] dst_cross_plane_pad (Optional) Bottom paddings for the output matrix in unit of elements (only if defined REINTERPRET_OUTPUT_AS_3D) |
| 866 | */ |
| 867 | __kernel void gemmlowp_mm_native(IMAGE_DECLARATION(lhs), |
| 868 | IMAGE_DECLARATION(rhs), |
| 869 | IMAGE_DECLARATION(dst), |
| 870 | uint lhs_stride_z, |
| 871 | uint rhs_stride_z, |
| 872 | uint dst_stride_z |
| 873 | #if defined(REINTERPRET_INPUT_AS_3D) |
| 874 | , |
| 875 | uint lhs_cross_plane_pad |
| 876 | #endif // REINTERPRET_INPUT_AS_3D |
| 877 | #if defined(REINTERPRET_OUTPUT_AS_3D) |
| 878 | , |
| 879 | uint dst_cross_plane_pad |
| 880 | #endif // REINTERPRET_OUTPUT_AS_3D |
| 881 | ) |
| 882 | { |
| 883 | uint x = get_global_id(0); |
| 884 | uint y = get_global_id(1); |
| 885 | uint z = get_global_id(2); |
| 886 | |
| 887 | #if defined(DUMMY_WORK_ITEMS) |
| 888 | if((x * N0 >= N) || (y * M0 >= M)) |
| 889 | { |
| 890 | return; |
| 891 | } |
| 892 | #endif // defined(DUMMY_WORK_ITEMS) |
| 893 | |
| 894 | // Compute LHS matrix address |
| 895 | uint lhs_offset = lhs_offset_first_element_in_bytes + y * M0 * (uint)lhs_stride_y; |
| 896 | |
| 897 | // Compute RHS matrix address |
| 898 | uint rhs_offset = rhs_offset_first_element_in_bytes + x * N0; |
| 899 | |
| 900 | #if defined(MATRIX_B_DEPTH) |
| 901 | // Do not slide matrix B if the matrix B has 3 dimensions and matrix A more than 3 |
| 902 | rhs_offset += (z % MATRIX_B_DEPTH) * rhs_stride_z; |
| 903 | #else // defined(MATRIX_B_DEPTH) |
| 904 | rhs_offset += z * rhs_stride_z; |
| 905 | #endif // defined(MATRIX_B_DEPTH) |
| 906 | |
| 907 | REPEAT_VAR_INIT_TO_CONST(8, uint, zlhs, 0); |
| 908 | REPEAT_VAR_INIT_TO_CONST(16, uint, zrhs, 0); |
| 909 | |
| 910 | #if defined(REINTERPRET_INPUT_AS_3D) |
| 911 | // The plane (zlhs) is calculated dividing M (y * M0) by HEIGHT_GEMM3D |
| 912 | CALCULATE_Z_OFFSET(M0, uint, zlhs, y, HEIGHT_GEMM3D, DEPTH_GEMM3D, lhs_cross_plane_pad, lhs_stride_y); |
| 913 | |
| 914 | // Add offset for batched GEMM. The batches will be in the fourth dimension and for this reason we |
| 915 | // multiply lhs_stride_z by DEPTH_GEMM3D |
| 916 | lhs_offset += z * lhs_stride_z * DEPTH_GEMM3D; |
| 917 | |
| 918 | #else // defined(REINTERPRET_INPUT_AS_3D) |
| 919 | |
| 920 | // Add offset for batched GEMM |
| 921 | lhs_offset += z * lhs_stride_z; |
| 922 | |
| 923 | #endif // defined(REINTERPRET_INPUT_AS_3D) |
| 924 | |
| 925 | // Initialize the accumulators |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 926 | REPEAT_VAR_INIT_TO_CONST(M0, VEC_DATA_TYPE(ACC_DATA_TYPE, N0), c, 0); //VEC_DATA_TYPE(ACC_DATA_TYPE, N0) c0=0,c1=0,c2=0,... c(M0-1)=0; |
Gian Marco Iodice | e751062 | 2019-06-03 17:28:17 +0100 | [diff] [blame] | 927 | |
| 928 | int i = 0; |
| 929 | |
| 930 | for(; i <= (K - K0); i += K0) |
| 931 | { |
| 932 | // Load values from LHS matrix |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 933 | LOAD_BLOCK(M0, K0, DATA_TYPE, a, lhs_ptr, lhs_offset, lhs_stride_y, zlhs); |
Gian Marco Iodice | e751062 | 2019-06-03 17:28:17 +0100 | [diff] [blame] | 934 | |
| 935 | // Load values from RHS matrix |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 936 | LOAD_BLOCK(K0, N0, DATA_TYPE, b, rhs_ptr, rhs_offset, rhs_stride_y, zrhs); |
Gian Marco Iodice | e751062 | 2019-06-03 17:28:17 +0100 | [diff] [blame] | 937 | |
| 938 | // Transpose the values from RHS matrix |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 939 | TRANSPOSE_K0XN0(K0, N0, b_t, b, DATA_TYPE); |
Gian Marco Iodice | e751062 | 2019-06-03 17:28:17 +0100 | [diff] [blame] | 940 | |
| 941 | // Partial matrix multiplication M0,N0,K0 |
| 942 | ARM_MM_K0XN0XM0(M0, N0, K0, a, b_t, c); |
| 943 | |
| 944 | // Update the offset |
| 945 | lhs_offset += K0; |
| 946 | rhs_offset += K0 * rhs_stride_y; |
| 947 | } |
| 948 | |
| 949 | // Left-over for loop |
| 950 | for(; i < K; ++i) |
| 951 | { |
| 952 | // Load values from LHS matrix |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 953 | LOAD_BLOCK(M0, 1, DATA_TYPE, a, lhs_ptr, lhs_offset, lhs_stride_y, zlhs); |
Gian Marco Iodice | e751062 | 2019-06-03 17:28:17 +0100 | [diff] [blame] | 954 | |
| 955 | // Load values from RHS matrix |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 956 | LOAD_BLOCK(1, N0, DATA_TYPE, b, rhs_ptr, rhs_offset, rhs_stride_y, zrhs); |
Gian Marco Iodice | e751062 | 2019-06-03 17:28:17 +0100 | [diff] [blame] | 957 | |
| 958 | // Transpose the values from RHS matrix |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 959 | TRANSPOSE_K0XN0(1, N0, b_t, b, DATA_TYPE); |
Gian Marco Iodice | e751062 | 2019-06-03 17:28:17 +0100 | [diff] [blame] | 960 | |
| 961 | // Partial matrix multiplication M0,N0,1 |
| 962 | ARM_MM_K0XN0XM0(M0, N0, 1, a, b_t, c); |
| 963 | |
| 964 | // Update the offset |
| 965 | lhs_offset += 1; |
| 966 | rhs_offset += rhs_stride_y; |
| 967 | } |
| 968 | |
| 969 | __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + (x * (uint)N0) * sizeof(int) + (y * (uint)M0 * dst_stride_y); |
| 970 | |
| 971 | REPEAT_VAR_INIT_TO_CONST(M0, uint, zout, 0); //uint zout0=0,zout1=0,zout2=0,... zout7=0; |
| 972 | |
| 973 | #if defined(REINTERPRET_OUTPUT_AS_3D) |
| 974 | // The plane (zout) is calculated dividing M (y * M0) by HEIGHT_GEMM3D |
| 975 | CALCULATE_Z_OFFSET(M0, uint, zout, y, HEIGHT_GEMM3D, DEPTH_GEMM3D, dst_cross_plane_pad, dst_stride_y); |
| 976 | |
| 977 | // Add offset for batched GEMM. The batches will be in the fourth dimension and for this reason we |
| 978 | // multiply dst_stride_z by DEPTH_GEMM3D |
| 979 | dst_addr += z * dst_stride_z * DEPTH_GEMM3D; |
| 980 | |
| 981 | #else // defined(REINTERPRET_OUTPUT_AS_3D) |
| 982 | |
| 983 | // Add offset for batched GEMM |
| 984 | dst_addr += z * dst_stride_z; |
| 985 | |
| 986 | #endif // defined(REINTERPRET_OUTPUT_AS_3D) |
| 987 | |
| 988 | // Convert and store output block |
| 989 | CONVERT_STORE_BLOCK(M0, N0, int, c, dst_addr, dst_stride_y, zout); |
| 990 | } |
| 991 | #endif // defined(M0) && defined(N0) && defined(K0) && defined(K) |
| 992 | |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 993 | #endif // defined(DATA_TYPE) && defined(ACC_DATA_TYPE) |
| 994 | |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 995 | #if defined(COLS_A) |
| 996 | /** OpenCL kernel used to compute the row-vectors of sums of all the entries in each row of Matrix A. |
| 997 | * |
| 998 | * @note This stage is needed to handle the offset of matrix product |
| 999 | * https://github.com/google/gemmlowp/blob/master/doc/low-precision.md |
| 1000 | * |
| 1001 | * @attention The number of matrix A columns needs to be passed at compile time using -DCOLS_A |
| 1002 | * |
| 1003 | * @param[in] src_ptr Pointer to the source tensor. Supported data type: QASYMM8 |
| 1004 | * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes) |
| 1005 | * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 1006 | * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes) |
| 1007 | * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 1008 | * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 1009 | * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes) |
| 1010 | * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor |
| 1011 | * @param[out] dst_ptr Pointer to the destination tensor Supported data type: S32 |
| 1012 | * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 1013 | * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes) |
| 1014 | * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes) |
| 1015 | * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes) |
| 1016 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor |
| 1017 | */ |
| 1018 | __kernel void gemmlowp_matrix_a_reduction(TENSOR3D_DECLARATION(src), |
| 1019 | IMAGE_DECLARATION(dst)) |
| 1020 | { |
| 1021 | // Compute source and destination addresses |
| 1022 | Tensor3D src = CONVERT_TO_TENSOR3D_STRUCT(src); |
| 1023 | Image dst = CONVERT_TO_IMAGE_STRUCT(dst); |
| 1024 | |
| 1025 | uint4 sum_row_u32 = (uint4)0; |
| 1026 | uint sum_row = 0; |
| 1027 | |
| 1028 | __global const uchar *matrix_a = (__global const uchar *)(src.ptr + get_global_id(0) * src_stride_y + get_global_id(1) * src_stride_z); |
| 1029 | |
| 1030 | int i = 0; |
| 1031 | |
| 1032 | // This for loop performs 16 accumulations |
| 1033 | for(; i <= ((int)COLS_A - 16); i += 16) |
| 1034 | { |
| 1035 | const uchar16 a0_u8 = vload16(0, matrix_a + i); |
| 1036 | |
| 1037 | sum_row_u32 += convert_uint4(a0_u8.s0123) + convert_uint4(a0_u8.s4567) + convert_uint4(a0_u8.s89AB) + convert_uint4(a0_u8.sCDEF); |
| 1038 | } |
| 1039 | |
| 1040 | // This for loop performs the leftover accumulations |
| 1041 | for(; i < COLS_A; ++i) |
| 1042 | { |
| 1043 | sum_row += matrix_a[i]; |
| 1044 | } |
| 1045 | |
| 1046 | sum_row += sum_row_u32.s0 + sum_row_u32.s1 + sum_row_u32.s2 + sum_row_u32.s3; |
| 1047 | |
| 1048 | *((__global int *)dst.ptr) = (int)sum_row; |
| 1049 | } |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1050 | |
| 1051 | #if defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8) |
| 1052 | /** OpenCL kernel used to compute the row-vectors of sums of all the entries in each row of Matrix A using the arm dot product instruction |
| 1053 | * |
| 1054 | * @note This stage is needed to handle the offset of matrix product |
| 1055 | * https://github.com/google/gemmlowp/blob/master/doc/low-precision.md |
| 1056 | * |
| 1057 | * @attention The number of matrix A columns needs to be passed at compile time using -DCOLS_A |
| 1058 | * |
| 1059 | * @param[in] src_ptr Pointer to the source tensor. Supported data type: QASYMM8 |
| 1060 | * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes) |
| 1061 | * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 1062 | * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes) |
| 1063 | * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 1064 | * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 1065 | * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes) |
| 1066 | * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor |
| 1067 | * @param[out] dst_ptr Pointer to the destination tensor Supported data type: S32 |
| 1068 | * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 1069 | * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes) |
| 1070 | * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes) |
| 1071 | * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes) |
| 1072 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor |
| 1073 | */ |
| 1074 | __kernel void gemmlowp_matrix_a_reduction_dot8(TENSOR3D_DECLARATION(src), |
| 1075 | IMAGE_DECLARATION(dst)) |
| 1076 | { |
| 1077 | // Compute source and destination addresses |
| 1078 | Tensor3D src = CONVERT_TO_TENSOR3D_STRUCT(src); |
| 1079 | Image dst = CONVERT_TO_IMAGE_STRUCT(dst); |
| 1080 | |
| 1081 | uint sum_row = 0; |
| 1082 | |
| 1083 | __global const uchar *matrix_a = (__global const uchar *)(src.ptr + get_global_id(0) * src_stride_y + get_global_id(1) * src_stride_z); |
| 1084 | |
| 1085 | int i = 0; |
| 1086 | |
| 1087 | // This for loop performs 16 accumulations |
| 1088 | for(; i <= ((int)COLS_A - 32); i += 32) |
| 1089 | { |
| 1090 | uchar16 a0_u8 = vload16(0, matrix_a + i); |
| 1091 | |
| 1092 | sum_row += arm_dot(a0_u8.s0123, (uchar4)(1)); |
| 1093 | sum_row += arm_dot(a0_u8.s4567, (uchar4)(1)); |
| 1094 | sum_row += arm_dot(a0_u8.s89AB, (uchar4)(1)); |
| 1095 | sum_row += arm_dot(a0_u8.sCDEF, (uchar4)(1)); |
| 1096 | |
| 1097 | a0_u8 = vload16(1, matrix_a + i); |
| 1098 | |
| 1099 | sum_row += arm_dot(a0_u8.s0123, (uchar4)(1)); |
| 1100 | sum_row += arm_dot(a0_u8.s4567, (uchar4)(1)); |
| 1101 | sum_row += arm_dot(a0_u8.s89AB, (uchar4)(1)); |
| 1102 | sum_row += arm_dot(a0_u8.sCDEF, (uchar4)(1)); |
| 1103 | } |
| 1104 | |
| 1105 | // This for loop performs the leftover accumulations |
| 1106 | for(; i < COLS_A; ++i) |
| 1107 | { |
| 1108 | sum_row += matrix_a[i]; |
| 1109 | } |
| 1110 | |
| 1111 | *((__global int *)dst.ptr) = (int)sum_row; |
| 1112 | } |
| 1113 | #endif // defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8) |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 1114 | #endif // defined(COLS_A) |
| 1115 | |
| 1116 | #if defined(COLS_B) && defined(ROWS_B) |
| 1117 | /** OpenCL kernel used to compute the row-vectors of sums of all the entries in each column of Matrix B. |
| 1118 | * |
| 1119 | * @note This stage is needed to handle the offset of matrix product |
| 1120 | * https://github.com/google/gemmlowp/blob/master/doc/low-precision.md |
| 1121 | * |
| 1122 | * @attention The number of matrix B columns and rows needs to be passed at compile time using -DCOLS_B and -DROWS_B |
| 1123 | * |
| 1124 | * @param[in] src_ptr Pointer to the source tensor. Supported data type: QASYMM8 |
| 1125 | * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes) |
| 1126 | * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 1127 | * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes) |
| 1128 | * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 1129 | * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 1130 | * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes) |
| 1131 | * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor |
| 1132 | * @param[out] dst_ptr Pointer to the destination tensor Supported data type: S32 |
| 1133 | * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 1134 | * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes) |
| 1135 | * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes) |
| 1136 | * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes) |
| 1137 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor |
| 1138 | */ |
| 1139 | __kernel void gemmlowp_matrix_b_reduction(TENSOR3D_DECLARATION(src), |
| 1140 | IMAGE_DECLARATION(dst)) |
| 1141 | { |
| 1142 | // Compute source and destination addresses |
| 1143 | Tensor3D src = CONVERT_TO_TENSOR3D_STRUCT(src); |
| 1144 | Image dst = CONVERT_TO_IMAGE_STRUCT(dst); |
| 1145 | |
| 1146 | uint16 sum_col_u32 = (uint16)0; |
| 1147 | |
| 1148 | __global const uchar *matrix_b = (__global const uchar *)(src.ptr + get_global_id(1) * src_stride_z); |
| 1149 | |
| 1150 | int i = 0; |
| 1151 | // This for loop performs 4 accumulations |
| 1152 | for(; i <= ((int)ROWS_B - 4); i += 4) |
| 1153 | { |
| 1154 | const uchar16 b0_u8 = vload16(0, matrix_b + 0 * src_stride_y); |
| 1155 | const uchar16 b1_u8 = vload16(0, matrix_b + 1 * src_stride_y); |
| 1156 | const uchar16 b2_u8 = vload16(0, matrix_b + 2 * src_stride_y); |
| 1157 | const uchar16 b3_u8 = vload16(0, matrix_b + 3 * src_stride_y); |
| 1158 | |
| 1159 | sum_col_u32 += convert_uint16(b0_u8) + convert_uint16(b1_u8) + convert_uint16(b2_u8) + convert_uint16(b3_u8); |
| 1160 | |
| 1161 | matrix_b += 4 * src_stride_y; |
| 1162 | } |
| 1163 | |
| 1164 | // This for loop perfoms the leftover accumulations |
| 1165 | for(; i < (int)ROWS_B; ++i) |
| 1166 | { |
| 1167 | const uchar16 b0_u8 = vload16(0, matrix_b); |
| 1168 | |
| 1169 | sum_col_u32 += convert_uint16(b0_u8); |
| 1170 | |
| 1171 | matrix_b += src_stride_y; |
| 1172 | } |
| 1173 | |
| 1174 | vstore16(convert_int16(sum_col_u32), 0, (__global int *)dst.ptr); |
| 1175 | } |
| 1176 | #endif // defined(COLS_B) && defined(ROWS_B) |
| 1177 | |
| 1178 | #if defined(K_OFFSET) |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1179 | |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 1180 | /* Helper function used to calculate the offset contribution after matrix multiplication. |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1181 | * |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 1182 | * This kernel takes a final int32 accumulator value (the output of matrix multiplication), |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1183 | * and calculates the offset contribution of matrix A and matrix B. |
| 1184 | * |
| 1185 | * @attention The k_offset = a_offset * b_offset * k (where k is the number of matrix A columns) needs to be passed at compile time using -DK_OFFSET (i.e. -DK_OFFSET=1200) |
| 1186 | * @note In case the offset contribution due to a_offset is required, a_offset needs to be passed at compile time using -DA_OFFSET (i.e. -DA_OFFSET=1) |
| 1187 | * @note In case the offset contribution due to b_offset is required, b_offset needs to be passed at compile time using -DB_OFFSET (i.e. -DB_OFFSET=6) |
| 1188 | * @note In case sum_col has batches, -DSUM_COL_HAS_BATCHES must be passed at compile time. Usually if gemmlowp is used to accelerate convolution layer, sum_col will not have batches |
| 1189 | * |
| 1190 | * @param[in] x get_global_id(0) * 4 |
| 1191 | * @param[in] y get_global_id(1) |
| 1192 | * @param[in] z get_global_id(2) |
| 1193 | * @param[in] sum_col_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr |
| 1194 | * @param[in] sum_col_stride_x (Optional) Stride of the source tensor in X dimension (in bytes) |
| 1195 | * @param[in] sum_col_step_x (Optional) sum_col_stride_x * number of elements along X processed per workitem(in bytes) |
| 1196 | * @param[in] sum_col_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes) |
| 1197 | * @param[in] sum_col_step_y (Optional) sum_col_stride_y * number of elements along Y processed per workitem(in bytes) |
| 1198 | * @param[in] sum_col_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor |
| 1199 | * @param[in] sum_row_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr |
| 1200 | * @param[in] sum_row_stride_x (Optional) Stride of the source tensor in X dimension (in bytes) |
| 1201 | * @param[in] sum_row_step_x (Optional) sum_row_stride_x * number of elements along X processed per workitem(in bytes) |
| 1202 | * @param[in] sum_row_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes) |
| 1203 | * @param[in] sum_row_step_y (Optional) sum_row_stride_y * number of elements along Y processed per workitem(in bytes) |
| 1204 | * @param[in] sum_row_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor |
| 1205 | * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: same as @p src_ptr |
| 1206 | * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes) |
| 1207 | * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes) |
| 1208 | * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases tensor |
| 1209 | */ |
| 1210 | inline int4 offset_contribution( |
| 1211 | int x, |
| 1212 | int y, |
| 1213 | int z |
| 1214 | #if defined(A_OFFSET) |
| 1215 | , |
| 1216 | IMAGE_DECLARATION(sum_col) |
| 1217 | #endif // defined(A_OFFSET) |
| 1218 | #if defined(B_OFFSET) |
| 1219 | , |
| 1220 | IMAGE_DECLARATION(sum_row) |
| 1221 | #endif // defined(B_OFFSET) |
| 1222 | #if defined(ADD_BIAS) |
| 1223 | , |
| 1224 | VECTOR_DECLARATION(biases) |
| 1225 | #endif // defined(ADD_BIAS) |
| 1226 | ) |
| 1227 | { |
| 1228 | int4 a_offset_s32 = (int4)0; |
| 1229 | int4 b_offset_s32 = (int4)0; |
| 1230 | |
| 1231 | int batch_id = z; |
| 1232 | #if defined(DEPTH_INPUT3D) |
| 1233 | batch_id /= (int)DEPTH_INPUT3D; |
| 1234 | #endif // defined(DEPTH_INPUT3D) |
| 1235 | |
| 1236 | #if defined(A_OFFSET) |
| 1237 | // Compute the offset contribution due to A_OFFSET |
| 1238 | __global uchar *sum_col_addr = sum_col_ptr + sum_col_offset_first_element_in_bytes + x * sizeof(int); |
| 1239 | |
| 1240 | // Compute the offset contribution due to A_OFFSET |
| 1241 | #if defined(SUM_COL_HAS_BATCHES) |
| 1242 | a_offset_s32 = vload4(0, (__global int *)(sum_col_addr + batch_id * sum_col_stride_y)); |
| 1243 | #else // defined(SUM_COL_HAS_BATCHES) |
| 1244 | a_offset_s32 = vload4(0, (__global int *)sum_col_addr); |
| 1245 | #endif // defined(SUM_COL_HAS_BATCHES) |
| 1246 | |
| 1247 | a_offset_s32 *= (int4)A_OFFSET; |
| 1248 | #endif // defined(A_OFFSET) |
| 1249 | |
| 1250 | #if defined(B_OFFSET) |
| 1251 | // Compute the offset contribution due to A_OFFSET |
| 1252 | __global uchar *sum_row_addr = sum_row_ptr + sum_row_offset_first_element_in_bytes + y * sizeof(int); |
| 1253 | |
| 1254 | // Compute the offset contribution due to B_OFFSET |
| 1255 | #if defined(HEIGHT_INPUT3D) && defined(DEPTH_INPUT3D) |
| 1256 | b_offset_s32 = (int4) * (((__global int *)(sum_row_addr + batch_id * sum_row_stride_y)) + (z % (int)DEPTH_INPUT3D) * (int)HEIGHT_INPUT3D); |
| 1257 | #else // defined(HEIGHT_INPUT3D) && defined(DEPTH_INPUT3D) |
| 1258 | b_offset_s32 = (int4) * (((__global int *)(sum_row_addr + batch_id * sum_row_stride_y))); |
| 1259 | #endif // defined(HEIGHT_INPUT3D) && defined(DEPTH_INPUT3D) |
| 1260 | b_offset_s32 *= (int4)B_OFFSET; |
| 1261 | #endif // defined(B_OFFSET) |
| 1262 | |
| 1263 | #if defined(ADD_BIAS) |
| 1264 | // Add bias |
| 1265 | __global uchar *bias_addr = biases_ptr + biases_offset_first_element_in_bytes + x * sizeof(int); |
| 1266 | |
| 1267 | int4 biases_values = vload4(0, (__global int *)bias_addr); |
| 1268 | b_offset_s32 += (int4)biases_values; |
| 1269 | #endif // defined(ADD_BIAS) |
| 1270 | |
| 1271 | return (int4)K_OFFSET + a_offset_s32 + b_offset_s32; |
| 1272 | } |
| 1273 | |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 1274 | /* OpenCL kernel used to add the offset contribution after matrix multiplication. The computation is performed in-place |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 1275 | * |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 1276 | * This kernel takes a final int32 accumulator value (the output of matrix multiplication), |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 1277 | * and adds to it the offset contribution of matrix A and matrix B in-place. |
| 1278 | * |
| 1279 | * @attention The k_offset = a_offset * b_offset * k (where k is the number of matrix A columns) needs to be passed at compile time using -DK_OFFSET (i.e. -DK_OFFSET=1200) |
| 1280 | * @note In case the offset contribution due to a_offset is required, a_offset needs to be passed at compile time using -DA_OFFSET (i.e. -DA_OFFSET=1) |
| 1281 | * @note In case the offset contribution due to b_offset is required, b_offset needs to be passed at compile time using -DB_OFFSET (i.e. -DB_OFFSET=6) |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 1282 | * @note In case sum_col has batches, -DSUM_COL_HAS_BATCHES must be passed at compile time. Usually if gemmlowp is used to accelerate convolution layer, sum_col will not have batches |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 1283 | * |
| 1284 | * The final result is: |
| 1285 | * |
| 1286 | * mm_result[i][k] = mm_result[i][k] + |
| 1287 | * (sum_col[k] * A_OFFSET) + |
| 1288 | * (sum_row[i] * B_OFFSET) + |
| 1289 | * (K_OFFSET) |
| 1290 | * |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 1291 | * @param[in] mm_result_ptr Pointer to the source tensor. Supported data type: S32 |
| 1292 | * @param[in] mm_result_stride_x Stride of the source tensor in X dimension (in bytes) |
| 1293 | * @param[in] mm_result_step_x mm_result_stride_x * number of elements along X processed per workitem(in bytes) |
| 1294 | * @param[in] mm_result_stride_y Stride of the source tensor in Y dimension (in bytes) |
| 1295 | * @param[in] mm_result_step_y mm_result_stride_y * number of elements along Y processed per workitem(in bytes) |
| 1296 | * @param[in] mm_result_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 1297 | * @param[in] mm_result_step_z mm_result_stride_z * number of elements along Z processed per workitem(in bytes) |
| 1298 | * @param[in] mm_result_offset_first_element_in_bytes The offset of the first element in the source tensor |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1299 | * @param[in] sum_col_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr |
| 1300 | * @param[in] sum_col_stride_x (Optional) Stride of the source tensor in X dimension (in bytes) |
| 1301 | * @param[in] sum_col_step_x (Optional) sum_col_stride_x * number of elements along X processed per workitem(in bytes) |
| 1302 | * @param[in] sum_col_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes) |
| 1303 | * @param[in] sum_col_step_y (Optional) sum_col_stride_y * number of elements along Y processed per workitem(in bytes) |
| 1304 | * @param[in] sum_col_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor |
| 1305 | * @param[in] sum_row_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr |
| 1306 | * @param[in] sum_row_stride_x (Optional) Stride of the source tensor in X dimension (in bytes) |
| 1307 | * @param[in] sum_row_step_x (Optional) sum_row_stride_x * number of elements along X processed per workitem(in bytes) |
| 1308 | * @param[in] sum_row_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes) |
| 1309 | * @param[in] sum_row_step_y (Optional) sum_row_stride_y * number of elements along Y processed per workitem(in bytes) |
| 1310 | * @param[in] sum_row_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor |
| 1311 | * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: same as @p src_ptr |
| 1312 | * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes) |
| 1313 | * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes) |
| 1314 | * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases tensor |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 1315 | */ |
| 1316 | __kernel void gemmlowp_offset_contribution(TENSOR3D_DECLARATION(mm_result) |
| 1317 | #if defined(A_OFFSET) |
| 1318 | , |
| 1319 | IMAGE_DECLARATION(sum_col) |
| 1320 | #endif // defined(A_OFFSET) |
| 1321 | #if defined(B_OFFSET) |
| 1322 | , |
| 1323 | IMAGE_DECLARATION(sum_row) |
| 1324 | #endif // defined(B_OFFSET) |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1325 | #if defined(ADD_BIAS) |
| 1326 | , |
| 1327 | VECTOR_DECLARATION(biases) |
| 1328 | #endif // defined(ADD_BIAS)) |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 1329 | ) |
| 1330 | { |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1331 | const int x = get_global_id(0) * 4; |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 1332 | const int y = get_global_id(1); |
| 1333 | const int z = get_global_id(2); |
| 1334 | |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1335 | // Compute offset contribution |
| 1336 | int4 offset_term_s32 = offset_contribution( |
| 1337 | x, y, z |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 1338 | #if defined(A_OFFSET) |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1339 | , |
| 1340 | sum_col_ptr, |
| 1341 | sum_col_stride_x, |
| 1342 | sum_col_step_x, |
| 1343 | sum_col_stride_y, |
| 1344 | sum_col_step_y, |
| 1345 | sum_col_offset_first_element_in_bytes |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 1346 | #endif // defined(A_OFFSET) |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 1347 | #if defined(B_OFFSET) |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1348 | , |
| 1349 | sum_row_ptr, |
| 1350 | sum_row_stride_x, |
| 1351 | sum_row_step_x, |
| 1352 | sum_row_stride_y, |
| 1353 | sum_row_step_y, |
| 1354 | sum_row_offset_first_element_in_bytes |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 1355 | #endif // defined(B_OFFSET) |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1356 | #if defined(ADD_BIAS) |
| 1357 | , |
| 1358 | biases_ptr, |
| 1359 | biases_stride_x, |
| 1360 | biases_step_x, |
| 1361 | biases_offset_first_element_in_bytes |
| 1362 | #endif // defined(ADD_BIAS) |
| 1363 | ); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 1364 | |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1365 | __global uchar *mm_result_addr = mm_result_ptr + mm_result_offset_first_element_in_bytes + x * sizeof(int) + y * mm_result_stride_y + z * mm_result_stride_z; |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 1366 | |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1367 | int4 in_s32 = vload4(0, (__global int *)mm_result_addr); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 1368 | |
| 1369 | // Add the offset terms to GEMM's result |
| 1370 | in_s32 += offset_term_s32; |
| 1371 | |
| 1372 | // Store the result with the offset contribution |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1373 | vstore4(in_s32, 0, (__global int *)mm_result_addr); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 1374 | } |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1375 | |
| 1376 | #if defined(RESULT_OFFSET) && defined(RESULT_MULTIPLIER) && defined(RESULT_SHIFT) |
| 1377 | /* OpenCL kernel used to add the offset contribution after @ref CLGEMMLowpMatrixMultiplyKernel and it quantizes down to uint8. |
| 1378 | * |
| 1379 | * This kernel takes a final int32 accumulator value (the output of @CLGEMMLowpMatrixMultiplyKernel), adds to it the offset contribution of matrix A and matrix B and quantizes to uint8 through the output stage. |
| 1380 | * |
| 1381 | * |
| 1382 | * @attention The k_offset = a_offset * b_offset * k (where k is the number of matrix A columns) needs to be passed at compile time using -DK_OFFSET (i.e. -DK_OFFSET=1200) |
| 1383 | * @note In case the offset contribution due to a_offset is required, a_offset needs to be passed at compile time using -DA_OFFSET (i.e. -DA_OFFSET=1) |
| 1384 | * @note In case the offset contribution due to b_offset is required, b_offset needs to be passed at compile time using -DB_OFFSET (i.e. -DB_OFFSET=6) |
| 1385 | * @note In case sum_col has batches, -DSUM_COL_HAS_BATCHES must be passed at compile time. Usually if gemmlowp is used to accelerate convolution layer, sum_col will not have batches |
| 1386 | * |
| 1387 | * The result before the output stage is: |
| 1388 | * |
| 1389 | * mm_result[i][k] = mm_result[i][k] + |
| 1390 | * (sum_col[k] * A_OFFSET) + |
| 1391 | * (sum_row[i] * B_OFFSET) + |
| 1392 | * (K_OFFSET) |
| 1393 | * |
| 1394 | * This result is quantized down to uint8 using the output stage. The output stage computes the following operations: |
| 1395 | * |
| 1396 | * -# Add offset terms to final result |
| 1397 | * -# Multiply each entry of result by result_mult_int |
| 1398 | * -# Add bias to final result (if -DADD_BIAS is passed at compile time) |
| 1399 | * -# Shift the int32 accumulator by result_shift |
| 1400 | * -# Clamp the value between the specified min and max bounds (if -DMIN_BOUND and/or -DMAX_BOUND are passed at compile time) |
| 1401 | * -# Clamp the resulting int32 values to the [0..255] range and cast to QASYMM8. |
| 1402 | * |
| 1403 | * @attention The offset, scalar scale factor and number of bits to shift right of output tensor must be passed at compile time using -DRESULT_OFFSET, -RESULT_MULT_INT and -DRESULT_SHIFT |
| 1404 | * |
| 1405 | * @note In case the addition of int32 biases is required, -DADD_BIAS should be passed at compile time |
| 1406 | * @note In case the clamping of the result is required, the min and max bounds can be passed at compile time using -DMIN_BOUND and -DMAX_BOUND. |
| 1407 | * These values can be used to implement "rectified linear unit" activation functions |
| 1408 | * |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 1409 | * @param[in] mm_result_ptr Pointer to the source tensor. Supported data type: S32 |
| 1410 | * @param[in] mm_result_stride_x Stride of the source tensor in X dimension (in bytes) |
| 1411 | * @param[in] mm_result_step_x mm_result_stride_x * number of elements along X processed per workitem(in bytes) |
| 1412 | * @param[in] mm_result_stride_y Stride of the source tensor in Y dimension (in bytes) |
| 1413 | * @param[in] mm_result_step_y mm_result_stride_y * number of elements along Y processed per workitem(in bytes) |
| 1414 | * @param[in] mm_result_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 1415 | * @param[in] mm_result_step_z mm_result_stride_z * number of elements along Z processed per workitem(in bytes) |
| 1416 | * @param[in] mm_result_offset_first_element_in_bytes The offset of the first element in the source tensor |
| 1417 | * @param[in] sum_col_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr |
| 1418 | * @param[in] sum_col_stride_x (Optional) Stride of the source tensor in X dimension (in bytes) |
| 1419 | * @param[in] sum_col_step_x (Optional) sum_col_stride_x * number of elements along X processed per workitem(in bytes) |
| 1420 | * @param[in] sum_col_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes) |
| 1421 | * @param[in] sum_col_step_y (Optional) sum_col_stride_y * number of elements along Y processed per workitem(in bytes) |
| 1422 | * @param[in] sum_col_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor |
| 1423 | * @param[in] sum_row_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr |
| 1424 | * @param[in] sum_row_stride_x (Optional) Stride of the source tensor in X dimension (in bytes) |
| 1425 | * @param[in] sum_row_step_x (Optional) sum_row_stride_x * number of elements along X processed per workitem(in bytes) |
| 1426 | * @param[in] sum_row_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes) |
| 1427 | * @param[in] sum_row_step_y (Optional) sum_row_stride_y * number of elements along Y processed per workitem(in bytes) |
| 1428 | * @param[in] sum_row_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor |
| 1429 | * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: same as @p src_ptr |
| 1430 | * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes) |
| 1431 | * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes) |
| 1432 | * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases tensor |
| 1433 | * @param[out] dst_ptr Pointer to the destination tensor Supported data type: QASYMM8 |
| 1434 | * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 1435 | * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes) |
| 1436 | * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes) |
| 1437 | * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes) |
| 1438 | * @param[in] dst_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 1439 | * @param[in] dst_step_z src_stride_z * number of elements along Z processed per workitem(in bytes) |
| 1440 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor |
| 1441 | * @param[in] result_multipliers_ptr (Optional) Pointer to the output multipliers vector for per-channel quantization. Supported data types: S32 |
| 1442 | * @param[in] result_multipliers_stride_x (Optional) Stride of the output multipliers vector in X dimension (in bytes) |
| 1443 | * @param[in] result_multipliers_step_x (Optional) output_multipliers_stride_x * number of elements along X processed per workitem(in bytes) |
| 1444 | * @param[in] result_multipliers_offset_first_element_in_bytes (Optional) The offset of the first element in the output multipliers vector |
| 1445 | * @param[in] result_shifts_ptr (Optional) Pointer to the output shifts vector for per-channel quantization. Supported data types: S32 |
| 1446 | * @param[in] result_shifts_stride_x (Optional) Stride of the output shifts vector in X dimension (in bytes) |
| 1447 | * @param[in] result_shifts_step_x (Optional) output_shifts_stride_x * number of elements along X processed per workitem(in bytes) |
| 1448 | * @param[in] result_shifts_offset_first_element_in_bytes (Optional) The offset of the first element in the output shifts vector |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1449 | */ |
| 1450 | __kernel void gemmlowp_offset_contribution_quantize_down(TENSOR3D_DECLARATION(mm_result) |
| 1451 | #if defined(A_OFFSET) |
| 1452 | , |
| 1453 | IMAGE_DECLARATION(sum_col) |
| 1454 | #endif // defined(A_OFFSET) |
| 1455 | #if defined(B_OFFSET) |
| 1456 | , |
| 1457 | IMAGE_DECLARATION(sum_row) |
| 1458 | #endif // defined(B_OFFSET) |
| 1459 | , |
| 1460 | #if defined(ADD_BIAS) |
| 1461 | VECTOR_DECLARATION(biases), |
| 1462 | #endif // defined(ADD_BIAS) |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 1463 | TENSOR3D_DECLARATION(dst) |
| 1464 | #if defined(PER_CHANNEL_QUANTIZATION) |
| 1465 | , |
| 1466 | VECTOR_DECLARATION(result_multipliers), |
| 1467 | VECTOR_DECLARATION(result_shifts) |
| 1468 | #endif // defined(PER_CHANNEL_QUANTIZATION) |
| 1469 | ) |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1470 | { |
| 1471 | const int x = get_global_id(0) * 4; |
| 1472 | const int y = get_global_id(1); |
| 1473 | const int z = get_global_id(2); |
| 1474 | |
| 1475 | __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x + y * dst_stride_y + z * dst_stride_z; |
| 1476 | |
| 1477 | // Compute offset contribution |
| 1478 | int4 offset_term_s32 = offset_contribution( |
| 1479 | x, y, z |
| 1480 | #if defined(A_OFFSET) |
| 1481 | , |
| 1482 | sum_col_ptr, |
| 1483 | sum_col_stride_x, |
| 1484 | sum_col_step_x, |
| 1485 | sum_col_stride_y, |
| 1486 | sum_col_step_y, |
| 1487 | sum_col_offset_first_element_in_bytes |
| 1488 | #endif // defined(A_OFFSET) |
| 1489 | #if defined(B_OFFSET) |
| 1490 | , |
| 1491 | sum_row_ptr, |
| 1492 | sum_row_stride_x, |
| 1493 | sum_row_step_x, |
| 1494 | sum_row_stride_y, |
| 1495 | sum_row_step_y, |
| 1496 | sum_row_offset_first_element_in_bytes |
| 1497 | #endif // defined(B_OFFSET) |
| 1498 | #if defined(ADD_BIAS) |
| 1499 | , |
| 1500 | biases_ptr, |
| 1501 | biases_stride_x, |
| 1502 | biases_step_x, |
| 1503 | biases_offset_first_element_in_bytes |
| 1504 | #endif // defined(ADD_BIAS) |
| 1505 | ); |
| 1506 | |
| 1507 | __global uchar *mm_result_addr = mm_result_ptr + mm_result_offset_first_element_in_bytes + x * sizeof(int) + y * mm_result_stride_y + z * mm_result_stride_z; |
| 1508 | |
| 1509 | int4 in_s32 = vload4(0, (__global int *)mm_result_addr); |
| 1510 | |
| 1511 | // Add the offset terms to GEMM's result |
| 1512 | in_s32 += offset_term_s32; |
| 1513 | |
| 1514 | // -------------- OUTPUT STAGE |
| 1515 | |
| 1516 | // Add the offset terms to GEMM's result |
| 1517 | in_s32 += (int4)RESULT_OFFSET; |
| 1518 | |
| 1519 | // Multiply by result_mult_int and shift |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 1520 | #if defined(PER_CHANNEL_QUANTIZATION) |
| 1521 | __global uchar *result_multipliers_addr = result_multipliers_ptr + result_multipliers_offset_first_element_in_bytes + x * sizeof(int); |
| 1522 | __global uchar *result_shifts_addr = result_shifts_ptr + result_shifts_offset_first_element_in_bytes + x * sizeof(int); |
| 1523 | int4 result_multipliers_values = vload4(0, (__global int *)result_multipliers_addr); |
| 1524 | int4 result_shifts_values = vload4(0, (__global int *)result_shifts_addr); |
| 1525 | |
| 1526 | in_s32 *= result_multipliers_values; |
| 1527 | in_s32 >>= result_shifts_values; |
| 1528 | #else // defined(PER_CHANNEL_QUANTIZATION) |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1529 | in_s32 *= RESULT_MULTIPLIER; |
| 1530 | |
| 1531 | in_s32 >>= RESULT_SHIFT; |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 1532 | #endif // defined(PER_CHANNEL_QUANTIZATION) |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1533 | |
| 1534 | uchar4 res = convert_uchar4_sat(in_s32); |
| 1535 | |
| 1536 | #if defined(MIN_BOUND) |
| 1537 | res = max(res, (uchar4)MIN_BOUND); |
| 1538 | #endif // defined(MIN_BOUND) |
| 1539 | #if defined(MAX_BOUND) |
| 1540 | res = min(res, (uchar4)MAX_BOUND); |
| 1541 | #endif // defined(MAX_BOUND) |
| 1542 | |
| 1543 | // Store the result |
| 1544 | vstore4(res, 0, dst_addr); |
| 1545 | } |
| 1546 | |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 1547 | /* OpenCL kernel used to add the offset contribution after matrix multiplication and it quantizes down to uint8. |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1548 | * |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 1549 | * This kernel takes a final int32 accumulator value (the output of matrix multiplication), adds to it the offset contribution of matrix A and matrix B and quantizes to uint8 through the output stage. |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1550 | * |
| 1551 | * |
| 1552 | * @attention The k_offset = a_offset * b_offset * k (where k is the number of matrix A columns) needs to be passed at compile time using -DK_OFFSET (i.e. -DK_OFFSET=1200) |
| 1553 | * @note In case the offset contribution due to a_offset is required, a_offset needs to be passed at compile time using -DA_OFFSET (i.e. -DA_OFFSET=1) |
| 1554 | * @note In case the offset contribution due to b_offset is required, b_offset needs to be passed at compile time using -DB_OFFSET (i.e. -DB_OFFSET=6) |
| 1555 | * @note In case sum_col has batches, -DSUM_COL_HAS_BATCHES must be passed at compile time. Usually if gemmlowp is used to accelerate convolution layer, sum_col will not have batches |
| 1556 | * |
| 1557 | * The result before the output stage is: |
| 1558 | * |
| 1559 | * mm_result[i][k] = mm_result[i][k] + |
| 1560 | * (sum_col[k] * A_OFFSET) + |
| 1561 | * (sum_row[i] * B_OFFSET) + |
| 1562 | * (K_OFFSET) |
| 1563 | * |
| 1564 | * This result is quantized down to uint8 using the output stage. The output stage computes the following operations: |
| 1565 | * |
| 1566 | * -# Compute fixed point multiplication between each entry of input by result_fixedpoint_multiplier |
| 1567 | * -# Add bias to final result if bias tensor is not a nullptr |
| 1568 | * -# Round to nearest division by a power-of-two using result_shift |
| 1569 | * -# Add offset to each result |
| 1570 | * -# Clamp the value between the specified min and max bounds |
| 1571 | * -# Clamp the resulting int32 values to the [0..255] range and cast to QASYMM8. |
| 1572 | * |
| 1573 | * @attention The offset, scalar scale factor and number of bits to shift right of output tensor must be passed at compile time using -DRESULT_OFFSET, -RESULT_MULT_INT and -DRESULT_SHIFT |
| 1574 | * |
| 1575 | * @note In case the addition of int32 biases is required, -DADD_BIAS should be passed at compile time |
| 1576 | * @note In case the clamping of the result is required, the min and max bounds can be passed at compile time using -DMIN_BOUND and -DMAX_BOUND. |
| 1577 | * These values can be used to implement "rectified linear unit" activation functions |
| 1578 | * |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 1579 | * @param[in] mm_result_ptr Pointer to the source tensor. Supported data type: S32 |
| 1580 | * @param[in] mm_result_stride_x Stride of the source tensor in X dimension (in bytes) |
| 1581 | * @param[in] mm_result_step_x mm_result_stride_x * number of elements along X processed per workitem(in bytes) |
| 1582 | * @param[in] mm_result_stride_y Stride of the source tensor in Y dimension (in bytes) |
| 1583 | * @param[in] mm_result_step_y mm_result_stride_y * number of elements along Y processed per workitem(in bytes) |
| 1584 | * @param[in] mm_result_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 1585 | * @param[in] mm_result_step_z mm_result_stride_z * number of elements along Z processed per workitem(in bytes) |
| 1586 | * @param[in] mm_result_offset_first_element_in_bytes The offset of the first element in the source tensor |
| 1587 | * @param[in] sum_col_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr |
| 1588 | * @param[in] sum_col_stride_x (Optional) Stride of the source tensor in X dimension (in bytes) |
| 1589 | * @param[in] sum_col_step_x (Optional) sum_col_stride_x * number of elements along X processed per workitem(in bytes) |
| 1590 | * @param[in] sum_col_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes) |
| 1591 | * @param[in] sum_col_step_y (Optional) sum_col_stride_y * number of elements along Y processed per workitem(in bytes) |
| 1592 | * @param[in] sum_col_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor |
| 1593 | * @param[in] sum_row_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr |
| 1594 | * @param[in] sum_row_stride_x (Optional) Stride of the source tensor in X dimension (in bytes) |
| 1595 | * @param[in] sum_row_step_x (Optional) sum_row_stride_x * number of elements along X processed per workitem(in bytes) |
| 1596 | * @param[in] sum_row_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes) |
| 1597 | * @param[in] sum_row_step_y (Optional) sum_row_stride_y * number of elements along Y processed per workitem(in bytes) |
| 1598 | * @param[in] sum_row_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor |
| 1599 | * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: same as @p src_ptr |
| 1600 | * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes) |
| 1601 | * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes) |
| 1602 | * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases tensor |
| 1603 | * @param[out] dst_ptr Pointer to the destination tensor Supported data type: QASYMM8 |
| 1604 | * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 1605 | * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes) |
| 1606 | * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes) |
| 1607 | * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes) |
| 1608 | * @param[in] dst_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 1609 | * @param[in] dst_step_z src_stride_z * number of elements along Z processed per workitem(in bytes) |
| 1610 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor |
| 1611 | * @param[in] result_multipliers_ptr (Optional) Pointer to the output multipliers vector for per-channel quantization. Supported data types: S32 |
| 1612 | * @param[in] result_multipliers_stride_x (Optional) Stride of the output multipliers vector in X dimension (in bytes) |
| 1613 | * @param[in] result_multipliers_step_x (Optional) output_multipliers_stride_x * number of elements along X processed per workitem(in bytes) |
| 1614 | * @param[in] result_multipliers_offset_first_element_in_bytes (Optional) The offset of the first element in the output multipliers vector |
| 1615 | * @param[in] result_shifts_ptr (Optional) Pointer to the output shifts vector for per-channel quantization. Supported data types: S32 |
| 1616 | * @param[in] result_shifts_stride_x (Optional) Stride of the output shifts vector in X dimension (in bytes) |
| 1617 | * @param[in] result_shifts_step_x (Optional) output_shifts_stride_x * number of elements along X processed per workitem(in bytes) |
| 1618 | * @param[in] result_shifts_offset_first_element_in_bytes (Optional) The offset of the first element in the output shifts vector |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1619 | */ |
| 1620 | __kernel void gemmlowp_offset_contribution_quantize_down_fixedpoint(TENSOR3D_DECLARATION(mm_result) |
| 1621 | #if defined(A_OFFSET) |
| 1622 | , |
| 1623 | IMAGE_DECLARATION(sum_col) |
| 1624 | #endif // defined(A_OFFSET) |
| 1625 | #if defined(B_OFFSET) |
| 1626 | , |
| 1627 | IMAGE_DECLARATION(sum_row) |
| 1628 | #endif // defined(B_OFFSET) |
| 1629 | , |
| 1630 | #if defined(ADD_BIAS) |
| 1631 | VECTOR_DECLARATION(biases), |
| 1632 | #endif // defined(ADD_BIAS) |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 1633 | TENSOR3D_DECLARATION(dst) |
| 1634 | #if defined(PER_CHANNEL_QUANTIZATION) |
| 1635 | , |
| 1636 | VECTOR_DECLARATION(result_multipliers), |
| 1637 | VECTOR_DECLARATION(result_shifts) |
| 1638 | #endif // defined(PER_CHANNEL_QUANTIZATION) |
| 1639 | ) |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1640 | { |
| 1641 | const int x = get_global_id(0) * 4; |
| 1642 | const int y = get_global_id(1); |
| 1643 | const int z = get_global_id(2); |
| 1644 | |
| 1645 | // Compute offset contribution |
| 1646 | int4 offset_term_s32 = offset_contribution( |
| 1647 | x, y, z |
| 1648 | #if defined(A_OFFSET) |
| 1649 | , |
| 1650 | sum_col_ptr, |
| 1651 | sum_col_stride_x, |
| 1652 | sum_col_step_x, |
| 1653 | sum_col_stride_y, |
| 1654 | sum_col_step_y, |
| 1655 | sum_col_offset_first_element_in_bytes |
| 1656 | #endif // defined(A_OFFSET) |
| 1657 | #if defined(B_OFFSET) |
| 1658 | , |
| 1659 | sum_row_ptr, |
| 1660 | sum_row_stride_x, |
| 1661 | sum_row_step_x, |
| 1662 | sum_row_stride_y, |
| 1663 | sum_row_step_y, |
| 1664 | sum_row_offset_first_element_in_bytes |
| 1665 | #endif // defined(B_OFFSET) |
| 1666 | #if defined(ADD_BIAS) |
| 1667 | , |
| 1668 | biases_ptr, |
| 1669 | biases_stride_x, |
| 1670 | biases_step_x, |
| 1671 | biases_offset_first_element_in_bytes |
| 1672 | #endif // defined(ADD_BIAS) |
| 1673 | ); |
| 1674 | |
| 1675 | __global uchar *mm_result_addr = mm_result_ptr + mm_result_offset_first_element_in_bytes + x * sizeof(int) + y * mm_result_stride_y + z * mm_result_stride_z; |
| 1676 | |
| 1677 | __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x + y * dst_stride_y + z * dst_stride_z; |
| 1678 | |
| 1679 | int4 in_s32 = vload4(0, (__global int *)mm_result_addr); |
| 1680 | |
| 1681 | // Add the offset terms to GEMM's result |
| 1682 | in_s32 += offset_term_s32; |
| 1683 | |
| 1684 | // -------------- OUTPUT STAGE |
| 1685 | |
| 1686 | // Multiply by result_mult_int and shift |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 1687 | #if defined(PER_CHANNEL_QUANTIZATION) |
| 1688 | __global uchar *result_multipliers_addr = result_multipliers_ptr + result_multipliers_offset_first_element_in_bytes + x * sizeof(int); |
| 1689 | __global uchar *result_shifts_addr = result_shifts_ptr + result_shifts_offset_first_element_in_bytes + x * sizeof(int); |
| 1690 | int4 result_multipliers_values = vload4(0, (__global int *)result_multipliers_addr); |
| 1691 | int4 result_shifts_values = vload4(0, (__global int *)result_shifts_addr); |
| 1692 | |
Michele Di Giorgio | 14cbfb2 | 2019-10-23 10:53:10 +0100 | [diff] [blame] | 1693 | int4 in_s32_shift_lt0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(in_s32, result_multipliers_values, result_shifts_values, 4); |
| 1694 | int4 in_s32_shift_gt0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(in_s32, result_multipliers_values, result_shifts_values, 4); |
| 1695 | in_s32 = select(in_s32_shift_lt0, in_s32_shift_gt0, result_shifts_values >= 0); |
| 1696 | #else // defined(PER_CHANNEL_QUANTIZATION) |
| 1697 | |
| 1698 | #if RESULT_SHIFT < 0 |
| 1699 | in_s32 = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(in_s32, RESULT_MULTIPLIER, RESULT_SHIFT, 4); |
| 1700 | #else // RESULT_SHIFT >= 0 |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1701 | in_s32 = ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(in_s32, RESULT_MULTIPLIER, RESULT_SHIFT, 4); |
Michele Di Giorgio | 14cbfb2 | 2019-10-23 10:53:10 +0100 | [diff] [blame] | 1702 | #endif // RESULT_SHIFT < 0 |
| 1703 | |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 1704 | #endif // defined(PER_CHANNEL_QUANTIZATION) |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1705 | |
| 1706 | // Add the offset terms to GEMM's result |
| 1707 | in_s32 += (int4)RESULT_OFFSET; |
| 1708 | |
| 1709 | uchar4 res = convert_uchar4_sat(in_s32); |
| 1710 | |
| 1711 | #if defined(MIN_BOUND) |
| 1712 | res = max(res, (uchar4)MIN_BOUND); |
| 1713 | #endif // defined(MIN_BOUND) |
| 1714 | #if defined(MAX_BOUND) |
| 1715 | res = min(res, (uchar4)MAX_BOUND); |
| 1716 | #endif // defined(MAX_BOUND) |
| 1717 | |
| 1718 | // Store the result |
| 1719 | vstore4(res, 0, dst_addr); |
| 1720 | } |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 1721 | #endif // defined(RESULT_OFFSET) && defined(RESULT_MULTIPLIER) && defined(RESULT_SHIFT) |
| 1722 | |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 1723 | #endif // defined(K_OFFSET) |
| 1724 | |
| 1725 | #if defined(RESULT_OFFSET) && defined(RESULT_MULT_INT) && defined(RESULT_SHIFT) |
| 1726 | /** This OpenCL kernel is used to quantize down the int32 accumulator values of GEMMLowp to QASYMM8 |
| 1727 | * |
| 1728 | * This kernel takes a final int32 accumulator value and processes it to obtain the final QASYMM8 value. |
| 1729 | * The following computations will be performed by the kernel: |
| 1730 | * |
| 1731 | * -# Add offset terms to final result |
| 1732 | * -# Multiply each entry of result by result_mult_int |
| 1733 | * -# Add bias to final result (if -DADD_BIAS is passed at compile time) |
| 1734 | * -# Shift the int32 accumulator by result_shift |
| 1735 | * -# Clamp the value between the specified min and max bounds (if -DMIN_BOUND and/or -DMAX_BOUND are passed at compile time) |
| 1736 | * -# Clamp the resulting int32 values to the [0..255] range and cast to QASYMM8. |
| 1737 | * |
| 1738 | * @attention The offset, scalar scale factor and number of bits to shift right of output tensor must be passed at compile time using -DRESULT_OFFSET, -RESULT_MULT_INT and -DRESULT_SHIFT |
| 1739 | * |
| 1740 | * @note In case the addition of int32 biases is required, -DADD_BIAS should be passed at compile time |
| 1741 | * @note In case the clamping of the result is required, the min and max bounds can be passed at compile time using -DMIN_BOUND and -DMAX_BOUND. |
| 1742 | * These values can be used to implement "rectified linear unit" activation functions |
| 1743 | * |
| 1744 | * @param[in] src_ptr Pointer to the source tensor. Supported data type: S32 |
| 1745 | * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes) |
| 1746 | * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 1747 | * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes) |
| 1748 | * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 1749 | * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 1750 | * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes) |
| 1751 | * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1752 | * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: same as @p src_ptr |
| 1753 | * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes) |
| 1754 | * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes) |
| 1755 | * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases tensor |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 1756 | * @param[out] dst_ptr Pointer to the destination tensor Supported data type: QASYMM8 |
| 1757 | * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 1758 | * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes) |
| 1759 | * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes) |
| 1760 | * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes) |
| 1761 | * @param[in] dst_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 1762 | * @param[in] dst_step_z src_stride_z * number of elements along Z processed per workitem(in bytes) |
| 1763 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor |
| 1764 | */ |
| 1765 | __kernel void gemmlowp_output_stage_quantize_down(TENSOR3D_DECLARATION(src), |
| 1766 | #if defined(ADD_BIAS) |
| 1767 | VECTOR_DECLARATION(biases), |
| 1768 | #endif // defined(ADD_BIAS) |
| 1769 | TENSOR3D_DECLARATION(dst)) |
| 1770 | { |
| 1771 | // Compute source and destination addresses |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1772 | int x = get_global_id(0) * 4; |
| 1773 | int y = get_global_id(1); |
| 1774 | int z = get_global_id(2); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 1775 | |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1776 | __global uchar *src_addr = src_ptr + src_offset_first_element_in_bytes + x * sizeof(int) + y * src_stride_y + z * src_stride_z; |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 1777 | |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1778 | __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x + y * dst_stride_y + z * dst_stride_z; |
| 1779 | |
| 1780 | int4 input_values = vload4(0, (__global int *)src_addr); |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 1781 | |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 1782 | #if defined(ADD_BIAS) |
| 1783 | // Add bias |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1784 | __global uchar *bias_addr = biases_ptr + biases_offset_first_element_in_bytes + x * sizeof(int); |
| 1785 | |
| 1786 | int4 biases_values = vload4(0, (__global int *)bias_addr); |
| 1787 | input_values += (int4)biases_values; |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 1788 | #endif // defined(ADD_BIAS) |
| 1789 | |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1790 | // Add the offset terms to GEMM's result |
| 1791 | input_values += (int4)RESULT_OFFSET; |
| 1792 | |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 1793 | // Multiply by result_mult_int and shift |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 1794 | input_values *= RESULT_MULT_INT; |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 1795 | |
Michele Di Giorgio | 14cbfb2 | 2019-10-23 10:53:10 +0100 | [diff] [blame] | 1796 | #if RESULT_SHIFT < 0 |
| 1797 | input_values >>= -RESULT_SHIFT; |
| 1798 | #else // RESULT_SHIFT >= 0 |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 1799 | input_values >>= RESULT_SHIFT; |
Michele Di Giorgio | 14cbfb2 | 2019-10-23 10:53:10 +0100 | [diff] [blame] | 1800 | #endif // RESULT_SHIFT < 0 |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 1801 | |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1802 | uchar4 res = convert_uchar4_sat(input_values); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 1803 | |
| 1804 | #if defined(MIN_BOUND) |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1805 | res = max(res, (uchar4)MIN_BOUND); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 1806 | #endif // defined(MIN_BOUND) |
| 1807 | #if defined(MAX_BOUND) |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1808 | res = min(res, (uchar4)MAX_BOUND); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 1809 | #endif // defined(MAX_BOUND) |
| 1810 | |
| 1811 | // Store the result |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1812 | vstore4(res, 0, dst_addr); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 1813 | } |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 1814 | #endif // defined(RESULT_OFFSET) && defined(RESULT_MULT_INT) && defined(RESULT_SHIFT) |
| 1815 | |
| 1816 | #if defined(RESULT_OFFSET_AFTER_SHIFT) && defined(RESULT_FIXEDPOINT_MULTIPLIER) && defined(RESULT_SHIFT) |
| 1817 | /** This OpenCL kernel is used to quantize down the int32 accumulator values of GEMMLowp to QASYMM8 |
| 1818 | * |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 1819 | * This kernel takes a final int32 accumulator value (the output of matrix multiplication), and processes it to obtain the final QASYMM8 value. |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 1820 | * The following computations will be performed by the kernel: |
| 1821 | * |
| 1822 | * -# Compute fixed point multiplication between each entry of input by result_fixedpoint_multiplier |
| 1823 | * -# Add bias to final result if bias tensor is not a nullptr |
| 1824 | * -# Round to nearest division by a power-of-two using result_shift |
| 1825 | * -# Add offset to each result |
| 1826 | * -# Clamp the value between the specified min and max bounds |
Manuel Bottini | 1f332d4 | 2019-11-29 17:25:25 +0000 | [diff] [blame] | 1827 | * -# Clamp the resulting int32 values: |
| 1828 | * - to the [0..255] range and cast to QASYMM8. |
| 1829 | * - to the [-128..127] range and cast to QASYMM8_SIGNED. |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 1830 | * |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1831 | * @attention The offset, scalar scale factor and number of bits to shift right of output tensor must be passed at compile time using -DRESULT_OFFSET_AFTER_SHIFT, -DRESULT_FIXEDPOINT_MULTIPLIER and -DRESULT_SHIFT |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 1832 | * |
| 1833 | * @note In case the addition of int32 biases is required, -DADD_BIAS should be passed at compile time |
Manuel Bottini | 1f332d4 | 2019-11-29 17:25:25 +0000 | [diff] [blame] | 1834 | * @note The output datatype should be passed at compile time using -DOUTPUT_DATA_TYPE |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 1835 | * @note In case the clamping of the result is required, the min and max bounds can be passed at compile time using -DMIN_BOUND and -DMAX_BOUND. |
| 1836 | * These values can be used to implement "rectified linear unit" activation functions |
| 1837 | * |
| 1838 | * @param[in] src_ptr Pointer to the source tensor. Supported data type: S32 |
| 1839 | * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes) |
| 1840 | * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 1841 | * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes) |
| 1842 | * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 1843 | * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 1844 | * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes) |
| 1845 | * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1846 | * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: same as @p src_ptr |
| 1847 | * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes) |
| 1848 | * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes) |
| 1849 | * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases tensor |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 1850 | * @param[out] dst_ptr Pointer to the destination tensor Supported data type: QASYMM8 |
| 1851 | * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 1852 | * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes) |
| 1853 | * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes) |
| 1854 | * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes) |
| 1855 | * @param[in] dst_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 1856 | * @param[in] dst_step_z src_stride_z * number of elements along Z processed per workitem(in bytes) |
| 1857 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor |
| 1858 | */ |
| 1859 | __kernel void gemmlowp_output_stage_quantize_down_fixedpoint(TENSOR3D_DECLARATION(src), |
| 1860 | #if defined(ADD_BIAS) |
| 1861 | VECTOR_DECLARATION(biases), |
| 1862 | #endif // defined(ADD_BIAS) |
| 1863 | TENSOR3D_DECLARATION(dst)) |
| 1864 | { |
| 1865 | // Compute source and destination addresses |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1866 | int x = get_global_id(0) * 4; |
| 1867 | int y = get_global_id(1); |
| 1868 | int z = get_global_id(2); |
Georgios Pinitas | 932491f | 2018-09-21 16:33:15 +0100 | [diff] [blame] | 1869 | |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1870 | __global uchar *src_addr = src_ptr + src_offset_first_element_in_bytes + x * sizeof(int) + y * src_stride_y + z * src_stride_z; |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 1871 | |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1872 | __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x + y * dst_stride_y + z * dst_stride_z; |
| 1873 | |
| 1874 | int4 input_values = vload4(0, (__global int *)src_addr); |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 1875 | |
| 1876 | #if defined(ADD_BIAS) |
| 1877 | // Add bias |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1878 | __global uchar *bias_addr = biases_ptr + biases_offset_first_element_in_bytes + x * sizeof(int); |
| 1879 | |
| 1880 | int4 biases_values = vload4(0, (__global int *)bias_addr); |
| 1881 | input_values += (int4)biases_values; |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 1882 | #endif // defined(ADD_BIAS) |
| 1883 | |
| 1884 | // Multiply by result_mult_int and shift |
Michele Di Giorgio | 14cbfb2 | 2019-10-23 10:53:10 +0100 | [diff] [blame] | 1885 | #if RESULT_SHIFT < 0 |
| 1886 | input_values = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(input_values, RESULT_FIXEDPOINT_MULTIPLIER, RESULT_SHIFT, 4); |
| 1887 | #else // RESULT_SHIFT >= 0 |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1888 | input_values = ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(input_values, RESULT_FIXEDPOINT_MULTIPLIER, RESULT_SHIFT, 4); |
Michele Di Giorgio | 14cbfb2 | 2019-10-23 10:53:10 +0100 | [diff] [blame] | 1889 | #endif // RESULT_SHIFT < 0 |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 1890 | |
| 1891 | // Add the offset terms to GEMM's result |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1892 | input_values += (int4)RESULT_OFFSET_AFTER_SHIFT; |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 1893 | |
Manuel Bottini | 1f332d4 | 2019-11-29 17:25:25 +0000 | [diff] [blame] | 1894 | VEC_DATA_TYPE(OUTPUT_DATA_TYPE, 4) |
| 1895 | res = CONVERT_SAT(input_values, VEC_DATA_TYPE(OUTPUT_DATA_TYPE, 4)); |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 1896 | |
| 1897 | #if defined(MIN_BOUND) |
Manuel Bottini | 1f332d4 | 2019-11-29 17:25:25 +0000 | [diff] [blame] | 1898 | res = max(res, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, 4))MIN_BOUND); |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 1899 | #endif // defined(MIN_BOUND) |
| 1900 | #if defined(MAX_BOUND) |
Manuel Bottini | 1f332d4 | 2019-11-29 17:25:25 +0000 | [diff] [blame] | 1901 | res = min(res, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, 4))MAX_BOUND); |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 1902 | #endif // defined(MAX_BOUND) |
| 1903 | |
| 1904 | // Store the result |
Manuel Bottini | 1f332d4 | 2019-11-29 17:25:25 +0000 | [diff] [blame] | 1905 | vstore4(res, 0, (__global OUTPUT_DATA_TYPE *)dst_addr); |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 1906 | } |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 1907 | #endif // defined(RESULT_OFFSET_AFTER_SHIFT) && defined(RESULT_FIXEDPOINT_MULTIPLIER) && defined(RESULT_SHIFT) |
Georgios Pinitas | 51e53a3 | 2018-10-22 13:49:08 +0100 | [diff] [blame] | 1908 | |
Manuel Bottini | 9c9b70b | 2019-07-01 17:35:56 +0100 | [diff] [blame] | 1909 | #if defined(RESULT_FIXEDPOINT_MULTIPLIER) && defined(RESULT_SHIFT) |
| 1910 | |
Michalis Spyrou | 51146c5 | 2019-07-12 14:42:29 +0100 | [diff] [blame] | 1911 | /** This OpenCL kernel is used to quantize down the int32 accumulator values of GEMMLowp to QSYMM16 |
Manuel Bottini | 9c9b70b | 2019-07-01 17:35:56 +0100 | [diff] [blame] | 1912 | * |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 1913 | * This kernel takes a final int32 accumulator value (the output of matrix multiplication), and processes it to obtain the final QSYMM16 value. |
Manuel Bottini | 9c9b70b | 2019-07-01 17:35:56 +0100 | [diff] [blame] | 1914 | * The following computations will be performed by the kernel: |
| 1915 | * |
| 1916 | * -# Compute fixed point multiplication between each entry of input by result_fixedpoint_multiplier |
| 1917 | * -# Add bias to final result if bias tensor is not a nullptr |
| 1918 | * -# Round to nearest division by a power-of-two using result_shift |
| 1919 | * -# Add offset to each result |
| 1920 | * -# Clamp the value between the specified min and max bounds |
| 1921 | * -# Clamp the resulting int32 values to the [-32768..32767] range and cast to QSYMM16. |
| 1922 | * |
| 1923 | * @attention The offset, scalar scale factor and number of bits to shift right of output tensor must be passed at compile time using -DRESULT_FIXEDPOINT_MULTIPLIER and -DRESULT_SHIFT |
| 1924 | * |
| 1925 | * @note In case the addition of int32 biases is required, -DADD_BIAS should be passed at compile time |
| 1926 | * @note In case the clamping of the result is required, the min and max bounds can be passed at compile time using -DMIN_BOUND and -DMAX_BOUND. |
| 1927 | * These values can be used to implement "rectified linear unit" activation functions |
| 1928 | * |
| 1929 | * @param[in] src_ptr Pointer to the source tensor. Supported data type: S32 |
| 1930 | * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes) |
| 1931 | * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 1932 | * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes) |
| 1933 | * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 1934 | * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 1935 | * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes) |
| 1936 | * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor |
| 1937 | * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: same as @p src_ptr |
| 1938 | * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes) |
| 1939 | * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes) |
| 1940 | * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases tensor |
| 1941 | * @param[out] dst_ptr Pointer to the destination tensor Supported data type: QASYMM8 |
| 1942 | * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 1943 | * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes) |
| 1944 | * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes) |
| 1945 | * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes) |
| 1946 | * @param[in] dst_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 1947 | * @param[in] dst_step_z src_stride_z * number of elements along Z processed per workitem(in bytes) |
| 1948 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor |
| 1949 | */ |
| 1950 | __kernel void gemmlowp_output_stage_quantize_down_fixedpoint_qsymm16(TENSOR3D_DECLARATION(src), |
| 1951 | #if defined(ADD_BIAS) |
| 1952 | VECTOR_DECLARATION(biases), |
| 1953 | #endif // defined(ADD_BIAS) |
| 1954 | TENSOR3D_DECLARATION(dst)) |
| 1955 | { |
| 1956 | // Compute source and destination addresses |
| 1957 | int x = get_global_id(0) * 4; |
| 1958 | int y = get_global_id(1); |
| 1959 | int z = get_global_id(2); |
| 1960 | |
Michalis Spyrou | 51146c5 | 2019-07-12 14:42:29 +0100 | [diff] [blame] | 1961 | __global uchar *src_addr = src_ptr + src_offset_first_element_in_bytes + x * sizeof(int) + y * src_stride_y + z * src_stride_z; |
Manuel Bottini | 9c9b70b | 2019-07-01 17:35:56 +0100 | [diff] [blame] | 1962 | |
Michalis Spyrou | 51146c5 | 2019-07-12 14:42:29 +0100 | [diff] [blame] | 1963 | __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x * 2 + y * dst_stride_y + z * dst_stride_z; |
Manuel Bottini | 9c9b70b | 2019-07-01 17:35:56 +0100 | [diff] [blame] | 1964 | |
| 1965 | int4 input_values = vload4(0, (__global int *)src_addr); |
| 1966 | |
| 1967 | #if defined(ADD_BIAS) |
| 1968 | // Add bias |
Michalis Spyrou | 51146c5 | 2019-07-12 14:42:29 +0100 | [diff] [blame] | 1969 | __global uchar *bias_addr = biases_ptr + biases_offset_first_element_in_bytes + x * sizeof(int); |
Manuel Bottini | 9c9b70b | 2019-07-01 17:35:56 +0100 | [diff] [blame] | 1970 | |
| 1971 | int4 biases_values = vload4(0, (__global int *)bias_addr); |
| 1972 | input_values += (int4)biases_values; |
| 1973 | #endif // defined(ADD_BIAS) |
| 1974 | |
| 1975 | // Multiply by result_mult_int and shift |
Manuel Bottini | 0726398 | 2019-10-17 18:37:26 +0100 | [diff] [blame] | 1976 | #if RESULT_SHIFT < 0 |
Michele Di Giorgio | 14cbfb2 | 2019-10-23 10:53:10 +0100 | [diff] [blame] | 1977 | input_values = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(input_values, RESULT_FIXEDPOINT_MULTIPLIER, RESULT_SHIFT, 4); |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 1978 | #else // RESULT_SHIFT >= 0 |
Manuel Bottini | 9c9b70b | 2019-07-01 17:35:56 +0100 | [diff] [blame] | 1979 | input_values = ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(input_values, RESULT_FIXEDPOINT_MULTIPLIER, RESULT_SHIFT, 4); |
Manuel Bottini | 0726398 | 2019-10-17 18:37:26 +0100 | [diff] [blame] | 1980 | #endif // RESULT_SHIFT < 0 |
Manuel Bottini | 9c9b70b | 2019-07-01 17:35:56 +0100 | [diff] [blame] | 1981 | |
| 1982 | short4 res = convert_short4_sat(input_values); |
| 1983 | |
| 1984 | #if defined(MIN_BOUND) |
| 1985 | res = max(res, (short4)MIN_BOUND); |
| 1986 | #endif // defined(MIN_BOUND) |
| 1987 | #if defined(MAX_BOUND) |
| 1988 | res = min(res, (short4)MAX_BOUND); |
| 1989 | #endif // defined(MAX_BOUND) |
| 1990 | |
| 1991 | // Store the result |
Michalis Spyrou | 51146c5 | 2019-07-12 14:42:29 +0100 | [diff] [blame] | 1992 | vstore4(res, 0, (__global short *)dst_addr); |
Manuel Bottini | 9c9b70b | 2019-07-01 17:35:56 +0100 | [diff] [blame] | 1993 | } |
| 1994 | #endif // defined(RESULT_FIXEDPOINT_MULTIPLIER) && defined(RESULT_SHIFT) |
| 1995 | |
Georgios Pinitas | 51e53a3 | 2018-10-22 13:49:08 +0100 | [diff] [blame] | 1996 | #if defined(REAL_MULTIPLIER) && defined(OUTPUT_OFFSET) |
| 1997 | /** This OpenCL kernel is used to quantize down the int32 accumulator values of GEMMLowp to QASYMM8 |
| 1998 | * |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 1999 | * This kernel takes a final int32 accumulator value (the output of matrix multiplication), and processes it to obtain the final QASYMM8 value. |
Georgios Pinitas | 51e53a3 | 2018-10-22 13:49:08 +0100 | [diff] [blame] | 2000 | * The following computations will be performed by the kernel: |
| 2001 | * |
| 2002 | * -# Compute fixed point multiplication between each entry of input by result_fixedpoint_multiplier |
| 2003 | * -# Add bias to final result if bias tensor is not a nullptr |
| 2004 | * -# Requantize |
| 2005 | * -# Add offset to each result |
| 2006 | * -# Clamp the value between the specified min and max bounds |
| 2007 | * -# Clamp the resulting int32 values to the [0..255] range and cast to QASYMM8. |
| 2008 | * |
| 2009 | * @attention The offset and scalar scale factor must be passed at compile time using -DRESULT_OFFSET, -DREAL_MULTIPLIER |
| 2010 | * |
| 2011 | * @note In case the addition of int32 biases is required, -DADD_BIAS should be passed at compile time |
| 2012 | * @note In case the clamping of the result is required, the min and max bounds can be passed at compile time using -DMIN_BOUND and -DMAX_BOUND. |
| 2013 | * These values can be used to implement "rectified linear unit" activation functions |
| 2014 | * |
| 2015 | * @param[in] src_ptr Pointer to the source tensor. Supported data type: S32 |
| 2016 | * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes) |
| 2017 | * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 2018 | * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes) |
| 2019 | * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 2020 | * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 2021 | * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes) |
| 2022 | * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor |
| 2023 | * @param[in] biases_ptr Pointer to the biases tensor. Supported data type: same as @p src_ptr |
| 2024 | * @param[in] biases_stride_x Stride of the biases tensor in X dimension (in bytes) |
| 2025 | * @param[in] biases_step_x biases_stride_x * number of elements along X processed per workitem(in bytes) |
| 2026 | * @param[in] biases_offset_first_element_in_bytes The offset of the first element in the biases tensor |
| 2027 | * @param[out] dst_ptr Pointer to the destination tensor Supported data type: QASYMM8 |
| 2028 | * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 2029 | * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes) |
| 2030 | * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes) |
| 2031 | * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes) |
| 2032 | * @param[in] dst_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 2033 | * @param[in] dst_step_z src_stride_z * number of elements along Z processed per workitem(in bytes) |
| 2034 | * @param[in] dst_stride_w Stride of the source tensor in W dimension (in bytes) |
| 2035 | * @param[in] dst_step_w src_stride_w * number of elements along W processed per workitem(in bytes) |
| 2036 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor |
| 2037 | */ |
| 2038 | __kernel void gemmlowp_output_stage_quantize_down_float(TENSOR3D_DECLARATION(src), |
| 2039 | #if defined(ADD_BIAS) |
| 2040 | VECTOR_DECLARATION(biases), |
| 2041 | #endif // defined(ADD_BIAS) |
| 2042 | #if defined(DST_HEIGHT) |
| 2043 | TENSOR4D_DECLARATION(dst)) |
| 2044 | #else // defined(DST_HEIGHT) |
| 2045 | TENSOR3D_DECLARATION(dst)) |
| 2046 | #endif // defined(DST_HEIGHT) |
| 2047 | { |
| 2048 | // Compute source and destination addresses |
Gian Marco Iodice | 0c54a62 | 2018-10-30 12:20:03 +0000 | [diff] [blame] | 2049 | int x = get_global_id(0) * 4; |
| 2050 | int y = get_global_id(1); |
| 2051 | int z = get_global_id(2); |
Georgios Pinitas | 51e53a3 | 2018-10-22 13:49:08 +0100 | [diff] [blame] | 2052 | |
Gian Marco Iodice | 0c54a62 | 2018-10-30 12:20:03 +0000 | [diff] [blame] | 2053 | __global uchar *src_addr = src_ptr + src_offset_first_element_in_bytes + x * sizeof(int) + y * src_stride_y + z * src_stride_z; |
Georgios Pinitas | 51e53a3 | 2018-10-22 13:49:08 +0100 | [diff] [blame] | 2054 | |
Gian Marco Iodice | 0c54a62 | 2018-10-30 12:20:03 +0000 | [diff] [blame] | 2055 | __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x + y * dst_stride_y + z * dst_stride_z; |
| 2056 | |
| 2057 | int4 input_values = vload4(0, (__global int *)src_addr); |
Georgios Pinitas | 51e53a3 | 2018-10-22 13:49:08 +0100 | [diff] [blame] | 2058 | |
| 2059 | #if defined(ADD_BIAS) |
| 2060 | // Add bias |
Gian Marco Iodice | 0c54a62 | 2018-10-30 12:20:03 +0000 | [diff] [blame] | 2061 | __global uchar *bias_addr = biases_ptr + biases_offset_first_element_in_bytes + x * sizeof(int); |
| 2062 | |
| 2063 | int4 biases_values = vload4(0, (__global int *)bias_addr); |
| 2064 | input_values += (int4)biases_values; |
Georgios Pinitas | 51e53a3 | 2018-10-22 13:49:08 +0100 | [diff] [blame] | 2065 | #endif // defined(ADD_BIAS) |
| 2066 | |
| 2067 | // Convert to float |
Gian Marco Iodice | 0c54a62 | 2018-10-30 12:20:03 +0000 | [diff] [blame] | 2068 | float16 input_values_f = convert_float4(input_values); |
Georgios Pinitas | 51e53a3 | 2018-10-22 13:49:08 +0100 | [diff] [blame] | 2069 | input_values_f = round(input_values_f * (float)REAL_MULTIPLIER + (float)OUTPUT_OFFSET); |
| 2070 | |
Gian Marco Iodice | 0c54a62 | 2018-10-30 12:20:03 +0000 | [diff] [blame] | 2071 | uchar4 res = convert_uchar4_sat(input_values_f); |
Georgios Pinitas | 51e53a3 | 2018-10-22 13:49:08 +0100 | [diff] [blame] | 2072 | |
| 2073 | #if defined(MIN_BOUND) |
Gian Marco Iodice | 0c54a62 | 2018-10-30 12:20:03 +0000 | [diff] [blame] | 2074 | res = max(res, (uchar4)MIN_BOUND); |
Georgios Pinitas | 51e53a3 | 2018-10-22 13:49:08 +0100 | [diff] [blame] | 2075 | #endif // defined(MIN_BOUND) |
| 2076 | #if defined(MAX_BOUND) |
Gian Marco Iodice | 0c54a62 | 2018-10-30 12:20:03 +0000 | [diff] [blame] | 2077 | res = min(res, (uchar4)MAX_BOUND); |
Georgios Pinitas | 51e53a3 | 2018-10-22 13:49:08 +0100 | [diff] [blame] | 2078 | #endif // defined(MAX_BOUND) |
| 2079 | |
| 2080 | // Store the result |
Gian Marco Iodice | 0c54a62 | 2018-10-30 12:20:03 +0000 | [diff] [blame] | 2081 | vstore4(res, 0, dst_addr); |
Georgios Pinitas | 51e53a3 | 2018-10-22 13:49:08 +0100 | [diff] [blame] | 2082 | } |
Gian Marco Iodice | db18a6f | 2019-05-30 09:53:10 +0100 | [diff] [blame] | 2083 | #endif // defined(REAL_MULTIPLIER) && defined(OUTPUT_OFFSET) |