blob: 47791fbe7465895afd76fb2d6ae9d6a53933a25f [file] [log] [blame]
Gian Marco05288a22017-11-21 10:57:50 +00001/*
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +00002 * Copyright (c) 2017-2019 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
1002 *
1003 * @param[in] src_ptr Pointer to the source tensor. Supported data type: QASYMM8
1004 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
1005 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
1006 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
1007 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
1008 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
1009 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1010 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
1011 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: S32
1012 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
1013 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
1014 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
1015 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
1016 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
1017 */
1018__kernel void gemmlowp_matrix_a_reduction(TENSOR3D_DECLARATION(src),
1019 IMAGE_DECLARATION(dst))
1020{
1021 // Compute source and destination addresses
1022 Tensor3D src = CONVERT_TO_TENSOR3D_STRUCT(src);
1023 Image dst = CONVERT_TO_IMAGE_STRUCT(dst);
1024
1025 uint4 sum_row_u32 = (uint4)0;
1026 uint sum_row = 0;
1027
1028 __global const uchar *matrix_a = (__global const uchar *)(src.ptr + get_global_id(0) * src_stride_y + get_global_id(1) * src_stride_z);
1029
1030 int i = 0;
1031
1032 // This for loop performs 16 accumulations
1033 for(; i <= ((int)COLS_A - 16); i += 16)
1034 {
1035 const uchar16 a0_u8 = vload16(0, matrix_a + i);
1036
1037 sum_row_u32 += convert_uint4(a0_u8.s0123) + convert_uint4(a0_u8.s4567) + convert_uint4(a0_u8.s89AB) + convert_uint4(a0_u8.sCDEF);
1038 }
1039
1040 // This for loop performs the leftover accumulations
1041 for(; i < COLS_A; ++i)
1042 {
1043 sum_row += matrix_a[i];
1044 }
1045
1046 sum_row += sum_row_u32.s0 + sum_row_u32.s1 + sum_row_u32.s2 + sum_row_u32.s3;
1047
1048 *((__global int *)dst.ptr) = (int)sum_row;
1049}
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001050
1051#if defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8)
1052/** OpenCL kernel used to compute the row-vectors of sums of all the entries in each row of Matrix A using the arm dot product instruction
1053 *
1054 * @note This stage is needed to handle the offset of matrix product
1055 * https://github.com/google/gemmlowp/blob/master/doc/low-precision.md
1056 *
1057 * @attention The number of matrix A columns needs to be passed at compile time using -DCOLS_A
1058 *
1059 * @param[in] src_ptr Pointer to the source tensor. Supported data type: QASYMM8
1060 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
1061 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
1062 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
1063 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
1064 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
1065 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1066 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
1067 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: S32
1068 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
1069 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
1070 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
1071 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
1072 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
1073 */
1074__kernel void gemmlowp_matrix_a_reduction_dot8(TENSOR3D_DECLARATION(src),
1075 IMAGE_DECLARATION(dst))
1076{
1077 // Compute source and destination addresses
1078 Tensor3D src = CONVERT_TO_TENSOR3D_STRUCT(src);
1079 Image dst = CONVERT_TO_IMAGE_STRUCT(dst);
1080
1081 uint sum_row = 0;
1082
1083 __global const uchar *matrix_a = (__global const uchar *)(src.ptr + get_global_id(0) * src_stride_y + get_global_id(1) * src_stride_z);
1084
1085 int i = 0;
1086
1087 // This for loop performs 16 accumulations
1088 for(; i <= ((int)COLS_A - 32); i += 32)
1089 {
1090 uchar16 a0_u8 = vload16(0, matrix_a + i);
1091
1092 sum_row += arm_dot(a0_u8.s0123, (uchar4)(1));
1093 sum_row += arm_dot(a0_u8.s4567, (uchar4)(1));
1094 sum_row += arm_dot(a0_u8.s89AB, (uchar4)(1));
1095 sum_row += arm_dot(a0_u8.sCDEF, (uchar4)(1));
1096
1097 a0_u8 = vload16(1, matrix_a + i);
1098
1099 sum_row += arm_dot(a0_u8.s0123, (uchar4)(1));
1100 sum_row += arm_dot(a0_u8.s4567, (uchar4)(1));
1101 sum_row += arm_dot(a0_u8.s89AB, (uchar4)(1));
1102 sum_row += arm_dot(a0_u8.sCDEF, (uchar4)(1));
1103 }
1104
1105 // This for loop performs the leftover accumulations
1106 for(; i < COLS_A; ++i)
1107 {
1108 sum_row += matrix_a[i];
1109 }
1110
1111 *((__global int *)dst.ptr) = (int)sum_row;
1112}
1113#endif // defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8)
Gian Marco05288a22017-11-21 10:57:50 +00001114#endif // defined(COLS_A)
1115
1116#if defined(COLS_B) && defined(ROWS_B)
1117/** OpenCL kernel used to compute the row-vectors of sums of all the entries in each column of Matrix B.
1118 *
1119 * @note This stage is needed to handle the offset of matrix product
1120 * https://github.com/google/gemmlowp/blob/master/doc/low-precision.md
1121 *
1122 * @attention The number of matrix B columns and rows needs to be passed at compile time using -DCOLS_B and -DROWS_B
1123 *
1124 * @param[in] src_ptr Pointer to the source tensor. Supported data type: QASYMM8
1125 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
1126 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
1127 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
1128 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
1129 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
1130 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1131 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
1132 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: S32
1133 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
1134 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
1135 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
1136 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
1137 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
1138 */
1139__kernel void gemmlowp_matrix_b_reduction(TENSOR3D_DECLARATION(src),
1140 IMAGE_DECLARATION(dst))
1141{
1142 // Compute source and destination addresses
1143 Tensor3D src = CONVERT_TO_TENSOR3D_STRUCT(src);
1144 Image dst = CONVERT_TO_IMAGE_STRUCT(dst);
1145
1146 uint16 sum_col_u32 = (uint16)0;
1147
1148 __global const uchar *matrix_b = (__global const uchar *)(src.ptr + get_global_id(1) * src_stride_z);
1149
1150 int i = 0;
1151 // This for loop performs 4 accumulations
1152 for(; i <= ((int)ROWS_B - 4); i += 4)
1153 {
1154 const uchar16 b0_u8 = vload16(0, matrix_b + 0 * src_stride_y);
1155 const uchar16 b1_u8 = vload16(0, matrix_b + 1 * src_stride_y);
1156 const uchar16 b2_u8 = vload16(0, matrix_b + 2 * src_stride_y);
1157 const uchar16 b3_u8 = vload16(0, matrix_b + 3 * src_stride_y);
1158
1159 sum_col_u32 += convert_uint16(b0_u8) + convert_uint16(b1_u8) + convert_uint16(b2_u8) + convert_uint16(b3_u8);
1160
1161 matrix_b += 4 * src_stride_y;
1162 }
1163
1164 // This for loop perfoms the leftover accumulations
1165 for(; i < (int)ROWS_B; ++i)
1166 {
1167 const uchar16 b0_u8 = vload16(0, matrix_b);
1168
1169 sum_col_u32 += convert_uint16(b0_u8);
1170
1171 matrix_b += src_stride_y;
1172 }
1173
1174 vstore16(convert_int16(sum_col_u32), 0, (__global int *)dst.ptr);
1175}
1176#endif // defined(COLS_B) && defined(ROWS_B)
1177
1178#if defined(K_OFFSET)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001179
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001180/* Helper function used to calculate the offset contribution after matrix multiplication.
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001181 *
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001182 * This kernel takes a final int32 accumulator value (the output of matrix multiplication),
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001183 * and calculates the offset contribution of matrix A and matrix B.
1184 *
1185 * @attention The k_offset = a_offset * b_offset * k (where k is the number of matrix A columns) needs to be passed at compile time using -DK_OFFSET (i.e. -DK_OFFSET=1200)
1186 * @note In case the offset contribution due to a_offset is required, a_offset needs to be passed at compile time using -DA_OFFSET (i.e. -DA_OFFSET=1)
1187 * @note In case the offset contribution due to b_offset is required, b_offset needs to be passed at compile time using -DB_OFFSET (i.e. -DB_OFFSET=6)
1188 * @note In case sum_col has batches, -DSUM_COL_HAS_BATCHES must be passed at compile time. Usually if gemmlowp is used to accelerate convolution layer, sum_col will not have batches
1189 *
1190 * @param[in] x get_global_id(0) * 4
1191 * @param[in] y get_global_id(1)
1192 * @param[in] z get_global_id(2)
1193 * @param[in] sum_col_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr
1194 * @param[in] sum_col_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
1195 * @param[in] sum_col_step_x (Optional) sum_col_stride_x * number of elements along X processed per workitem(in bytes)
1196 * @param[in] sum_col_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
1197 * @param[in] sum_col_step_y (Optional) sum_col_stride_y * number of elements along Y processed per workitem(in bytes)
1198 * @param[in] sum_col_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
1199 * @param[in] sum_row_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr
1200 * @param[in] sum_row_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
1201 * @param[in] sum_row_step_x (Optional) sum_row_stride_x * number of elements along X processed per workitem(in bytes)
1202 * @param[in] sum_row_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
1203 * @param[in] sum_row_step_y (Optional) sum_row_stride_y * number of elements along Y processed per workitem(in bytes)
1204 * @param[in] sum_row_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
1205 * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: same as @p src_ptr
1206 * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes)
1207 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
1208 * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases tensor
1209 */
1210inline int4 offset_contribution(
1211 int x,
1212 int y,
1213 int z
1214#if defined(A_OFFSET)
1215 ,
1216 IMAGE_DECLARATION(sum_col)
1217#endif // defined(A_OFFSET)
1218#if defined(B_OFFSET)
1219 ,
1220 IMAGE_DECLARATION(sum_row)
1221#endif // defined(B_OFFSET)
1222#if defined(ADD_BIAS)
1223 ,
1224 VECTOR_DECLARATION(biases)
1225#endif // defined(ADD_BIAS)
1226)
1227{
1228 int4 a_offset_s32 = (int4)0;
1229 int4 b_offset_s32 = (int4)0;
1230
1231 int batch_id = z;
1232#if defined(DEPTH_INPUT3D)
1233 batch_id /= (int)DEPTH_INPUT3D;
1234#endif // defined(DEPTH_INPUT3D)
1235
1236#if defined(A_OFFSET)
1237 // Compute the offset contribution due to A_OFFSET
1238 __global uchar *sum_col_addr = sum_col_ptr + sum_col_offset_first_element_in_bytes + x * sizeof(int);
1239
1240 // Compute the offset contribution due to A_OFFSET
1241#if defined(SUM_COL_HAS_BATCHES)
1242 a_offset_s32 = vload4(0, (__global int *)(sum_col_addr + batch_id * sum_col_stride_y));
1243#else // defined(SUM_COL_HAS_BATCHES)
1244 a_offset_s32 = vload4(0, (__global int *)sum_col_addr);
1245#endif // defined(SUM_COL_HAS_BATCHES)
1246
1247 a_offset_s32 *= (int4)A_OFFSET;
1248#endif // defined(A_OFFSET)
1249
1250#if defined(B_OFFSET)
1251 // Compute the offset contribution due to A_OFFSET
1252 __global uchar *sum_row_addr = sum_row_ptr + sum_row_offset_first_element_in_bytes + y * sizeof(int);
1253
1254 // Compute the offset contribution due to B_OFFSET
1255#if defined(HEIGHT_INPUT3D) && defined(DEPTH_INPUT3D)
1256 b_offset_s32 = (int4) * (((__global int *)(sum_row_addr + batch_id * sum_row_stride_y)) + (z % (int)DEPTH_INPUT3D) * (int)HEIGHT_INPUT3D);
1257#else // defined(HEIGHT_INPUT3D) && defined(DEPTH_INPUT3D)
1258 b_offset_s32 = (int4) * (((__global int *)(sum_row_addr + batch_id * sum_row_stride_y)));
1259#endif // defined(HEIGHT_INPUT3D) && defined(DEPTH_INPUT3D)
1260 b_offset_s32 *= (int4)B_OFFSET;
1261#endif // defined(B_OFFSET)
1262
1263#if defined(ADD_BIAS)
1264 // Add bias
1265 __global uchar *bias_addr = biases_ptr + biases_offset_first_element_in_bytes + x * sizeof(int);
1266
1267 int4 biases_values = vload4(0, (__global int *)bias_addr);
1268 b_offset_s32 += (int4)biases_values;
1269#endif // defined(ADD_BIAS)
1270
1271 return (int4)K_OFFSET + a_offset_s32 + b_offset_s32;
1272}
1273
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001274/* OpenCL kernel used to add the offset contribution after matrix multiplication. The computation is performed in-place
Gian Marco05288a22017-11-21 10:57:50 +00001275 *
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001276 * This kernel takes a final int32 accumulator value (the output of matrix multiplication),
Gian Marco05288a22017-11-21 10:57:50 +00001277 * and adds to it the offset contribution of matrix A and matrix B in-place.
1278 *
1279 * @attention The k_offset = a_offset * b_offset * k (where k is the number of matrix A columns) needs to be passed at compile time using -DK_OFFSET (i.e. -DK_OFFSET=1200)
1280 * @note In case the offset contribution due to a_offset is required, a_offset needs to be passed at compile time using -DA_OFFSET (i.e. -DA_OFFSET=1)
1281 * @note In case the offset contribution due to b_offset is required, b_offset needs to be passed at compile time using -DB_OFFSET (i.e. -DB_OFFSET=6)
Chunosov5124be52017-11-22 20:42:13 +07001282 * @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 +00001283 *
1284 * The final result is:
1285 *
1286 * mm_result[i][k] = mm_result[i][k] +
1287 * (sum_col[k] * A_OFFSET) +
1288 * (sum_row[i] * B_OFFSET) +
1289 * (K_OFFSET)
1290 *
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +01001291 * @param[in] mm_result_ptr Pointer to the source tensor. Supported data type: S32
1292 * @param[in] mm_result_stride_x Stride of the source tensor in X dimension (in bytes)
1293 * @param[in] mm_result_step_x mm_result_stride_x * number of elements along X processed per workitem(in bytes)
1294 * @param[in] mm_result_stride_y Stride of the source tensor in Y dimension (in bytes)
1295 * @param[in] mm_result_step_y mm_result_stride_y * number of elements along Y processed per workitem(in bytes)
1296 * @param[in] mm_result_stride_z Stride of the source tensor in Z dimension (in bytes)
1297 * @param[in] mm_result_step_z mm_result_stride_z * number of elements along Z processed per workitem(in bytes)
1298 * @param[in] mm_result_offset_first_element_in_bytes The offset of the first element in the source tensor
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001299 * @param[in] sum_col_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr
1300 * @param[in] sum_col_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
1301 * @param[in] sum_col_step_x (Optional) sum_col_stride_x * number of elements along X processed per workitem(in bytes)
1302 * @param[in] sum_col_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
1303 * @param[in] sum_col_step_y (Optional) sum_col_stride_y * number of elements along Y processed per workitem(in bytes)
1304 * @param[in] sum_col_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
1305 * @param[in] sum_row_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr
1306 * @param[in] sum_row_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
1307 * @param[in] sum_row_step_x (Optional) sum_row_stride_x * number of elements along X processed per workitem(in bytes)
1308 * @param[in] sum_row_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
1309 * @param[in] sum_row_step_y (Optional) sum_row_stride_y * number of elements along Y processed per workitem(in bytes)
1310 * @param[in] sum_row_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
1311 * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: same as @p src_ptr
1312 * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes)
1313 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
1314 * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases tensor
Gian Marco05288a22017-11-21 10:57:50 +00001315 */
1316__kernel void gemmlowp_offset_contribution(TENSOR3D_DECLARATION(mm_result)
1317#if defined(A_OFFSET)
1318 ,
1319 IMAGE_DECLARATION(sum_col)
1320#endif // defined(A_OFFSET)
1321#if defined(B_OFFSET)
1322 ,
1323 IMAGE_DECLARATION(sum_row)
1324#endif // defined(B_OFFSET)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001325#if defined(ADD_BIAS)
1326 ,
1327 VECTOR_DECLARATION(biases)
1328#endif // defined(ADD_BIAS))
Gian Marco05288a22017-11-21 10:57:50 +00001329 )
1330{
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001331 const int x = get_global_id(0) * 4;
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +01001332 const int y = get_global_id(1);
1333 const int z = get_global_id(2);
1334
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001335 // Compute offset contribution
1336 int4 offset_term_s32 = offset_contribution(
1337 x, y, z
Gian Marco05288a22017-11-21 10:57:50 +00001338#if defined(A_OFFSET)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001339 ,
1340 sum_col_ptr,
1341 sum_col_stride_x,
1342 sum_col_step_x,
1343 sum_col_stride_y,
1344 sum_col_step_y,
1345 sum_col_offset_first_element_in_bytes
Gian Marco05288a22017-11-21 10:57:50 +00001346#endif // defined(A_OFFSET)
Gian Marco05288a22017-11-21 10:57:50 +00001347#if defined(B_OFFSET)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001348 ,
1349 sum_row_ptr,
1350 sum_row_stride_x,
1351 sum_row_step_x,
1352 sum_row_stride_y,
1353 sum_row_step_y,
1354 sum_row_offset_first_element_in_bytes
Gian Marco05288a22017-11-21 10:57:50 +00001355#endif // defined(B_OFFSET)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001356#if defined(ADD_BIAS)
1357 ,
1358 biases_ptr,
1359 biases_stride_x,
1360 biases_step_x,
1361 biases_offset_first_element_in_bytes
1362#endif // defined(ADD_BIAS)
1363 );
Gian Marco05288a22017-11-21 10:57:50 +00001364
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001365 __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 +00001366
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001367 int4 in_s32 = vload4(0, (__global int *)mm_result_addr);
Gian Marco05288a22017-11-21 10:57:50 +00001368
1369 // Add the offset terms to GEMM's result
1370 in_s32 += offset_term_s32;
1371
1372 // Store the result with the offset contribution
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001373 vstore4(in_s32, 0, (__global int *)mm_result_addr);
Gian Marco05288a22017-11-21 10:57:50 +00001374}
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001375
1376#if defined(RESULT_OFFSET) && defined(RESULT_MULTIPLIER) && defined(RESULT_SHIFT)
1377/* OpenCL kernel used to add the offset contribution after @ref CLGEMMLowpMatrixMultiplyKernel and it quantizes down to uint8.
1378 *
1379 * This kernel takes a final int32 accumulator value (the output of @CLGEMMLowpMatrixMultiplyKernel), adds to it the offset contribution of matrix A and matrix B and quantizes to uint8 through the output stage.
1380 *
1381 *
1382 * @attention The k_offset = a_offset * b_offset * k (where k is the number of matrix A columns) needs to be passed at compile time using -DK_OFFSET (i.e. -DK_OFFSET=1200)
1383 * @note In case the offset contribution due to a_offset is required, a_offset needs to be passed at compile time using -DA_OFFSET (i.e. -DA_OFFSET=1)
1384 * @note In case the offset contribution due to b_offset is required, b_offset needs to be passed at compile time using -DB_OFFSET (i.e. -DB_OFFSET=6)
1385 * @note In case sum_col has batches, -DSUM_COL_HAS_BATCHES must be passed at compile time. Usually if gemmlowp is used to accelerate convolution layer, sum_col will not have batches
1386 *
1387 * The result before the output stage is:
1388 *
1389 * mm_result[i][k] = mm_result[i][k] +
1390 * (sum_col[k] * A_OFFSET) +
1391 * (sum_row[i] * B_OFFSET) +
1392 * (K_OFFSET)
1393 *
1394 * This result is quantized down to uint8 using the output stage. The output stage computes the following operations:
1395 *
1396 * -# Add offset terms to final result
1397 * -# Multiply each entry of result by result_mult_int
1398 * -# Add bias to final result (if -DADD_BIAS is passed at compile time)
1399 * -# Shift the int32 accumulator by result_shift
1400 * -# Clamp the value between the specified min and max bounds (if -DMIN_BOUND and/or -DMAX_BOUND are passed at compile time)
1401 * -# Clamp the resulting int32 values to the [0..255] range and cast to QASYMM8.
1402 *
1403 * @attention The offset, scalar scale factor and number of bits to shift right of output tensor must be passed at compile time using -DRESULT_OFFSET, -RESULT_MULT_INT and -DRESULT_SHIFT
1404 *
1405 * @note In case the addition of int32 biases is required, -DADD_BIAS should be passed at compile time
1406 * @note In case the clamping of the result is required, the min and max bounds can be passed at compile time using -DMIN_BOUND and -DMAX_BOUND.
1407 * These values can be used to implement "rectified linear unit" activation functions
1408 *
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001409 * @param[in] mm_result_ptr Pointer to the source tensor. Supported data type: S32
1410 * @param[in] mm_result_stride_x Stride of the source tensor in X dimension (in bytes)
1411 * @param[in] mm_result_step_x mm_result_stride_x * number of elements along X processed per workitem(in bytes)
1412 * @param[in] mm_result_stride_y Stride of the source tensor in Y dimension (in bytes)
1413 * @param[in] mm_result_step_y mm_result_stride_y * number of elements along Y processed per workitem(in bytes)
1414 * @param[in] mm_result_stride_z Stride of the source tensor in Z dimension (in bytes)
1415 * @param[in] mm_result_step_z mm_result_stride_z * number of elements along Z processed per workitem(in bytes)
1416 * @param[in] mm_result_offset_first_element_in_bytes The offset of the first element in the source tensor
1417 * @param[in] sum_col_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr
1418 * @param[in] sum_col_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
1419 * @param[in] sum_col_step_x (Optional) sum_col_stride_x * number of elements along X processed per workitem(in bytes)
1420 * @param[in] sum_col_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
1421 * @param[in] sum_col_step_y (Optional) sum_col_stride_y * number of elements along Y processed per workitem(in bytes)
1422 * @param[in] sum_col_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
1423 * @param[in] sum_row_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr
1424 * @param[in] sum_row_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
1425 * @param[in] sum_row_step_x (Optional) sum_row_stride_x * number of elements along X processed per workitem(in bytes)
1426 * @param[in] sum_row_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
1427 * @param[in] sum_row_step_y (Optional) sum_row_stride_y * number of elements along Y processed per workitem(in bytes)
1428 * @param[in] sum_row_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
1429 * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: same as @p src_ptr
1430 * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes)
1431 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
1432 * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases tensor
1433 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: QASYMM8
1434 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
1435 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
1436 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
1437 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
1438 * @param[in] dst_stride_z Stride of the source tensor in Z dimension (in bytes)
1439 * @param[in] dst_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1440 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
1441 * @param[in] result_multipliers_ptr (Optional) Pointer to the output multipliers vector for per-channel quantization. Supported data types: S32
1442 * @param[in] result_multipliers_stride_x (Optional) Stride of the output multipliers vector in X dimension (in bytes)
1443 * @param[in] result_multipliers_step_x (Optional) output_multipliers_stride_x * number of elements along X processed per workitem(in bytes)
1444 * @param[in] result_multipliers_offset_first_element_in_bytes (Optional) The offset of the first element in the output multipliers vector
1445 * @param[in] result_shifts_ptr (Optional) Pointer to the output shifts vector for per-channel quantization. Supported data types: S32
1446 * @param[in] result_shifts_stride_x (Optional) Stride of the output shifts vector in X dimension (in bytes)
1447 * @param[in] result_shifts_step_x (Optional) output_shifts_stride_x * number of elements along X processed per workitem(in bytes)
1448 * @param[in] result_shifts_offset_first_element_in_bytes (Optional) The offset of the first element in the output shifts vector
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001449 */
1450__kernel void gemmlowp_offset_contribution_quantize_down(TENSOR3D_DECLARATION(mm_result)
1451#if defined(A_OFFSET)
1452 ,
1453 IMAGE_DECLARATION(sum_col)
1454#endif // defined(A_OFFSET)
1455#if defined(B_OFFSET)
1456 ,
1457 IMAGE_DECLARATION(sum_row)
1458#endif // defined(B_OFFSET)
1459 ,
1460#if defined(ADD_BIAS)
1461 VECTOR_DECLARATION(biases),
1462#endif // defined(ADD_BIAS)
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001463 TENSOR3D_DECLARATION(dst)
1464#if defined(PER_CHANNEL_QUANTIZATION)
1465 ,
1466 VECTOR_DECLARATION(result_multipliers),
1467 VECTOR_DECLARATION(result_shifts)
1468#endif // defined(PER_CHANNEL_QUANTIZATION)
1469 )
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001470{
1471 const int x = get_global_id(0) * 4;
1472 const int y = get_global_id(1);
1473 const int z = get_global_id(2);
1474
1475 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x + y * dst_stride_y + z * dst_stride_z;
1476
1477 // Compute offset contribution
1478 int4 offset_term_s32 = offset_contribution(
1479 x, y, z
1480#if defined(A_OFFSET)
1481 ,
1482 sum_col_ptr,
1483 sum_col_stride_x,
1484 sum_col_step_x,
1485 sum_col_stride_y,
1486 sum_col_step_y,
1487 sum_col_offset_first_element_in_bytes
1488#endif // defined(A_OFFSET)
1489#if defined(B_OFFSET)
1490 ,
1491 sum_row_ptr,
1492 sum_row_stride_x,
1493 sum_row_step_x,
1494 sum_row_stride_y,
1495 sum_row_step_y,
1496 sum_row_offset_first_element_in_bytes
1497#endif // defined(B_OFFSET)
1498#if defined(ADD_BIAS)
1499 ,
1500 biases_ptr,
1501 biases_stride_x,
1502 biases_step_x,
1503 biases_offset_first_element_in_bytes
1504#endif // defined(ADD_BIAS)
1505 );
1506
1507 __global uchar *mm_result_addr = mm_result_ptr + mm_result_offset_first_element_in_bytes + x * sizeof(int) + y * mm_result_stride_y + z * mm_result_stride_z;
1508
1509 int4 in_s32 = vload4(0, (__global int *)mm_result_addr);
1510
1511 // Add the offset terms to GEMM's result
1512 in_s32 += offset_term_s32;
1513
1514 // -------------- OUTPUT STAGE
1515
1516 // Add the offset terms to GEMM's result
1517 in_s32 += (int4)RESULT_OFFSET;
1518
1519 // Multiply by result_mult_int and shift
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001520#if defined(PER_CHANNEL_QUANTIZATION)
1521 __global uchar *result_multipliers_addr = result_multipliers_ptr + result_multipliers_offset_first_element_in_bytes + x * sizeof(int);
1522 __global uchar *result_shifts_addr = result_shifts_ptr + result_shifts_offset_first_element_in_bytes + x * sizeof(int);
1523 int4 result_multipliers_values = vload4(0, (__global int *)result_multipliers_addr);
1524 int4 result_shifts_values = vload4(0, (__global int *)result_shifts_addr);
1525
1526 in_s32 *= result_multipliers_values;
1527 in_s32 >>= result_shifts_values;
1528#else // defined(PER_CHANNEL_QUANTIZATION)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001529 in_s32 *= RESULT_MULTIPLIER;
1530
1531 in_s32 >>= RESULT_SHIFT;
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001532#endif // defined(PER_CHANNEL_QUANTIZATION)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001533
1534 uchar4 res = convert_uchar4_sat(in_s32);
1535
1536#if defined(MIN_BOUND)
1537 res = max(res, (uchar4)MIN_BOUND);
1538#endif // defined(MIN_BOUND)
1539#if defined(MAX_BOUND)
1540 res = min(res, (uchar4)MAX_BOUND);
1541#endif // defined(MAX_BOUND)
1542
1543 // Store the result
1544 vstore4(res, 0, dst_addr);
1545}
1546
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001547/* 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 +01001548 *
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001549 * 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 +01001550 *
1551 *
1552 * @attention The k_offset = a_offset * b_offset * k (where k is the number of matrix A columns) needs to be passed at compile time using -DK_OFFSET (i.e. -DK_OFFSET=1200)
1553 * @note In case the offset contribution due to a_offset is required, a_offset needs to be passed at compile time using -DA_OFFSET (i.e. -DA_OFFSET=1)
1554 * @note In case the offset contribution due to b_offset is required, b_offset needs to be passed at compile time using -DB_OFFSET (i.e. -DB_OFFSET=6)
1555 * @note In case sum_col has batches, -DSUM_COL_HAS_BATCHES must be passed at compile time. Usually if gemmlowp is used to accelerate convolution layer, sum_col will not have batches
1556 *
1557 * The result before the output stage is:
1558 *
1559 * mm_result[i][k] = mm_result[i][k] +
1560 * (sum_col[k] * A_OFFSET) +
1561 * (sum_row[i] * B_OFFSET) +
1562 * (K_OFFSET)
1563 *
1564 * This result is quantized down to uint8 using the output stage. The output stage computes the following operations:
1565 *
1566 * -# Compute fixed point multiplication between each entry of input by result_fixedpoint_multiplier
1567 * -# Add bias to final result if bias tensor is not a nullptr
1568 * -# Round to nearest division by a power-of-two using result_shift
1569 * -# Add offset to each result
1570 * -# Clamp the value between the specified min and max bounds
1571 * -# Clamp the resulting int32 values to the [0..255] range and cast to QASYMM8.
1572 *
1573 * @attention The offset, scalar scale factor and number of bits to shift right of output tensor must be passed at compile time using -DRESULT_OFFSET, -RESULT_MULT_INT and -DRESULT_SHIFT
1574 *
1575 * @note In case the addition of int32 biases is required, -DADD_BIAS should be passed at compile time
1576 * @note In case the clamping of the result is required, the min and max bounds can be passed at compile time using -DMIN_BOUND and -DMAX_BOUND.
1577 * These values can be used to implement "rectified linear unit" activation functions
1578 *
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001579 * @param[in] mm_result_ptr Pointer to the source tensor. Supported data type: S32
1580 * @param[in] mm_result_stride_x Stride of the source tensor in X dimension (in bytes)
1581 * @param[in] mm_result_step_x mm_result_stride_x * number of elements along X processed per workitem(in bytes)
1582 * @param[in] mm_result_stride_y Stride of the source tensor in Y dimension (in bytes)
1583 * @param[in] mm_result_step_y mm_result_stride_y * number of elements along Y processed per workitem(in bytes)
1584 * @param[in] mm_result_stride_z Stride of the source tensor in Z dimension (in bytes)
1585 * @param[in] mm_result_step_z mm_result_stride_z * number of elements along Z processed per workitem(in bytes)
1586 * @param[in] mm_result_offset_first_element_in_bytes The offset of the first element in the source tensor
1587 * @param[in] sum_col_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr
1588 * @param[in] sum_col_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
1589 * @param[in] sum_col_step_x (Optional) sum_col_stride_x * number of elements along X processed per workitem(in bytes)
1590 * @param[in] sum_col_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
1591 * @param[in] sum_col_step_y (Optional) sum_col_stride_y * number of elements along Y processed per workitem(in bytes)
1592 * @param[in] sum_col_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
1593 * @param[in] sum_row_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr
1594 * @param[in] sum_row_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
1595 * @param[in] sum_row_step_x (Optional) sum_row_stride_x * number of elements along X processed per workitem(in bytes)
1596 * @param[in] sum_row_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
1597 * @param[in] sum_row_step_y (Optional) sum_row_stride_y * number of elements along Y processed per workitem(in bytes)
1598 * @param[in] sum_row_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
1599 * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: same as @p src_ptr
1600 * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes)
1601 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
1602 * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases tensor
1603 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: QASYMM8
1604 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
1605 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
1606 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
1607 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
1608 * @param[in] dst_stride_z Stride of the source tensor in Z dimension (in bytes)
1609 * @param[in] dst_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1610 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
1611 * @param[in] result_multipliers_ptr (Optional) Pointer to the output multipliers vector for per-channel quantization. Supported data types: S32
1612 * @param[in] result_multipliers_stride_x (Optional) Stride of the output multipliers vector in X dimension (in bytes)
1613 * @param[in] result_multipliers_step_x (Optional) output_multipliers_stride_x * number of elements along X processed per workitem(in bytes)
1614 * @param[in] result_multipliers_offset_first_element_in_bytes (Optional) The offset of the first element in the output multipliers vector
1615 * @param[in] result_shifts_ptr (Optional) Pointer to the output shifts vector for per-channel quantization. Supported data types: S32
1616 * @param[in] result_shifts_stride_x (Optional) Stride of the output shifts vector in X dimension (in bytes)
1617 * @param[in] result_shifts_step_x (Optional) output_shifts_stride_x * number of elements along X processed per workitem(in bytes)
1618 * @param[in] result_shifts_offset_first_element_in_bytes (Optional) The offset of the first element in the output shifts vector
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001619 */
1620__kernel void gemmlowp_offset_contribution_quantize_down_fixedpoint(TENSOR3D_DECLARATION(mm_result)
1621#if defined(A_OFFSET)
1622 ,
1623 IMAGE_DECLARATION(sum_col)
1624#endif // defined(A_OFFSET)
1625#if defined(B_OFFSET)
1626 ,
1627 IMAGE_DECLARATION(sum_row)
1628#endif // defined(B_OFFSET)
1629 ,
1630#if defined(ADD_BIAS)
1631 VECTOR_DECLARATION(biases),
1632#endif // defined(ADD_BIAS)
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001633 TENSOR3D_DECLARATION(dst)
1634#if defined(PER_CHANNEL_QUANTIZATION)
1635 ,
1636 VECTOR_DECLARATION(result_multipliers),
1637 VECTOR_DECLARATION(result_shifts)
1638#endif // defined(PER_CHANNEL_QUANTIZATION)
1639 )
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001640{
1641 const int x = get_global_id(0) * 4;
1642 const int y = get_global_id(1);
1643 const int z = get_global_id(2);
1644
1645 // Compute offset contribution
1646 int4 offset_term_s32 = offset_contribution(
1647 x, y, z
1648#if defined(A_OFFSET)
1649 ,
1650 sum_col_ptr,
1651 sum_col_stride_x,
1652 sum_col_step_x,
1653 sum_col_stride_y,
1654 sum_col_step_y,
1655 sum_col_offset_first_element_in_bytes
1656#endif // defined(A_OFFSET)
1657#if defined(B_OFFSET)
1658 ,
1659 sum_row_ptr,
1660 sum_row_stride_x,
1661 sum_row_step_x,
1662 sum_row_stride_y,
1663 sum_row_step_y,
1664 sum_row_offset_first_element_in_bytes
1665#endif // defined(B_OFFSET)
1666#if defined(ADD_BIAS)
1667 ,
1668 biases_ptr,
1669 biases_stride_x,
1670 biases_step_x,
1671 biases_offset_first_element_in_bytes
1672#endif // defined(ADD_BIAS)
1673 );
1674
1675 __global uchar *mm_result_addr = mm_result_ptr + mm_result_offset_first_element_in_bytes + x * sizeof(int) + y * mm_result_stride_y + z * mm_result_stride_z;
1676
1677 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x + y * dst_stride_y + z * dst_stride_z;
1678
1679 int4 in_s32 = vload4(0, (__global int *)mm_result_addr);
1680
1681 // Add the offset terms to GEMM's result
1682 in_s32 += offset_term_s32;
1683
1684 // -------------- OUTPUT STAGE
1685
1686 // Multiply by result_mult_int and shift
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001687#if defined(PER_CHANNEL_QUANTIZATION)
1688 __global uchar *result_multipliers_addr = result_multipliers_ptr + result_multipliers_offset_first_element_in_bytes + x * sizeof(int);
1689 __global uchar *result_shifts_addr = result_shifts_ptr + result_shifts_offset_first_element_in_bytes + x * sizeof(int);
1690 int4 result_multipliers_values = vload4(0, (__global int *)result_multipliers_addr);
1691 int4 result_shifts_values = vload4(0, (__global int *)result_shifts_addr);
1692
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +01001693 int4 in_s32_shift_lt0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(in_s32, result_multipliers_values, result_shifts_values, 4);
1694 int4 in_s32_shift_gt0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(in_s32, result_multipliers_values, result_shifts_values, 4);
1695 in_s32 = select(in_s32_shift_lt0, in_s32_shift_gt0, result_shifts_values >= 0);
1696#else // defined(PER_CHANNEL_QUANTIZATION)
1697
1698#if RESULT_SHIFT < 0
1699 in_s32 = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(in_s32, RESULT_MULTIPLIER, RESULT_SHIFT, 4);
1700#else // RESULT_SHIFT >= 0
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001701 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 +01001702#endif // RESULT_SHIFT < 0
1703
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001704#endif // defined(PER_CHANNEL_QUANTIZATION)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001705
1706 // Add the offset terms to GEMM's result
1707 in_s32 += (int4)RESULT_OFFSET;
1708
1709 uchar4 res = convert_uchar4_sat(in_s32);
1710
1711#if defined(MIN_BOUND)
1712 res = max(res, (uchar4)MIN_BOUND);
1713#endif // defined(MIN_BOUND)
1714#if defined(MAX_BOUND)
1715 res = min(res, (uchar4)MAX_BOUND);
1716#endif // defined(MAX_BOUND)
1717
1718 // Store the result
1719 vstore4(res, 0, dst_addr);
1720}
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001721#endif // defined(RESULT_OFFSET) && defined(RESULT_MULTIPLIER) && defined(RESULT_SHIFT)
1722
Gian Marco05288a22017-11-21 10:57:50 +00001723#endif // defined(K_OFFSET)
1724
1725#if defined(RESULT_OFFSET) && defined(RESULT_MULT_INT) && defined(RESULT_SHIFT)
1726/** This OpenCL kernel is used to quantize down the int32 accumulator values of GEMMLowp to QASYMM8
1727 *
1728 * This kernel takes a final int32 accumulator value and processes it to obtain the final QASYMM8 value.
1729 * The following computations will be performed by the kernel:
1730 *
1731 * -# Add offset terms to final result
1732 * -# Multiply each entry of result by result_mult_int
1733 * -# Add bias to final result (if -DADD_BIAS is passed at compile time)
1734 * -# Shift the int32 accumulator by result_shift
1735 * -# Clamp the value between the specified min and max bounds (if -DMIN_BOUND and/or -DMAX_BOUND are passed at compile time)
1736 * -# Clamp the resulting int32 values to the [0..255] range and cast to QASYMM8.
1737 *
1738 * @attention The offset, scalar scale factor and number of bits to shift right of output tensor must be passed at compile time using -DRESULT_OFFSET, -RESULT_MULT_INT and -DRESULT_SHIFT
1739 *
1740 * @note In case the addition of int32 biases is required, -DADD_BIAS should be passed at compile time
1741 * @note In case the clamping of the result is required, the min and max bounds can be passed at compile time using -DMIN_BOUND and -DMAX_BOUND.
1742 * These values can be used to implement "rectified linear unit" activation functions
1743 *
1744 * @param[in] src_ptr Pointer to the source tensor. Supported data type: S32
1745 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
1746 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
1747 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
1748 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
1749 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
1750 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1751 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001752 * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: same as @p src_ptr
1753 * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes)
1754 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
1755 * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases tensor
Gian Marco05288a22017-11-21 10:57:50 +00001756 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: QASYMM8
1757 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
1758 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
1759 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
1760 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
1761 * @param[in] dst_stride_z Stride of the source tensor in Z dimension (in bytes)
1762 * @param[in] dst_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1763 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
1764 */
1765__kernel void gemmlowp_output_stage_quantize_down(TENSOR3D_DECLARATION(src),
1766#if defined(ADD_BIAS)
1767 VECTOR_DECLARATION(biases),
1768#endif // defined(ADD_BIAS)
1769 TENSOR3D_DECLARATION(dst))
1770{
1771 // Compute source and destination addresses
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001772 int x = get_global_id(0) * 4;
1773 int y = get_global_id(1);
1774 int z = get_global_id(2);
Gian Marco05288a22017-11-21 10:57:50 +00001775
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001776 __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 +00001777
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001778 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x + y * dst_stride_y + z * dst_stride_z;
1779
1780 int4 input_values = vload4(0, (__global int *)src_addr);
Gian Marco58c57942017-11-28 09:10:03 +00001781
Gian Marco05288a22017-11-21 10:57:50 +00001782#if defined(ADD_BIAS)
1783 // Add bias
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001784 __global uchar *bias_addr = biases_ptr + biases_offset_first_element_in_bytes + x * sizeof(int);
1785
1786 int4 biases_values = vload4(0, (__global int *)bias_addr);
1787 input_values += (int4)biases_values;
Gian Marco05288a22017-11-21 10:57:50 +00001788#endif // defined(ADD_BIAS)
1789
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001790 // Add the offset terms to GEMM's result
1791 input_values += (int4)RESULT_OFFSET;
1792
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +00001793 // Multiply by result_mult_int and shift
Gian Marco58c57942017-11-28 09:10:03 +00001794 input_values *= RESULT_MULT_INT;
Gian Marco05288a22017-11-21 10:57:50 +00001795
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +01001796#if RESULT_SHIFT < 0
1797 input_values >>= -RESULT_SHIFT;
1798#else // RESULT_SHIFT >= 0
Gian Marco58c57942017-11-28 09:10:03 +00001799 input_values >>= RESULT_SHIFT;
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +01001800#endif // RESULT_SHIFT < 0
Gian Marco05288a22017-11-21 10:57:50 +00001801
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001802 uchar4 res = convert_uchar4_sat(input_values);
Gian Marco05288a22017-11-21 10:57:50 +00001803
1804#if defined(MIN_BOUND)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001805 res = max(res, (uchar4)MIN_BOUND);
Gian Marco05288a22017-11-21 10:57:50 +00001806#endif // defined(MIN_BOUND)
1807#if defined(MAX_BOUND)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001808 res = min(res, (uchar4)MAX_BOUND);
Gian Marco05288a22017-11-21 10:57:50 +00001809#endif // defined(MAX_BOUND)
1810
1811 // Store the result
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001812 vstore4(res, 0, dst_addr);
Gian Marco05288a22017-11-21 10:57:50 +00001813}
Gian Marco58c57942017-11-28 09:10:03 +00001814#endif // defined(RESULT_OFFSET) && defined(RESULT_MULT_INT) && defined(RESULT_SHIFT)
1815
1816#if defined(RESULT_OFFSET_AFTER_SHIFT) && defined(RESULT_FIXEDPOINT_MULTIPLIER) && defined(RESULT_SHIFT)
1817/** This OpenCL kernel is used to quantize down the int32 accumulator values of GEMMLowp to QASYMM8
1818 *
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001819 * This kernel takes a final int32 accumulator value (the output of matrix multiplication), and processes it to obtain the final QASYMM8 value.
Gian Marco58c57942017-11-28 09:10:03 +00001820 * The following computations will be performed by the kernel:
1821 *
1822 * -# Compute fixed point multiplication between each entry of input by result_fixedpoint_multiplier
1823 * -# Add bias to final result if bias tensor is not a nullptr
1824 * -# Round to nearest division by a power-of-two using result_shift
1825 * -# Add offset to each result
1826 * -# Clamp the value between the specified min and max bounds
1827 * -# Clamp the resulting int32 values to the [0..255] range and cast to QASYMM8.
1828 *
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001829 * @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 +00001830 *
1831 * @note In case the addition of int32 biases is required, -DADD_BIAS should be passed at compile time
1832 * @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.
1833 * These values can be used to implement "rectified linear unit" activation functions
1834 *
1835 * @param[in] src_ptr Pointer to the source tensor. Supported data type: S32
1836 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
1837 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
1838 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
1839 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
1840 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
1841 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1842 * @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 +01001843 * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: same as @p src_ptr
1844 * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes)
1845 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
1846 * @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 +00001847 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: QASYMM8
1848 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
1849 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
1850 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
1851 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
1852 * @param[in] dst_stride_z Stride of the source tensor in Z dimension (in bytes)
1853 * @param[in] dst_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1854 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
1855 */
1856__kernel void gemmlowp_output_stage_quantize_down_fixedpoint(TENSOR3D_DECLARATION(src),
1857#if defined(ADD_BIAS)
1858 VECTOR_DECLARATION(biases),
1859#endif // defined(ADD_BIAS)
1860 TENSOR3D_DECLARATION(dst))
1861{
1862 // Compute source and destination addresses
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001863 int x = get_global_id(0) * 4;
1864 int y = get_global_id(1);
1865 int z = get_global_id(2);
Georgios Pinitas932491f2018-09-21 16:33:15 +01001866
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001867 __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 +00001868
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001869 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x + y * dst_stride_y + z * dst_stride_z;
1870
1871 int4 input_values = vload4(0, (__global int *)src_addr);
Gian Marco58c57942017-11-28 09:10:03 +00001872
1873#if defined(ADD_BIAS)
1874 // Add bias
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001875 __global uchar *bias_addr = biases_ptr + biases_offset_first_element_in_bytes + x * sizeof(int);
1876
1877 int4 biases_values = vload4(0, (__global int *)bias_addr);
1878 input_values += (int4)biases_values;
Gian Marco58c57942017-11-28 09:10:03 +00001879#endif // defined(ADD_BIAS)
1880
1881 // Multiply by result_mult_int and shift
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +01001882#if RESULT_SHIFT < 0
1883 input_values = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(input_values, RESULT_FIXEDPOINT_MULTIPLIER, RESULT_SHIFT, 4);
1884#else // RESULT_SHIFT >= 0
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001885 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 +01001886#endif // RESULT_SHIFT < 0
Gian Marco58c57942017-11-28 09:10:03 +00001887
1888 // Add the offset terms to GEMM's result
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001889 input_values += (int4)RESULT_OFFSET_AFTER_SHIFT;
Gian Marco58c57942017-11-28 09:10:03 +00001890
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001891 uchar4 res = convert_uchar4_sat(input_values);
Gian Marco58c57942017-11-28 09:10:03 +00001892
1893#if defined(MIN_BOUND)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001894 res = max(res, (uchar4)MIN_BOUND);
Gian Marco58c57942017-11-28 09:10:03 +00001895#endif // defined(MIN_BOUND)
1896#if defined(MAX_BOUND)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001897 res = min(res, (uchar4)MAX_BOUND);
Gian Marco58c57942017-11-28 09:10:03 +00001898#endif // defined(MAX_BOUND)
1899
1900 // Store the result
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001901 vstore4(res, 0, dst_addr);
Gian Marco58c57942017-11-28 09:10:03 +00001902}
Chunosov5124be52017-11-22 20:42:13 +07001903#endif // defined(RESULT_OFFSET_AFTER_SHIFT) && defined(RESULT_FIXEDPOINT_MULTIPLIER) && defined(RESULT_SHIFT)
Georgios Pinitas51e53a32018-10-22 13:49:08 +01001904
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01001905#if defined(RESULT_FIXEDPOINT_MULTIPLIER) && defined(RESULT_SHIFT)
1906
Michalis Spyrou51146c52019-07-12 14:42:29 +01001907/** This OpenCL kernel is used to quantize down the int32 accumulator values of GEMMLowp to QSYMM16
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01001908 *
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001909 * 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 +01001910 * The following computations will be performed by the kernel:
1911 *
1912 * -# Compute fixed point multiplication between each entry of input by result_fixedpoint_multiplier
1913 * -# Add bias to final result if bias tensor is not a nullptr
1914 * -# Round to nearest division by a power-of-two using result_shift
1915 * -# Add offset to each result
1916 * -# Clamp the value between the specified min and max bounds
1917 * -# Clamp the resulting int32 values to the [-32768..32767] range and cast to QSYMM16.
1918 *
1919 * @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
1920 *
1921 * @note In case the addition of int32 biases is required, -DADD_BIAS should be passed at compile time
1922 * @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.
1923 * These values can be used to implement "rectified linear unit" activation functions
1924 *
1925 * @param[in] src_ptr Pointer to the source tensor. Supported data type: S32
1926 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
1927 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
1928 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
1929 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
1930 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
1931 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1932 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
1933 * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: same as @p src_ptr
1934 * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes)
1935 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
1936 * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases tensor
1937 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: QASYMM8
1938 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
1939 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
1940 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
1941 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
1942 * @param[in] dst_stride_z Stride of the source tensor in Z dimension (in bytes)
1943 * @param[in] dst_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1944 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
1945 */
1946__kernel void gemmlowp_output_stage_quantize_down_fixedpoint_qsymm16(TENSOR3D_DECLARATION(src),
1947#if defined(ADD_BIAS)
1948 VECTOR_DECLARATION(biases),
1949#endif // defined(ADD_BIAS)
1950 TENSOR3D_DECLARATION(dst))
1951{
1952 // Compute source and destination addresses
1953 int x = get_global_id(0) * 4;
1954 int y = get_global_id(1);
1955 int z = get_global_id(2);
1956
Michalis Spyrou51146c52019-07-12 14:42:29 +01001957 __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 +01001958
Michalis Spyrou51146c52019-07-12 14:42:29 +01001959 __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 +01001960
1961 int4 input_values = vload4(0, (__global int *)src_addr);
1962
1963#if defined(ADD_BIAS)
1964 // Add bias
Michalis Spyrou51146c52019-07-12 14:42:29 +01001965 __global uchar *bias_addr = biases_ptr + biases_offset_first_element_in_bytes + x * sizeof(int);
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01001966
1967 int4 biases_values = vload4(0, (__global int *)bias_addr);
1968 input_values += (int4)biases_values;
1969#endif // defined(ADD_BIAS)
1970
1971 // Multiply by result_mult_int and shift
Manuel Bottini07263982019-10-17 18:37:26 +01001972#if RESULT_SHIFT < 0
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +01001973 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 +00001974#else // RESULT_SHIFT >= 0
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01001975 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 +01001976#endif // RESULT_SHIFT < 0
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01001977
1978 short4 res = convert_short4_sat(input_values);
1979
1980#if defined(MIN_BOUND)
1981 res = max(res, (short4)MIN_BOUND);
1982#endif // defined(MIN_BOUND)
1983#if defined(MAX_BOUND)
1984 res = min(res, (short4)MAX_BOUND);
1985#endif // defined(MAX_BOUND)
1986
1987 // Store the result
Michalis Spyrou51146c52019-07-12 14:42:29 +01001988 vstore4(res, 0, (__global short *)dst_addr);
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01001989}
1990#endif // defined(RESULT_FIXEDPOINT_MULTIPLIER) && defined(RESULT_SHIFT)
1991
Georgios Pinitas51e53a32018-10-22 13:49:08 +01001992#if defined(REAL_MULTIPLIER) && defined(OUTPUT_OFFSET)
1993/** This OpenCL kernel is used to quantize down the int32 accumulator values of GEMMLowp to QASYMM8
1994 *
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001995 * 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 +01001996 * The following computations will be performed by the kernel:
1997 *
1998 * -# Compute fixed point multiplication between each entry of input by result_fixedpoint_multiplier
1999 * -# Add bias to final result if bias tensor is not a nullptr
2000 * -# Requantize
2001 * -# Add offset to each result
2002 * -# Clamp the value between the specified min and max bounds
2003 * -# Clamp the resulting int32 values to the [0..255] range and cast to QASYMM8.
2004 *
2005 * @attention The offset and scalar scale factor must be passed at compile time using -DRESULT_OFFSET, -DREAL_MULTIPLIER
2006 *
2007 * @note In case the addition of int32 biases is required, -DADD_BIAS should be passed at compile time
2008 * @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.
2009 * These values can be used to implement "rectified linear unit" activation functions
2010 *
2011 * @param[in] src_ptr Pointer to the source tensor. Supported data type: S32
2012 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
2013 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
2014 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
2015 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
2016 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
2017 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
2018 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
2019 * @param[in] biases_ptr Pointer to the biases tensor. Supported data type: same as @p src_ptr
2020 * @param[in] biases_stride_x Stride of the biases tensor in X dimension (in bytes)
2021 * @param[in] biases_step_x biases_stride_x * number of elements along X processed per workitem(in bytes)
2022 * @param[in] biases_offset_first_element_in_bytes The offset of the first element in the biases tensor
2023 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: QASYMM8
2024 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
2025 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
2026 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
2027 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
2028 * @param[in] dst_stride_z Stride of the source tensor in Z dimension (in bytes)
2029 * @param[in] dst_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
2030 * @param[in] dst_stride_w Stride of the source tensor in W dimension (in bytes)
2031 * @param[in] dst_step_w src_stride_w * number of elements along W processed per workitem(in bytes)
2032 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
2033 */
2034__kernel void gemmlowp_output_stage_quantize_down_float(TENSOR3D_DECLARATION(src),
2035#if defined(ADD_BIAS)
2036 VECTOR_DECLARATION(biases),
2037#endif // defined(ADD_BIAS)
2038#if defined(DST_HEIGHT)
2039 TENSOR4D_DECLARATION(dst))
2040#else // defined(DST_HEIGHT)
2041 TENSOR3D_DECLARATION(dst))
2042#endif // defined(DST_HEIGHT)
2043{
2044 // Compute source and destination addresses
Gian Marco Iodice0c54a622018-10-30 12:20:03 +00002045 int x = get_global_id(0) * 4;
2046 int y = get_global_id(1);
2047 int z = get_global_id(2);
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002048
Gian Marco Iodice0c54a622018-10-30 12:20:03 +00002049 __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 +01002050
Gian Marco Iodice0c54a622018-10-30 12:20:03 +00002051 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x + y * dst_stride_y + z * dst_stride_z;
2052
2053 int4 input_values = vload4(0, (__global int *)src_addr);
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002054
2055#if defined(ADD_BIAS)
2056 // Add bias
Gian Marco Iodice0c54a622018-10-30 12:20:03 +00002057 __global uchar *bias_addr = biases_ptr + biases_offset_first_element_in_bytes + x * sizeof(int);
2058
2059 int4 biases_values = vload4(0, (__global int *)bias_addr);
2060 input_values += (int4)biases_values;
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002061#endif // defined(ADD_BIAS)
2062
2063 // Convert to float
Gian Marco Iodice0c54a622018-10-30 12:20:03 +00002064 float16 input_values_f = convert_float4(input_values);
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002065 input_values_f = round(input_values_f * (float)REAL_MULTIPLIER + (float)OUTPUT_OFFSET);
2066
Gian Marco Iodice0c54a622018-10-30 12:20:03 +00002067 uchar4 res = convert_uchar4_sat(input_values_f);
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002068
2069#if defined(MIN_BOUND)
Gian Marco Iodice0c54a622018-10-30 12:20:03 +00002070 res = max(res, (uchar4)MIN_BOUND);
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002071#endif // defined(MIN_BOUND)
2072#if defined(MAX_BOUND)
Gian Marco Iodice0c54a622018-10-30 12:20:03 +00002073 res = min(res, (uchar4)MAX_BOUND);
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002074#endif // defined(MAX_BOUND)
2075
2076 // Store the result
Gian Marco Iodice0c54a622018-10-30 12:20:03 +00002077 vstore4(res, 0, dst_addr);
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002078}
Gian Marco Iodicedb18a6f2019-05-30 09:53:10 +01002079#endif // defined(REAL_MULTIPLIER) && defined(OUTPUT_OFFSET)