blob: 74ea96551dad273cd1cee558ba2b4a667f6f15c7 [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)
202/** This OpenCL kernel computes the matrix multiplication between matrix A (src0) and matrix B (src1) in case both matrices have not beed reshaped
203 *
204 * @attention The number of matrix A columns needs to be passed at compile time using -DCOLS_A
205 *
Michele Di 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)
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100484/** This OpenCL kernel computes the matrix multiplication between 2 matrices with QASYMM 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 *
510 * @param[in] lhs_ptr Pointer to the LHS reshaped matrix. Supported data type: QASYMM8
511 * @param[in] lhs_stride_x Stride of the LHS reshaped matrix in X dimension (in bytes)
512 * @param[in] lhs_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
513 * @param[in] lhs_stride_y Stride of the LHS reshaped matrix in Y dimension (in bytes)
514 * @param[in] lhs_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
515 * @param[in] lhs_offset_first_element_in_bytes The offset of the first element in the LHS reshaped matrix
516 * @param[in] rhs_ptr Pointer to the RHS reshaped matrix. Supported data type: same as @p lhs_ptr
517 * @param[in] rhs_stride_x Stride of the RHS reshaped matrix in X dimension (in bytes)
518 * @param[in] rhs_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
519 * @param[in] rhs_stride_y Stride of the RHS reshaped matrix in Y dimension (in bytes)
520 * @param[in] rhs_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
521 * @param[in] rhs_offset_first_element_in_bytes The offset of the first element in the RHS reshaped matrix
522 * @param[out] dst_ptr Pointer to the destination matrix Supported data type: same as @p lhs_ptr
523 * @param[in] dst_stride_x Stride of the destination matrix in X dimension (in bytes)
524 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
525 * @param[in] dst_stride_y Stride of the destination matrix in Y dimension (in bytes)
526 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
527 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination matrix
528 * @param[in] k Number of columns in LHS matrix and rows in RHS matrix not reshaped.
529 * @param[in] lhs_stride_z Stride of the LHS reshaped matrix in Z dimension (in bytes)
530 * @param[in] rhs_stride_z Stride of the RHS reshaped matrix in Z dimension (in bytes)
531 * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes)
532 * @param[in] dst_cross_plane_pad (Optional) Bottom paddings in unit of elements (only if defined REINTERPRET_OUTPUT_AS_3D)
533 */
534__kernel void gemmlowp_mm_reshaped_lhs_nt_rhs_t(IMAGE_DECLARATION(lhs),
535 IMAGE_DECLARATION(rhs),
536 IMAGE_DECLARATION(dst),
537 uint k,
538 uint lhs_stride_z,
539 uint rhs_stride_z,
540 uint dst_stride_z
541#if defined(REINTERPRET_OUTPUT_AS_3D)
542 ,
543 uint dst_cross_plane_pad
544#endif // REINTERPRET_OUTPUT_AS_3D
545 )
546{
547 // Block size
548#define LHS_BLOCK_SIZE ((K0) * (M0))
549
550#if defined(LHS_INTERLEAVE)
551#define LHS_OFFSET_X (K0)
552#define LHS_STEP_X ((K0) * (V0))
553#define LHS_STEP_LOOP (1)
554#else // defined(INTERLEAVE)
555#define LHS_OFFSET_X (LHS_BLOCK_SIZE)
556#define LHS_STEP_X (K0)
557#define LHS_STEP_LOOP (V0)
558#endif // defined(INTERLEAVE)
559
560 // Block size
561#define RHS_BLOCK_SIZE ((K0) * (N0))
562
563 // RHS offset and step X
564#if defined(RHS_INTERLEAVE)
565#define RHS_OFFSET_X (K0)
566#define RHS_STEP_X ((K0) * (H0))
567#define RHS_STEP_LOOP (1)
568#else // defined(RHS_INTERLEAVE)
569#define RHS_OFFSET_X (RHS_BLOCK_SIZE)
570#define RHS_STEP_X (K0)
571#define RHS_STEP_LOOP (H0)
572#endif // defined(RHS_INTERLEAVE)
573
Gian Marco 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
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100586 __global uchar *lhs_addr = lhs_ptr + lhs_offset_first_element_in_bytes + (y % V0) * (uint)LHS_OFFSET_X + (y / V0) * (uint)lhs_stride_y + (z * lhs_stride_z);
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000587
588 // Compute RHS matrix address
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100589 __global uchar *rhs_addr = rhs_ptr + rhs_offset_first_element_in_bytes + (x % H0) * (uint)RHS_OFFSET_X + (x / (uint)H0) * rhs_stride_y;
Gian Marco 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}
817#endif // defined(M0) && defined(N0) && defined(K0) && defined(H0) && defined(DATA_TYPE) && defined(K)
818
Gian Marco Iodicee7510622019-06-03 17:28:17 +0100819#if defined(M0) && defined(N0) && defined(K0) && defined(K)
820
821/** This OpenCL kernel computes the matrix multiplication between 2 matrices.
822 * The LHS matrix is NOT reshaped
823 * The RHS matrix is NOT reshaped
824 *
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000825 * @note The input data type must be passed at compile time using -DDATA_TYPE (i.e. -DDATA_TYPE=uchar)
826 * @note The accumulator data type must be passed at compile time using -DACC_DATA_TYPE (i.e. -DACC_DATA_TYPE=uint)
Gian Marco Iodicee7510622019-06-03 17:28:17 +0100827 * @note The number of columns of LHS matrix must be passed at compile time using -DK (i.e. -DK=64)
828 * @note The number of M0 rows to process must be passed at compile time using -DM0 (i.e. -DM0=2)
829 * @note The number of N0 columns to process must be passed at compile time using -DN0 (i.e. -DN0=2)
830 * @note The number of K0 partial accumulations must be passed at compile time using -DK0 (i.e., -DK0=2)
831 * @note Only the following configurations of M0, N0 and K0 are currently supported:
832 * - M0 = 1, 2, 3, 4, 5, 6, 7, 8
833 * - N0 = 2, 3, 4, 8, 16
834 * - K0 = 2, 3, 4, 8, 16
835 *
836 * @note In case the input or output have to be reinterpreted as a 3D tensor, the following information must be passed at compile time:
837 * -# REINTERPRET_INPUT_AS_3D: To reinterpret the input as 3D
838 * -# REINTERPRET_OUTPUT_AS_3D: To reinterpret the output as 3D
839 * -# HEIGHT_GEMM3D: The height of the output in case it has to be reinterpreted as a 3D tensor.
840 * -# DEPTH_GEMM3D: The depth of the output in case it has to be reinterpreted as a 3D tensor
841 * (HEIGHT_GEMM3D * DEPTH_GEMM3D) = columns LHS matrix
842 *
843 * @param[in] lhs_ptr Pointer to the LHS reshaped matrix. Supported data type: F16/F32
844 * @param[in] lhs_stride_x Stride of the LHS reshaped matrix in X dimension (in bytes)
845 * @param[in] lhs_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
846 * @param[in] lhs_stride_y Stride of the LHS reshaped matrix in Y dimension (in bytes)
847 * @param[in] lhs_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
848 * @param[in] lhs_offset_first_element_in_bytes The offset of the first element in the LHS reshaped matrix
849 * @param[in] rhs_ptr Pointer to the RHS reshaped matrix. Supported data type: same as @p lhs_ptr
850 * @param[in] rhs_stride_x Stride of the RHS reshaped matrix in X dimension (in bytes)
851 * @param[in] rhs_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
852 * @param[in] rhs_stride_y Stride of the RHS reshaped matrix in Y dimension (in bytes)
853 * @param[in] rhs_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
854 * @param[in] rhs_offset_first_element_in_bytes The offset of the first element in the RHS reshaped matrix
855 * @param[out] dst_ptr Pointer to the destination matrix Supported data type: same as @p lhs_ptr
856 * @param[in] dst_stride_x Stride of the destination matrix in X dimension (in bytes)
857 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
858 * @param[in] dst_stride_y Stride of the destination matrix in Y dimension (in bytes)
859 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
860 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination matrix
861 * @param[in] lhs_stride_z Stride of the LHS reshaped matrix in Z dimension (in bytes)
862 * @param[in] rhs_stride_z Stride of the RHS reshaped matrix in Z dimension (in bytes)
863 * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes)
864 * @param[in] lhs_cross_plane_pad (Optional) Bottom paddings for LHS matrix in unit of elements (only if defined REINTERPRET_INPUT_AS_3D)
865 * @param[in] dst_cross_plane_pad (Optional) Bottom paddings for the output matrix in unit of elements (only if defined REINTERPRET_OUTPUT_AS_3D)
866 */
867__kernel void gemmlowp_mm_native(IMAGE_DECLARATION(lhs),
868 IMAGE_DECLARATION(rhs),
869 IMAGE_DECLARATION(dst),
870 uint lhs_stride_z,
871 uint rhs_stride_z,
872 uint dst_stride_z
873#if defined(REINTERPRET_INPUT_AS_3D)
874 ,
875 uint lhs_cross_plane_pad
876#endif // REINTERPRET_INPUT_AS_3D
877#if defined(REINTERPRET_OUTPUT_AS_3D)
878 ,
879 uint dst_cross_plane_pad
880#endif // REINTERPRET_OUTPUT_AS_3D
881 )
882{
883 uint x = get_global_id(0);
884 uint y = get_global_id(1);
885 uint z = get_global_id(2);
886
887#if defined(DUMMY_WORK_ITEMS)
888 if((x * N0 >= N) || (y * M0 >= M))
889 {
890 return;
891 }
892#endif // defined(DUMMY_WORK_ITEMS)
893
894 // Compute LHS matrix address
895 uint lhs_offset = lhs_offset_first_element_in_bytes + y * M0 * (uint)lhs_stride_y;
896
897 // Compute RHS matrix address
898 uint rhs_offset = rhs_offset_first_element_in_bytes + x * N0;
899
900#if defined(MATRIX_B_DEPTH)
901 // Do not slide matrix B if the matrix B has 3 dimensions and matrix A more than 3
902 rhs_offset += (z % MATRIX_B_DEPTH) * rhs_stride_z;
903#else // defined(MATRIX_B_DEPTH)
904 rhs_offset += z * rhs_stride_z;
905#endif // defined(MATRIX_B_DEPTH)
906
907 REPEAT_VAR_INIT_TO_CONST(8, uint, zlhs, 0);
908 REPEAT_VAR_INIT_TO_CONST(16, uint, zrhs, 0);
909
910#if defined(REINTERPRET_INPUT_AS_3D)
911 // The plane (zlhs) is calculated dividing M (y * M0) by HEIGHT_GEMM3D
912 CALCULATE_Z_OFFSET(M0, uint, zlhs, y, HEIGHT_GEMM3D, DEPTH_GEMM3D, lhs_cross_plane_pad, lhs_stride_y);
913
914 // Add offset for batched GEMM. The batches will be in the fourth dimension and for this reason we
915 // multiply lhs_stride_z by DEPTH_GEMM3D
916 lhs_offset += z * lhs_stride_z * DEPTH_GEMM3D;
917
918#else // defined(REINTERPRET_INPUT_AS_3D)
919
920 // Add offset for batched GEMM
921 lhs_offset += z * lhs_stride_z;
922
923#endif // defined(REINTERPRET_INPUT_AS_3D)
924
925 // Initialize the accumulators
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000926 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 +0100927
928 int i = 0;
929
930 for(; i <= (K - K0); i += K0)
931 {
932 // Load values from LHS matrix
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000933 LOAD_BLOCK(M0, K0, DATA_TYPE, a, lhs_ptr, lhs_offset, lhs_stride_y, zlhs);
Gian Marco Iodicee7510622019-06-03 17:28:17 +0100934
935 // Load values from RHS matrix
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000936 LOAD_BLOCK(K0, N0, DATA_TYPE, b, rhs_ptr, rhs_offset, rhs_stride_y, zrhs);
Gian Marco Iodicee7510622019-06-03 17:28:17 +0100937
938 // Transpose the values from RHS matrix
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000939 TRANSPOSE_K0XN0(K0, N0, b_t, b, DATA_TYPE);
Gian Marco Iodicee7510622019-06-03 17:28:17 +0100940
941 // Partial matrix multiplication M0,N0,K0
942 ARM_MM_K0XN0XM0(M0, N0, K0, a, b_t, c);
943
944 // Update the offset
945 lhs_offset += K0;
946 rhs_offset += K0 * rhs_stride_y;
947 }
948
949 // Left-over for loop
950 for(; i < K; ++i)
951 {
952 // Load values from LHS matrix
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000953 LOAD_BLOCK(M0, 1, DATA_TYPE, a, lhs_ptr, lhs_offset, lhs_stride_y, zlhs);
Gian Marco Iodicee7510622019-06-03 17:28:17 +0100954
955 // Load values from RHS matrix
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000956 LOAD_BLOCK(1, N0, DATA_TYPE, b, rhs_ptr, rhs_offset, rhs_stride_y, zrhs);
Gian Marco Iodicee7510622019-06-03 17:28:17 +0100957
958 // Transpose the values from RHS matrix
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000959 TRANSPOSE_K0XN0(1, N0, b_t, b, DATA_TYPE);
Gian Marco Iodicee7510622019-06-03 17:28:17 +0100960
961 // Partial matrix multiplication M0,N0,1
962 ARM_MM_K0XN0XM0(M0, N0, 1, a, b_t, c);
963
964 // Update the offset
965 lhs_offset += 1;
966 rhs_offset += rhs_stride_y;
967 }
968
969 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + (x * (uint)N0) * sizeof(int) + (y * (uint)M0 * dst_stride_y);
970
971 REPEAT_VAR_INIT_TO_CONST(M0, uint, zout, 0); //uint zout0=0,zout1=0,zout2=0,... zout7=0;
972
973#if defined(REINTERPRET_OUTPUT_AS_3D)
974 // The plane (zout) is calculated dividing M (y * M0) by HEIGHT_GEMM3D
975 CALCULATE_Z_OFFSET(M0, uint, zout, y, HEIGHT_GEMM3D, DEPTH_GEMM3D, dst_cross_plane_pad, dst_stride_y);
976
977 // Add offset for batched GEMM. The batches will be in the fourth dimension and for this reason we
978 // multiply dst_stride_z by DEPTH_GEMM3D
979 dst_addr += z * dst_stride_z * DEPTH_GEMM3D;
980
981#else // defined(REINTERPRET_OUTPUT_AS_3D)
982
983 // Add offset for batched GEMM
984 dst_addr += z * dst_stride_z;
985
986#endif // defined(REINTERPRET_OUTPUT_AS_3D)
987
988 // Convert and store output block
989 CONVERT_STORE_BLOCK(M0, N0, int, c, dst_addr, dst_stride_y, zout);
990}
991#endif // defined(M0) && defined(N0) && defined(K0) && defined(K)
992
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000993#endif // defined(DATA_TYPE) && defined(ACC_DATA_TYPE)
994
Gian Marco05288a22017-11-21 10:57:50 +0000995#if defined(COLS_A)
996/** OpenCL kernel used to compute the row-vectors of sums of all the entries in each row of Matrix A.
997 *
998 * @note This stage is needed to handle the offset of matrix product
999 * https://github.com/google/gemmlowp/blob/master/doc/low-precision.md
1000 *
1001 * @attention The number of matrix A columns needs to be passed at compile time using -DCOLS_A
Manuel Bottini959c26d2019-12-02 16:22:35 +00001002 * @note The input data type must be passed at compile time using -DDATA_TYPE (i.e. -DDATA_TYPE=uchar)
1003 * @note The data type for the accumulation must be passed at compile time using -DDATA_ACC_TYPE (i.e. -DDATA_ACC_TYPE=uint)
Gian Marco05288a22017-11-21 10:57:50 +00001004 *
1005 * @param[in] src_ptr Pointer to the source tensor. Supported data type: QASYMM8
1006 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
1007 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
1008 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
1009 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
1010 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
1011 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1012 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
1013 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: S32
1014 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
1015 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
1016 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
1017 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
1018 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
1019 */
1020__kernel void gemmlowp_matrix_a_reduction(TENSOR3D_DECLARATION(src),
1021 IMAGE_DECLARATION(dst))
1022{
1023 // Compute source and destination addresses
1024 Tensor3D src = CONVERT_TO_TENSOR3D_STRUCT(src);
1025 Image dst = CONVERT_TO_IMAGE_STRUCT(dst);
1026
Manuel Bottini959c26d2019-12-02 16:22:35 +00001027 VEC_DATA_TYPE(DATA_ACC_TYPE, 4)
1028 sum_row_32 = (VEC_DATA_TYPE(DATA_ACC_TYPE, 4))0;
1029 DATA_ACC_TYPE sum_row = 0;
Gian Marco05288a22017-11-21 10:57:50 +00001030
Manuel Bottini959c26d2019-12-02 16:22:35 +00001031 __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 +00001032
1033 int i = 0;
1034
1035 // This for loop performs 16 accumulations
1036 for(; i <= ((int)COLS_A - 16); i += 16)
1037 {
Manuel Bottini959c26d2019-12-02 16:22:35 +00001038 const VEC_DATA_TYPE(DATA_TYPE, 16) a0 = vload16(0, matrix_a + i);
Gian Marco05288a22017-11-21 10:57:50 +00001039
Manuel Bottini959c26d2019-12-02 16:22:35 +00001040 sum_row_32 += CONVERT(a0.s0123, VEC_DATA_TYPE(DATA_ACC_TYPE, 4)) + CONVERT(a0.s4567, VEC_DATA_TYPE(DATA_ACC_TYPE, 4)) + CONVERT(a0.s89AB, VEC_DATA_TYPE(DATA_ACC_TYPE, 4)) + CONVERT(a0.sCDEF,
1041 VEC_DATA_TYPE(DATA_ACC_TYPE, 4));
Gian Marco05288a22017-11-21 10:57:50 +00001042 }
1043
1044 // This for loop performs the leftover accumulations
1045 for(; i < COLS_A; ++i)
1046 {
Manuel Bottini959c26d2019-12-02 16:22:35 +00001047 sum_row += (DATA_ACC_TYPE)matrix_a[i];
Gian Marco05288a22017-11-21 10:57:50 +00001048 }
1049
Manuel Bottini959c26d2019-12-02 16:22:35 +00001050 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 +00001051
1052 *((__global int *)dst.ptr) = (int)sum_row;
1053}
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001054
1055#if defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8)
1056/** 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
1057 *
1058 * @note This stage is needed to handle the offset of matrix product
1059 * https://github.com/google/gemmlowp/blob/master/doc/low-precision.md
1060 *
1061 * @attention The number of matrix A columns needs to be passed at compile time using -DCOLS_A
Manuel Bottini959c26d2019-12-02 16:22:35 +00001062 * @note The input data type must be passed at compile time using -DDATA_TYPE (i.e. -DDATA_TYPE=uchar)
1063 * @note The data type for the accumulation must be passed at compile time using -DDATA_ACC_TYPE (i.e. -DDATA_ACC_TYPE=uint)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001064 *
1065 * @param[in] src_ptr Pointer to the source tensor. Supported data type: QASYMM8
1066 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
1067 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
1068 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
1069 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
1070 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
1071 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1072 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
1073 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: S32
1074 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
1075 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
1076 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
1077 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
1078 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
1079 */
1080__kernel void gemmlowp_matrix_a_reduction_dot8(TENSOR3D_DECLARATION(src),
1081 IMAGE_DECLARATION(dst))
1082{
1083 // Compute source and destination addresses
1084 Tensor3D src = CONVERT_TO_TENSOR3D_STRUCT(src);
1085 Image dst = CONVERT_TO_IMAGE_STRUCT(dst);
1086
Manuel Bottini959c26d2019-12-02 16:22:35 +00001087 DATA_ACC_TYPE sum_row = 0;
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001088
Manuel Bottini959c26d2019-12-02 16:22:35 +00001089 __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 +01001090
1091 int i = 0;
1092
1093 // This for loop performs 16 accumulations
1094 for(; i <= ((int)COLS_A - 32); i += 32)
1095 {
Manuel Bottini959c26d2019-12-02 16:22:35 +00001096 VEC_DATA_TYPE(DATA_TYPE, 16)
1097 a0 = vload16(0, matrix_a + i);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001098
Manuel Bottini959c26d2019-12-02 16:22:35 +00001099 sum_row += arm_dot(a0.s0123, (VEC_DATA_TYPE(DATA_TYPE, 4))(1));
1100 sum_row += arm_dot(a0.s4567, (VEC_DATA_TYPE(DATA_TYPE, 4))(1));
1101 sum_row += arm_dot(a0.s89AB, (VEC_DATA_TYPE(DATA_TYPE, 4))(1));
1102 sum_row += arm_dot(a0.sCDEF, (VEC_DATA_TYPE(DATA_TYPE, 4))(1));
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001103
Manuel Bottini959c26d2019-12-02 16:22:35 +00001104 a0 = vload16(1, matrix_a + i);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001105
Manuel Bottini959c26d2019-12-02 16:22:35 +00001106 sum_row += arm_dot(a0.s0123, (VEC_DATA_TYPE(DATA_TYPE, 4))(1));
1107 sum_row += arm_dot(a0.s4567, (VEC_DATA_TYPE(DATA_TYPE, 4))(1));
1108 sum_row += arm_dot(a0.s89AB, (VEC_DATA_TYPE(DATA_TYPE, 4))(1));
1109 sum_row += arm_dot(a0.sCDEF, (VEC_DATA_TYPE(DATA_TYPE, 4))(1));
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001110 }
1111
1112 // This for loop performs the leftover accumulations
1113 for(; i < COLS_A; ++i)
1114 {
Manuel Bottini959c26d2019-12-02 16:22:35 +00001115 sum_row += (DATA_ACC_TYPE)matrix_a[i];
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001116 }
1117
1118 *((__global int *)dst.ptr) = (int)sum_row;
1119}
1120#endif // defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8)
Gian Marco05288a22017-11-21 10:57:50 +00001121#endif // defined(COLS_A)
1122
1123#if defined(COLS_B) && defined(ROWS_B)
1124/** OpenCL kernel used to compute the row-vectors of sums of all the entries in each column of Matrix B.
1125 *
1126 * @note This stage is needed to handle the offset of matrix product
1127 * https://github.com/google/gemmlowp/blob/master/doc/low-precision.md
1128 *
1129 * @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 +00001130 * @note The input data type must be passed at compile time using -DDATA_TYPE (i.e. -DDATA_TYPE=uchar)
1131 * @note The data type for the accumulation must be passed at compile time using -DDATA_ACC_TYPE (i.e. -DDATA_ACC_TYPE=uint)
Gian Marco05288a22017-11-21 10:57:50 +00001132 *
1133 * @param[in] src_ptr Pointer to the source tensor. Supported data type: QASYMM8
1134 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
1135 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
1136 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
1137 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
1138 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
1139 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1140 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
1141 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: S32
1142 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
1143 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
1144 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
1145 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
1146 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
1147 */
1148__kernel void gemmlowp_matrix_b_reduction(TENSOR3D_DECLARATION(src),
1149 IMAGE_DECLARATION(dst))
1150{
1151 // Compute source and destination addresses
1152 Tensor3D src = CONVERT_TO_TENSOR3D_STRUCT(src);
1153 Image dst = CONVERT_TO_IMAGE_STRUCT(dst);
1154
Manuel Bottini959c26d2019-12-02 16:22:35 +00001155 VEC_DATA_TYPE(DATA_ACC_TYPE, 16)
1156 sum_col_32 = (VEC_DATA_TYPE(DATA_ACC_TYPE, 16))0;
Gian Marco05288a22017-11-21 10:57:50 +00001157
Manuel Bottini959c26d2019-12-02 16:22:35 +00001158 __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 +00001159
1160 int i = 0;
1161 // This for loop performs 4 accumulations
1162 for(; i <= ((int)ROWS_B - 4); i += 4)
1163 {
Manuel Bottini959c26d2019-12-02 16:22:35 +00001164 const VEC_DATA_TYPE(DATA_TYPE, 16)
1165 b0 = vload16(0, matrix_b + 0 * src_stride_y);
1166 const VEC_DATA_TYPE(DATA_TYPE, 16)
1167 b1 = vload16(0, matrix_b + 1 * src_stride_y);
1168 const VEC_DATA_TYPE(DATA_TYPE, 16)
1169 b2 = vload16(0, matrix_b + 2 * src_stride_y);
1170 const VEC_DATA_TYPE(DATA_TYPE, 16)
1171 b3 = vload16(0, matrix_b + 3 * src_stride_y);
Gian Marco05288a22017-11-21 10:57:50 +00001172
Manuel Bottini959c26d2019-12-02 16:22:35 +00001173 sum_col_32 += CONVERT(b0, VEC_DATA_TYPE(DATA_ACC_TYPE, 16)) + CONVERT(b1, VEC_DATA_TYPE(DATA_ACC_TYPE, 16)) + CONVERT(b2, VEC_DATA_TYPE(DATA_ACC_TYPE, 16)) + CONVERT(b3, VEC_DATA_TYPE(DATA_ACC_TYPE,
1174 16));
Gian Marco05288a22017-11-21 10:57:50 +00001175
1176 matrix_b += 4 * src_stride_y;
1177 }
1178
1179 // This for loop perfoms the leftover accumulations
1180 for(; i < (int)ROWS_B; ++i)
1181 {
Manuel Bottini959c26d2019-12-02 16:22:35 +00001182 const VEC_DATA_TYPE(DATA_TYPE, 16)
1183 b0 = vload16(0, matrix_b);
Gian Marco05288a22017-11-21 10:57:50 +00001184
Manuel Bottini959c26d2019-12-02 16:22:35 +00001185 sum_col_32 += CONVERT(b0, VEC_DATA_TYPE(DATA_ACC_TYPE, 16));
Gian Marco05288a22017-11-21 10:57:50 +00001186
1187 matrix_b += src_stride_y;
1188 }
1189
Manuel Bottini959c26d2019-12-02 16:22:35 +00001190 vstore16(convert_int16(sum_col_32), 0, (__global int *)dst.ptr);
Gian Marco05288a22017-11-21 10:57:50 +00001191}
1192#endif // defined(COLS_B) && defined(ROWS_B)
1193
1194#if defined(K_OFFSET)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001195
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001196/* Helper function used to calculate the offset contribution after matrix multiplication.
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001197 *
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001198 * This kernel takes a final int32 accumulator value (the output of matrix multiplication),
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001199 * and calculates the offset contribution of matrix A and matrix B.
1200 *
1201 * @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)
1202 * @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)
1203 * @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)
1204 * @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
1205 *
1206 * @param[in] x get_global_id(0) * 4
1207 * @param[in] y get_global_id(1)
1208 * @param[in] z get_global_id(2)
1209 * @param[in] sum_col_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr
1210 * @param[in] sum_col_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
1211 * @param[in] sum_col_step_x (Optional) sum_col_stride_x * number of elements along X processed per workitem(in bytes)
1212 * @param[in] sum_col_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
1213 * @param[in] sum_col_step_y (Optional) sum_col_stride_y * number of elements along Y processed per workitem(in bytes)
1214 * @param[in] sum_col_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
1215 * @param[in] sum_row_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr
1216 * @param[in] sum_row_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
1217 * @param[in] sum_row_step_x (Optional) sum_row_stride_x * number of elements along X processed per workitem(in bytes)
1218 * @param[in] sum_row_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
1219 * @param[in] sum_row_step_y (Optional) sum_row_stride_y * number of elements along Y processed per workitem(in bytes)
1220 * @param[in] sum_row_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
1221 * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: same as @p src_ptr
1222 * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes)
1223 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
1224 * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases tensor
1225 */
1226inline int4 offset_contribution(
1227 int x,
1228 int y,
1229 int z
1230#if defined(A_OFFSET)
1231 ,
1232 IMAGE_DECLARATION(sum_col)
1233#endif // defined(A_OFFSET)
1234#if defined(B_OFFSET)
1235 ,
1236 IMAGE_DECLARATION(sum_row)
1237#endif // defined(B_OFFSET)
1238#if defined(ADD_BIAS)
1239 ,
1240 VECTOR_DECLARATION(biases)
1241#endif // defined(ADD_BIAS)
1242)
1243{
1244 int4 a_offset_s32 = (int4)0;
1245 int4 b_offset_s32 = (int4)0;
1246
1247 int batch_id = z;
1248#if defined(DEPTH_INPUT3D)
1249 batch_id /= (int)DEPTH_INPUT3D;
1250#endif // defined(DEPTH_INPUT3D)
1251
1252#if defined(A_OFFSET)
1253 // Compute the offset contribution due to A_OFFSET
1254 __global uchar *sum_col_addr = sum_col_ptr + sum_col_offset_first_element_in_bytes + x * sizeof(int);
1255
1256 // Compute the offset contribution due to A_OFFSET
1257#if defined(SUM_COL_HAS_BATCHES)
1258 a_offset_s32 = vload4(0, (__global int *)(sum_col_addr + batch_id * sum_col_stride_y));
1259#else // defined(SUM_COL_HAS_BATCHES)
1260 a_offset_s32 = vload4(0, (__global int *)sum_col_addr);
1261#endif // defined(SUM_COL_HAS_BATCHES)
1262
1263 a_offset_s32 *= (int4)A_OFFSET;
1264#endif // defined(A_OFFSET)
1265
1266#if defined(B_OFFSET)
1267 // Compute the offset contribution due to A_OFFSET
1268 __global uchar *sum_row_addr = sum_row_ptr + sum_row_offset_first_element_in_bytes + y * sizeof(int);
1269
1270 // Compute the offset contribution due to B_OFFSET
1271#if defined(HEIGHT_INPUT3D) && defined(DEPTH_INPUT3D)
1272 b_offset_s32 = (int4) * (((__global int *)(sum_row_addr + batch_id * sum_row_stride_y)) + (z % (int)DEPTH_INPUT3D) * (int)HEIGHT_INPUT3D);
1273#else // defined(HEIGHT_INPUT3D) && defined(DEPTH_INPUT3D)
1274 b_offset_s32 = (int4) * (((__global int *)(sum_row_addr + batch_id * sum_row_stride_y)));
1275#endif // defined(HEIGHT_INPUT3D) && defined(DEPTH_INPUT3D)
1276 b_offset_s32 *= (int4)B_OFFSET;
1277#endif // defined(B_OFFSET)
1278
1279#if defined(ADD_BIAS)
1280 // Add bias
1281 __global uchar *bias_addr = biases_ptr + biases_offset_first_element_in_bytes + x * sizeof(int);
1282
1283 int4 biases_values = vload4(0, (__global int *)bias_addr);
1284 b_offset_s32 += (int4)biases_values;
1285#endif // defined(ADD_BIAS)
1286
1287 return (int4)K_OFFSET + a_offset_s32 + b_offset_s32;
1288}
1289
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001290/* OpenCL kernel used to add the offset contribution after matrix multiplication. The computation is performed in-place
Gian Marco05288a22017-11-21 10:57:50 +00001291 *
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001292 * This kernel takes a final int32 accumulator value (the output of matrix multiplication),
Gian Marco05288a22017-11-21 10:57:50 +00001293 * and adds to it the offset contribution of matrix A and matrix B in-place.
1294 *
1295 * @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)
1296 * @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)
1297 * @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 +07001298 * @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 +00001299 *
1300 * The final result is:
1301 *
1302 * mm_result[i][k] = mm_result[i][k] +
1303 * (sum_col[k] * A_OFFSET) +
1304 * (sum_row[i] * B_OFFSET) +
1305 * (K_OFFSET)
1306 *
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +01001307 * @param[in] mm_result_ptr Pointer to the source tensor. Supported data type: S32
1308 * @param[in] mm_result_stride_x Stride of the source tensor in X dimension (in bytes)
1309 * @param[in] mm_result_step_x mm_result_stride_x * number of elements along X processed per workitem(in bytes)
1310 * @param[in] mm_result_stride_y Stride of the source tensor in Y dimension (in bytes)
1311 * @param[in] mm_result_step_y mm_result_stride_y * number of elements along Y processed per workitem(in bytes)
1312 * @param[in] mm_result_stride_z Stride of the source tensor in Z dimension (in bytes)
1313 * @param[in] mm_result_step_z mm_result_stride_z * number of elements along Z processed per workitem(in bytes)
1314 * @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 +01001315 * @param[in] sum_col_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr
1316 * @param[in] sum_col_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
1317 * @param[in] sum_col_step_x (Optional) sum_col_stride_x * number of elements along X processed per workitem(in bytes)
1318 * @param[in] sum_col_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
1319 * @param[in] sum_col_step_y (Optional) sum_col_stride_y * number of elements along Y processed per workitem(in bytes)
1320 * @param[in] sum_col_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
1321 * @param[in] sum_row_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr
1322 * @param[in] sum_row_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
1323 * @param[in] sum_row_step_x (Optional) sum_row_stride_x * number of elements along X processed per workitem(in bytes)
1324 * @param[in] sum_row_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
1325 * @param[in] sum_row_step_y (Optional) sum_row_stride_y * number of elements along Y processed per workitem(in bytes)
1326 * @param[in] sum_row_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
1327 * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: same as @p src_ptr
1328 * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes)
1329 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
1330 * @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 +00001331 */
1332__kernel void gemmlowp_offset_contribution(TENSOR3D_DECLARATION(mm_result)
1333#if defined(A_OFFSET)
1334 ,
1335 IMAGE_DECLARATION(sum_col)
1336#endif // defined(A_OFFSET)
1337#if defined(B_OFFSET)
1338 ,
1339 IMAGE_DECLARATION(sum_row)
1340#endif // defined(B_OFFSET)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001341#if defined(ADD_BIAS)
1342 ,
1343 VECTOR_DECLARATION(biases)
1344#endif // defined(ADD_BIAS))
Gian Marco05288a22017-11-21 10:57:50 +00001345 )
1346{
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001347 const int x = get_global_id(0) * 4;
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +01001348 const int y = get_global_id(1);
1349 const int z = get_global_id(2);
1350
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001351 // Compute offset contribution
1352 int4 offset_term_s32 = offset_contribution(
1353 x, y, z
Gian Marco05288a22017-11-21 10:57:50 +00001354#if defined(A_OFFSET)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001355 ,
1356 sum_col_ptr,
1357 sum_col_stride_x,
1358 sum_col_step_x,
1359 sum_col_stride_y,
1360 sum_col_step_y,
1361 sum_col_offset_first_element_in_bytes
Gian Marco05288a22017-11-21 10:57:50 +00001362#endif // defined(A_OFFSET)
Gian Marco05288a22017-11-21 10:57:50 +00001363#if defined(B_OFFSET)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001364 ,
1365 sum_row_ptr,
1366 sum_row_stride_x,
1367 sum_row_step_x,
1368 sum_row_stride_y,
1369 sum_row_step_y,
1370 sum_row_offset_first_element_in_bytes
Gian Marco05288a22017-11-21 10:57:50 +00001371#endif // defined(B_OFFSET)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001372#if defined(ADD_BIAS)
1373 ,
1374 biases_ptr,
1375 biases_stride_x,
1376 biases_step_x,
1377 biases_offset_first_element_in_bytes
1378#endif // defined(ADD_BIAS)
1379 );
Gian Marco05288a22017-11-21 10:57:50 +00001380
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001381 __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 +00001382
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001383 int4 in_s32 = vload4(0, (__global int *)mm_result_addr);
Gian Marco05288a22017-11-21 10:57:50 +00001384
1385 // Add the offset terms to GEMM's result
1386 in_s32 += offset_term_s32;
1387
1388 // Store the result with the offset contribution
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001389 vstore4(in_s32, 0, (__global int *)mm_result_addr);
Gian Marco05288a22017-11-21 10:57:50 +00001390}
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001391
1392#if defined(RESULT_OFFSET) && defined(RESULT_MULTIPLIER) && defined(RESULT_SHIFT)
1393/* OpenCL kernel used to add the offset contribution after @ref CLGEMMLowpMatrixMultiplyKernel and it quantizes down to uint8.
1394 *
1395 * 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.
1396 *
1397 *
1398 * @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)
1399 * @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)
1400 * @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)
1401 * @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
1402 *
1403 * The result before the output stage is:
1404 *
1405 * mm_result[i][k] = mm_result[i][k] +
1406 * (sum_col[k] * A_OFFSET) +
1407 * (sum_row[i] * B_OFFSET) +
1408 * (K_OFFSET)
1409 *
Manuel Bottini959c26d2019-12-02 16:22:35 +00001410 * 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 +01001411 *
1412 * -# Add offset terms to final result
1413 * -# Multiply each entry of result by result_mult_int
1414 * -# Add bias to final result (if -DADD_BIAS is passed at compile time)
1415 * -# Shift the int32 accumulator by result_shift
1416 * -# 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 +00001417 * -# Clamp the resulting int32 values:
1418 * - to the [0..255] range and cast to QASYMM8.
1419 * - to the [-128..127] range and cast to QASYMM8_SIGNED.
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001420 *
1421 * @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
1422 *
1423 * @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 +00001424 * @note The output datatype should be passed at compile time using -DOUTPUT_DATA_TYPE
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001425 * @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.
1426 * These values can be used to implement "rectified linear unit" activation functions
1427 *
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001428 * @param[in] mm_result_ptr Pointer to the source tensor. Supported data type: S32
1429 * @param[in] mm_result_stride_x Stride of the source tensor in X dimension (in bytes)
1430 * @param[in] mm_result_step_x mm_result_stride_x * number of elements along X processed per workitem(in bytes)
1431 * @param[in] mm_result_stride_y Stride of the source tensor in Y dimension (in bytes)
1432 * @param[in] mm_result_step_y mm_result_stride_y * number of elements along Y processed per workitem(in bytes)
1433 * @param[in] mm_result_stride_z Stride of the source tensor in Z dimension (in bytes)
1434 * @param[in] mm_result_step_z mm_result_stride_z * number of elements along Z processed per workitem(in bytes)
1435 * @param[in] mm_result_offset_first_element_in_bytes The offset of the first element in the source tensor
1436 * @param[in] sum_col_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr
1437 * @param[in] sum_col_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
1438 * @param[in] sum_col_step_x (Optional) sum_col_stride_x * number of elements along X processed per workitem(in bytes)
1439 * @param[in] sum_col_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
1440 * @param[in] sum_col_step_y (Optional) sum_col_stride_y * number of elements along Y processed per workitem(in bytes)
1441 * @param[in] sum_col_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
1442 * @param[in] sum_row_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr
1443 * @param[in] sum_row_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
1444 * @param[in] sum_row_step_x (Optional) sum_row_stride_x * number of elements along X processed per workitem(in bytes)
1445 * @param[in] sum_row_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
1446 * @param[in] sum_row_step_y (Optional) sum_row_stride_y * number of elements along Y processed per workitem(in bytes)
1447 * @param[in] sum_row_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
1448 * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: same as @p src_ptr
1449 * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes)
1450 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
1451 * @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 +00001452 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: QASYMM8/QASYMM8_SIGNED
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001453 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
1454 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
1455 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
1456 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
1457 * @param[in] dst_stride_z Stride of the source tensor in Z dimension (in bytes)
1458 * @param[in] dst_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1459 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
1460 * @param[in] result_multipliers_ptr (Optional) Pointer to the output multipliers vector for per-channel quantization. Supported data types: S32
1461 * @param[in] result_multipliers_stride_x (Optional) Stride of the output multipliers vector in X dimension (in bytes)
1462 * @param[in] result_multipliers_step_x (Optional) output_multipliers_stride_x * number of elements along X processed per workitem(in bytes)
1463 * @param[in] result_multipliers_offset_first_element_in_bytes (Optional) The offset of the first element in the output multipliers vector
1464 * @param[in] result_shifts_ptr (Optional) Pointer to the output shifts vector for per-channel quantization. Supported data types: S32
1465 * @param[in] result_shifts_stride_x (Optional) Stride of the output shifts vector in X dimension (in bytes)
1466 * @param[in] result_shifts_step_x (Optional) output_shifts_stride_x * number of elements along X processed per workitem(in bytes)
1467 * @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 +01001468 */
1469__kernel void gemmlowp_offset_contribution_quantize_down(TENSOR3D_DECLARATION(mm_result)
1470#if defined(A_OFFSET)
1471 ,
1472 IMAGE_DECLARATION(sum_col)
1473#endif // defined(A_OFFSET)
1474#if defined(B_OFFSET)
1475 ,
1476 IMAGE_DECLARATION(sum_row)
1477#endif // defined(B_OFFSET)
1478 ,
1479#if defined(ADD_BIAS)
1480 VECTOR_DECLARATION(biases),
1481#endif // defined(ADD_BIAS)
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001482 TENSOR3D_DECLARATION(dst)
1483#if defined(PER_CHANNEL_QUANTIZATION)
1484 ,
1485 VECTOR_DECLARATION(result_multipliers),
1486 VECTOR_DECLARATION(result_shifts)
1487#endif // defined(PER_CHANNEL_QUANTIZATION)
1488 )
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001489{
1490 const int x = get_global_id(0) * 4;
1491 const int y = get_global_id(1);
1492 const int z = get_global_id(2);
1493
1494 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x + y * dst_stride_y + z * dst_stride_z;
1495
1496 // Compute offset contribution
1497 int4 offset_term_s32 = offset_contribution(
1498 x, y, z
1499#if defined(A_OFFSET)
1500 ,
1501 sum_col_ptr,
1502 sum_col_stride_x,
1503 sum_col_step_x,
1504 sum_col_stride_y,
1505 sum_col_step_y,
1506 sum_col_offset_first_element_in_bytes
1507#endif // defined(A_OFFSET)
1508#if defined(B_OFFSET)
1509 ,
1510 sum_row_ptr,
1511 sum_row_stride_x,
1512 sum_row_step_x,
1513 sum_row_stride_y,
1514 sum_row_step_y,
1515 sum_row_offset_first_element_in_bytes
1516#endif // defined(B_OFFSET)
1517#if defined(ADD_BIAS)
1518 ,
1519 biases_ptr,
1520 biases_stride_x,
1521 biases_step_x,
1522 biases_offset_first_element_in_bytes
1523#endif // defined(ADD_BIAS)
1524 );
1525
1526 __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;
1527
1528 int4 in_s32 = vload4(0, (__global int *)mm_result_addr);
1529
1530 // Add the offset terms to GEMM's result
1531 in_s32 += offset_term_s32;
1532
1533 // -------------- OUTPUT STAGE
1534
1535 // Add the offset terms to GEMM's result
1536 in_s32 += (int4)RESULT_OFFSET;
1537
1538 // Multiply by result_mult_int and shift
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001539#if defined(PER_CHANNEL_QUANTIZATION)
1540 __global uchar *result_multipliers_addr = result_multipliers_ptr + result_multipliers_offset_first_element_in_bytes + x * sizeof(int);
1541 __global uchar *result_shifts_addr = result_shifts_ptr + result_shifts_offset_first_element_in_bytes + x * sizeof(int);
1542 int4 result_multipliers_values = vload4(0, (__global int *)result_multipliers_addr);
1543 int4 result_shifts_values = vload4(0, (__global int *)result_shifts_addr);
1544
1545 in_s32 *= result_multipliers_values;
1546 in_s32 >>= result_shifts_values;
1547#else // defined(PER_CHANNEL_QUANTIZATION)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001548 in_s32 *= RESULT_MULTIPLIER;
1549
1550 in_s32 >>= RESULT_SHIFT;
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001551#endif // defined(PER_CHANNEL_QUANTIZATION)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001552
Manuel Bottini959c26d2019-12-02 16:22:35 +00001553 VEC_DATA_TYPE(OUTPUT_DATA_TYPE, 4)
1554 res = CONVERT_SAT(in_s32, VEC_DATA_TYPE(OUTPUT_DATA_TYPE, 4));
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001555
1556#if defined(MIN_BOUND)
Manuel Bottini959c26d2019-12-02 16:22:35 +00001557 res = max(res, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, 4))MIN_BOUND);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001558#endif // defined(MIN_BOUND)
1559#if defined(MAX_BOUND)
Manuel Bottini959c26d2019-12-02 16:22:35 +00001560 res = min(res, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, 4))MAX_BOUND);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001561#endif // defined(MAX_BOUND)
1562
1563 // Store the result
Manuel Bottini959c26d2019-12-02 16:22:35 +00001564 vstore4(res, 0, (__global OUTPUT_DATA_TYPE *)dst_addr);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001565}
1566
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001567/* 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 +01001568 *
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001569 * 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 +01001570 *
1571 *
1572 * @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)
1573 * @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)
1574 * @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)
1575 * @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
1576 *
1577 * The result before the output stage is:
1578 *
1579 * mm_result[i][k] = mm_result[i][k] +
1580 * (sum_col[k] * A_OFFSET) +
1581 * (sum_row[i] * B_OFFSET) +
1582 * (K_OFFSET)
1583 *
Manuel Bottini959c26d2019-12-02 16:22:35 +00001584 * 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 +01001585 *
1586 * -# Compute fixed point multiplication between each entry of input by result_fixedpoint_multiplier
1587 * -# Add bias to final result if bias tensor is not a nullptr
1588 * -# Round to nearest division by a power-of-two using result_shift
1589 * -# Add offset to each result
1590 * -# Clamp the value between the specified min and max bounds
Manuel Bottini959c26d2019-12-02 16:22:35 +00001591 * -# Clamp the resulting int32 values:
1592 * - to the [0..255] range and cast to QASYMM8.
1593 * - to the [-128..127] range and cast to QASYMM8_SIGNED.
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001594 *
1595 * @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
1596 *
1597 * @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 +00001598 * @note The output datatype should be passed at compile time using -DOUTPUT_DATA_TYPE
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001599 * @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.
1600 * These values can be used to implement "rectified linear unit" activation functions
1601 *
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001602 * @param[in] mm_result_ptr Pointer to the source tensor. Supported data type: S32
1603 * @param[in] mm_result_stride_x Stride of the source tensor in X dimension (in bytes)
1604 * @param[in] mm_result_step_x mm_result_stride_x * number of elements along X processed per workitem(in bytes)
1605 * @param[in] mm_result_stride_y Stride of the source tensor in Y dimension (in bytes)
1606 * @param[in] mm_result_step_y mm_result_stride_y * number of elements along Y processed per workitem(in bytes)
1607 * @param[in] mm_result_stride_z Stride of the source tensor in Z dimension (in bytes)
1608 * @param[in] mm_result_step_z mm_result_stride_z * number of elements along Z processed per workitem(in bytes)
1609 * @param[in] mm_result_offset_first_element_in_bytes The offset of the first element in the source tensor
1610 * @param[in] sum_col_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr
1611 * @param[in] sum_col_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
1612 * @param[in] sum_col_step_x (Optional) sum_col_stride_x * number of elements along X processed per workitem(in bytes)
1613 * @param[in] sum_col_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
1614 * @param[in] sum_col_step_y (Optional) sum_col_stride_y * number of elements along Y processed per workitem(in bytes)
1615 * @param[in] sum_col_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
1616 * @param[in] sum_row_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr
1617 * @param[in] sum_row_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
1618 * @param[in] sum_row_step_x (Optional) sum_row_stride_x * number of elements along X processed per workitem(in bytes)
1619 * @param[in] sum_row_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
1620 * @param[in] sum_row_step_y (Optional) sum_row_stride_y * number of elements along Y processed per workitem(in bytes)
1621 * @param[in] sum_row_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
1622 * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: same as @p src_ptr
1623 * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes)
1624 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
1625 * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases tensor
1626 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: QASYMM8
1627 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
1628 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
1629 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
1630 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
1631 * @param[in] dst_stride_z Stride of the source tensor in Z dimension (in bytes)
1632 * @param[in] dst_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1633 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
1634 * @param[in] result_multipliers_ptr (Optional) Pointer to the output multipliers vector for per-channel quantization. Supported data types: S32
1635 * @param[in] result_multipliers_stride_x (Optional) Stride of the output multipliers vector in X dimension (in bytes)
1636 * @param[in] result_multipliers_step_x (Optional) output_multipliers_stride_x * number of elements along X processed per workitem(in bytes)
1637 * @param[in] result_multipliers_offset_first_element_in_bytes (Optional) The offset of the first element in the output multipliers vector
1638 * @param[in] result_shifts_ptr (Optional) Pointer to the output shifts vector for per-channel quantization. Supported data types: S32
1639 * @param[in] result_shifts_stride_x (Optional) Stride of the output shifts vector in X dimension (in bytes)
1640 * @param[in] result_shifts_step_x (Optional) output_shifts_stride_x * number of elements along X processed per workitem(in bytes)
1641 * @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 +01001642 */
1643__kernel void gemmlowp_offset_contribution_quantize_down_fixedpoint(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)
1652 ,
1653#if defined(ADD_BIAS)
1654 VECTOR_DECLARATION(biases),
1655#endif // defined(ADD_BIAS)
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001656 TENSOR3D_DECLARATION(dst)
1657#if defined(PER_CHANNEL_QUANTIZATION)
1658 ,
1659 VECTOR_DECLARATION(result_multipliers),
1660 VECTOR_DECLARATION(result_shifts)
1661#endif // defined(PER_CHANNEL_QUANTIZATION)
1662 )
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001663{
1664 const int x = get_global_id(0) * 4;
1665 const int y = get_global_id(1);
1666 const int z = get_global_id(2);
1667
1668 // Compute offset contribution
1669 int4 offset_term_s32 = offset_contribution(
1670 x, y, z
1671#if defined(A_OFFSET)
1672 ,
1673 sum_col_ptr,
1674 sum_col_stride_x,
1675 sum_col_step_x,
1676 sum_col_stride_y,
1677 sum_col_step_y,
1678 sum_col_offset_first_element_in_bytes
1679#endif // defined(A_OFFSET)
1680#if defined(B_OFFSET)
1681 ,
1682 sum_row_ptr,
1683 sum_row_stride_x,
1684 sum_row_step_x,
1685 sum_row_stride_y,
1686 sum_row_step_y,
1687 sum_row_offset_first_element_in_bytes
1688#endif // defined(B_OFFSET)
1689#if defined(ADD_BIAS)
1690 ,
1691 biases_ptr,
1692 biases_stride_x,
1693 biases_step_x,
1694 biases_offset_first_element_in_bytes
1695#endif // defined(ADD_BIAS)
1696 );
1697
1698 __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;
1699
1700 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x + y * dst_stride_y + z * dst_stride_z;
1701
1702 int4 in_s32 = vload4(0, (__global int *)mm_result_addr);
1703
1704 // Add the offset terms to GEMM's result
1705 in_s32 += offset_term_s32;
1706
1707 // -------------- OUTPUT STAGE
1708
1709 // Multiply by result_mult_int and shift
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001710#if defined(PER_CHANNEL_QUANTIZATION)
1711 __global uchar *result_multipliers_addr = result_multipliers_ptr + result_multipliers_offset_first_element_in_bytes + x * sizeof(int);
1712 __global uchar *result_shifts_addr = result_shifts_ptr + result_shifts_offset_first_element_in_bytes + x * sizeof(int);
1713 int4 result_multipliers_values = vload4(0, (__global int *)result_multipliers_addr);
1714 int4 result_shifts_values = vload4(0, (__global int *)result_shifts_addr);
1715
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +01001716 int4 in_s32_shift_lt0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(in_s32, result_multipliers_values, result_shifts_values, 4);
1717 int4 in_s32_shift_gt0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(in_s32, result_multipliers_values, result_shifts_values, 4);
1718 in_s32 = select(in_s32_shift_lt0, in_s32_shift_gt0, result_shifts_values >= 0);
1719#else // defined(PER_CHANNEL_QUANTIZATION)
1720
1721#if RESULT_SHIFT < 0
1722 in_s32 = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(in_s32, RESULT_MULTIPLIER, RESULT_SHIFT, 4);
1723#else // RESULT_SHIFT >= 0
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001724 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 +01001725#endif // RESULT_SHIFT < 0
1726
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001727#endif // defined(PER_CHANNEL_QUANTIZATION)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001728
1729 // Add the offset terms to GEMM's result
1730 in_s32 += (int4)RESULT_OFFSET;
1731
Manuel Bottini959c26d2019-12-02 16:22:35 +00001732 VEC_DATA_TYPE(OUTPUT_DATA_TYPE, 4)
1733 res = CONVERT_SAT(in_s32, VEC_DATA_TYPE(OUTPUT_DATA_TYPE, 4));
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001734
1735#if defined(MIN_BOUND)
Manuel Bottini959c26d2019-12-02 16:22:35 +00001736 res = max(res, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, 4))MIN_BOUND);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001737#endif // defined(MIN_BOUND)
1738#if defined(MAX_BOUND)
Manuel Bottini959c26d2019-12-02 16:22:35 +00001739 res = min(res, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, 4))MAX_BOUND);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001740#endif // defined(MAX_BOUND)
1741
1742 // Store the result
Manuel Bottini959c26d2019-12-02 16:22:35 +00001743 vstore4(res, 0, (__global OUTPUT_DATA_TYPE *)dst_addr);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001744}
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001745#endif // defined(RESULT_OFFSET) && defined(RESULT_MULTIPLIER) && defined(RESULT_SHIFT)
1746
Gian Marco05288a22017-11-21 10:57:50 +00001747#endif // defined(K_OFFSET)
1748
1749#if defined(RESULT_OFFSET) && defined(RESULT_MULT_INT) && defined(RESULT_SHIFT)
1750/** This OpenCL kernel is used to quantize down the int32 accumulator values of GEMMLowp to QASYMM8
1751 *
1752 * This kernel takes a final int32 accumulator value and processes it to obtain the final QASYMM8 value.
1753 * The following computations will be performed by the kernel:
1754 *
1755 * -# Add offset terms to final result
1756 * -# Multiply each entry of result by result_mult_int
1757 * -# Add bias to final result (if -DADD_BIAS is passed at compile time)
1758 * -# Shift the int32 accumulator by result_shift
1759 * -# Clamp the value between the specified min and max bounds (if -DMIN_BOUND and/or -DMAX_BOUND are passed at compile time)
1760 * -# Clamp the resulting int32 values to the [0..255] range and cast to QASYMM8.
1761 *
1762 * @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
1763 *
1764 * @note In case the addition of int32 biases is required, -DADD_BIAS should be passed at compile time
1765 * @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.
1766 * These values can be used to implement "rectified linear unit" activation functions
1767 *
1768 * @param[in] src_ptr Pointer to the source tensor. Supported data type: S32
1769 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
1770 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
1771 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
1772 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
1773 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
1774 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1775 * @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 +01001776 * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: same as @p src_ptr
1777 * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes)
1778 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
1779 * @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 +00001780 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: QASYMM8
1781 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
1782 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
1783 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
1784 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
1785 * @param[in] dst_stride_z Stride of the source tensor in Z dimension (in bytes)
1786 * @param[in] dst_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1787 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
1788 */
1789__kernel void gemmlowp_output_stage_quantize_down(TENSOR3D_DECLARATION(src),
1790#if defined(ADD_BIAS)
1791 VECTOR_DECLARATION(biases),
1792#endif // defined(ADD_BIAS)
1793 TENSOR3D_DECLARATION(dst))
1794{
1795 // Compute source and destination addresses
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001796 int x = get_global_id(0) * 4;
1797 int y = get_global_id(1);
1798 int z = get_global_id(2);
Gian Marco05288a22017-11-21 10:57:50 +00001799
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001800 __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 +00001801
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001802 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x + y * dst_stride_y + z * dst_stride_z;
1803
1804 int4 input_values = vload4(0, (__global int *)src_addr);
Gian Marco58c57942017-11-28 09:10:03 +00001805
Gian Marco05288a22017-11-21 10:57:50 +00001806#if defined(ADD_BIAS)
1807 // Add bias
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001808 __global uchar *bias_addr = biases_ptr + biases_offset_first_element_in_bytes + x * sizeof(int);
1809
1810 int4 biases_values = vload4(0, (__global int *)bias_addr);
1811 input_values += (int4)biases_values;
Gian Marco05288a22017-11-21 10:57:50 +00001812#endif // defined(ADD_BIAS)
1813
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001814 // Add the offset terms to GEMM's result
1815 input_values += (int4)RESULT_OFFSET;
1816
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +00001817 // Multiply by result_mult_int and shift
Gian Marco58c57942017-11-28 09:10:03 +00001818 input_values *= RESULT_MULT_INT;
Gian Marco05288a22017-11-21 10:57:50 +00001819
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +01001820#if RESULT_SHIFT < 0
1821 input_values >>= -RESULT_SHIFT;
1822#else // RESULT_SHIFT >= 0
Gian Marco58c57942017-11-28 09:10:03 +00001823 input_values >>= RESULT_SHIFT;
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +01001824#endif // RESULT_SHIFT < 0
Gian Marco05288a22017-11-21 10:57:50 +00001825
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001826 uchar4 res = convert_uchar4_sat(input_values);
Gian Marco05288a22017-11-21 10:57:50 +00001827
1828#if defined(MIN_BOUND)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001829 res = max(res, (uchar4)MIN_BOUND);
Gian Marco05288a22017-11-21 10:57:50 +00001830#endif // defined(MIN_BOUND)
1831#if defined(MAX_BOUND)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001832 res = min(res, (uchar4)MAX_BOUND);
Gian Marco05288a22017-11-21 10:57:50 +00001833#endif // defined(MAX_BOUND)
1834
1835 // Store the result
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001836 vstore4(res, 0, dst_addr);
Gian Marco05288a22017-11-21 10:57:50 +00001837}
Gian Marco58c57942017-11-28 09:10:03 +00001838#endif // defined(RESULT_OFFSET) && defined(RESULT_MULT_INT) && defined(RESULT_SHIFT)
1839
1840#if defined(RESULT_OFFSET_AFTER_SHIFT) && defined(RESULT_FIXEDPOINT_MULTIPLIER) && defined(RESULT_SHIFT)
Manuel Bottini959c26d2019-12-02 16:22:35 +00001841/** 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 +00001842 *
Manuel Bottini959c26d2019-12-02 16:22:35 +00001843 * 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 +00001844 * The following computations will be performed by the kernel:
1845 *
1846 * -# Compute fixed point multiplication between each entry of input by result_fixedpoint_multiplier
1847 * -# Add bias to final result if bias tensor is not a nullptr
1848 * -# Round to nearest division by a power-of-two using result_shift
1849 * -# Add offset to each result
1850 * -# Clamp the value between the specified min and max bounds
Manuel Bottini1f332d42019-11-29 17:25:25 +00001851 * -# Clamp the resulting int32 values:
1852 * - to the [0..255] range and cast to QASYMM8.
1853 * - to the [-128..127] range and cast to QASYMM8_SIGNED.
Gian Marco58c57942017-11-28 09:10:03 +00001854 *
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001855 * @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 +00001856 *
1857 * @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 +00001858 * @note The output datatype should be passed at compile time using -DOUTPUT_DATA_TYPE
Gian Marco58c57942017-11-28 09:10:03 +00001859 * @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.
1860 * These values can be used to implement "rectified linear unit" activation functions
1861 *
1862 * @param[in] src_ptr Pointer to the source tensor. Supported data type: S32
1863 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
1864 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
1865 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
1866 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
1867 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
1868 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1869 * @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 +01001870 * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: same as @p src_ptr
1871 * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes)
1872 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
1873 * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases tensor
Gian Marco58c57942017-11-28 09:10:03 +00001874 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: QASYMM8
1875 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
1876 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
1877 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
1878 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
1879 * @param[in] dst_stride_z Stride of the source tensor in Z dimension (in bytes)
1880 * @param[in] dst_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1881 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
1882 */
1883__kernel void gemmlowp_output_stage_quantize_down_fixedpoint(TENSOR3D_DECLARATION(src),
1884#if defined(ADD_BIAS)
1885 VECTOR_DECLARATION(biases),
1886#endif // defined(ADD_BIAS)
1887 TENSOR3D_DECLARATION(dst))
1888{
1889 // Compute source and destination addresses
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001890 int x = get_global_id(0) * 4;
1891 int y = get_global_id(1);
1892 int z = get_global_id(2);
Georgios Pinitas932491f2018-09-21 16:33:15 +01001893
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001894 __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 +00001895
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001896 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x + y * dst_stride_y + z * dst_stride_z;
1897
1898 int4 input_values = vload4(0, (__global int *)src_addr);
Gian Marco58c57942017-11-28 09:10:03 +00001899
1900#if defined(ADD_BIAS)
1901 // Add bias
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001902 __global uchar *bias_addr = biases_ptr + biases_offset_first_element_in_bytes + x * sizeof(int);
1903
1904 int4 biases_values = vload4(0, (__global int *)bias_addr);
1905 input_values += (int4)biases_values;
Gian Marco58c57942017-11-28 09:10:03 +00001906#endif // defined(ADD_BIAS)
1907
1908 // Multiply by result_mult_int and shift
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +01001909#if RESULT_SHIFT < 0
1910 input_values = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(input_values, RESULT_FIXEDPOINT_MULTIPLIER, RESULT_SHIFT, 4);
1911#else // RESULT_SHIFT >= 0
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001912 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 +01001913#endif // RESULT_SHIFT < 0
Gian Marco58c57942017-11-28 09:10:03 +00001914
1915 // Add the offset terms to GEMM's result
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001916 input_values += (int4)RESULT_OFFSET_AFTER_SHIFT;
Gian Marco58c57942017-11-28 09:10:03 +00001917
Manuel Bottini1f332d42019-11-29 17:25:25 +00001918 VEC_DATA_TYPE(OUTPUT_DATA_TYPE, 4)
1919 res = CONVERT_SAT(input_values, VEC_DATA_TYPE(OUTPUT_DATA_TYPE, 4));
Gian Marco58c57942017-11-28 09:10:03 +00001920
1921#if defined(MIN_BOUND)
Manuel Bottini1f332d42019-11-29 17:25:25 +00001922 res = max(res, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, 4))MIN_BOUND);
Gian Marco58c57942017-11-28 09:10:03 +00001923#endif // defined(MIN_BOUND)
1924#if defined(MAX_BOUND)
Manuel Bottini1f332d42019-11-29 17:25:25 +00001925 res = min(res, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, 4))MAX_BOUND);
Gian Marco58c57942017-11-28 09:10:03 +00001926#endif // defined(MAX_BOUND)
1927
1928 // Store the result
Manuel Bottini1f332d42019-11-29 17:25:25 +00001929 vstore4(res, 0, (__global OUTPUT_DATA_TYPE *)dst_addr);
Gian Marco58c57942017-11-28 09:10:03 +00001930}
Chunosov5124be52017-11-22 20:42:13 +07001931#endif // defined(RESULT_OFFSET_AFTER_SHIFT) && defined(RESULT_FIXEDPOINT_MULTIPLIER) && defined(RESULT_SHIFT)
Georgios Pinitas51e53a32018-10-22 13:49:08 +01001932
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01001933#if defined(RESULT_FIXEDPOINT_MULTIPLIER) && defined(RESULT_SHIFT)
1934
Michalis Spyrou51146c52019-07-12 14:42:29 +01001935/** This OpenCL kernel is used to quantize down the int32 accumulator values of GEMMLowp to QSYMM16
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01001936 *
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001937 * 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 +01001938 * The following computations will be performed by the kernel:
1939 *
1940 * -# Compute fixed point multiplication between each entry of input by result_fixedpoint_multiplier
1941 * -# Add bias to final result if bias tensor is not a nullptr
1942 * -# Round to nearest division by a power-of-two using result_shift
1943 * -# Add offset to each result
1944 * -# Clamp the value between the specified min and max bounds
1945 * -# Clamp the resulting int32 values to the [-32768..32767] range and cast to QSYMM16.
1946 *
1947 * @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
1948 *
1949 * @note In case the addition of int32 biases is required, -DADD_BIAS should be passed at compile time
1950 * @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.
1951 * These values can be used to implement "rectified linear unit" activation functions
1952 *
1953 * @param[in] src_ptr Pointer to the source tensor. Supported data type: S32
1954 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
1955 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
1956 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
1957 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
1958 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
1959 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1960 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
1961 * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: same as @p src_ptr
1962 * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes)
1963 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
1964 * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases tensor
1965 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: QASYMM8
1966 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
1967 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
1968 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
1969 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
1970 * @param[in] dst_stride_z Stride of the source tensor in Z dimension (in bytes)
1971 * @param[in] dst_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1972 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
1973 */
1974__kernel void gemmlowp_output_stage_quantize_down_fixedpoint_qsymm16(TENSOR3D_DECLARATION(src),
1975#if defined(ADD_BIAS)
1976 VECTOR_DECLARATION(biases),
1977#endif // defined(ADD_BIAS)
1978 TENSOR3D_DECLARATION(dst))
1979{
1980 // Compute source and destination addresses
1981 int x = get_global_id(0) * 4;
1982 int y = get_global_id(1);
1983 int z = get_global_id(2);
1984
Michalis Spyrou51146c52019-07-12 14:42:29 +01001985 __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 +01001986
Michalis Spyrou51146c52019-07-12 14:42:29 +01001987 __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 +01001988
1989 int4 input_values = vload4(0, (__global int *)src_addr);
1990
1991#if defined(ADD_BIAS)
1992 // Add bias
Michalis Spyrou51146c52019-07-12 14:42:29 +01001993 __global uchar *bias_addr = biases_ptr + biases_offset_first_element_in_bytes + x * sizeof(int);
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01001994
1995 int4 biases_values = vload4(0, (__global int *)bias_addr);
1996 input_values += (int4)biases_values;
1997#endif // defined(ADD_BIAS)
1998
1999 // Multiply by result_mult_int and shift
Manuel Bottini07263982019-10-17 18:37:26 +01002000#if RESULT_SHIFT < 0
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +01002001 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 +00002002#else // RESULT_SHIFT >= 0
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002003 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 +01002004#endif // RESULT_SHIFT < 0
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002005
2006 short4 res = convert_short4_sat(input_values);
2007
2008#if defined(MIN_BOUND)
2009 res = max(res, (short4)MIN_BOUND);
2010#endif // defined(MIN_BOUND)
2011#if defined(MAX_BOUND)
2012 res = min(res, (short4)MAX_BOUND);
2013#endif // defined(MAX_BOUND)
2014
2015 // Store the result
Michalis Spyrou51146c52019-07-12 14:42:29 +01002016 vstore4(res, 0, (__global short *)dst_addr);
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002017}
2018#endif // defined(RESULT_FIXEDPOINT_MULTIPLIER) && defined(RESULT_SHIFT)
2019
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002020#if defined(REAL_MULTIPLIER) && defined(OUTPUT_OFFSET)
2021/** This OpenCL kernel is used to quantize down the int32 accumulator values of GEMMLowp to QASYMM8
2022 *
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00002023 * This kernel takes a final int32 accumulator value (the output of matrix multiplication), and processes it to obtain the final QASYMM8 value.
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002024 * The following computations will be performed by the kernel:
2025 *
2026 * -# Compute fixed point multiplication between each entry of input by result_fixedpoint_multiplier
2027 * -# Add bias to final result if bias tensor is not a nullptr
2028 * -# Requantize
2029 * -# Add offset to each result
2030 * -# Clamp the value between the specified min and max bounds
2031 * -# Clamp the resulting int32 values to the [0..255] range and cast to QASYMM8.
2032 *
2033 * @attention The offset and scalar scale factor must be passed at compile time using -DRESULT_OFFSET, -DREAL_MULTIPLIER
2034 *
2035 * @note In case the addition of int32 biases is required, -DADD_BIAS should be passed at compile time
2036 * @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.
2037 * These values can be used to implement "rectified linear unit" activation functions
2038 *
2039 * @param[in] src_ptr Pointer to the source tensor. Supported data type: S32
2040 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
2041 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
2042 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
2043 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
2044 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
2045 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
2046 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
2047 * @param[in] biases_ptr Pointer to the biases tensor. Supported data type: same as @p src_ptr
2048 * @param[in] biases_stride_x Stride of the biases tensor in X dimension (in bytes)
2049 * @param[in] biases_step_x biases_stride_x * number of elements along X processed per workitem(in bytes)
2050 * @param[in] biases_offset_first_element_in_bytes The offset of the first element in the biases tensor
2051 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: QASYMM8
2052 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
2053 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
2054 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
2055 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
2056 * @param[in] dst_stride_z Stride of the source tensor in Z dimension (in bytes)
2057 * @param[in] dst_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
2058 * @param[in] dst_stride_w Stride of the source tensor in W dimension (in bytes)
2059 * @param[in] dst_step_w src_stride_w * number of elements along W processed per workitem(in bytes)
2060 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
2061 */
2062__kernel void gemmlowp_output_stage_quantize_down_float(TENSOR3D_DECLARATION(src),
2063#if defined(ADD_BIAS)
2064 VECTOR_DECLARATION(biases),
2065#endif // defined(ADD_BIAS)
2066#if defined(DST_HEIGHT)
2067 TENSOR4D_DECLARATION(dst))
2068#else // defined(DST_HEIGHT)
2069 TENSOR3D_DECLARATION(dst))
2070#endif // defined(DST_HEIGHT)
2071{
2072 // Compute source and destination addresses
Gian Marco Iodice0c54a622018-10-30 12:20:03 +00002073 int x = get_global_id(0) * 4;
2074 int y = get_global_id(1);
2075 int z = get_global_id(2);
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002076
Gian Marco Iodice0c54a622018-10-30 12:20:03 +00002077 __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 +01002078
Gian Marco Iodice0c54a622018-10-30 12:20:03 +00002079 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x + y * dst_stride_y + z * dst_stride_z;
2080
2081 int4 input_values = vload4(0, (__global int *)src_addr);
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002082
2083#if defined(ADD_BIAS)
2084 // Add bias
Gian Marco Iodice0c54a622018-10-30 12:20:03 +00002085 __global uchar *bias_addr = biases_ptr + biases_offset_first_element_in_bytes + x * sizeof(int);
2086
2087 int4 biases_values = vload4(0, (__global int *)bias_addr);
2088 input_values += (int4)biases_values;
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002089#endif // defined(ADD_BIAS)
2090
2091 // Convert to float
Gian Marco Iodice0c54a622018-10-30 12:20:03 +00002092 float16 input_values_f = convert_float4(input_values);
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002093 input_values_f = round(input_values_f * (float)REAL_MULTIPLIER + (float)OUTPUT_OFFSET);
2094
Gian Marco Iodice0c54a622018-10-30 12:20:03 +00002095 uchar4 res = convert_uchar4_sat(input_values_f);
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002096
2097#if defined(MIN_BOUND)
Gian Marco Iodice0c54a622018-10-30 12:20:03 +00002098 res = max(res, (uchar4)MIN_BOUND);
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002099#endif // defined(MIN_BOUND)
2100#if defined(MAX_BOUND)
Gian Marco Iodice0c54a622018-10-30 12:20:03 +00002101 res = min(res, (uchar4)MAX_BOUND);
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002102#endif // defined(MAX_BOUND)
2103
2104 // Store the result
Gian Marco Iodice0c54a622018-10-30 12:20:03 +00002105 vstore4(res, 0, dst_addr);
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002106}
Gian Marco Iodicedb18a6f2019-05-30 09:53:10 +01002107#endif // defined(REAL_MULTIPLIER) && defined(OUTPUT_OFFSET)