blob: 127df063f64234134ce15c341db988b3221f097f [file] [log] [blame]
Gian Marco05288a22017-11-21 10:57:50 +00001/*
Manuel Bottini959c26d2019-12-02 16:22:35 +00002 * Copyright (c) 2017-2020 ARM Limited.
Gian Marco05288a22017-11-21 10:57:50 +00003 *
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 Iodice43a129e2019-05-14 10:14:08 +010024#include "gemm_helpers.h"
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +000025#include "helpers_asymm.h"
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +000026#include "repeat.h"
Gian Marco05288a22017-11-21 10:57:50 +000027
Michele Di Giorgiof9179d32019-11-27 16:17:30 +000028#if defined(DATA_TYPE) && defined(ACC_DATA_TYPE)
29
Georgios Pinitasdaa38552018-08-28 17:43:18 +010030#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 Iodice4b908652018-10-18 10:21:02 +010032#define ARM_DOT(x, y, val) val = arm_dot_acc((x), (y), (val));
Georgios Pinitasdaa38552018-08-28 17:43:18 +010033#else // defined(ARM_COMPUTE_OPENCL_DOT8_ACC_ENABLED) && defined(cl_arm_integer_dot_product_accumulate_int8)
Gian Marco Iodice4b908652018-10-18 10:21:02 +010034#define ARM_DOT(x, y, val) val += arm_dot((x), (y));
Georgios Pinitasdaa38552018-08-28 17:43:18 +010035#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 Arenac50da382018-07-26 15:50:09 +010037
Gian Marco Iodice43a129e2019-05-14 10:14:08 +010038#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 Giorgiof9179d32019-11-27 16:17:30 +000041#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 Iodice43a129e2019-05-14 10:14:08 +010044 })
Michele Di Giorgiof9179d32019-11-27 16:17:30 +000045#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 Iodice43a129e2019-05-14 10:14:08 +010048 })
Michele Di Giorgiof9179d32019-11-27 16:17:30 +000049#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 Iodice43a129e2019-05-14 10:14:08 +010052 })
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 Giorgiof9179d32019-11-27 16:17:30 +000071#define ARM_DOT1(a, b, c) \
72 ({ \
73 c += (ACC_DATA_TYPE)a * b; \
Gian Marco Iodice43a129e2019-05-14 10:14:08 +010074 })
Michele Di Giorgiof9179d32019-11-27 16:17:30 +000075#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 Iodice43a129e2019-05-14 10:14:08 +010079 })
Michele Di Giorgiof9179d32019-11-27 16:17:30 +000080#define ARM_DOT3(a, b, c) \
81 ({ \
82 ARM_DOT2(a, b, c); \
83 c += (ACC_DATA_TYPE)a.s2 * b.s2; \
Gian Marco Iodice43a129e2019-05-14 10:14:08 +010084 })
Michele Di Giorgiof9179d32019-11-27 16:17:30 +000085#define ARM_DOT4(a, b, c) \
86 ({ \
87 ARM_DOT3(a, b, c); \
88 c += (ACC_DATA_TYPE)a.s3 * b.s3; \
Gian Marco Iodice43a129e2019-05-14 10:14:08 +010089 })
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 Pinitas705fd3d2019-06-17 17:23:22 +0100139/** Specialized macros to perform a a partial matrix multiplication with dimensions M0,N0,K0 */
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100140#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 Marco05288a22017-11-21 10:57:50 +0000198#if defined(NUM_ELEMS_PROCESSED_PER_THREAD_X) && defined(NUM_ELEMS_PROCESSED_PER_THREAD_Y) && defined(COLS_A)
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000199#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 Marco05288a22017-11-21 10:57:50 +0000201#define VECTOR_INT VEC_DATA_TYPE(int, NUM_ELEMS_PROCESSED_PER_THREAD_X)
Sheri Zhang28287af2020-02-25 14:13:54 +0000202/** This OpenCL kernel computes the matrix multiplication between matrix A (src0) and matrix B (src1) in case both matrices have not been reshaped
Gian Marco05288a22017-11-21 10:57:50 +0000203 *
204 * @attention The number of matrix A columns needs to be passed at compile time using -DCOLS_A
205 *
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000206 * @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 Pinitasebf6b8a2018-09-24 16:31:08 +0100208 * @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 Marco05288a22017-11-21 10:57:50 +0000215 * @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 Pinitasebf6b8a2018-09-24 16:31:08 +0100233 * @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 Marco05288a22017-11-21 10:57:50 +0000238 */
Gian Marco7b4d5472018-01-10 15:56:30 +0000239__kernel void gemmlowp_mm_midgard(IMAGE_DECLARATION(src0),
240 IMAGE_DECLARATION(src1),
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100241 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 Marco05288a22017-11-21 10:57:50 +0000254{
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 Pinitasebf6b8a2018-09-24 16:31:08 +0100266#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 Marco05288a22017-11-21 10:57:50 +0000307 int end_row_vec_a = src_addr.s0 + COLS_A;
308
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000309 VECTOR_ACC_TYPE acc0 = 0;
Gian Marco05288a22017-11-21 10:57:50 +0000310#if NUM_ELEMS_PROCESSED_PER_THREAD_Y > 1
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000311 VECTOR_ACC_TYPE acc1 = 0;
Gian Marco05288a22017-11-21 10:57:50 +0000312#endif // NUM_ELEMS_PROCESSED_PER_THREAD_Y > 1
313#if NUM_ELEMS_PROCESSED_PER_THREAD_Y > 2
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000314 VECTOR_ACC_TYPE acc2 = 0;
Gian Marco05288a22017-11-21 10:57:50 +0000315#endif // NUM_ELEMS_PROCESSED_PER_THREAD_Y > 2
316#if NUM_ELEMS_PROCESSED_PER_THREAD_Y > 3
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000317 VECTOR_ACC_TYPE acc3 = 0;
Gian Marco05288a22017-11-21 10:57:50 +0000318#endif // NUM_ELEMS_PROCESSED_PER_THREAD_Y > 3
Gian Marco7b4d5472018-01-10 15:56:30 +0000319#if NUM_ELEMS_PROCESSED_PER_THREAD_Y > 4
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000320 VECTOR_ACC_TYPE acc4 = 0;
Gian Marco7b4d5472018-01-10 15:56:30 +0000321#endif // NUM_ELEMS_PROCESSED_PER_THREAD_Y > 4
Gian Marco05288a22017-11-21 10:57:50 +0000322
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 Giorgiof9179d32019-11-27 16:17:30 +0000326 VEC_DATA_TYPE(DATA_TYPE, 2)
327 a0 = vload2(0, (__global DATA_TYPE *)(src0_ptr + src_addr.s0 + 0 * src0_stride_y));
Gian Marco05288a22017-11-21 10:57:50 +0000328#if NUM_ELEMS_PROCESSED_PER_THREAD_Y > 1
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000329 VEC_DATA_TYPE(DATA_TYPE, 2)
330 a1 = vload2(0, (__global DATA_TYPE *)(src0_ptr + src_addr.s0 + 1 * src0_stride_y));
Gian Marco05288a22017-11-21 10:57:50 +0000331#endif // NUM_ELEMS_PROCESSED_PER_THREAD_Y > 1
332#if NUM_ELEMS_PROCESSED_PER_THREAD_Y > 2
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000333 VEC_DATA_TYPE(DATA_TYPE, 2)
334 a2 = vload2(0, (__global DATA_TYPE *)(src0_ptr + src_addr.s0 + 2 * src0_stride_y));
Gian Marco05288a22017-11-21 10:57:50 +0000335#endif // NUM_ELEMS_PROCESSED_PER_THREAD_Y > 2
336#if NUM_ELEMS_PROCESSED_PER_THREAD_Y > 3
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000337 VEC_DATA_TYPE(DATA_TYPE, 2)
338 a3 = vload2(0, (__global DATA_TYPE *)(src0_ptr + src_addr.s0 + 3 * src0_stride_y));
Gian Marco05288a22017-11-21 10:57:50 +0000339#endif // NUM_ELEMS_PROCESSED_PER_THREAD_Y > 3
Gian Marco7b4d5472018-01-10 15:56:30 +0000340#if NUM_ELEMS_PROCESSED_PER_THREAD_Y > 4
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000341 VEC_DATA_TYPE(DATA_TYPE, 2)
342 a4 = vload2(0, (__global DATA_TYPE *)(src0_ptr + src_addr.s0 + 4 * src0_stride_y));
Gian Marco7b4d5472018-01-10 15:56:30 +0000343#endif // NUM_ELEMS_PROCESSED_PER_THREAD_Y > 4
Gian Marco05288a22017-11-21 10:57:50 +0000344 // Load values from matrix B
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000345 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 Marco05288a22017-11-21 10:57:50 +0000347
348 // Accumulate
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000349 acc0 += CONVERT(b0, VECTOR_ACC_TYPE) * (VECTOR_ACC_TYPE)a0.s0;
350 acc0 += CONVERT(b1, VECTOR_ACC_TYPE) * (VECTOR_ACC_TYPE)a0.s1;
Gian Marco05288a22017-11-21 10:57:50 +0000351#if NUM_ELEMS_PROCESSED_PER_THREAD_Y > 1
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000352 acc1 += CONVERT(b0, VECTOR_ACC_TYPE) * (VECTOR_ACC_TYPE)a1.s0;
353 acc1 += CONVERT(b1, VECTOR_ACC_TYPE) * (VECTOR_ACC_TYPE)a1.s1;
Gian Marco05288a22017-11-21 10:57:50 +0000354#endif // NUM_ELEMS_PROCESSED_PER_THREAD_Y > 1
355#if NUM_ELEMS_PROCESSED_PER_THREAD_Y > 2
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000356 acc2 += CONVERT(b0, VECTOR_ACC_TYPE) * (VECTOR_ACC_TYPE)a2.s0;
357 acc2 += CONVERT(b1, VECTOR_ACC_TYPE) * (VECTOR_ACC_TYPE)a2.s1;
Gian Marco05288a22017-11-21 10:57:50 +0000358#endif // NUM_ELEMS_PROCESSED_PER_THREAD_Y > 2
359#if NUM_ELEMS_PROCESSED_PER_THREAD_Y > 3
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000360 acc3 += CONVERT(b0, VECTOR_ACC_TYPE) * (VECTOR_ACC_TYPE)a3.s0;
361 acc3 += CONVERT(b1, VECTOR_ACC_TYPE) * (VECTOR_ACC_TYPE)a3.s1;
Gian Marco05288a22017-11-21 10:57:50 +0000362#endif // NUM_ELEMS_PROCESSED_PER_THREAD_Y > 3
Gian Marco7b4d5472018-01-10 15:56:30 +0000363#if NUM_ELEMS_PROCESSED_PER_THREAD_Y > 4
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000364 acc4 += CONVERT(b0, VECTOR_ACC_TYPE) * (VECTOR_ACC_TYPE)a4.s0;
365 acc4 += CONVERT(b1, VECTOR_ACC_TYPE) * (VECTOR_ACC_TYPE)a4.s1;
Gian Marco7b4d5472018-01-10 15:56:30 +0000366#endif // NUM_ELEMS_PROCESSED_PER_THREAD_Y > 4
Gian Marco05288a22017-11-21 10:57:50 +0000367 }
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 Giorgiof9179d32019-11-27 16:17:30 +0000372 DATA_TYPE a0 = *((__global DATA_TYPE *)(src0_ptr + src_addr.s0 + 0 * src0_stride_y));
Gian Marco05288a22017-11-21 10:57:50 +0000373#if NUM_ELEMS_PROCESSED_PER_THREAD_Y > 1
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000374 DATA_TYPE a1 = *((__global DATA_TYPE *)(src0_ptr + src_addr.s0 + 1 * src0_stride_y));
Gian Marco05288a22017-11-21 10:57:50 +0000375#endif // NUM_ELEMS_PROCESSED_PER_THREAD_Y > 1
376#if NUM_ELEMS_PROCESSED_PER_THREAD_Y > 2
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000377 DATA_TYPE a2 = *((__global DATA_TYPE *)(src0_ptr + src_addr.s0 + 2 * src0_stride_y));
Gian Marco05288a22017-11-21 10:57:50 +0000378#endif // NUM_ELEMS_PROCESSED_PER_THREAD_Y > 2
379#if NUM_ELEMS_PROCESSED_PER_THREAD_Y > 3
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000380 DATA_TYPE a3 = *((__global DATA_TYPE *)(src0_ptr + src_addr.s0 + 3 * src0_stride_y));
Gian Marco05288a22017-11-21 10:57:50 +0000381#endif // NUM_ELEMS_PROCESSED_PER_THREAD_Y > 3
Gian Marco7b4d5472018-01-10 15:56:30 +0000382#if NUM_ELEMS_PROCESSED_PER_THREAD_Y > 4
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000383 DATA_TYPE a4 = *((__global DATA_TYPE *)(src0_ptr + src_addr.s0 + 4 * src0_stride_y));
Gian Marco7b4d5472018-01-10 15:56:30 +0000384#endif // NUM_ELEMS_PROCESSED_PER_THREAD_Y > 4
Gian Marco05288a22017-11-21 10:57:50 +0000385 // Load values from matrix B
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000386 VECTOR_TYPE b0 = VLOAD(NUM_ELEMS_PROCESSED_PER_THREAD_X)(0, (__global DATA_TYPE *)(src1_ptr + src_addr.s1));
Gian Marco05288a22017-11-21 10:57:50 +0000387
388 // Accumulate
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000389 acc0 += CONVERT(b0, VECTOR_ACC_TYPE) * (VECTOR_ACC_TYPE)a0;
Gian Marco05288a22017-11-21 10:57:50 +0000390#if NUM_ELEMS_PROCESSED_PER_THREAD_Y > 1
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000391 acc1 += CONVERT(b0, VECTOR_ACC_TYPE) * (VECTOR_ACC_TYPE)a1;
Gian Marco05288a22017-11-21 10:57:50 +0000392#endif // NUM_ELEMS_PROCESSED_PER_THREAD_Y > 1
393#if NUM_ELEMS_PROCESSED_PER_THREAD_Y > 2
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000394 acc2 += CONVERT(b0, VECTOR_ACC_TYPE) * (VECTOR_ACC_TYPE)a2;
Gian Marco05288a22017-11-21 10:57:50 +0000395#endif // NUM_ELEMS_PROCESSED_PER_THREAD_Y > 2
396#if NUM_ELEMS_PROCESSED_PER_THREAD_Y > 3
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000397 acc3 += CONVERT(b0, VECTOR_ACC_TYPE) * (VECTOR_ACC_TYPE)a3;
Gian Marco05288a22017-11-21 10:57:50 +0000398#endif // NUM_ELEMS_PROCESSED_PER_THREAD_Y > 3
Gian Marco7b4d5472018-01-10 15:56:30 +0000399#if NUM_ELEMS_PROCESSED_PER_THREAD_Y > 4
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000400 acc4 += CONVERT(b0, VECTOR_ACC_TYPE) * (VECTOR_ACC_TYPE)a4;
Gian Marco7b4d5472018-01-10 15:56:30 +0000401#endif // NUM_ELEMS_PROCESSED_PER_THREAD_Y > 4
Gian Marco05288a22017-11-21 10:57:50 +0000402 }
403
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100404 const int z = get_global_id(2);
405
Gian Marco05288a22017-11-21 10:57:50 +0000406 // Compute destination address
407 Image dst = CONVERT_TO_IMAGE_STRUCT(dst);
408
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100409#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 Marco05288a22017-11-21 10:57:50 +0000436 // Store the result
437 VSTORE(NUM_ELEMS_PROCESSED_PER_THREAD_X)
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100438 (CONVERT(acc0, VECTOR_INT), 0, (__global int *)(dst.ptr + 0 * dst_stride_y + zout.s0));
Gian Marco05288a22017-11-21 10:57:50 +0000439#if NUM_ELEMS_PROCESSED_PER_THREAD_Y > 1
440 VSTORE(NUM_ELEMS_PROCESSED_PER_THREAD_X)
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100441 (CONVERT(acc1, VECTOR_INT), 0, (__global int *)(dst.ptr + 1 * dst_stride_y + zout.s1));
Gian Marco05288a22017-11-21 10:57:50 +0000442#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 Pinitasebf6b8a2018-09-24 16:31:08 +0100445 (CONVERT(acc2, VECTOR_INT), 0, (__global int *)(dst.ptr + 2 * dst_stride_y + zout.s2));
Gian Marco05288a22017-11-21 10:57:50 +0000446#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 Pinitasebf6b8a2018-09-24 16:31:08 +0100449 (CONVERT(acc3, VECTOR_INT), 0, (__global int *)(dst.ptr + 3 * dst_stride_y + zout.s3));
Gian Marco05288a22017-11-21 10:57:50 +0000450#endif // NUM_ELEMS_PROCESSED_PER_THREAD_Y > 3
Gian Marco7b4d5472018-01-10 15:56:30 +0000451#if NUM_ELEMS_PROCESSED_PER_THREAD_Y > 4
452 VSTORE(NUM_ELEMS_PROCESSED_PER_THREAD_X)
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100453 (CONVERT(acc4, VECTOR_INT), 0, (__global int *)(dst.ptr + 4 * dst_stride_y + zout.s4));
Gian Marco7b4d5472018-01-10 15:56:30 +0000454#endif // NUM_ELEMS_PROCESSED_PER_THREAD_Y > 4
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100455
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 Marco7b4d5472018-01-10 15:56:30 +0000480}
Gian Marco05288a22017-11-21 10:57:50 +0000481#endif // defined(NUM_ELEMS_PROCESSED_PER_THREAD_X) && defined(NUM_ELEMS_PROCESSED_PER_THREAD_Y) && defined(COLS_A)
482
Gian Marco Iodiceb0c50372019-03-15 10:13:05 +0000483#if defined(M0) && defined(N0) && defined(K0) && defined(V0) && defined(H0) && defined(M) && defined(N)
Sheri Zhang28287af2020-02-25 14:13:54 +0000484/** This OpenCL kernel computes the matrix multiplication between 2 matrices with QASYMM/QASYMM_SIGNED data type.
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000485 * 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 Giorgiof9179d32019-11-27 16:17:30 +0000488 * @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 Iodiceb0c50372019-03-15 10:13:05 +0000490 * @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 Iodicedb63b9c2019-01-17 09:47:04 +0000492 * @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 Iodice62251f72019-03-11 16:07:12 +0000501 * - V0 >= 1
502 * - H0 >= 1
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000503 *
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 *
Sheri Zhang28287af2020-02-25 14:13:54 +0000510 * @param[in] lhs_ptr Pointer to the LHS reshaped matrix. Supported data type: QASYMM8/QASYMM_SIGNED
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000511 * @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
Sheri Zhang28287af2020-02-25 14:13:54 +0000522 * @param[out] dst_ptr Pointer to the destination matrix Supported data type: S32
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000523 * @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 Iodice43a129e2019-05-14 10:14:08 +0100574 uint x = get_global_id(0);
575 uint y = get_global_id(1);
576 uint z = get_global_id(2);
577
Gian Marco Iodiceb0c50372019-03-15 10:13:05 +0000578#if defined(DUMMY_WORK_ITEMS)
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100579 if((x * N0 >= N) || (y * M0 >= M))
Gian Marco Iodiceb0c50372019-03-15 10:13:05 +0000580 {
581 return;
582 }
583#endif // defined(DUMMY_WORK_ITEMS)
584
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000585 // Compute LHS matrix address
Sheri Zhang28287af2020-02-25 14:13:54 +0000586 __global DATA_TYPE *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 Iodicedb63b9c2019-01-17 09:47:04 +0000587
588 // Compute RHS matrix address
Sheri Zhang28287af2020-02-25 14:13:54 +0000589 __global DATA_TYPE *rhs_addr = rhs_ptr + rhs_offset_first_element_in_bytes + (x % H0) * (uint)RHS_OFFSET_X + (x / (uint)H0) * rhs_stride_y;
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000590
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 Iodice43a129e2019-05-14 10:14:08 +0100593 rhs_addr += (z % MATRIX_B_DEPTH) * rhs_stride_z;
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000594#else // defined(MATRIX_B_DEPTH)
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100595 rhs_addr += z * rhs_stride_z;
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000596#endif // defined(MATRIX_B_DEPTH)
597
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100598 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 Iodicedb63b9c2019-01-17 09:47:04 +0000601 // Initialize the accumulators
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000602 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 Iodicedb63b9c2019-01-17 09:47:04 +0000603
604 for(int i = 0; i < k; i += K0)
605 {
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000606 // Load values from LHS matrix
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000607 LOAD_BLOCK(M0, K0, DATA_TYPE, a, lhs_addr, 0, LHS_STEP_X, zlhs);
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000608
609 // Load values from RHS matrix
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000610 LOAD_BLOCK(N0, K0, DATA_TYPE, b, rhs_addr, 0, RHS_STEP_X, zrhs);
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000611
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100612 // Partial matrix multiplication M0,N0,K0
613 ARM_MM_K0XN0XM0(M0, N0, K0, a, b, c);
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000614
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100615 // Update address
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000616 lhs_addr += (M0 * LHS_STEP_X * LHS_STEP_LOOP);
617 rhs_addr += (N0 * RHS_STEP_X * RHS_STEP_LOOP);
618 }
619
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100620 __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 Iodicedb63b9c2019-01-17 09:47:04 +0000621
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 Iodice43a129e2019-05-14 10:14:08 +0100625 // 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 Iodicedb63b9c2019-01-17 09:47:04 +0000627
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 Iodice43a129e2019-05-14 10:14:08 +0100630 dst_addr += z * dst_stride_z * DEPTH_GEMM3D;
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000631
632#else // defined(REINTERPRET_OUTPUT_AS_3D)
633
634 // Add offset for batched GEMM
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100635 dst_addr += z * dst_stride_z;
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000636
637#endif // defined(REINTERPRET_OUTPUT_AS_3D)
638
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100639 // Convert and store output block
640 CONVERT_STORE_BLOCK(M0, N0, int, c, dst_addr, dst_stride_y, zout);
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000641
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 Iodicedb63b9c2019-01-17 09:47:04 +0000649#endif // defined(M0) && defined(N0) && defined(K0) && defined(V0) && defined(H0) && defined(K)
650
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000651#if defined(M0) && defined(N0) && defined(K0) && defined(H0) && defined(K)
652
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000653/** This OpenCL kernel computes the matrix multiplication between 2 matrices.
654 * The LHS matrix is NOT reshaped
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100655 * The RHS matrix is reshaped with @ref CLGEMMReshapeRHSMatrixKernel and the block K0xN0 is transposed
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000656 *
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000657 * @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 Iodice62251f72019-03-11 16:07:12 +0000659 * @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 Giorgiof9179d32019-11-27 16:17:30 +0000677 * @param[in] lhs_ptr Pointer to the LHS reshaped matrix. Supported data type: QASYMM8/QASYMM8_SIGNED
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000678 * @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 Giorgiof9179d32019-11-27 16:17:30 +0000689 * @param[out] dst_ptr Pointer to the destination matrix Supported data type: S32
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000690 * @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 Iodice86cfffe2019-04-02 11:02:20 +0100735#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 Iodice62251f72019-03-11 16:07:12 +0000742 // 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 Iodice43a129e2019-05-14 10:14:08 +0100755 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 Iodice62251f72019-03-11 16:07:12 +0000757
758#if defined(REINTERPRET_INPUT_AS_3D)
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100759 // 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 Iodice62251f72019-03-11 16:07:12 +0000761
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 Giorgiof9179d32019-11-27 16:17:30 +0000774 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 Iodice62251f72019-03-11 16:07:12 +0000775
776 for(int i = 0; i < K; i += K0)
777 {
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000778 // Load values from LHS matrix
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000779 LOAD_BLOCK(M0, K0, DATA_TYPE, a, lhs_ptr, lhs_offset, lhs_stride_y, zlhs);
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000780
781 // Load values from RHS matrix
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000782 LOAD_BLOCK(N0, K0, DATA_TYPE, b, rhs_ptr, rhs_offset, RHS_STEP_X, zrhs);
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000783
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100784 // Partial matrix multiplication M0,N0,K0
785 ARM_MM_K0XN0XM0(M0, N0, K0, a, b, c);
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000786
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 Iodice62251f72019-03-11 16:07:12 +0000796 // The plane (zout) is calculated dividing M (y * M0) by HEIGHT_GEMM3D
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100797 CALCULATE_Z_OFFSET(M0, uint, zout, y, HEIGHT_GEMM3D, DEPTH_GEMM3D, dst_cross_plane_pad, dst_stride_y);
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000798
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 Iodice43a129e2019-05-14 10:14:08 +0100810 // Convert and store output block
811 CONVERT_STORE_BLOCK(M0, N0, int, c, dst_addr, dst_stride_y, zout);
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000812
813#undef RHS_BLOCK_SIZE
814#undef RHS_OFFSET_X
815#undef RHS_STEP_X
816}
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000817
818#if defined(RESULT_OFFSET) && defined(RESULT_SHIFT) && defined(RESULT_MULTIPLIER)
819/** This OpenCL kernel computes the matrix multiplication between 2 matrices with fused output stage using fixed-point arithmetic.
820 * The LHS matrix is NOT reshaped
821 * The RHS matrix is reshaped with @ref CLGEMMReshapeRHSMatrixKernel and the block K0xN0 is transposed
822 *
823 * @note The input data type must be passed at compile time using -DDATA_TYPE (i.e. -DDATA_TYPE=uchar)
824 * @note The accumulator data type must be passed at compile time using -DACC_DATA_TYPE (i.e. -DACC_DATA_TYPE=uint)
825 * @note The number of columns of LHS matrix must be passed at compile time using -DK (i.e. -DK=64)
826 * @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).
827 * @note The number of M0 rows to process must be passed at compile time using -DM0 (i.e. -DM0=2)
828 * @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)
829 * @note If the K0xN0 blocks in the reshaped RHS matrix have been interleaved, the option -DRHS_INTERLEAVE must passed at compile time.
830 * @note Only the following configurations of M0, N0 and K0 are currently supported:
831 * - M0 = 1, 2, 3, 4, 5, 6, 7, 8
832 * - N0 = 2, 3, 4, 8, 16
833 * - K0 = 2, 3, 4, 8, 16
834 * - H0 >= 1
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 * @note 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_MULTIPLIER and -DRESULT_SHIFT
844 * @note In case the addition of int32 biases is required, -DADD_BIAS should be passed at compile time
845 * @note The output datatype should be passed at compile time using -DOUTPUT_DATA_TYPE
846 * @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.
847 * These values can be used to implement "rectified linear unit" activation functions
848 * @note In case of per-channel quantization of matrix B, -DPER_CHANNEL_QUANTIZATION must be passed at compile time.
849 *
850 * @param[in] lhs_ptr Pointer to the LHS reshaped matrix. Supported data type: QASYMM8/QASYMM8_SIGNED
851 * @param[in] lhs_stride_x Stride of the LHS reshaped matrix in X dimension (in bytes)
852 * @param[in] lhs_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
853 * @param[in] lhs_stride_y Stride of the LHS reshaped matrix in Y dimension (in bytes)
854 * @param[in] lhs_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
855 * @param[in] lhs_offset_first_element_in_bytes The offset of the first element in the LHS reshaped matrix
856 * @param[in] rhs_ptr Pointer to the RHS reshaped matrix. Supported data type: same as @p lhs_ptr
857 * @param[in] rhs_stride_x Stride of the RHS reshaped matrix in X dimension (in bytes)
858 * @param[in] rhs_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
859 * @param[in] rhs_stride_y Stride of the RHS reshaped matrix in Y dimension (in bytes)
860 * @param[in] rhs_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
861 * @param[in] rhs_offset_first_element_in_bytes The offset of the first element in the RHS reshaped matrix
862 * @param[out] dst_ptr Pointer to the destination matrix Supported data type: same as @p lhs_ptr
863 * @param[in] dst_stride_x Stride of the destination matrix in X dimension (in bytes)
864 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
865 * @param[in] dst_stride_y Stride of the destination matrix in Y dimension (in bytes)
866 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
867 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination matrix
868 * @param[in] lhs_stride_z Stride of the LHS reshaped matrix in Z dimension (in bytes)
869 * @param[in] rhs_stride_z Stride of the RHS reshaped matrix in Z dimension (in bytes)
870 * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes)
871 * @param[in] lhs_cross_plane_pad (Optional) Bottom paddings for LHS matrix in unit of elements (only if defined REINTERPRET_INPUT_AS_3D)
872 * @param[in] dst_cross_plane_pad (Optional) Bottom paddings for the output matrix in unit of elements (only if defined REINTERPRET_OUTPUT_AS_3D)
873 * @param[in] sum_col_ptr (Optional) Pointer to the source tensor. Supported data type: S32
874 * @param[in] sum_col_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
875 * @param[in] sum_col_step_x (Optional) sum_col_stride_x * number of elements along X processed per workitem(in bytes)
876 * @param[in] sum_col_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
877 * @param[in] sum_col_step_y (Optional) sum_col_stride_y * number of elements along Y processed per workitem(in bytes)
878 * @param[in] sum_col_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
879 * @param[in] sum_row_ptr (Optional) Pointer to the source tensor. Supported data type: S32
880 * @param[in] sum_row_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
881 * @param[in] sum_row_step_x (Optional) sum_row_stride_x * number of elements along X processed per workitem(in bytes)
882 * @param[in] sum_row_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
883 * @param[in] sum_row_step_y (Optional) sum_row_stride_y * number of elements along Y processed per workitem(in bytes)
884 * @param[in] sum_row_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
885 * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: S32
886 * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes)
887 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
888 * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases tensor
889 * @param[in] result_multipliers_ptr (Optional) Pointer to the output multipliers vector for per-channel quantization. Supported data types: S32
890 * @param[in] result_multipliers_stride_x (Optional) Stride of the output multipliers vector in X dimension (in bytes)
891 * @param[in] result_multipliers_step_x (Optional) output_multipliers_stride_x * number of elements along X processed per workitem(in bytes)
892 * @param[in] result_multipliers_offset_first_element_in_bytes (Optional) The offset of the first element in the output multipliers vector
893 * @param[in] result_shifts_ptr (Optional) Pointer to the output shifts vector for per-channel quantization. Supported data types: S32
894 * @param[in] result_shifts_stride_x (Optional) Stride of the output shifts vector in X dimension (in bytes)
895 * @param[in] result_shifts_step_x (Optional) output_shifts_stride_x * number of elements along X processed per workitem(in bytes)
896 * @param[in] result_shifts_offset_first_element_in_bytes (Optional) The offset of the first element in the output shifts vector
897 */
898__kernel void gemmlowp_mm_reshaped_only_rhs_t_fused_output_stage_fixedpoint(IMAGE_DECLARATION(lhs),
899 IMAGE_DECLARATION(rhs),
900 IMAGE_DECLARATION(dst),
901 uint lhs_stride_z,
902 uint rhs_stride_z,
903 uint dst_stride_z
904#if defined(REINTERPRET_INPUT_AS_3D)
905 ,
906 uint lhs_cross_plane_pad
907#endif // REINTERPRET_INPUT_AS_3D
908#if defined(REINTERPRET_OUTPUT_AS_3D)
909 ,
910 uint dst_cross_plane_pad
911#endif // REINTERPRET_OUTPUT_AS_3D
912#if defined(A_OFFSET)
913 ,
914 IMAGE_DECLARATION(sum_col)
915#endif // defined(A_OFFSET)
916#if defined(B_OFFSET)
917 ,
918 IMAGE_DECLARATION(sum_row)
919#endif // defined(B_OFFSET)
920#if defined(ADD_BIAS)
921 ,
922 VECTOR_DECLARATION(biases)
923#endif // defined(ADD_BIAS)
924#if defined(PER_CHANNEL_QUANTIZATION)
925 ,
926 VECTOR_DECLARATION(result_multipliers),
927 VECTOR_DECLARATION(result_shifts)
928#endif // defined(PER_CHANNEL_QUANTIZATION)
929 )
930{
931 // Block size
932#define RHS_BLOCK_SIZE ((K0) * (N0))
933
934 // RHS offset and step X
935#if defined(RHS_INTERLEAVE)
936#define RHS_OFFSET_X (K0)
937#define RHS_STEP_X ((K0) * (H0))
938#define RHS_STEP_LOOP (1)
939#else // defined(RHS_INTERLEAVE)
940#define RHS_OFFSET_X (RHS_BLOCK_SIZE)
941#define RHS_STEP_X (K0)
942#define RHS_STEP_LOOP (H0)
943#endif // defined(RHS_INTERLEAVE)
944
945 uint x = get_global_id(0);
946 uint y = get_global_id(1);
947 uint z = get_global_id(2);
948
949#if defined(DUMMY_WORK_ITEMS)
950 if((x * N0 >= N) || (y * M0 >= M))
951 {
952 return;
953 }
954#endif // defined(DUMMY_WORK_ITEMS)
955
956 // Compute LHS matrix address
957 uint lhs_offset = lhs_offset_first_element_in_bytes + y * M0 * (uint)lhs_stride_y;
958
959 // Compute RHS matrix address
960 uint rhs_offset = rhs_offset_first_element_in_bytes + (x % H0) * (uint)RHS_OFFSET_X + (x / (uint)H0) * rhs_stride_y;
961
962#if defined(MATRIX_B_DEPTH)
963 // Do not slide matrix B if the matrix B has 3 dimensions and matrix A more than 3
964 rhs_offset += (z % MATRIX_B_DEPTH) * rhs_stride_z;
965#else // defined(MATRIX_B_DEPTH)
966 rhs_offset += z * rhs_stride_z;
967#endif // defined(MATRIX_B_DEPTH)
968
969 REPEAT_VAR_INIT_TO_CONST(8, uint, zlhs, 0); //uint zout0=0,zout1=0,zout2=0,... zout7=0;
970 REPEAT_VAR_INIT_TO_CONST(16, uint, zrhs, 0);
971
972#if defined(REINTERPRET_INPUT_AS_3D)
973 // The plane (zlhs) is calculated dividing M (y * M0) by HEIGHT_GEMM3D
974 CALCULATE_Z_OFFSET(M0, uint, zlhs, y, HEIGHT_GEMM3D, DEPTH_GEMM3D, lhs_cross_plane_pad, lhs_stride_y);
975
976 // Add offset for batched GEMM. The batches will be in the fourth dimension and for this reason we
977 // multiply lhs_stride_z by DEPTH_GEMM3D
978 lhs_offset += z * lhs_stride_z * DEPTH_GEMM3D;
979
980#else // defined(REINTERPRET_INPUT_AS_3D)
981
982 // Add offset for batched GEMM
983 lhs_offset += z * lhs_stride_z;
984
985#endif // defined(REINTERPRET_INPUT_AS_3D)
986
987 // Initialize the accumulators
988 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;
989
990 for(int i = 0; i < K; i += K0)
991 {
992 // Load values from LHS matrix
993 LOAD_BLOCK(M0, K0, DATA_TYPE, a, lhs_ptr, lhs_offset, lhs_stride_y, zlhs);
994
995 // Load values from RHS matrix
996 LOAD_BLOCK(N0, K0, DATA_TYPE, b, rhs_ptr, rhs_offset, RHS_STEP_X, zrhs);
997
998 // Partial matrix multiplication M0,N0,K0
999 ARM_MM_K0XN0XM0(M0, N0, K0, a, b, c);
1000
1001 lhs_offset += K0;
1002 rhs_offset += N0 * RHS_STEP_X * RHS_STEP_LOOP;
1003 }
1004
1005 // Result of MM is of type DATA_TYPE
1006 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + (x * (uint)N0) * sizeof(DATA_TYPE) + (y * (uint)M0 * dst_stride_y);
1007
1008 REPEAT_VAR_INIT_TO_CONST(8, uint, zout, 0); //uint zout0=0,zout1=0,zout2=0,... zout7=0;
1009
1010#if defined(REINTERPRET_OUTPUT_AS_3D)
1011 // The plane (zout) is calculated dividing M (y * M0) by HEIGHT_GEMM3D
1012 CALCULATE_Z_OFFSET(M0, uint, zout, y, HEIGHT_GEMM3D, DEPTH_GEMM3D, dst_cross_plane_pad, dst_stride_y);
1013
1014 // Add offset for batched GEMM. The batches will be in the fourth dimension and for this reason we
1015 // multiply dst_stride_z by DEPTH_GEMM3D
1016 dst_addr += z * dst_stride_z * DEPTH_GEMM3D;
1017
1018#else // defined(REINTERPRET_OUTPUT_AS_3D)
1019
1020 // Add offset for batched GEMM
1021 dst_addr += z * dst_stride_z;
1022
1023#endif // defined(REINTERPRET_OUTPUT_AS_3D)
1024
1025 // Convert result of matrix multiplication to S32
1026 REPEAT_VAR_INIT_CONVERT_SAT(M0, VEC_DATA_TYPE(int, N0), c, c_int);
1027
1028 int batch_id = z;
1029#if defined(DEPTH_GEMM3D)
1030 batch_id /= (int)DEPTH_GEMM3D;
1031#endif // defined(DEPTH_GEMM3D)
1032
1033 // Offset contribution: c += (A_OFFSET * sum_col) + (B_OFFSET * sum_row) + K_OFFSET;
1034 REPEAT_VAR_INIT_TO_CONST(M0, VEC_DATA_TYPE(int, N0), offset_s32_, K_OFFSET);
1035
1036#if defined(A_OFFSET)
1037 // Compute the offset contribution due to A_OFFSET
1038 __global uchar *sum_col_addr = sum_col_ptr + sum_col_offset_first_element_in_bytes + (x * (uint)N0) * sizeof(int);
1039
1040#if defined(SUM_COL_HAS_BATCHES)
1041 sum_col_addr += z * sum_col_stride_y;
1042#endif // defined(SUM_COL_HAS_BATCHES)
1043 VEC_DATA_TYPE(int, N0)
1044 a_offset_s32 = VLOAD(N0)(0, (__global int *)sum_col_addr);
1045 a_offset_s32 *= (VEC_DATA_TYPE(int, N0))A_OFFSET;
1046
1047 REPEAT_ADD_VECTOR_TO_VAR(M0, offset_s32_, a_offset_s32);
1048#endif // defined(A_OFFSET)
1049
1050#if defined(B_OFFSET)
1051 // Compute the offset contribution due to B_OFFSET
1052 __global uchar *sum_row_addr = sum_row_ptr + sum_row_offset_first_element_in_bytes + (y * (uint)M0) * sizeof(int) + z * sum_row_stride_y;
1053
1054#if defined(HEIGHT_GEMM3D) && defined(DEPTH_GEMM3D)
1055 sum_row_addr += (batch_id % (int)DEPTH_GEMM3D) * (int)HEIGHT_GEMM3D * sizeof(int);
1056#endif // defined(HEIGHT_GEMM3D) && defined(DEPTH_GEMM3D)
1057 LOAD_SCALAR_AS_VECTOR(M0, N0, int, b_offset_s32_, sum_row_addr, 0, sum_row_stride_x);
1058
1059 REPEAT_MLA_VAR_WITH_CONST_VEC(M0, offset_s32_, b_offset_s32_, (VEC_DATA_TYPE(int, N0))B_OFFSET);
1060#endif // defined(B_OFFSET)
1061
1062#if defined(ADD_BIAS)
1063 // Add bias
1064 __global uchar *bias_addr = biases_ptr + biases_offset_first_element_in_bytes + (x * (uint)N0) * sizeof(int);
1065
1066 VEC_DATA_TYPE(int, N0)
1067 bias_values = VLOAD(N0)(0, (__global int *)bias_addr);
1068 REPEAT_ADD_VECTOR_TO_VAR(M0, offset_s32_, bias_values);
1069#endif // defined(ADD_BIAS)
1070
1071 REPEAT_ADD_TWO_VARS(M0, c_int, offset_s32_);
1072
1073 // Multiply by result_mult_int and shift
1074#if defined(PER_CHANNEL_QUANTIZATION)
1075 __global uchar *result_multipliers_addr = result_multipliers_ptr + result_multipliers_offset_first_element_in_bytes + (x * (uint)N0) * sizeof(int);
1076 __global uchar *result_shifts_addr = result_shifts_ptr + result_shifts_offset_first_element_in_bytes + (x * (uint)N0) * sizeof(int);
1077
1078 VEC_DATA_TYPE(int, N0)
1079 res_mul = VLOAD(N0)(0, (__global int *)result_multipliers_addr);
1080 VEC_DATA_TYPE(int, N0)
1081 res_shift = VLOAD(N0)(0, (__global int *)result_shifts_addr);
1082
1083 REPEAT_ASYMM_MULT_BY_QUANT_MULTIPLIER_PER_CHANNEL(M0, N0, c_int, res_mul, res_shift);
1084#else // defined(PER_CHANNEL_QUANTIZATION)
1085
1086#if RESULT_SHIFT < 0
1087 REPEAT_ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(M0, N0, c_int, RESULT_MULTIPLIER, RESULT_SHIFT);
1088#else // RESULT_SHIFT >= 0
1089 REPEAT_ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(M0, N0, c_int, RESULT_MULTIPLIER, RESULT_SHIFT);
1090#endif // RESULT_SHIFT < 0
1091
1092#endif // defined(PER_CHANNEL_QUANTIZATION)
1093
1094 // Add the offset terms to GEMM's result
1095 REPEAT_ADD_CONST_TO_VAR(M0, VEC_DATA_TYPE(int, N0), c_int, RESULT_OFFSET);
1096
1097#if defined(MIN_BOUND)
1098 REPEAT_MAX_CONST_VAR(M0, VEC_DATA_TYPE(int, N0), c_int, MIN_BOUND);
1099#endif // defined(MIN_BOUND)
1100#if defined(MAX_BOUND)
1101 REPEAT_MIN_CONST_VAR(M0, VEC_DATA_TYPE(int, N0), c_int, MAX_BOUND);
1102#endif // defined(MAX_BOUND)
1103
1104 // Convert and store output block (does convert saturate)
1105 CONVERT_STORE_BLOCK(M0, N0, DATA_TYPE, c_int, dst_addr, dst_stride_y, zout);
1106
1107#undef RHS_BLOCK_SIZE
1108#undef RHS_OFFSET_X
1109#undef RHS_STEP_X
1110}
1111#endif // defined(RESULT_OFFSET) && defined(RESULT_SHIFT) && defined(RESULT_MULTIPLIER)
Gian Marco Iodice62251f72019-03-11 16:07:12 +00001112#endif // defined(M0) && defined(N0) && defined(K0) && defined(H0) && defined(DATA_TYPE) && defined(K)
1113
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001114#if defined(M0) && defined(N0) && defined(K0) && defined(K)
1115
1116/** This OpenCL kernel computes the matrix multiplication between 2 matrices.
1117 * The LHS matrix is NOT reshaped
1118 * The RHS matrix is NOT reshaped
1119 *
Michele Di Giorgiof9179d32019-11-27 16:17:30 +00001120 * @note The input data type must be passed at compile time using -DDATA_TYPE (i.e. -DDATA_TYPE=uchar)
1121 * @note The accumulator data type must be passed at compile time using -DACC_DATA_TYPE (i.e. -DACC_DATA_TYPE=uint)
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001122 * @note The number of columns of LHS matrix must be passed at compile time using -DK (i.e. -DK=64)
1123 * @note The number of M0 rows to process must be passed at compile time using -DM0 (i.e. -DM0=2)
1124 * @note The number of N0 columns to process must be passed at compile time using -DN0 (i.e. -DN0=2)
1125 * @note The number of K0 partial accumulations must be passed at compile time using -DK0 (i.e., -DK0=2)
1126 * @note Only the following configurations of M0, N0 and K0 are currently supported:
1127 * - M0 = 1, 2, 3, 4, 5, 6, 7, 8
1128 * - N0 = 2, 3, 4, 8, 16
1129 * - K0 = 2, 3, 4, 8, 16
1130 *
1131 * @note In case the input or output have to be reinterpreted as a 3D tensor, the following information must be passed at compile time:
1132 * -# REINTERPRET_INPUT_AS_3D: To reinterpret the input as 3D
1133 * -# REINTERPRET_OUTPUT_AS_3D: To reinterpret the output as 3D
1134 * -# HEIGHT_GEMM3D: The height of the output in case it has to be reinterpreted as a 3D tensor.
1135 * -# DEPTH_GEMM3D: The depth of the output in case it has to be reinterpreted as a 3D tensor
1136 * (HEIGHT_GEMM3D * DEPTH_GEMM3D) = columns LHS matrix
1137 *
Michele Di Giorgiob54ba282020-01-14 15:31:55 +00001138 * @param[in] lhs_ptr Pointer to the LHS reshaped matrix. Supported data type: QASYMM8
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001139 * @param[in] lhs_stride_x Stride of the LHS reshaped matrix in X dimension (in bytes)
1140 * @param[in] lhs_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
1141 * @param[in] lhs_stride_y Stride of the LHS reshaped matrix in Y dimension (in bytes)
1142 * @param[in] lhs_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
1143 * @param[in] lhs_offset_first_element_in_bytes The offset of the first element in the LHS reshaped matrix
1144 * @param[in] rhs_ptr Pointer to the RHS reshaped matrix. Supported data type: same as @p lhs_ptr
1145 * @param[in] rhs_stride_x Stride of the RHS reshaped matrix in X dimension (in bytes)
1146 * @param[in] rhs_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
1147 * @param[in] rhs_stride_y Stride of the RHS reshaped matrix in Y dimension (in bytes)
1148 * @param[in] rhs_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
1149 * @param[in] rhs_offset_first_element_in_bytes The offset of the first element in the RHS reshaped matrix
Michele Di Giorgiob54ba282020-01-14 15:31:55 +00001150 * @param[out] dst_ptr Pointer to the destination matrix Supported data type: S32
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001151 * @param[in] dst_stride_x Stride of the destination matrix in X dimension (in bytes)
1152 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
1153 * @param[in] dst_stride_y Stride of the destination matrix in Y dimension (in bytes)
1154 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
1155 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination matrix
1156 * @param[in] lhs_stride_z Stride of the LHS reshaped matrix in Z dimension (in bytes)
1157 * @param[in] rhs_stride_z Stride of the RHS reshaped matrix in Z dimension (in bytes)
1158 * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes)
1159 * @param[in] lhs_cross_plane_pad (Optional) Bottom paddings for LHS matrix in unit of elements (only if defined REINTERPRET_INPUT_AS_3D)
1160 * @param[in] dst_cross_plane_pad (Optional) Bottom paddings for the output matrix in unit of elements (only if defined REINTERPRET_OUTPUT_AS_3D)
1161 */
1162__kernel void gemmlowp_mm_native(IMAGE_DECLARATION(lhs),
1163 IMAGE_DECLARATION(rhs),
1164 IMAGE_DECLARATION(dst),
1165 uint lhs_stride_z,
1166 uint rhs_stride_z,
1167 uint dst_stride_z
1168#if defined(REINTERPRET_INPUT_AS_3D)
1169 ,
1170 uint lhs_cross_plane_pad
1171#endif // REINTERPRET_INPUT_AS_3D
1172#if defined(REINTERPRET_OUTPUT_AS_3D)
1173 ,
1174 uint dst_cross_plane_pad
1175#endif // REINTERPRET_OUTPUT_AS_3D
1176 )
1177{
1178 uint x = get_global_id(0);
1179 uint y = get_global_id(1);
1180 uint z = get_global_id(2);
1181
1182#if defined(DUMMY_WORK_ITEMS)
1183 if((x * N0 >= N) || (y * M0 >= M))
1184 {
1185 return;
1186 }
1187#endif // defined(DUMMY_WORK_ITEMS)
1188
1189 // Compute LHS matrix address
1190 uint lhs_offset = lhs_offset_first_element_in_bytes + y * M0 * (uint)lhs_stride_y;
1191
1192 // Compute RHS matrix address
1193 uint rhs_offset = rhs_offset_first_element_in_bytes + x * N0;
1194
1195#if defined(MATRIX_B_DEPTH)
1196 // Do not slide matrix B if the matrix B has 3 dimensions and matrix A more than 3
1197 rhs_offset += (z % MATRIX_B_DEPTH) * rhs_stride_z;
1198#else // defined(MATRIX_B_DEPTH)
1199 rhs_offset += z * rhs_stride_z;
1200#endif // defined(MATRIX_B_DEPTH)
1201
1202 REPEAT_VAR_INIT_TO_CONST(8, uint, zlhs, 0);
1203 REPEAT_VAR_INIT_TO_CONST(16, uint, zrhs, 0);
1204
1205#if defined(REINTERPRET_INPUT_AS_3D)
1206 // The plane (zlhs) is calculated dividing M (y * M0) by HEIGHT_GEMM3D
1207 CALCULATE_Z_OFFSET(M0, uint, zlhs, y, HEIGHT_GEMM3D, DEPTH_GEMM3D, lhs_cross_plane_pad, lhs_stride_y);
1208
1209 // Add offset for batched GEMM. The batches will be in the fourth dimension and for this reason we
1210 // multiply lhs_stride_z by DEPTH_GEMM3D
1211 lhs_offset += z * lhs_stride_z * DEPTH_GEMM3D;
1212
1213#else // defined(REINTERPRET_INPUT_AS_3D)
1214
1215 // Add offset for batched GEMM
1216 lhs_offset += z * lhs_stride_z;
1217
1218#endif // defined(REINTERPRET_INPUT_AS_3D)
1219
1220 // Initialize the accumulators
Michele Di Giorgiof9179d32019-11-27 16:17:30 +00001221 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 Iodicee7510622019-06-03 17:28:17 +01001222
1223 int i = 0;
1224
1225 for(; i <= (K - K0); i += K0)
1226 {
1227 // Load values from LHS matrix
Michele Di Giorgiof9179d32019-11-27 16:17:30 +00001228 LOAD_BLOCK(M0, K0, DATA_TYPE, a, lhs_ptr, lhs_offset, lhs_stride_y, zlhs);
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001229
1230 // Load values from RHS matrix
Michele Di Giorgiof9179d32019-11-27 16:17:30 +00001231 LOAD_BLOCK(K0, N0, DATA_TYPE, b, rhs_ptr, rhs_offset, rhs_stride_y, zrhs);
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001232
1233 // Transpose the values from RHS matrix
Michele Di Giorgiof9179d32019-11-27 16:17:30 +00001234 TRANSPOSE_K0XN0(K0, N0, b_t, b, DATA_TYPE);
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001235
1236 // Partial matrix multiplication M0,N0,K0
1237 ARM_MM_K0XN0XM0(M0, N0, K0, a, b_t, c);
1238
1239 // Update the offset
1240 lhs_offset += K0;
1241 rhs_offset += K0 * rhs_stride_y;
1242 }
1243
1244 // Left-over for loop
1245 for(; i < K; ++i)
1246 {
1247 // Load values from LHS matrix
Michele Di Giorgiof9179d32019-11-27 16:17:30 +00001248 LOAD_BLOCK(M0, 1, DATA_TYPE, a, lhs_ptr, lhs_offset, lhs_stride_y, zlhs);
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001249
1250 // Load values from RHS matrix
Michele Di Giorgiof9179d32019-11-27 16:17:30 +00001251 LOAD_BLOCK(1, N0, DATA_TYPE, b, rhs_ptr, rhs_offset, rhs_stride_y, zrhs);
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001252
1253 // Transpose the values from RHS matrix
Michele Di Giorgiof9179d32019-11-27 16:17:30 +00001254 TRANSPOSE_K0XN0(1, N0, b_t, b, DATA_TYPE);
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001255
1256 // Partial matrix multiplication M0,N0,1
1257 ARM_MM_K0XN0XM0(M0, N0, 1, a, b_t, c);
1258
1259 // Update the offset
1260 lhs_offset += 1;
1261 rhs_offset += rhs_stride_y;
1262 }
1263
1264 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + (x * (uint)N0) * sizeof(int) + (y * (uint)M0 * dst_stride_y);
1265
1266 REPEAT_VAR_INIT_TO_CONST(M0, uint, zout, 0); //uint zout0=0,zout1=0,zout2=0,... zout7=0;
1267
1268#if defined(REINTERPRET_OUTPUT_AS_3D)
1269 // The plane (zout) is calculated dividing M (y * M0) by HEIGHT_GEMM3D
1270 CALCULATE_Z_OFFSET(M0, uint, zout, y, HEIGHT_GEMM3D, DEPTH_GEMM3D, dst_cross_plane_pad, dst_stride_y);
1271
1272 // Add offset for batched GEMM. The batches will be in the fourth dimension and for this reason we
1273 // multiply dst_stride_z by DEPTH_GEMM3D
1274 dst_addr += z * dst_stride_z * DEPTH_GEMM3D;
1275
1276#else // defined(REINTERPRET_OUTPUT_AS_3D)
1277
1278 // Add offset for batched GEMM
1279 dst_addr += z * dst_stride_z;
1280
1281#endif // defined(REINTERPRET_OUTPUT_AS_3D)
1282
1283 // Convert and store output block
1284 CONVERT_STORE_BLOCK(M0, N0, int, c, dst_addr, dst_stride_y, zout);
1285}
1286#endif // defined(M0) && defined(N0) && defined(K0) && defined(K)
1287
Gian Marco05288a22017-11-21 10:57:50 +00001288#if defined(COLS_A)
1289/** OpenCL kernel used to compute the row-vectors of sums of all the entries in each row of Matrix A.
Michele Di Giorgiof64d3362020-04-03 12:40:10 +01001290 * It is also possible to multiply each reduced row by a scalar value, if SCALAR is passed at compile time.
Gian Marco05288a22017-11-21 10:57:50 +00001291 *
1292 * @note This stage is needed to handle the offset of matrix product
1293 * https://github.com/google/gemmlowp/blob/master/doc/low-precision.md
1294 *
1295 * @attention The number of matrix A columns needs to be passed at compile time using -DCOLS_A
Manuel Bottini959c26d2019-12-02 16:22:35 +00001296 * @note The input data type must be passed at compile time using -DDATA_TYPE (i.e. -DDATA_TYPE=uchar)
Michele Di Giorgioe7b333e2020-01-15 10:30:51 +00001297 * @note The data type for the accumulation must be passed at compile time using -DACC_DATA_TYPE (i.e. -DACC_DATA_TYPE=uint)
Michele Di Giorgiof64d3362020-04-03 12:40:10 +01001298 * @note In case of scaling the scalar value must be passed at compile time using -DSCALAR (e.g. -DSCALAR=3)
Gian Marco05288a22017-11-21 10:57:50 +00001299 *
Michele Di Giorgiof64d3362020-04-03 12:40:10 +01001300 * @param[in] src_ptr Pointer to the source tensor. Supported data type: QASYMM8/QASYMM8_SIGNED
Gian Marco05288a22017-11-21 10:57:50 +00001301 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
1302 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
1303 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
1304 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
1305 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
1306 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1307 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
1308 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: S32
1309 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
1310 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
1311 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
1312 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
1313 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
1314 */
1315__kernel void gemmlowp_matrix_a_reduction(TENSOR3D_DECLARATION(src),
1316 IMAGE_DECLARATION(dst))
1317{
1318 // Compute source and destination addresses
1319 Tensor3D src = CONVERT_TO_TENSOR3D_STRUCT(src);
1320 Image dst = CONVERT_TO_IMAGE_STRUCT(dst);
1321
Michele Di Giorgioe7b333e2020-01-15 10:30:51 +00001322 VEC_DATA_TYPE(ACC_DATA_TYPE, 4)
1323 sum_row_32 = (VEC_DATA_TYPE(ACC_DATA_TYPE, 4))0;
1324 ACC_DATA_TYPE sum_row = 0;
Gian Marco05288a22017-11-21 10:57:50 +00001325
Manuel Bottini959c26d2019-12-02 16:22:35 +00001326 __global const DATA_TYPE *matrix_a = (__global const DATA_TYPE *)(src.ptr + get_global_id(0) * src_stride_y + get_global_id(1) * src_stride_z);
Gian Marco05288a22017-11-21 10:57:50 +00001327
1328 int i = 0;
1329
1330 // This for loop performs 16 accumulations
1331 for(; i <= ((int)COLS_A - 16); i += 16)
1332 {
Manuel Bottini959c26d2019-12-02 16:22:35 +00001333 const VEC_DATA_TYPE(DATA_TYPE, 16) a0 = vload16(0, matrix_a + i);
Gian Marco05288a22017-11-21 10:57:50 +00001334
Michele Di Giorgioe7b333e2020-01-15 10:30:51 +00001335 sum_row_32 += CONVERT(a0.s0123, VEC_DATA_TYPE(ACC_DATA_TYPE, 4)) + CONVERT(a0.s4567, VEC_DATA_TYPE(ACC_DATA_TYPE, 4)) + CONVERT(a0.s89AB, VEC_DATA_TYPE(ACC_DATA_TYPE, 4)) + CONVERT(a0.sCDEF,
1336 VEC_DATA_TYPE(ACC_DATA_TYPE, 4));
Gian Marco05288a22017-11-21 10:57:50 +00001337 }
1338
1339 // This for loop performs the leftover accumulations
1340 for(; i < COLS_A; ++i)
1341 {
Michele Di Giorgioe7b333e2020-01-15 10:30:51 +00001342 sum_row += (ACC_DATA_TYPE)matrix_a[i];
Gian Marco05288a22017-11-21 10:57:50 +00001343 }
1344
Manuel Bottini959c26d2019-12-02 16:22:35 +00001345 sum_row += sum_row_32.s0 + sum_row_32.s1 + sum_row_32.s2 + sum_row_32.s3;
Gian Marco05288a22017-11-21 10:57:50 +00001346
Michele Di Giorgiof64d3362020-04-03 12:40:10 +01001347#if defined(SCALAR)
1348 sum_row *= (int)SCALAR;
1349#endif // defined(SCALAR)
Gian Marco05288a22017-11-21 10:57:50 +00001350 *((__global int *)dst.ptr) = (int)sum_row;
1351}
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001352
1353#if defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8)
Michele Di Giorgiof64d3362020-04-03 12:40:10 +01001354/** 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.
1355 * It is also possible to multiply each reduced row by a scalar value, if SCALAR is passed at compile time.
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001356 *
1357 * @note This stage is needed to handle the offset of matrix product
1358 * https://github.com/google/gemmlowp/blob/master/doc/low-precision.md
1359 *
1360 * @attention The number of matrix A columns needs to be passed at compile time using -DCOLS_A
Manuel Bottini959c26d2019-12-02 16:22:35 +00001361 * @note The input data type must be passed at compile time using -DDATA_TYPE (i.e. -DDATA_TYPE=uchar)
Michele Di Giorgioe7b333e2020-01-15 10:30:51 +00001362 * @note The data type for the accumulation must be passed at compile time using -DACC_DATA_TYPE (i.e. -DACC_DATA_TYPE=uint)
Michele Di Giorgiof64d3362020-04-03 12:40:10 +01001363 * @note In case of scaling the scalar value must be passed at compile time using -DSCALAR (e.g. -DSCALAR=3)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001364 *
Michele Di Giorgiof64d3362020-04-03 12:40:10 +01001365 * @param[in] src_ptr Pointer to the source tensor. Supported data type: QASYMM8/QASYMM8_SIGNED
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001366 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
1367 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
1368 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
1369 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
1370 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
1371 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1372 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
1373 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: S32
1374 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
1375 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
1376 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
1377 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
1378 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
1379 */
1380__kernel void gemmlowp_matrix_a_reduction_dot8(TENSOR3D_DECLARATION(src),
1381 IMAGE_DECLARATION(dst))
1382{
1383 // Compute source and destination addresses
1384 Tensor3D src = CONVERT_TO_TENSOR3D_STRUCT(src);
1385 Image dst = CONVERT_TO_IMAGE_STRUCT(dst);
1386
Michele Di Giorgioe7b333e2020-01-15 10:30:51 +00001387 ACC_DATA_TYPE sum_row = 0;
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001388
Manuel Bottini959c26d2019-12-02 16:22:35 +00001389 __global const DATA_TYPE *matrix_a = (__global const DATA_TYPE *)(src.ptr + get_global_id(0) * src_stride_y + get_global_id(1) * src_stride_z);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001390
1391 int i = 0;
1392
1393 // This for loop performs 16 accumulations
1394 for(; i <= ((int)COLS_A - 32); i += 32)
1395 {
Manuel Bottini959c26d2019-12-02 16:22:35 +00001396 VEC_DATA_TYPE(DATA_TYPE, 16)
1397 a0 = vload16(0, matrix_a + i);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001398
Manuel Bottini959c26d2019-12-02 16:22:35 +00001399 sum_row += arm_dot(a0.s0123, (VEC_DATA_TYPE(DATA_TYPE, 4))(1));
1400 sum_row += arm_dot(a0.s4567, (VEC_DATA_TYPE(DATA_TYPE, 4))(1));
1401 sum_row += arm_dot(a0.s89AB, (VEC_DATA_TYPE(DATA_TYPE, 4))(1));
1402 sum_row += arm_dot(a0.sCDEF, (VEC_DATA_TYPE(DATA_TYPE, 4))(1));
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001403
Manuel Bottini959c26d2019-12-02 16:22:35 +00001404 a0 = vload16(1, matrix_a + i);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001405
Manuel Bottini959c26d2019-12-02 16:22:35 +00001406 sum_row += arm_dot(a0.s0123, (VEC_DATA_TYPE(DATA_TYPE, 4))(1));
1407 sum_row += arm_dot(a0.s4567, (VEC_DATA_TYPE(DATA_TYPE, 4))(1));
1408 sum_row += arm_dot(a0.s89AB, (VEC_DATA_TYPE(DATA_TYPE, 4))(1));
1409 sum_row += arm_dot(a0.sCDEF, (VEC_DATA_TYPE(DATA_TYPE, 4))(1));
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001410 }
1411
1412 // This for loop performs the leftover accumulations
1413 for(; i < COLS_A; ++i)
1414 {
Michele Di Giorgioe7b333e2020-01-15 10:30:51 +00001415 sum_row += (ACC_DATA_TYPE)matrix_a[i];
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001416 }
1417
Michele Di Giorgiof64d3362020-04-03 12:40:10 +01001418#if defined(SCALAR)
1419 sum_row *= (int)SCALAR;
1420#endif // defined(SCALAR)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001421 *((__global int *)dst.ptr) = (int)sum_row;
1422}
1423#endif // defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8)
Gian Marco05288a22017-11-21 10:57:50 +00001424#endif // defined(COLS_A)
1425
1426#if defined(COLS_B) && defined(ROWS_B)
1427/** OpenCL kernel used to compute the row-vectors of sums of all the entries in each column of Matrix B.
Michele Di Giorgiof64d3362020-04-03 12:40:10 +01001428 * It is also possible to multiply each reduced column by a scalar value, if SCALAR is passed at compile time.
Gian Marco05288a22017-11-21 10:57:50 +00001429 *
1430 * @note This stage is needed to handle the offset of matrix product
1431 * https://github.com/google/gemmlowp/blob/master/doc/low-precision.md
1432 *
1433 * @attention The number of matrix B columns and rows needs to be passed at compile time using -DCOLS_B and -DROWS_B
Manuel Bottini959c26d2019-12-02 16:22:35 +00001434 * @note The input data type must be passed at compile time using -DDATA_TYPE (i.e. -DDATA_TYPE=uchar)
Michele Di Giorgioe7b333e2020-01-15 10:30:51 +00001435 * @note The data type for the accumulation must be passed at compile time using -DACC_DATA_TYPE (i.e. -DACC_DATA_TYPE=uint)
Michele Di Giorgiof64d3362020-04-03 12:40:10 +01001436 * @note In case of scaling the scalar value must be passed at compile time using -DSCALAR (i.e. -DSCALAR=3)
Gian Marco05288a22017-11-21 10:57:50 +00001437 *
Michele Di Giorgiof64d3362020-04-03 12:40:10 +01001438 * @param[in] src_ptr Pointer to the source tensor. Supported data type: QASYMM8/QASYMM8_SIGNED
Gian Marco05288a22017-11-21 10:57:50 +00001439 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
1440 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
1441 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
1442 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
1443 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
1444 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1445 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
1446 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: S32
1447 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
1448 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
1449 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
1450 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
1451 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
1452 */
1453__kernel void gemmlowp_matrix_b_reduction(TENSOR3D_DECLARATION(src),
1454 IMAGE_DECLARATION(dst))
1455{
1456 // Compute source and destination addresses
1457 Tensor3D src = CONVERT_TO_TENSOR3D_STRUCT(src);
1458 Image dst = CONVERT_TO_IMAGE_STRUCT(dst);
1459
Michele Di Giorgioe7b333e2020-01-15 10:30:51 +00001460 VEC_DATA_TYPE(ACC_DATA_TYPE, 16)
1461 sum_col_32 = (VEC_DATA_TYPE(ACC_DATA_TYPE, 16))0;
Gian Marco05288a22017-11-21 10:57:50 +00001462
Manuel Bottini959c26d2019-12-02 16:22:35 +00001463 __global const DATA_TYPE *matrix_b = (__global const DATA_TYPE *)(src.ptr + get_global_id(1) * src_stride_z);
Gian Marco05288a22017-11-21 10:57:50 +00001464
1465 int i = 0;
1466 // This for loop performs 4 accumulations
1467 for(; i <= ((int)ROWS_B - 4); i += 4)
1468 {
Manuel Bottini959c26d2019-12-02 16:22:35 +00001469 const VEC_DATA_TYPE(DATA_TYPE, 16)
1470 b0 = vload16(0, matrix_b + 0 * src_stride_y);
1471 const VEC_DATA_TYPE(DATA_TYPE, 16)
1472 b1 = vload16(0, matrix_b + 1 * src_stride_y);
1473 const VEC_DATA_TYPE(DATA_TYPE, 16)
1474 b2 = vload16(0, matrix_b + 2 * src_stride_y);
1475 const VEC_DATA_TYPE(DATA_TYPE, 16)
1476 b3 = vload16(0, matrix_b + 3 * src_stride_y);
Gian Marco05288a22017-11-21 10:57:50 +00001477
Michele Di Giorgioe7b333e2020-01-15 10:30:51 +00001478 sum_col_32 += CONVERT(b0, VEC_DATA_TYPE(ACC_DATA_TYPE, 16)) + CONVERT(b1, VEC_DATA_TYPE(ACC_DATA_TYPE, 16)) + CONVERT(b2, VEC_DATA_TYPE(ACC_DATA_TYPE, 16)) + CONVERT(b3, VEC_DATA_TYPE(ACC_DATA_TYPE,
Manuel Bottini959c26d2019-12-02 16:22:35 +00001479 16));
Gian Marco05288a22017-11-21 10:57:50 +00001480
1481 matrix_b += 4 * src_stride_y;
1482 }
1483
1484 // This for loop perfoms the leftover accumulations
1485 for(; i < (int)ROWS_B; ++i)
1486 {
Manuel Bottini959c26d2019-12-02 16:22:35 +00001487 const VEC_DATA_TYPE(DATA_TYPE, 16)
1488 b0 = vload16(0, matrix_b);
Gian Marco05288a22017-11-21 10:57:50 +00001489
Michele Di Giorgioe7b333e2020-01-15 10:30:51 +00001490 sum_col_32 += CONVERT(b0, VEC_DATA_TYPE(ACC_DATA_TYPE, 16));
Gian Marco05288a22017-11-21 10:57:50 +00001491
1492 matrix_b += src_stride_y;
1493 }
1494
Michele Di Giorgiof64d3362020-04-03 12:40:10 +01001495#if defined(SCALAR)
1496 sum_col_32 *= (VEC_DATA_TYPE(ACC_DATA_TYPE, 16))SCALAR;
1497#endif // defined(SCALAR)
1498 VSTORE(16)
Gian Marco Iodice19fe0a92020-04-14 14:43:03 +01001499 (convert_int16(sum_col_32), 0, (__global int *)dst.ptr);
Gian Marco05288a22017-11-21 10:57:50 +00001500}
1501#endif // defined(COLS_B) && defined(ROWS_B)
1502
Michele Di Giorgioe7b333e2020-01-15 10:30:51 +00001503#endif // defined(DATA_TYPE) && defined(ACC_DATA_TYPE)
1504
Gian Marco05288a22017-11-21 10:57:50 +00001505#if defined(K_OFFSET)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001506
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001507/* Helper function used to calculate the offset contribution after matrix multiplication.
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001508 *
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001509 * This kernel takes a final int32 accumulator value (the output of matrix multiplication),
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001510 * and calculates the offset contribution of matrix A and matrix B.
1511 *
1512 * @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)
1513 * @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)
1514 * @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)
1515 * @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
1516 *
1517 * @param[in] x get_global_id(0) * 4
1518 * @param[in] y get_global_id(1)
1519 * @param[in] z get_global_id(2)
1520 * @param[in] sum_col_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr
1521 * @param[in] sum_col_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
1522 * @param[in] sum_col_step_x (Optional) sum_col_stride_x * number of elements along X processed per workitem(in bytes)
1523 * @param[in] sum_col_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
1524 * @param[in] sum_col_step_y (Optional) sum_col_stride_y * number of elements along Y processed per workitem(in bytes)
1525 * @param[in] sum_col_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
1526 * @param[in] sum_row_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr
1527 * @param[in] sum_row_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
1528 * @param[in] sum_row_step_x (Optional) sum_row_stride_x * number of elements along X processed per workitem(in bytes)
1529 * @param[in] sum_row_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
1530 * @param[in] sum_row_step_y (Optional) sum_row_stride_y * number of elements along Y processed per workitem(in bytes)
1531 * @param[in] sum_row_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
1532 * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: same as @p src_ptr
1533 * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes)
1534 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
1535 * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases tensor
1536 */
1537inline int4 offset_contribution(
1538 int x,
1539 int y,
1540 int z
1541#if defined(A_OFFSET)
1542 ,
1543 IMAGE_DECLARATION(sum_col)
1544#endif // defined(A_OFFSET)
1545#if defined(B_OFFSET)
1546 ,
1547 IMAGE_DECLARATION(sum_row)
1548#endif // defined(B_OFFSET)
1549#if defined(ADD_BIAS)
1550 ,
1551 VECTOR_DECLARATION(biases)
1552#endif // defined(ADD_BIAS)
1553)
1554{
1555 int4 a_offset_s32 = (int4)0;
1556 int4 b_offset_s32 = (int4)0;
1557
1558 int batch_id = z;
1559#if defined(DEPTH_INPUT3D)
1560 batch_id /= (int)DEPTH_INPUT3D;
1561#endif // defined(DEPTH_INPUT3D)
1562
1563#if defined(A_OFFSET)
1564 // Compute the offset contribution due to A_OFFSET
1565 __global uchar *sum_col_addr = sum_col_ptr + sum_col_offset_first_element_in_bytes + x * sizeof(int);
1566
1567 // Compute the offset contribution due to A_OFFSET
1568#if defined(SUM_COL_HAS_BATCHES)
1569 a_offset_s32 = vload4(0, (__global int *)(sum_col_addr + batch_id * sum_col_stride_y));
1570#else // defined(SUM_COL_HAS_BATCHES)
1571 a_offset_s32 = vload4(0, (__global int *)sum_col_addr);
1572#endif // defined(SUM_COL_HAS_BATCHES)
1573
1574 a_offset_s32 *= (int4)A_OFFSET;
1575#endif // defined(A_OFFSET)
1576
1577#if defined(B_OFFSET)
1578 // Compute the offset contribution due to A_OFFSET
1579 __global uchar *sum_row_addr = sum_row_ptr + sum_row_offset_first_element_in_bytes + y * sizeof(int);
1580
1581 // Compute the offset contribution due to B_OFFSET
1582#if defined(HEIGHT_INPUT3D) && defined(DEPTH_INPUT3D)
1583 b_offset_s32 = (int4) * (((__global int *)(sum_row_addr + batch_id * sum_row_stride_y)) + (z % (int)DEPTH_INPUT3D) * (int)HEIGHT_INPUT3D);
1584#else // defined(HEIGHT_INPUT3D) && defined(DEPTH_INPUT3D)
1585 b_offset_s32 = (int4) * (((__global int *)(sum_row_addr + batch_id * sum_row_stride_y)));
1586#endif // defined(HEIGHT_INPUT3D) && defined(DEPTH_INPUT3D)
1587 b_offset_s32 *= (int4)B_OFFSET;
1588#endif // defined(B_OFFSET)
1589
1590#if defined(ADD_BIAS)
1591 // Add bias
1592 __global uchar *bias_addr = biases_ptr + biases_offset_first_element_in_bytes + x * sizeof(int);
1593
1594 int4 biases_values = vload4(0, (__global int *)bias_addr);
1595 b_offset_s32 += (int4)biases_values;
1596#endif // defined(ADD_BIAS)
1597
1598 return (int4)K_OFFSET + a_offset_s32 + b_offset_s32;
1599}
1600
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001601/* OpenCL kernel used to add the offset contribution after matrix multiplication. The computation is performed in-place
Gian Marco05288a22017-11-21 10:57:50 +00001602 *
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001603 * This kernel takes a final int32 accumulator value (the output of matrix multiplication),
Gian Marco05288a22017-11-21 10:57:50 +00001604 * and adds to it the offset contribution of matrix A and matrix B in-place.
1605 *
1606 * @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)
1607 * @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)
1608 * @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)
Chunosov5124be52017-11-22 20:42:13 +07001609 * @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 Marco05288a22017-11-21 10:57:50 +00001610 *
1611 * The final result is:
1612 *
1613 * mm_result[i][k] = mm_result[i][k] +
1614 * (sum_col[k] * A_OFFSET) +
1615 * (sum_row[i] * B_OFFSET) +
1616 * (K_OFFSET)
1617 *
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +01001618 * @param[in] mm_result_ptr Pointer to the source tensor. Supported data type: S32
1619 * @param[in] mm_result_stride_x Stride of the source tensor in X dimension (in bytes)
1620 * @param[in] mm_result_step_x mm_result_stride_x * number of elements along X processed per workitem(in bytes)
1621 * @param[in] mm_result_stride_y Stride of the source tensor in Y dimension (in bytes)
1622 * @param[in] mm_result_step_y mm_result_stride_y * number of elements along Y processed per workitem(in bytes)
1623 * @param[in] mm_result_stride_z Stride of the source tensor in Z dimension (in bytes)
1624 * @param[in] mm_result_step_z mm_result_stride_z * number of elements along Z processed per workitem(in bytes)
1625 * @param[in] mm_result_offset_first_element_in_bytes The offset of the first element in the source tensor
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001626 * @param[in] sum_col_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr
1627 * @param[in] sum_col_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
1628 * @param[in] sum_col_step_x (Optional) sum_col_stride_x * number of elements along X processed per workitem(in bytes)
1629 * @param[in] sum_col_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
1630 * @param[in] sum_col_step_y (Optional) sum_col_stride_y * number of elements along Y processed per workitem(in bytes)
1631 * @param[in] sum_col_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
1632 * @param[in] sum_row_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr
1633 * @param[in] sum_row_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
1634 * @param[in] sum_row_step_x (Optional) sum_row_stride_x * number of elements along X processed per workitem(in bytes)
1635 * @param[in] sum_row_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
1636 * @param[in] sum_row_step_y (Optional) sum_row_stride_y * number of elements along Y processed per workitem(in bytes)
1637 * @param[in] sum_row_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
1638 * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: same as @p src_ptr
1639 * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes)
1640 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
1641 * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases tensor
Gian Marco05288a22017-11-21 10:57:50 +00001642 */
1643__kernel void gemmlowp_offset_contribution(TENSOR3D_DECLARATION(mm_result)
1644#if defined(A_OFFSET)
1645 ,
1646 IMAGE_DECLARATION(sum_col)
1647#endif // defined(A_OFFSET)
1648#if defined(B_OFFSET)
1649 ,
1650 IMAGE_DECLARATION(sum_row)
1651#endif // defined(B_OFFSET)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001652#if defined(ADD_BIAS)
1653 ,
1654 VECTOR_DECLARATION(biases)
1655#endif // defined(ADD_BIAS))
Gian Marco05288a22017-11-21 10:57:50 +00001656 )
1657{
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001658 const int x = get_global_id(0) * 4;
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +01001659 const int y = get_global_id(1);
1660 const int z = get_global_id(2);
1661
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001662 // Compute offset contribution
1663 int4 offset_term_s32 = offset_contribution(
1664 x, y, z
Gian Marco05288a22017-11-21 10:57:50 +00001665#if defined(A_OFFSET)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001666 ,
1667 sum_col_ptr,
1668 sum_col_stride_x,
1669 sum_col_step_x,
1670 sum_col_stride_y,
1671 sum_col_step_y,
1672 sum_col_offset_first_element_in_bytes
Gian Marco05288a22017-11-21 10:57:50 +00001673#endif // defined(A_OFFSET)
Gian Marco05288a22017-11-21 10:57:50 +00001674#if defined(B_OFFSET)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001675 ,
1676 sum_row_ptr,
1677 sum_row_stride_x,
1678 sum_row_step_x,
1679 sum_row_stride_y,
1680 sum_row_step_y,
1681 sum_row_offset_first_element_in_bytes
Gian Marco05288a22017-11-21 10:57:50 +00001682#endif // defined(B_OFFSET)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001683#if defined(ADD_BIAS)
1684 ,
1685 biases_ptr,
1686 biases_stride_x,
1687 biases_step_x,
1688 biases_offset_first_element_in_bytes
1689#endif // defined(ADD_BIAS)
1690 );
Gian Marco05288a22017-11-21 10:57:50 +00001691
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001692 __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 Marco05288a22017-11-21 10:57:50 +00001693
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001694 int4 in_s32 = vload4(0, (__global int *)mm_result_addr);
Gian Marco05288a22017-11-21 10:57:50 +00001695
1696 // Add the offset terms to GEMM's result
1697 in_s32 += offset_term_s32;
1698
1699 // Store the result with the offset contribution
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001700 vstore4(in_s32, 0, (__global int *)mm_result_addr);
Gian Marco05288a22017-11-21 10:57:50 +00001701}
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001702
Michele Di Giorgiob54ba282020-01-14 15:31:55 +00001703#if defined(RESULT_OFFSET) && defined(RESULT_MULTIPLIER) && defined(RESULT_SHIFT) && defined(OUTPUT_DATA_TYPE)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001704/* OpenCL kernel used to add the offset contribution after @ref CLGEMMLowpMatrixMultiplyKernel and it quantizes down to uint8.
1705 *
1706 * 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.
1707 *
1708 *
1709 * @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)
1710 * @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)
1711 * @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)
1712 * @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
1713 *
1714 * The result before the output stage is:
1715 *
1716 * mm_result[i][k] = mm_result[i][k] +
1717 * (sum_col[k] * A_OFFSET) +
1718 * (sum_row[i] * B_OFFSET) +
1719 * (K_OFFSET)
1720 *
Manuel Bottini959c26d2019-12-02 16:22:35 +00001721 * This result is quantized down to uint8/int8 using the output stage. The output stage computes the following operations:
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001722 *
1723 * -# Add offset terms to final result
1724 * -# Multiply each entry of result by result_mult_int
1725 * -# Add bias to final result (if -DADD_BIAS is passed at compile time)
1726 * -# Shift the int32 accumulator by result_shift
1727 * -# Clamp the value between the specified min and max bounds (if -DMIN_BOUND and/or -DMAX_BOUND are passed at compile time)
Manuel Bottini959c26d2019-12-02 16:22:35 +00001728 * -# Clamp the resulting int32 values:
1729 * - to the [0..255] range and cast to QASYMM8.
1730 * - to the [-128..127] range and cast to QASYMM8_SIGNED.
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001731 *
1732 * @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
1733 *
1734 * @note In case the addition of int32 biases is required, -DADD_BIAS should be passed at compile time
Manuel Bottini959c26d2019-12-02 16:22:35 +00001735 * @note The output datatype should be passed at compile time using -DOUTPUT_DATA_TYPE
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001736 * @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.
1737 * These values can be used to implement "rectified linear unit" activation functions
1738 *
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001739 * @param[in] mm_result_ptr Pointer to the source tensor. Supported data type: S32
1740 * @param[in] mm_result_stride_x Stride of the source tensor in X dimension (in bytes)
1741 * @param[in] mm_result_step_x mm_result_stride_x * number of elements along X processed per workitem(in bytes)
1742 * @param[in] mm_result_stride_y Stride of the source tensor in Y dimension (in bytes)
1743 * @param[in] mm_result_step_y mm_result_stride_y * number of elements along Y processed per workitem(in bytes)
1744 * @param[in] mm_result_stride_z Stride of the source tensor in Z dimension (in bytes)
1745 * @param[in] mm_result_step_z mm_result_stride_z * number of elements along Z processed per workitem(in bytes)
1746 * @param[in] mm_result_offset_first_element_in_bytes The offset of the first element in the source tensor
1747 * @param[in] sum_col_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr
1748 * @param[in] sum_col_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
1749 * @param[in] sum_col_step_x (Optional) sum_col_stride_x * number of elements along X processed per workitem(in bytes)
1750 * @param[in] sum_col_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
1751 * @param[in] sum_col_step_y (Optional) sum_col_stride_y * number of elements along Y processed per workitem(in bytes)
1752 * @param[in] sum_col_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
1753 * @param[in] sum_row_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr
1754 * @param[in] sum_row_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
1755 * @param[in] sum_row_step_x (Optional) sum_row_stride_x * number of elements along X processed per workitem(in bytes)
1756 * @param[in] sum_row_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
1757 * @param[in] sum_row_step_y (Optional) sum_row_stride_y * number of elements along Y processed per workitem(in bytes)
1758 * @param[in] sum_row_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
1759 * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: same as @p src_ptr
1760 * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes)
1761 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
1762 * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases tensor
Manuel Bottini959c26d2019-12-02 16:22:35 +00001763 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: QASYMM8/QASYMM8_SIGNED
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001764 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
1765 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
1766 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
1767 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
1768 * @param[in] dst_stride_z Stride of the source tensor in Z dimension (in bytes)
1769 * @param[in] dst_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1770 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
1771 * @param[in] result_multipliers_ptr (Optional) Pointer to the output multipliers vector for per-channel quantization. Supported data types: S32
1772 * @param[in] result_multipliers_stride_x (Optional) Stride of the output multipliers vector in X dimension (in bytes)
1773 * @param[in] result_multipliers_step_x (Optional) output_multipliers_stride_x * number of elements along X processed per workitem(in bytes)
1774 * @param[in] result_multipliers_offset_first_element_in_bytes (Optional) The offset of the first element in the output multipliers vector
1775 * @param[in] result_shifts_ptr (Optional) Pointer to the output shifts vector for per-channel quantization. Supported data types: S32
1776 * @param[in] result_shifts_stride_x (Optional) Stride of the output shifts vector in X dimension (in bytes)
1777 * @param[in] result_shifts_step_x (Optional) output_shifts_stride_x * number of elements along X processed per workitem(in bytes)
1778 * @param[in] result_shifts_offset_first_element_in_bytes (Optional) The offset of the first element in the output shifts vector
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001779 */
1780__kernel void gemmlowp_offset_contribution_quantize_down(TENSOR3D_DECLARATION(mm_result)
1781#if defined(A_OFFSET)
1782 ,
1783 IMAGE_DECLARATION(sum_col)
1784#endif // defined(A_OFFSET)
1785#if defined(B_OFFSET)
1786 ,
1787 IMAGE_DECLARATION(sum_row)
1788#endif // defined(B_OFFSET)
1789 ,
1790#if defined(ADD_BIAS)
1791 VECTOR_DECLARATION(biases),
1792#endif // defined(ADD_BIAS)
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001793 TENSOR3D_DECLARATION(dst)
1794#if defined(PER_CHANNEL_QUANTIZATION)
1795 ,
1796 VECTOR_DECLARATION(result_multipliers),
1797 VECTOR_DECLARATION(result_shifts)
1798#endif // defined(PER_CHANNEL_QUANTIZATION)
1799 )
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001800{
1801 const int x = get_global_id(0) * 4;
1802 const int y = get_global_id(1);
1803 const int z = get_global_id(2);
1804
1805 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x + y * dst_stride_y + z * dst_stride_z;
1806
1807 // Compute offset contribution
1808 int4 offset_term_s32 = offset_contribution(
1809 x, y, z
1810#if defined(A_OFFSET)
1811 ,
1812 sum_col_ptr,
1813 sum_col_stride_x,
1814 sum_col_step_x,
1815 sum_col_stride_y,
1816 sum_col_step_y,
1817 sum_col_offset_first_element_in_bytes
1818#endif // defined(A_OFFSET)
1819#if defined(B_OFFSET)
1820 ,
1821 sum_row_ptr,
1822 sum_row_stride_x,
1823 sum_row_step_x,
1824 sum_row_stride_y,
1825 sum_row_step_y,
1826 sum_row_offset_first_element_in_bytes
1827#endif // defined(B_OFFSET)
1828#if defined(ADD_BIAS)
1829 ,
1830 biases_ptr,
1831 biases_stride_x,
1832 biases_step_x,
1833 biases_offset_first_element_in_bytes
1834#endif // defined(ADD_BIAS)
1835 );
1836
1837 __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;
1838
1839 int4 in_s32 = vload4(0, (__global int *)mm_result_addr);
1840
1841 // Add the offset terms to GEMM's result
1842 in_s32 += offset_term_s32;
1843
1844 // -------------- OUTPUT STAGE
1845
1846 // Add the offset terms to GEMM's result
1847 in_s32 += (int4)RESULT_OFFSET;
1848
1849 // Multiply by result_mult_int and shift
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001850#if defined(PER_CHANNEL_QUANTIZATION)
1851 __global uchar *result_multipliers_addr = result_multipliers_ptr + result_multipliers_offset_first_element_in_bytes + x * sizeof(int);
1852 __global uchar *result_shifts_addr = result_shifts_ptr + result_shifts_offset_first_element_in_bytes + x * sizeof(int);
1853 int4 result_multipliers_values = vload4(0, (__global int *)result_multipliers_addr);
1854 int4 result_shifts_values = vload4(0, (__global int *)result_shifts_addr);
1855
1856 in_s32 *= result_multipliers_values;
1857 in_s32 >>= result_shifts_values;
1858#else // defined(PER_CHANNEL_QUANTIZATION)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001859 in_s32 *= RESULT_MULTIPLIER;
1860
1861 in_s32 >>= RESULT_SHIFT;
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001862#endif // defined(PER_CHANNEL_QUANTIZATION)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001863
Manuel Bottini959c26d2019-12-02 16:22:35 +00001864 VEC_DATA_TYPE(OUTPUT_DATA_TYPE, 4)
1865 res = CONVERT_SAT(in_s32, VEC_DATA_TYPE(OUTPUT_DATA_TYPE, 4));
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001866
1867#if defined(MIN_BOUND)
Manuel Bottini959c26d2019-12-02 16:22:35 +00001868 res = max(res, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, 4))MIN_BOUND);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001869#endif // defined(MIN_BOUND)
1870#if defined(MAX_BOUND)
Manuel Bottini959c26d2019-12-02 16:22:35 +00001871 res = min(res, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, 4))MAX_BOUND);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001872#endif // defined(MAX_BOUND)
1873
1874 // Store the result
Manuel Bottini959c26d2019-12-02 16:22:35 +00001875 vstore4(res, 0, (__global OUTPUT_DATA_TYPE *)dst_addr);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001876}
1877
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001878/* OpenCL kernel used to add the offset contribution after matrix multiplication and it quantizes down to uint8.
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001879 *
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001880 * 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 Iodice4b908652018-10-18 10:21:02 +01001881 *
1882 *
1883 * @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)
1884 * @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)
1885 * @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)
1886 * @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
1887 *
1888 * The result before the output stage is:
1889 *
1890 * mm_result[i][k] = mm_result[i][k] +
1891 * (sum_col[k] * A_OFFSET) +
1892 * (sum_row[i] * B_OFFSET) +
1893 * (K_OFFSET)
1894 *
Manuel Bottini959c26d2019-12-02 16:22:35 +00001895 * This result is quantized down to uint8/int8 using the output stage. The output stage computes the following operations:
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001896 *
1897 * -# Compute fixed point multiplication between each entry of input by result_fixedpoint_multiplier
1898 * -# Add bias to final result if bias tensor is not a nullptr
1899 * -# Round to nearest division by a power-of-two using result_shift
1900 * -# Add offset to each result
1901 * -# Clamp the value between the specified min and max bounds
Manuel Bottini959c26d2019-12-02 16:22:35 +00001902 * -# Clamp the resulting int32 values:
1903 * - to the [0..255] range and cast to QASYMM8.
1904 * - to the [-128..127] range and cast to QASYMM8_SIGNED.
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001905 *
1906 * @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
1907 *
1908 * @note In case the addition of int32 biases is required, -DADD_BIAS should be passed at compile time
Manuel Bottini959c26d2019-12-02 16:22:35 +00001909 * @note The output datatype should be passed at compile time using -DOUTPUT_DATA_TYPE
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001910 * @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.
1911 * These values can be used to implement "rectified linear unit" activation functions
1912 *
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001913 * @param[in] mm_result_ptr Pointer to the source tensor. Supported data type: S32
1914 * @param[in] mm_result_stride_x Stride of the source tensor in X dimension (in bytes)
1915 * @param[in] mm_result_step_x mm_result_stride_x * number of elements along X processed per workitem(in bytes)
1916 * @param[in] mm_result_stride_y Stride of the source tensor in Y dimension (in bytes)
1917 * @param[in] mm_result_step_y mm_result_stride_y * number of elements along Y processed per workitem(in bytes)
1918 * @param[in] mm_result_stride_z Stride of the source tensor in Z dimension (in bytes)
1919 * @param[in] mm_result_step_z mm_result_stride_z * number of elements along Z processed per workitem(in bytes)
1920 * @param[in] mm_result_offset_first_element_in_bytes The offset of the first element in the source tensor
1921 * @param[in] sum_col_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr
1922 * @param[in] sum_col_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
1923 * @param[in] sum_col_step_x (Optional) sum_col_stride_x * number of elements along X processed per workitem(in bytes)
1924 * @param[in] sum_col_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
1925 * @param[in] sum_col_step_y (Optional) sum_col_stride_y * number of elements along Y processed per workitem(in bytes)
1926 * @param[in] sum_col_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
1927 * @param[in] sum_row_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr
1928 * @param[in] sum_row_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
1929 * @param[in] sum_row_step_x (Optional) sum_row_stride_x * number of elements along X processed per workitem(in bytes)
1930 * @param[in] sum_row_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
1931 * @param[in] sum_row_step_y (Optional) sum_row_stride_y * number of elements along Y processed per workitem(in bytes)
1932 * @param[in] sum_row_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
1933 * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: same as @p src_ptr
1934 * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes)
1935 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
1936 * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases tensor
1937 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: QASYMM8
1938 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
1939 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
1940 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
1941 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
1942 * @param[in] dst_stride_z Stride of the source tensor in Z dimension (in bytes)
1943 * @param[in] dst_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1944 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
1945 * @param[in] result_multipliers_ptr (Optional) Pointer to the output multipliers vector for per-channel quantization. Supported data types: S32
1946 * @param[in] result_multipliers_stride_x (Optional) Stride of the output multipliers vector in X dimension (in bytes)
1947 * @param[in] result_multipliers_step_x (Optional) output_multipliers_stride_x * number of elements along X processed per workitem(in bytes)
1948 * @param[in] result_multipliers_offset_first_element_in_bytes (Optional) The offset of the first element in the output multipliers vector
1949 * @param[in] result_shifts_ptr (Optional) Pointer to the output shifts vector for per-channel quantization. Supported data types: S32
1950 * @param[in] result_shifts_stride_x (Optional) Stride of the output shifts vector in X dimension (in bytes)
1951 * @param[in] result_shifts_step_x (Optional) output_shifts_stride_x * number of elements along X processed per workitem(in bytes)
1952 * @param[in] result_shifts_offset_first_element_in_bytes (Optional) The offset of the first element in the output shifts vector
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001953 */
1954__kernel void gemmlowp_offset_contribution_quantize_down_fixedpoint(TENSOR3D_DECLARATION(mm_result)
1955#if defined(A_OFFSET)
1956 ,
1957 IMAGE_DECLARATION(sum_col)
1958#endif // defined(A_OFFSET)
1959#if defined(B_OFFSET)
1960 ,
1961 IMAGE_DECLARATION(sum_row)
1962#endif // defined(B_OFFSET)
1963 ,
1964#if defined(ADD_BIAS)
1965 VECTOR_DECLARATION(biases),
1966#endif // defined(ADD_BIAS)
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001967 TENSOR3D_DECLARATION(dst)
1968#if defined(PER_CHANNEL_QUANTIZATION)
1969 ,
1970 VECTOR_DECLARATION(result_multipliers),
1971 VECTOR_DECLARATION(result_shifts)
1972#endif // defined(PER_CHANNEL_QUANTIZATION)
1973 )
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001974{
1975 const int x = get_global_id(0) * 4;
1976 const int y = get_global_id(1);
1977 const int z = get_global_id(2);
1978
1979 // Compute offset contribution
1980 int4 offset_term_s32 = offset_contribution(
1981 x, y, z
1982#if defined(A_OFFSET)
1983 ,
1984 sum_col_ptr,
1985 sum_col_stride_x,
1986 sum_col_step_x,
1987 sum_col_stride_y,
1988 sum_col_step_y,
1989 sum_col_offset_first_element_in_bytes
1990#endif // defined(A_OFFSET)
1991#if defined(B_OFFSET)
1992 ,
1993 sum_row_ptr,
1994 sum_row_stride_x,
1995 sum_row_step_x,
1996 sum_row_stride_y,
1997 sum_row_step_y,
1998 sum_row_offset_first_element_in_bytes
1999#endif // defined(B_OFFSET)
2000#if defined(ADD_BIAS)
2001 ,
2002 biases_ptr,
2003 biases_stride_x,
2004 biases_step_x,
2005 biases_offset_first_element_in_bytes
2006#endif // defined(ADD_BIAS)
2007 );
2008
2009 __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;
2010
2011 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x + y * dst_stride_y + z * dst_stride_z;
2012
2013 int4 in_s32 = vload4(0, (__global int *)mm_result_addr);
2014
2015 // Add the offset terms to GEMM's result
2016 in_s32 += offset_term_s32;
2017
2018 // -------------- OUTPUT STAGE
2019
2020 // Multiply by result_mult_int and shift
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00002021#if defined(PER_CHANNEL_QUANTIZATION)
2022 __global uchar *result_multipliers_addr = result_multipliers_ptr + result_multipliers_offset_first_element_in_bytes + x * sizeof(int);
2023 __global uchar *result_shifts_addr = result_shifts_ptr + result_shifts_offset_first_element_in_bytes + x * sizeof(int);
2024 int4 result_multipliers_values = vload4(0, (__global int *)result_multipliers_addr);
2025 int4 result_shifts_values = vload4(0, (__global int *)result_shifts_addr);
2026
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +01002027 int4 in_s32_shift_lt0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(in_s32, result_multipliers_values, result_shifts_values, 4);
2028 int4 in_s32_shift_gt0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(in_s32, result_multipliers_values, result_shifts_values, 4);
2029 in_s32 = select(in_s32_shift_lt0, in_s32_shift_gt0, result_shifts_values >= 0);
2030#else // defined(PER_CHANNEL_QUANTIZATION)
2031
2032#if RESULT_SHIFT < 0
2033 in_s32 = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(in_s32, RESULT_MULTIPLIER, RESULT_SHIFT, 4);
2034#else // RESULT_SHIFT >= 0
Gian Marco Iodice4b908652018-10-18 10:21:02 +01002035 in_s32 = ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(in_s32, RESULT_MULTIPLIER, RESULT_SHIFT, 4);
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +01002036#endif // RESULT_SHIFT < 0
2037
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00002038#endif // defined(PER_CHANNEL_QUANTIZATION)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01002039
2040 // Add the offset terms to GEMM's result
2041 in_s32 += (int4)RESULT_OFFSET;
2042
Manuel Bottini959c26d2019-12-02 16:22:35 +00002043 VEC_DATA_TYPE(OUTPUT_DATA_TYPE, 4)
2044 res = CONVERT_SAT(in_s32, VEC_DATA_TYPE(OUTPUT_DATA_TYPE, 4));
Gian Marco Iodice4b908652018-10-18 10:21:02 +01002045
2046#if defined(MIN_BOUND)
Manuel Bottini959c26d2019-12-02 16:22:35 +00002047 res = max(res, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, 4))MIN_BOUND);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01002048#endif // defined(MIN_BOUND)
2049#if defined(MAX_BOUND)
Manuel Bottini959c26d2019-12-02 16:22:35 +00002050 res = min(res, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, 4))MAX_BOUND);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01002051#endif // defined(MAX_BOUND)
2052
2053 // Store the result
Manuel Bottini959c26d2019-12-02 16:22:35 +00002054 vstore4(res, 0, (__global OUTPUT_DATA_TYPE *)dst_addr);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01002055}
Michele Di Giorgiob54ba282020-01-14 15:31:55 +00002056#endif // defined(RESULT_OFFSET) && defined(RESULT_MULTIPLIER) && defined(RESULT_SHIFT) && defined(OUTPUT_DATA_TYPE)
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00002057
Gian Marco05288a22017-11-21 10:57:50 +00002058#endif // defined(K_OFFSET)
2059
2060#if defined(RESULT_OFFSET) && defined(RESULT_MULT_INT) && defined(RESULT_SHIFT)
Luca Foschiani689c9682020-02-26 14:30:14 +00002061/** This OpenCL kernel is used to quantize down the int32 accumulator values of GEMMLowp to QASYMM8/QASYMM8_SIGNED
Gian Marco05288a22017-11-21 10:57:50 +00002062 *
Luca Foschiani689c9682020-02-26 14:30:14 +00002063 * This kernel takes a final int32 accumulator value and processes it to obtain the final QASYMM8/QASYMM8_SIGNED value.
Gian Marco05288a22017-11-21 10:57:50 +00002064 * The following computations will be performed by the kernel:
2065 *
2066 * -# Add offset terms to final result
2067 * -# Multiply each entry of result by result_mult_int
2068 * -# Add bias to final result (if -DADD_BIAS is passed at compile time)
2069 * -# Shift the int32 accumulator by result_shift
2070 * -# Clamp the value between the specified min and max bounds (if -DMIN_BOUND and/or -DMAX_BOUND are passed at compile time)
Luca Foschiani689c9682020-02-26 14:30:14 +00002071 * -# Clamp the resulting int32 values:
2072 * -# - to the [0..255] range and cast to QASYMM8.
2073 * -# - to the [-128..127] range and cast to QASYMM8_SIGNED.
Gian Marco05288a22017-11-21 10:57:50 +00002074 *
2075 * @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
2076 *
2077 * @note In case the addition of int32 biases is required, -DADD_BIAS should be passed at compile time
Luca Foschiani689c9682020-02-26 14:30:14 +00002078 * @note The output datatype should be passed at compile time using -DOUTPUT_DATA_TYPE
Gian Marco05288a22017-11-21 10:57:50 +00002079 * @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.
2080 * These values can be used to implement "rectified linear unit" activation functions
2081 *
2082 * @param[in] src_ptr Pointer to the source tensor. Supported data type: S32
2083 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
2084 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
2085 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
2086 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
2087 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
2088 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
2089 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
Gian Marco Iodice4b908652018-10-18 10:21:02 +01002090 * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: same as @p src_ptr
2091 * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes)
2092 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
2093 * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases tensor
Luca Foschiani689c9682020-02-26 14:30:14 +00002094 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: QASYMM8/QASYMM8_SIGNED
Gian Marco05288a22017-11-21 10:57:50 +00002095 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
2096 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
2097 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
2098 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
2099 * @param[in] dst_stride_z Stride of the source tensor in Z dimension (in bytes)
2100 * @param[in] dst_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
2101 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
2102 */
2103__kernel void gemmlowp_output_stage_quantize_down(TENSOR3D_DECLARATION(src),
2104#if defined(ADD_BIAS)
2105 VECTOR_DECLARATION(biases),
2106#endif // defined(ADD_BIAS)
2107 TENSOR3D_DECLARATION(dst))
2108{
2109 // Compute source and destination addresses
Gian Marco Iodice4b908652018-10-18 10:21:02 +01002110 int x = get_global_id(0) * 4;
2111 int y = get_global_id(1);
2112 int z = get_global_id(2);
Gian Marco05288a22017-11-21 10:57:50 +00002113
Gian Marco Iodice4b908652018-10-18 10:21:02 +01002114 __global uchar *src_addr = src_ptr + src_offset_first_element_in_bytes + x * sizeof(int) + y * src_stride_y + z * src_stride_z;
Gian Marco05288a22017-11-21 10:57:50 +00002115
Gian Marco Iodice4b908652018-10-18 10:21:02 +01002116 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x + y * dst_stride_y + z * dst_stride_z;
2117
2118 int4 input_values = vload4(0, (__global int *)src_addr);
Gian Marco58c57942017-11-28 09:10:03 +00002119
Gian Marco05288a22017-11-21 10:57:50 +00002120#if defined(ADD_BIAS)
2121 // Add bias
Gian Marco Iodice4b908652018-10-18 10:21:02 +01002122 __global uchar *bias_addr = biases_ptr + biases_offset_first_element_in_bytes + x * sizeof(int);
2123
2124 int4 biases_values = vload4(0, (__global int *)bias_addr);
2125 input_values += (int4)biases_values;
Gian Marco05288a22017-11-21 10:57:50 +00002126#endif // defined(ADD_BIAS)
2127
Gian Marco Iodice4b908652018-10-18 10:21:02 +01002128 // Add the offset terms to GEMM's result
2129 input_values += (int4)RESULT_OFFSET;
2130
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +00002131 // Multiply by result_mult_int and shift
Gian Marco58c57942017-11-28 09:10:03 +00002132 input_values *= RESULT_MULT_INT;
Gian Marco05288a22017-11-21 10:57:50 +00002133
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +01002134#if RESULT_SHIFT < 0
2135 input_values >>= -RESULT_SHIFT;
2136#else // RESULT_SHIFT >= 0
Gian Marco58c57942017-11-28 09:10:03 +00002137 input_values >>= RESULT_SHIFT;
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +01002138#endif // RESULT_SHIFT < 0
Gian Marco05288a22017-11-21 10:57:50 +00002139
Luca Foschiani689c9682020-02-26 14:30:14 +00002140 VEC_DATA_TYPE(OUTPUT_DATA_TYPE, 4)
2141 res = CONVERT_SAT(input_values, VEC_DATA_TYPE(OUTPUT_DATA_TYPE, 4));
Gian Marco05288a22017-11-21 10:57:50 +00002142
2143#if defined(MIN_BOUND)
Luca Foschiani689c9682020-02-26 14:30:14 +00002144 res = max(res, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, 4))MIN_BOUND);
Gian Marco05288a22017-11-21 10:57:50 +00002145#endif // defined(MIN_BOUND)
2146#if defined(MAX_BOUND)
Luca Foschiani689c9682020-02-26 14:30:14 +00002147 res = min(res, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, 4))MAX_BOUND);
Gian Marco05288a22017-11-21 10:57:50 +00002148#endif // defined(MAX_BOUND)
2149
2150 // Store the result
Luca Foschiani5219ed82020-03-27 15:04:13 +00002151 vstore4(res, 0, (__global OUTPUT_DATA_TYPE *)dst_addr);
Gian Marco05288a22017-11-21 10:57:50 +00002152}
Gian Marco58c57942017-11-28 09:10:03 +00002153#endif // defined(RESULT_OFFSET) && defined(RESULT_MULT_INT) && defined(RESULT_SHIFT)
2154
2155#if defined(RESULT_OFFSET_AFTER_SHIFT) && defined(RESULT_FIXEDPOINT_MULTIPLIER) && defined(RESULT_SHIFT)
Manuel Bottini959c26d2019-12-02 16:22:35 +00002156/** This OpenCL kernel is used to quantize down the int32 accumulator values of GEMMLowp to QASYMM8/QASYMM8_SIGNED
Gian Marco58c57942017-11-28 09:10:03 +00002157 *
Manuel Bottini959c26d2019-12-02 16:22:35 +00002158 * This kernel takes a final int32 accumulator value (the output of matrix multiplication), and processes it to obtain the final QASYMM8/QASYMM8_SIGNED value.
Gian Marco58c57942017-11-28 09:10:03 +00002159 * The following computations will be performed by the kernel:
2160 *
2161 * -# Compute fixed point multiplication between each entry of input by result_fixedpoint_multiplier
2162 * -# Add bias to final result if bias tensor is not a nullptr
2163 * -# Round to nearest division by a power-of-two using result_shift
2164 * -# Add offset to each result
2165 * -# Clamp the value between the specified min and max bounds
Manuel Bottini1f332d42019-11-29 17:25:25 +00002166 * -# Clamp the resulting int32 values:
2167 * - to the [0..255] range and cast to QASYMM8.
2168 * - to the [-128..127] range and cast to QASYMM8_SIGNED.
Gian Marco58c57942017-11-28 09:10:03 +00002169 *
Gian Marco Iodice4b908652018-10-18 10:21:02 +01002170 * @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 Marco58c57942017-11-28 09:10:03 +00002171 *
2172 * @note In case the addition of int32 biases is required, -DADD_BIAS should be passed at compile time
Manuel Bottini1f332d42019-11-29 17:25:25 +00002173 * @note The output datatype should be passed at compile time using -DOUTPUT_DATA_TYPE
Gian Marco58c57942017-11-28 09:10:03 +00002174 * @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.
2175 * These values can be used to implement "rectified linear unit" activation functions
2176 *
2177 * @param[in] src_ptr Pointer to the source tensor. Supported data type: S32
2178 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
2179 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
2180 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
2181 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
2182 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
2183 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
2184 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
Gian Marco Iodice4b908652018-10-18 10:21:02 +01002185 * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: same as @p src_ptr
2186 * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes)
2187 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
2188 * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases tensor
Sheri Zhang0cdbda52020-02-25 15:57:21 +00002189 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: QASYMM8/QASYMM8_SIGNED
Gian Marco58c57942017-11-28 09:10:03 +00002190 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
2191 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
2192 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
2193 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
2194 * @param[in] dst_stride_z Stride of the source tensor in Z dimension (in bytes)
2195 * @param[in] dst_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
2196 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
2197 */
2198__kernel void gemmlowp_output_stage_quantize_down_fixedpoint(TENSOR3D_DECLARATION(src),
2199#if defined(ADD_BIAS)
2200 VECTOR_DECLARATION(biases),
2201#endif // defined(ADD_BIAS)
2202 TENSOR3D_DECLARATION(dst))
2203{
2204 // Compute source and destination addresses
Gian Marco Iodice4b908652018-10-18 10:21:02 +01002205 int x = get_global_id(0) * 4;
2206 int y = get_global_id(1);
2207 int z = get_global_id(2);
Georgios Pinitas932491f2018-09-21 16:33:15 +01002208
Gian Marco Iodice4b908652018-10-18 10:21:02 +01002209 __global uchar *src_addr = src_ptr + src_offset_first_element_in_bytes + x * sizeof(int) + y * src_stride_y + z * src_stride_z;
Gian Marco58c57942017-11-28 09:10:03 +00002210
Gian Marco Iodice4b908652018-10-18 10:21:02 +01002211 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x + y * dst_stride_y + z * dst_stride_z;
2212
2213 int4 input_values = vload4(0, (__global int *)src_addr);
Gian Marco58c57942017-11-28 09:10:03 +00002214
2215#if defined(ADD_BIAS)
2216 // Add bias
Gian Marco Iodice4b908652018-10-18 10:21:02 +01002217 __global uchar *bias_addr = biases_ptr + biases_offset_first_element_in_bytes + x * sizeof(int);
2218
2219 int4 biases_values = vload4(0, (__global int *)bias_addr);
2220 input_values += (int4)biases_values;
Gian Marco58c57942017-11-28 09:10:03 +00002221#endif // defined(ADD_BIAS)
2222
2223 // Multiply by result_mult_int and shift
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +01002224#if RESULT_SHIFT < 0
2225 input_values = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(input_values, RESULT_FIXEDPOINT_MULTIPLIER, RESULT_SHIFT, 4);
2226#else // RESULT_SHIFT >= 0
Gian Marco Iodice4b908652018-10-18 10:21:02 +01002227 input_values = ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(input_values, RESULT_FIXEDPOINT_MULTIPLIER, RESULT_SHIFT, 4);
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +01002228#endif // RESULT_SHIFT < 0
Gian Marco58c57942017-11-28 09:10:03 +00002229
2230 // Add the offset terms to GEMM's result
Gian Marco Iodice4b908652018-10-18 10:21:02 +01002231 input_values += (int4)RESULT_OFFSET_AFTER_SHIFT;
Gian Marco58c57942017-11-28 09:10:03 +00002232
Manuel Bottini1f332d42019-11-29 17:25:25 +00002233 VEC_DATA_TYPE(OUTPUT_DATA_TYPE, 4)
2234 res = CONVERT_SAT(input_values, VEC_DATA_TYPE(OUTPUT_DATA_TYPE, 4));
Gian Marco58c57942017-11-28 09:10:03 +00002235
2236#if defined(MIN_BOUND)
Manuel Bottini1f332d42019-11-29 17:25:25 +00002237 res = max(res, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, 4))MIN_BOUND);
Gian Marco58c57942017-11-28 09:10:03 +00002238#endif // defined(MIN_BOUND)
2239#if defined(MAX_BOUND)
Manuel Bottini1f332d42019-11-29 17:25:25 +00002240 res = min(res, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, 4))MAX_BOUND);
Gian Marco58c57942017-11-28 09:10:03 +00002241#endif // defined(MAX_BOUND)
2242
2243 // Store the result
Manuel Bottini1f332d42019-11-29 17:25:25 +00002244 vstore4(res, 0, (__global OUTPUT_DATA_TYPE *)dst_addr);
Gian Marco58c57942017-11-28 09:10:03 +00002245}
Chunosov5124be52017-11-22 20:42:13 +07002246#endif // defined(RESULT_OFFSET_AFTER_SHIFT) && defined(RESULT_FIXEDPOINT_MULTIPLIER) && defined(RESULT_SHIFT)
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002247
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002248#if defined(RESULT_FIXEDPOINT_MULTIPLIER) && defined(RESULT_SHIFT)
2249
Michalis Spyrou51146c52019-07-12 14:42:29 +01002250/** This OpenCL kernel is used to quantize down the int32 accumulator values of GEMMLowp to QSYMM16
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002251 *
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00002252 * This kernel takes a final int32 accumulator value (the output of matrix multiplication), and processes it to obtain the final QSYMM16 value.
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002253 * The following computations will be performed by the kernel:
2254 *
2255 * -# Compute fixed point multiplication between each entry of input by result_fixedpoint_multiplier
2256 * -# Add bias to final result if bias tensor is not a nullptr
2257 * -# Round to nearest division by a power-of-two using result_shift
2258 * -# Add offset to each result
2259 * -# Clamp the value between the specified min and max bounds
2260 * -# Clamp the resulting int32 values to the [-32768..32767] range and cast to QSYMM16.
2261 *
2262 * @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
2263 *
2264 * @note In case the addition of int32 biases is required, -DADD_BIAS should be passed at compile time
2265 * @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.
2266 * These values can be used to implement "rectified linear unit" activation functions
2267 *
2268 * @param[in] src_ptr Pointer to the source tensor. Supported data type: S32
2269 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
2270 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
2271 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
2272 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
2273 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
2274 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
2275 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
2276 * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: same as @p src_ptr
2277 * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes)
2278 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
2279 * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases tensor
Sheri Zhangb18252d2020-04-07 11:04:57 +01002280 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: QSYMM16
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002281 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
2282 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
2283 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
2284 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
2285 * @param[in] dst_stride_z Stride of the source tensor in Z dimension (in bytes)
2286 * @param[in] dst_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
2287 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
2288 */
2289__kernel void gemmlowp_output_stage_quantize_down_fixedpoint_qsymm16(TENSOR3D_DECLARATION(src),
2290#if defined(ADD_BIAS)
2291 VECTOR_DECLARATION(biases),
2292#endif // defined(ADD_BIAS)
2293 TENSOR3D_DECLARATION(dst))
2294{
2295 // Compute source and destination addresses
2296 int x = get_global_id(0) * 4;
2297 int y = get_global_id(1);
2298 int z = get_global_id(2);
2299
Michalis Spyrou51146c52019-07-12 14:42:29 +01002300 __global uchar *src_addr = src_ptr + src_offset_first_element_in_bytes + x * sizeof(int) + y * src_stride_y + z * src_stride_z;
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002301
Michalis Spyrou51146c52019-07-12 14:42:29 +01002302 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x * 2 + y * dst_stride_y + z * dst_stride_z;
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002303
2304 int4 input_values = vload4(0, (__global int *)src_addr);
2305
2306#if defined(ADD_BIAS)
2307 // Add bias
Michalis Spyrou51146c52019-07-12 14:42:29 +01002308 __global uchar *bias_addr = biases_ptr + biases_offset_first_element_in_bytes + x * sizeof(int);
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002309
2310 int4 biases_values = vload4(0, (__global int *)bias_addr);
2311 input_values += (int4)biases_values;
2312#endif // defined(ADD_BIAS)
2313
2314 // Multiply by result_mult_int and shift
Manuel Bottini07263982019-10-17 18:37:26 +01002315#if RESULT_SHIFT < 0
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +01002316 input_values = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(input_values, RESULT_FIXEDPOINT_MULTIPLIER, RESULT_SHIFT, 4);
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00002317#else // RESULT_SHIFT >= 0
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002318 input_values = ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(input_values, RESULT_FIXEDPOINT_MULTIPLIER, RESULT_SHIFT, 4);
Manuel Bottini07263982019-10-17 18:37:26 +01002319#endif // RESULT_SHIFT < 0
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002320
2321 short4 res = convert_short4_sat(input_values);
2322
2323#if defined(MIN_BOUND)
2324 res = max(res, (short4)MIN_BOUND);
2325#endif // defined(MIN_BOUND)
2326#if defined(MAX_BOUND)
2327 res = min(res, (short4)MAX_BOUND);
2328#endif // defined(MAX_BOUND)
2329
2330 // Store the result
Michalis Spyrou51146c52019-07-12 14:42:29 +01002331 vstore4(res, 0, (__global short *)dst_addr);
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002332}
2333#endif // defined(RESULT_FIXEDPOINT_MULTIPLIER) && defined(RESULT_SHIFT)
2334
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002335#if defined(REAL_MULTIPLIER) && defined(OUTPUT_OFFSET)
Sheri Zhang1b14c752020-03-09 14:29:52 +00002336/** This OpenCL kernel is used to quantize down the int32 accumulator values of GEMMLowp to QASYMM8/QASYMM8_SIGNED
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002337 *
Sheri Zhang1b14c752020-03-09 14:29:52 +00002338 * This kernel takes a final int32 accumulator value (the output of matrix multiplication), and processes it to obtain the final QASYMM8/QASYMM8_SIGNED value.
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002339 * The following computations will be performed by the kernel:
2340 *
2341 * -# Compute fixed point multiplication between each entry of input by result_fixedpoint_multiplier
2342 * -# Add bias to final result if bias tensor is not a nullptr
2343 * -# Requantize
2344 * -# Add offset to each result
2345 * -# Clamp the value between the specified min and max bounds
Sheri Zhang1b14c752020-03-09 14:29:52 +00002346 * -# Clamp the resulting int32 values:
2347 * - to the [0..255] range and cast to QASYMM8.
2348 * - to the [-128..127] range and cast to QASYMM8_SIGNED.
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002349 *
2350 * @attention The offset and scalar scale factor must be passed at compile time using -DRESULT_OFFSET, -DREAL_MULTIPLIER
2351 *
2352 * @note In case the addition of int32 biases is required, -DADD_BIAS should be passed at compile time
Sheri Zhang1b14c752020-03-09 14:29:52 +00002353 * @note The output datatype should be passed at compile time using -DOUTPUT_DATA_TYPE
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002354 * @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.
2355 * These values can be used to implement "rectified linear unit" activation functions
2356 *
2357 * @param[in] src_ptr Pointer to the source tensor. Supported data type: S32
2358 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
2359 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
2360 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
2361 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
2362 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
2363 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
2364 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
2365 * @param[in] biases_ptr Pointer to the biases tensor. Supported data type: same as @p src_ptr
2366 * @param[in] biases_stride_x Stride of the biases tensor in X dimension (in bytes)
2367 * @param[in] biases_step_x biases_stride_x * number of elements along X processed per workitem(in bytes)
2368 * @param[in] biases_offset_first_element_in_bytes The offset of the first element in the biases tensor
2369 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: QASYMM8
2370 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
2371 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
2372 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
2373 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
2374 * @param[in] dst_stride_z Stride of the source tensor in Z dimension (in bytes)
2375 * @param[in] dst_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
2376 * @param[in] dst_stride_w Stride of the source tensor in W dimension (in bytes)
2377 * @param[in] dst_step_w src_stride_w * number of elements along W processed per workitem(in bytes)
2378 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
2379 */
2380__kernel void gemmlowp_output_stage_quantize_down_float(TENSOR3D_DECLARATION(src),
2381#if defined(ADD_BIAS)
2382 VECTOR_DECLARATION(biases),
2383#endif // defined(ADD_BIAS)
2384#if defined(DST_HEIGHT)
2385 TENSOR4D_DECLARATION(dst))
2386#else // defined(DST_HEIGHT)
2387 TENSOR3D_DECLARATION(dst))
2388#endif // defined(DST_HEIGHT)
2389{
2390 // Compute source and destination addresses
Gian Marco Iodice0c54a622018-10-30 12:20:03 +00002391 int x = get_global_id(0) * 4;
2392 int y = get_global_id(1);
2393 int z = get_global_id(2);
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002394
Gian Marco Iodice0c54a622018-10-30 12:20:03 +00002395 __global uchar *src_addr = src_ptr + src_offset_first_element_in_bytes + x * sizeof(int) + y * src_stride_y + z * src_stride_z;
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002396
Gian Marco Iodice0c54a622018-10-30 12:20:03 +00002397 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x + y * dst_stride_y + z * dst_stride_z;
2398
2399 int4 input_values = vload4(0, (__global int *)src_addr);
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002400
2401#if defined(ADD_BIAS)
2402 // Add bias
Gian Marco Iodice0c54a622018-10-30 12:20:03 +00002403 __global uchar *bias_addr = biases_ptr + biases_offset_first_element_in_bytes + x * sizeof(int);
2404
2405 int4 biases_values = vload4(0, (__global int *)bias_addr);
2406 input_values += (int4)biases_values;
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002407#endif // defined(ADD_BIAS)
2408
2409 // Convert to float
Sheri Zhang1b14c752020-03-09 14:29:52 +00002410 float4 input_values_f = convert_float4(input_values);
2411 input_values_f = round(input_values_f * (float)REAL_MULTIPLIER + (float)OUTPUT_OFFSET);
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002412
Sheri Zhang1b14c752020-03-09 14:29:52 +00002413 VEC_DATA_TYPE(OUTPUT_DATA_TYPE, 4)
2414 res = CONVERT_SAT(input_values_f, VEC_DATA_TYPE(OUTPUT_DATA_TYPE, 4));
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002415
2416#if defined(MIN_BOUND)
Sheri Zhang1b14c752020-03-09 14:29:52 +00002417 res = max(res, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, 4))MIN_BOUND);
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002418#endif // defined(MIN_BOUND)
2419#if defined(MAX_BOUND)
Sheri Zhang1b14c752020-03-09 14:29:52 +00002420 res = min(res, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, 4))MAX_BOUND);
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002421#endif // defined(MAX_BOUND)
2422
2423 // Store the result
Sheri Zhang1b14c752020-03-09 14:29:52 +00002424 vstore4(res, 0, (__global OUTPUT_DATA_TYPE *)dst_addr);
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002425}
Sheri Zhang1b14c752020-03-09 14:29:52 +00002426#endif // defined(REAL_MULTIPLIER) && defined(OUTPUT_OFFSET)