blob: 059c2e14dfa4e8ac8906bf47397b608d231e3f9f [file] [log] [blame]
Gian Marco05288a22017-11-21 10:57:50 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
Gian Marco05288a22017-11-21 10:57:50 +00003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
Gian Marco Iodice43a129e2019-05-14 10:14:08 +010024#include "gemm_helpers.h"
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +000025#include "helpers_asymm.h"
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +000026#include "repeat.h"
Gian Marco05288a22017-11-21 10:57:50 +000027
Michele Di Giorgiof9179d32019-11-27 16:17:30 +000028#if defined(DATA_TYPE) && defined(ACC_DATA_TYPE)
29
Georgios Pinitasdaa38552018-08-28 17:43:18 +010030#if defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8)
31#if defined(ARM_COMPUTE_OPENCL_DOT8_ACC_ENABLED) && defined(cl_arm_integer_dot_product_accumulate_int8)
Gian Marco Iodice4b908652018-10-18 10:21:02 +010032#define ARM_DOT(x, y, val) val = arm_dot_acc((x), (y), (val));
Georgios Pinitasdaa38552018-08-28 17:43:18 +010033#else // defined(ARM_COMPUTE_OPENCL_DOT8_ACC_ENABLED) && defined(cl_arm_integer_dot_product_accumulate_int8)
Gian Marco Iodice4b908652018-10-18 10:21:02 +010034#define ARM_DOT(x, y, val) val += arm_dot((x), (y));
Georgios Pinitasdaa38552018-08-28 17:43:18 +010035#endif // defined(ARM_COMPUTE_OPENCL_DOT8_ACC_ENABLED) && defined(cl_arm_integer_dot_product_accumulate_int8)
36#endif // defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8)
Giorgio Arenac50da382018-07-26 15:50:09 +010037
Gian Marco Iodice43a129e2019-05-14 10:14:08 +010038#if defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8)
39
40/** Specialized macros to perform the dot product instruction between two vectors of size N [1,16]. These macros use the dot8 instruction */
Michele Di Giorgiof9179d32019-11-27 16:17:30 +000041#define ARM_DOT1(a, b, c) \
42 ({ \
43 ARM_DOT((VEC_DATA_TYPE(DATA_TYPE, 4))(a, (VEC_DATA_TYPE(DATA_TYPE, 3))0), (VEC_DATA_TYPE(DATA_TYPE, 4))(b, (VEC_DATA_TYPE(DATA_TYPE, 3))0), c); \
Gian Marco Iodice43a129e2019-05-14 10:14:08 +010044 })
Michele Di Giorgiof9179d32019-11-27 16:17:30 +000045#define ARM_DOT2(a, b, c) \
46 ({ \
47 ARM_DOT((VEC_DATA_TYPE(DATA_TYPE, 4))(a, (VEC_DATA_TYPE(DATA_TYPE, 2))0), (VEC_DATA_TYPE(DATA_TYPE, 4))(b, (VEC_DATA_TYPE(DATA_TYPE, 2))0), c); \
Gian Marco Iodice43a129e2019-05-14 10:14:08 +010048 })
Michele Di Giorgiof9179d32019-11-27 16:17:30 +000049#define ARM_DOT3(a, b, c) \
50 ({ \
51 ARM_DOT((VEC_DATA_TYPE(DATA_TYPE, 4))(a, (DATA_TYPE)0), (VEC_DATA_TYPE(DATA_TYPE, 4))(b, (DATA_TYPE)0), c); \
Gian Marco Iodice43a129e2019-05-14 10:14:08 +010052 })
53#define ARM_DOT4(a, b, c) \
54 ({ \
55 ARM_DOT(a, b, c); \
56 })
57#define ARM_DOT8(a, b, c) \
58 ({ \
59 ARM_DOT4((a.lo), (b.lo), c); \
60 ARM_DOT4((a.hi), (b.hi), c); \
61 })
62#define ARM_DOT16(a, b, c) \
63 ({ \
64 ARM_DOT8((a.lo), (b.lo), c); \
65 ARM_DOT8((a.hi), (b.hi), c); \
66 })
67
68#else // defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8)
69
70/** Specialized macros to perform the dot product instruction between two vectors of size K0 [1,16] without using the dot8 instruction. */
Michele Di Giorgiof9179d32019-11-27 16:17:30 +000071#define ARM_DOT1(a, b, c) \
72 ({ \
73 c += (ACC_DATA_TYPE)a * b; \
Gian Marco Iodice43a129e2019-05-14 10:14:08 +010074 })
Michele Di Giorgiof9179d32019-11-27 16:17:30 +000075#define ARM_DOT2(a, b, c) \
76 ({ \
77 c += (ACC_DATA_TYPE)a.s0 * b.s0; \
78 c += (ACC_DATA_TYPE)a.s1 * b.s1; \
Gian Marco Iodice43a129e2019-05-14 10:14:08 +010079 })
Michele Di Giorgiof9179d32019-11-27 16:17:30 +000080#define ARM_DOT3(a, b, c) \
81 ({ \
82 ARM_DOT2(a, b, c); \
83 c += (ACC_DATA_TYPE)a.s2 * b.s2; \
Gian Marco Iodice43a129e2019-05-14 10:14:08 +010084 })
Michele Di Giorgiof9179d32019-11-27 16:17:30 +000085#define ARM_DOT4(a, b, c) \
86 ({ \
87 ARM_DOT3(a, b, c); \
88 c += (ACC_DATA_TYPE)a.s3 * b.s3; \
Gian Marco Iodice43a129e2019-05-14 10:14:08 +010089 })
90#define ARM_DOT8(a, b, c) \
91 ({ \
92 ARM_DOT4((a.lo), (b.lo), c); \
93 ARM_DOT4((a.hi), (b.hi), c); \
94 })
95#define ARM_DOT16(a, b, c) \
96 ({ \
97 ARM_DOT8((a.lo), (b.lo), c); \
98 ARM_DOT8((a.hi), (b.hi), c); \
99 })
100#endif // defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8)
101
102/** Specialized macros to perform a broadcast dot product operation between one vector "a" and N0 vectors "b" of size K0 [1,16] */
Gian Marco Iodice061eefd2020-04-23 13:40:00 +0100103#define ARM_DOT_K0X1(k0, a, b, c) \
104 ({ \
105 ARM_DOT_K0(k0, (a), (b##0), (c)); \
106 })
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100107#define ARM_DOT_K0X2(k0, a, b, c) \
108 ({ \
109 ARM_DOT_K0(k0, (a), (b##0), (c.s0)); \
110 ARM_DOT_K0(k0, (a), (b##1), (c.s1)); \
111 })
112#define ARM_DOT_K0X3(k0, a, b, c) \
113 ({ \
114 ARM_DOT_K0X2(k0, a, b, c); \
115 ARM_DOT_K0(k0, (a), (b##2), (c.s2)); \
116 })
117#define ARM_DOT_K0X4(k0, a, b, c) \
118 ({ \
119 ARM_DOT_K0X3(k0, a, b, c); \
120 ARM_DOT_K0(k0, (a), (b##3), (c.s3)); \
121 })
122#define ARM_DOT_K0X8(k0, a, b, c) \
123 ({ \
124 ARM_DOT_K0X4(k0, a, b, c); \
125 ARM_DOT_K0(k0, (a), (b##4), (c.s4)); \
126 ARM_DOT_K0(k0, (a), (b##5), (c.s5)); \
127 ARM_DOT_K0(k0, (a), (b##6), (c.s6)); \
128 ARM_DOT_K0(k0, (a), (b##7), (c.s7)); \
129 })
130#define ARM_DOT_K0X16(k0, a, b, c) \
131 ({ \
132 ARM_DOT_K0X8(k0, a, b, c); \
133 ARM_DOT_K0(k0, (a), (b##8), (c.s8)); \
134 ARM_DOT_K0(k0, (a), (b##9), (c.s9)); \
135 ARM_DOT_K0(k0, (a), (b##A), (c.sA)); \
136 ARM_DOT_K0(k0, (a), (b##B), (c.sB)); \
137 ARM_DOT_K0(k0, (a), (b##C), (c.sC)); \
138 ARM_DOT_K0(k0, (a), (b##D), (c.sD)); \
139 ARM_DOT_K0(k0, (a), (b##E), (c.sE)); \
140 ARM_DOT_K0(k0, (a), (b##F), (c.sF)); \
141 })
142
SiCong Li738893e2020-05-01 12:55:16 +0100143/** Specialized macros to perform a partial matrix multiplication with dimensions M0,N0,K0 */
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100144#define ARM_MM_K0XN0X1(n0, k0, a, b, c) \
145 ({ \
146 ARM_DOT_K0XN0(n0, k0, (a##0), b, (c##0)); \
147 })
148#define ARM_MM_K0XN0X2(n0, k0, a, b, c) \
149 ({ \
150 ARM_MM_K0XN0X1(n0, k0, a, b, c); \
151 ARM_DOT_K0XN0(n0, k0, (a##1), b, (c##1)); \
152 })
153#define ARM_MM_K0XN0X3(n0, k0, a, b, c) \
154 ({ \
155 ARM_MM_K0XN0X2(n0, k0, a, b, c); \
156 ARM_DOT_K0XN0(n0, k0, (a##2), b, (c##2)); \
157 })
158#define ARM_MM_K0XN0X4(n0, k0, a, b, c) \
159 ({ \
160 ARM_MM_K0XN0X3(n0, k0, a, b, c); \
161 ARM_DOT_K0XN0(n0, k0, (a##3), b, (c##3)); \
162 })
163#define ARM_MM_K0XN0X5(n0, k0, a, b, c) \
164 ({ \
165 ARM_MM_K0XN0X4(n0, k0, a, b, c); \
166 ARM_DOT_K0XN0(n0, k0, (a##4), b, (c##4)); \
167 })
168#define ARM_MM_K0XN0X6(n0, k0, a, b, c) \
169 ({ \
170 ARM_MM_K0XN0X5(n0, k0, a, b, c); \
171 ARM_DOT_K0XN0(n0, k0, (a##5), b, (c##5)); \
172 })
173#define ARM_MM_K0XN0X7(n0, k0, a, b, c) \
174 ({ \
175 ARM_MM_K0XN0X6(n0, k0, a, b, c); \
176 ARM_DOT_K0XN0(n0, k0, (a##6), b, (c##6)); \
177 })
178#define ARM_MM_K0XN0X8(n0, k0, a, b, c) \
179 ({ \
180 ARM_MM_K0XN0X7(n0, k0, a, b, c); \
181 ARM_DOT_K0XN0(n0, k0, (a##7), b, (c##7)); \
182 })
183
184#define ARM_DOT_K0(k0, a, b, c) \
185 ({ \
186 CONCAT(ARM_DOT, k0) \
187 ((a), (b), (c)); \
188 })
189
190#define ARM_DOT_K0XN0(n0, k0, a, b, c) \
191 ({ \
192 CONCAT(ARM_DOT_K0X, n0) \
193 (k0, (a), b, (c)); \
194 })
195
196#define ARM_MM_K0XN0XM0(m0, n0, k0, a, b, c) \
197 ({ \
198 CONCAT(ARM_MM_K0XN0X, m0) \
199 (n0, k0, a, b, c); \
200 })
201
SiCong Li738893e2020-05-01 12:55:16 +0100202/** Specialized macros to perform a broadcast dot product operation between one vector "a" and N0 vectors "b" of size K0 [1,16] */
203#define ARM_MUL_N0X1(VECTOR_ACC_TYPE, a, b, c) \
204 ({ \
205 c += CONVERT(b##0, VECTOR_ACC_TYPE) * a; \
206 })
207#define ARM_MUL_N0X2(VECTOR_ACC_TYPE, a, b, c) \
208 ({ \
209 c += CONVERT(b##0, VECTOR_ACC_TYPE) * a.s##0; \
210 c += CONVERT(b##1, VECTOR_ACC_TYPE) * a.s##1; \
211 })
212#define ARM_MUL_N0X3(VECTOR_ACC_TYPE, a, b, c) \
213 ({ \
214 ARM_MUL_N0X2(VECTOR_ACC_TYPE, a, b, c); \
215 c += CONVERT(b##2, VECTOR_ACC_TYPE) * a.s##2; \
216 })
217#define ARM_MUL_N0X4(VECTOR_ACC_TYPE, a, b, c) \
218 ({ \
219 ARM_MUL_N0X3(VECTOR_ACC_TYPE, a, b, c); \
220 c += CONVERT(b##3, VECTOR_ACC_TYPE) * a.s##3; \
221 })
222#define ARM_MUL_N0X8(VECTOR_ACC_TYPE, a, b, c) \
223 ({ \
224 ARM_MUL_N0X4(VECTOR_ACC_TYPE, a, b, c); \
225 c += CONVERT(b##4, VECTOR_ACC_TYPE) * a.s##4; \
226 c += CONVERT(b##5, VECTOR_ACC_TYPE) * a.s##5; \
227 c += CONVERT(b##6, VECTOR_ACC_TYPE) * a.s##6; \
228 c += CONVERT(b##7, VECTOR_ACC_TYPE) * a.s##7; \
229 })
230#define ARM_MUL_N0X16(VECTOR_ACC_TYPE, a, b, c) \
231 ({ \
232 ARM_MUL_N0X8(VECTOR_ACC_TYPE, a, b, c); \
233 c += CONVERT(b##8, VECTOR_ACC_TYPE) * a.s##8; \
234 c += CONVERT(b##9, VECTOR_ACC_TYPE) * a.s##9; \
235 c += CONVERT(b##A, VECTOR_ACC_TYPE) * a.s##A; \
236 c += CONVERT(b##B, VECTOR_ACC_TYPE) * a.s##B; \
237 c += CONVERT(b##C, VECTOR_ACC_TYPE) * a.s##C; \
238 c += CONVERT(b##D, VECTOR_ACC_TYPE) * a.s##D; \
239 c += CONVERT(b##E, VECTOR_ACC_TYPE) * a.s##E; \
240 c += CONVERT(b##F, VECTOR_ACC_TYPE) * a.s##F; \
241 })
242/** Specialized macros to perform a a partial matrix multiplication with dimensions M0,N0,K0 */
243#define ARM_MM_NATIVE_N0XK0X1(VECTOR_ACC_TYPE, k0, a, b, c) \
244 ({ \
245 ARM_MUL_N0XK0(VECTOR_ACC_TYPE, k0, (a##0), b, (c##0)); \
246 })
247#define ARM_MM_NATIVE_N0XK0X2(VECTOR_ACC_TYPE, k0, a, b, c) \
248 ({ \
249 ARM_MM_NATIVE_N0XK0X1(VECTOR_ACC_TYPE, k0, a, b, c); \
250 ARM_MUL_N0XK0(VECTOR_ACC_TYPE, k0, (a##1), b, (c##1)); \
251 })
252#define ARM_MM_NATIVE_N0XK0X3(VECTOR_ACC_TYPE, k0, a, b, c) \
253 ({ \
254 ARM_MM_NATIVE_N0XK0X2(VECTOR_ACC_TYPE, k0, a, b, c); \
255 ARM_MUL_N0XK0(VECTOR_ACC_TYPE, k0, (a##2), b, (c##2)); \
256 })
257#define ARM_MM_NATIVE_N0XK0X4(VECTOR_ACC_TYPE, k0, a, b, c) \
258 ({ \
259 ARM_MM_NATIVE_N0XK0X3(VECTOR_ACC_TYPE, k0, a, b, c); \
260 ARM_MUL_N0XK0(VECTOR_ACC_TYPE, k0, (a##3), b, (c##3)); \
261 })
262#define ARM_MM_NATIVE_N0XK0X5(VECTOR_ACC_TYPE, k0, a, b, c) \
263 ({ \
264 ARM_MM_NATIVE_N0XK0X4(VECTOR_ACC_TYPE, k0, a, b, c); \
265 ARM_MUL_N0XK0(VECTOR_ACC_TYPE, k0, (a##4), b, (c##4)); \
266 })
267#define ARM_MM_NATIVE_N0XK0X6(VECTOR_ACC_TYPE, k0, a, b, c) \
268 ({ \
269 ARM_MM_NATIVE_N0XK0X5(VECTOR_ACC_TYPE, k0, a, b, c); \
270 ARM_MUL_N0XK0(VECTOR_ACC_TYPE, k0, (a##5), b, (c##5)); \
271 })
272#define ARM_MM_NATIVE_N0XK0X7(VECTOR_ACC_TYPE, k0, a, b, c) \
273 ({ \
274 ARM_MM_NATIVE_N0XK0X6(VECTOR_ACC_TYPE, k0, a, b, c); \
275 ARM_MUL_N0XK0(VECTOR_ACC_TYPE, k0, (a##6), b, (c##6)); \
276 })
277#define ARM_MM_NATIVE_N0XK0X8(VECTOR_ACC_TYPE, k0, a, b, c) \
278 ({ \
279 ARM_MM_NATIVE_N0XK0X7(VECTOR_ACC_TYPE, k0, a, b, c); \
280 ARM_MUL_N0XK0(VECTOR_ACC_TYPE, k0, (a##7), b, (c##7)); \
281 })
282#define ARM_MUL_N0XK0(VECTOR_ACC_TYPE, k0, a, b, c) \
283 ({ \
284 CONCAT(ARM_MUL_N0X, k0) \
285 (VECTOR_ACC_TYPE, (a), b, (c)); \
286 })
287#define ARM_MM_NATIVE_N0XK0XM0(VECTOR_ACC_TYPE, m0, k0, a, b, c) \
288 ({ \
289 CONCAT(ARM_MM_NATIVE_N0XK0X, m0) \
290 (VECTOR_ACC_TYPE, k0, a, b, c); \
291 })
292
Manuel Bottini8cf753f2020-10-21 12:34:38 +0100293#if defined(M0) && defined(N0) && defined(K0) && defined(V0) && defined(H0) && defined(M) && defined(N) && defined(PARTIAL_STORE_M0) && defined(PARTIAL_STORE_N0)
Sheri Zhang28287af2020-02-25 14:13:54 +0000294/** This OpenCL kernel computes the matrix multiplication between 2 matrices with QASYMM/QASYMM_SIGNED data type.
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000295 * The LHS matrix must be reshaped with @ref CLGEMMReshapeLHSMatrixKernel and the M0xK0 must be NOT transposed
296 * The RHS matrix must be reshaped with @ref CLGEMMReshapeRHSMatrixKernel and the K0xN0 must be transposed
297 *
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000298 * @note The input data type must be passed at compile time using -DDATA_TYPE (i.e. -DDATA_TYPE=uchar)
299 * @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 +0000300 * @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.
301 * @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 +0000302 * @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).
303 * @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)
304 * @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)
305 * @note If the M0xK0 blocks in the reshaped LHS matrix have been interleaved, the option -DLHS_INTERLEAVE must passed at compile time.
306 * @note If the K0xN0 blocks in the reshaped RHS matrix have been interleaved, the option -DRHS_INTERLEAVE must passed at compile time.
307 * @note Only the following configurations of M0, N0 and K0 are currently supported:
308 * - M0 = 2, 3, 4, 5, 6, 7, 8
309 * - N0 = 2, 3, 4, 8, 16
310 * - K0 = 2, 3, 4, 8, 16
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000311 * - V0 >= 1
312 * - H0 >= 1
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000313 *
314 * @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:
315 * -# REINTERPRET_OUTPUT_AS_3D: To reinterpret the output as 3D
316 * -# HEIGHT_GEMM3D: The height of the output in case it has to be reinterpreted as a 3D tensor.
317 * -# DEPTH_GEMM3D: The depth of the output in case it has to be reinterpreted as a 3D tensor
318 * (HEIGHT_GEMM3D * DEPTH_GEMM3D) = columns LHS matrix NOT reshaped
319 *
Sheri Zhang28287af2020-02-25 14:13:54 +0000320 * @param[in] lhs_ptr Pointer to the LHS reshaped matrix. Supported data type: QASYMM8/QASYMM_SIGNED
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000321 * @param[in] lhs_stride_x Stride of the LHS reshaped matrix in X dimension (in bytes)
322 * @param[in] lhs_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
323 * @param[in] lhs_stride_y Stride of the LHS reshaped matrix in Y dimension (in bytes)
324 * @param[in] lhs_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
325 * @param[in] lhs_offset_first_element_in_bytes The offset of the first element in the LHS reshaped matrix
326 * @param[in] rhs_ptr Pointer to the RHS reshaped matrix. Supported data type: same as @p lhs_ptr
327 * @param[in] rhs_stride_x Stride of the RHS reshaped matrix in X dimension (in bytes)
328 * @param[in] rhs_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
329 * @param[in] rhs_stride_y Stride of the RHS reshaped matrix in Y dimension (in bytes)
330 * @param[in] rhs_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
331 * @param[in] rhs_offset_first_element_in_bytes The offset of the first element in the RHS reshaped matrix
Sheri Zhang28287af2020-02-25 14:13:54 +0000332 * @param[out] dst_ptr Pointer to the destination matrix Supported data type: S32
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000333 * @param[in] dst_stride_x Stride of the destination matrix in X dimension (in bytes)
334 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
335 * @param[in] dst_stride_y Stride of the destination matrix in Y dimension (in bytes)
336 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
337 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination matrix
338 * @param[in] k Number of columns in LHS matrix and rows in RHS matrix not reshaped.
339 * @param[in] lhs_stride_z Stride of the LHS reshaped matrix in Z dimension (in bytes)
340 * @param[in] rhs_stride_z Stride of the RHS reshaped matrix in Z dimension (in bytes)
341 * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes)
342 * @param[in] dst_cross_plane_pad (Optional) Bottom paddings in unit of elements (only if defined REINTERPRET_OUTPUT_AS_3D)
343 */
344__kernel void gemmlowp_mm_reshaped_lhs_nt_rhs_t(IMAGE_DECLARATION(lhs),
345 IMAGE_DECLARATION(rhs),
346 IMAGE_DECLARATION(dst),
347 uint k,
348 uint lhs_stride_z,
349 uint rhs_stride_z,
350 uint dst_stride_z
351#if defined(REINTERPRET_OUTPUT_AS_3D)
352 ,
353 uint dst_cross_plane_pad
354#endif // REINTERPRET_OUTPUT_AS_3D
355 )
356{
357 // Block size
358#define LHS_BLOCK_SIZE ((K0) * (M0))
359
360#if defined(LHS_INTERLEAVE)
361#define LHS_OFFSET_X (K0)
362#define LHS_STEP_X ((K0) * (V0))
363#define LHS_STEP_LOOP (1)
364#else // defined(INTERLEAVE)
365#define LHS_OFFSET_X (LHS_BLOCK_SIZE)
366#define LHS_STEP_X (K0)
367#define LHS_STEP_LOOP (V0)
368#endif // defined(INTERLEAVE)
369
370 // Block size
371#define RHS_BLOCK_SIZE ((K0) * (N0))
372
373 // RHS offset and step X
374#if defined(RHS_INTERLEAVE)
375#define RHS_OFFSET_X (K0)
376#define RHS_STEP_X ((K0) * (H0))
377#define RHS_STEP_LOOP (1)
378#else // defined(RHS_INTERLEAVE)
379#define RHS_OFFSET_X (RHS_BLOCK_SIZE)
380#define RHS_STEP_X (K0)
381#define RHS_STEP_LOOP (H0)
382#endif // defined(RHS_INTERLEAVE)
383
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100384 uint x = get_global_id(0);
385 uint y = get_global_id(1);
386 uint z = get_global_id(2);
387
Gian Marco Iodiceb0c50372019-03-15 10:13:05 +0000388#if defined(DUMMY_WORK_ITEMS)
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100389 if((x * N0 >= N) || (y * M0 >= M))
Gian Marco Iodiceb0c50372019-03-15 10:13:05 +0000390 {
391 return;
392 }
393#endif // defined(DUMMY_WORK_ITEMS)
394
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000395 // Compute LHS matrix address
Sheri Zhang28287af2020-02-25 14:13:54 +0000396 __global DATA_TYPE *lhs_addr = lhs_ptr + lhs_offset_first_element_in_bytes + (y % V0) * (uint)LHS_OFFSET_X + (y / V0) * (uint)lhs_stride_y + (z * lhs_stride_z);
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000397
398 // Compute RHS matrix address
Sheri Zhang28287af2020-02-25 14:13:54 +0000399 __global DATA_TYPE *rhs_addr = rhs_ptr + rhs_offset_first_element_in_bytes + (x % H0) * (uint)RHS_OFFSET_X + (x / (uint)H0) * rhs_stride_y;
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000400
401#if defined(MATRIX_B_DEPTH)
402 // 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 +0100403 rhs_addr += (z % MATRIX_B_DEPTH) * rhs_stride_z;
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000404#else // defined(MATRIX_B_DEPTH)
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100405 rhs_addr += z * rhs_stride_z;
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000406#endif // defined(MATRIX_B_DEPTH)
407
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100408 REPEAT_VAR_INIT_TO_CONST(8, uint, zlhs, 0); //uint zout0=0,zout1=0,zout2=0,... zout7=0;
409 REPEAT_VAR_INIT_TO_CONST(16, uint, zrhs, 0);
410
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000411 // Initialize the accumulators
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000412 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 +0000413
414 for(int i = 0; i < k; i += K0)
415 {
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000416 // Load values from LHS matrix
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000417 LOAD_BLOCK(M0, K0, DATA_TYPE, a, lhs_addr, 0, LHS_STEP_X, zlhs);
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000418
419 // Load values from RHS matrix
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000420 LOAD_BLOCK(N0, K0, DATA_TYPE, b, rhs_addr, 0, RHS_STEP_X, zrhs);
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000421
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100422 // Partial matrix multiplication M0,N0,K0
423 ARM_MM_K0XN0XM0(M0, N0, K0, a, b, c);
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000424
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100425 // Update address
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000426 lhs_addr += (M0 * LHS_STEP_X * LHS_STEP_LOOP);
427 rhs_addr += (N0 * RHS_STEP_X * RHS_STEP_LOOP);
428 }
429
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100430 __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 +0000431
432 REPEAT_VAR_INIT_TO_CONST(8, uint, zout, 0); //uint zout0=0,zout1=0,zout2=0,... zout7=0;
433
434#if defined(REINTERPRET_OUTPUT_AS_3D)
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100435 // The plane (zout) is calculated dividing M (y * M0) by HEIGHT_GEMM3D
Gian Marco Iodice9ae06d42020-10-22 16:37:12 +0100436 CALCULATE_Z_OFFSET(M0, uint, zout, y * M0, HEIGHT_GEMM3D, DEPTH_GEMM3D, dst_cross_plane_pad, dst_stride_y);
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000437
438 // Add offset for batched GEMM. The batches will be in the fourth dimension and for this reason we
439 // multiply dst_stride_z by DEPTH_GEMM3D
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100440 dst_addr += z * dst_stride_z * DEPTH_GEMM3D;
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000441
442#else // defined(REINTERPRET_OUTPUT_AS_3D)
443
444 // Add offset for batched GEMM
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100445 dst_addr += z * dst_stride_z;
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000446
447#endif // defined(REINTERPRET_OUTPUT_AS_3D)
448
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100449 // Convert and store output block
Manuel Bottini8cf753f2020-10-21 12:34:38 +0100450 const bool cond_y = ((get_global_id(1) + 1) * M0 >= M);
451 const bool cond_x = ((get_global_id(0) + 1) * N0 >= N);
452
453 // Store output block
454 REPEAT_VAR_INIT_CONVERT_SAT(M0, VEC_DATA_TYPE(int, N0), c, c_lp);
455 STORE_BLOCK_BOUNDARY_AWARE(M0, N0, int, c_lp, dst_addr, dst_stride_y, zout, PARTIAL_STORE_M0, PARTIAL_STORE_N0, cond_y, cond_x);
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000456
457#undef LHS_BLOCK_SIZE
458#undef LHS_OFFSET_X
459#undef LHS_STEP_X
460#undef RHS_BLOCK_SIZE
461#undef RHS_OFFSET_X
462#undef RHS_STEP_X
463}
Manuel Bottini8cf753f2020-10-21 12:34:38 +0100464#endif // defined(M0) && defined(N0) && defined(K0) && defined(V0) && defined(H0) && defined(M) && defined(N) && defined(PARTIAL_STORE_M0) && defined(PARTIAL_STORE_N0)
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000465
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000466#if defined(M0) && defined(N0) && defined(K0) && defined(H0) && defined(K)
467
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000468/** This OpenCL kernel computes the matrix multiplication between 2 matrices.
469 * The LHS matrix is NOT reshaped
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100470 * The RHS matrix is reshaped with @ref CLGEMMReshapeRHSMatrixKernel and the block K0xN0 is transposed
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000471 *
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000472 * @note The input data type must be passed at compile time using -DDATA_TYPE (i.e. -DDATA_TYPE=uchar)
473 * @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 +0000474 * @note The number of columns of LHS matrix must be passed at compile time using -DK (i.e. -DK=64)
475 * @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).
476 * @note The number of M0 rows to process must be passed at compile time using -DM0 (i.e. -DM0=2)
477 * @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)
478 * @note If the K0xN0 blocks in the reshaped RHS matrix have been interleaved, the option -DRHS_INTERLEAVE must passed at compile time.
479 * @note Only the following configurations of M0, N0 and K0 are currently supported:
480 * - M0 = 1, 2, 3, 4, 5, 6, 7, 8
481 * - N0 = 2, 3, 4, 8, 16
482 * - K0 = 2, 3, 4, 8, 16
483 * - H0 >= 1
484 *
485 * @note In case the input or output have to be reinterpreted as a 3D tensor, the following information must be passed at compile time:
486 * -# REINTERPRET_INPUT_AS_3D: To reinterpret the input as 3D
487 * -# REINTERPRET_OUTPUT_AS_3D: To reinterpret the output as 3D
488 * -# HEIGHT_GEMM3D: The height of the output in case it has to be reinterpreted as a 3D tensor.
489 * -# DEPTH_GEMM3D: The depth of the output in case it has to be reinterpreted as a 3D tensor
490 * (HEIGHT_GEMM3D * DEPTH_GEMM3D) = columns LHS matrix
491 *
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000492 * @param[in] lhs_ptr Pointer to the LHS reshaped matrix. Supported data type: QASYMM8/QASYMM8_SIGNED
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000493 * @param[in] lhs_stride_x Stride of the LHS reshaped matrix in X dimension (in bytes)
494 * @param[in] lhs_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
495 * @param[in] lhs_stride_y Stride of the LHS reshaped matrix in Y dimension (in bytes)
496 * @param[in] lhs_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
497 * @param[in] lhs_offset_first_element_in_bytes The offset of the first element in the LHS reshaped matrix
498 * @param[in] rhs_ptr Pointer to the RHS reshaped matrix. Supported data type: same as @p lhs_ptr
499 * @param[in] rhs_stride_x Stride of the RHS reshaped matrix in X dimension (in bytes)
500 * @param[in] rhs_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
501 * @param[in] rhs_stride_y Stride of the RHS reshaped matrix in Y dimension (in bytes)
502 * @param[in] rhs_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
503 * @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 +0000504 * @param[out] dst_ptr Pointer to the destination matrix Supported data type: S32
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000505 * @param[in] dst_stride_x Stride of the destination matrix in X dimension (in bytes)
506 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
507 * @param[in] dst_stride_y Stride of the destination matrix in Y dimension (in bytes)
508 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
509 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination matrix
510 * @param[in] lhs_stride_z Stride of the LHS reshaped matrix in Z dimension (in bytes)
511 * @param[in] rhs_stride_z Stride of the RHS reshaped matrix in Z dimension (in bytes)
512 * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes)
513 * @param[in] lhs_cross_plane_pad (Optional) Bottom paddings for LHS matrix in unit of elements (only if defined REINTERPRET_INPUT_AS_3D)
514 * @param[in] dst_cross_plane_pad (Optional) Bottom paddings for the output matrix in unit of elements (only if defined REINTERPRET_OUTPUT_AS_3D)
515 */
516__kernel void gemmlowp_mm_reshaped_only_rhs_t(IMAGE_DECLARATION(lhs),
517 IMAGE_DECLARATION(rhs),
518 IMAGE_DECLARATION(dst),
519 uint lhs_stride_z,
520 uint rhs_stride_z,
521 uint dst_stride_z
522#if defined(REINTERPRET_INPUT_AS_3D)
523 ,
524 uint lhs_cross_plane_pad
525#endif // REINTERPRET_INPUT_AS_3D
526#if defined(REINTERPRET_OUTPUT_AS_3D)
527 ,
528 uint dst_cross_plane_pad
529#endif // REINTERPRET_OUTPUT_AS_3D
530 )
531{
532 // Block size
533#define RHS_BLOCK_SIZE ((K0) * (N0))
534
535 // RHS offset and step X
536#if defined(RHS_INTERLEAVE)
537#define RHS_OFFSET_X (K0)
538#define RHS_STEP_X ((K0) * (H0))
539#define RHS_STEP_LOOP (1)
540#else // defined(RHS_INTERLEAVE)
541#define RHS_OFFSET_X (RHS_BLOCK_SIZE)
542#define RHS_STEP_X (K0)
543#define RHS_STEP_LOOP (H0)
544#endif // defined(RHS_INTERLEAVE)
545
546 uint x = get_global_id(0);
547 uint y = get_global_id(1);
548 uint z = get_global_id(2);
549
Gian Marco Iodice86cfffe2019-04-02 11:02:20 +0100550#if defined(DUMMY_WORK_ITEMS)
551 if((x * N0 >= N) || (y * M0 >= M))
552 {
553 return;
554 }
555#endif // defined(DUMMY_WORK_ITEMS)
556
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000557 // Compute LHS matrix address
558 uint lhs_offset = lhs_offset_first_element_in_bytes + y * M0 * (uint)lhs_stride_y;
559
560 // Compute RHS matrix address
561 uint rhs_offset = rhs_offset_first_element_in_bytes + (x % H0) * (uint)RHS_OFFSET_X + (x / (uint)H0) * rhs_stride_y;
562
563#if defined(MATRIX_B_DEPTH)
564 // Do not slide matrix B if the matrix B has 3 dimensions and matrix A more than 3
565 rhs_offset += (z % MATRIX_B_DEPTH) * rhs_stride_z;
566#else // defined(MATRIX_B_DEPTH)
567 rhs_offset += z * rhs_stride_z;
568#endif // defined(MATRIX_B_DEPTH)
569
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100570 REPEAT_VAR_INIT_TO_CONST(8, uint, zlhs, 0); //uint zout0=0,zout1=0,zout2=0,... zout7=0;
571 REPEAT_VAR_INIT_TO_CONST(16, uint, zrhs, 0);
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000572
573#if defined(REINTERPRET_INPUT_AS_3D)
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100574 // The plane (zlhs) is calculated dividing M (y * M0) by HEIGHT_GEMM3D
Gian Marco Iodice9ae06d42020-10-22 16:37:12 +0100575 CALCULATE_Z_OFFSET(M0, uint, zlhs, y * M0, HEIGHT_GEMM3D, DEPTH_GEMM3D, lhs_cross_plane_pad, lhs_stride_y);
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000576
577 // Add offset for batched GEMM. The batches will be in the fourth dimension and for this reason we
578 // multiply lhs_stride_z by DEPTH_GEMM3D
579 lhs_offset += z * lhs_stride_z * DEPTH_GEMM3D;
580
581#else // defined(REINTERPRET_INPUT_AS_3D)
582
583 // Add offset for batched GEMM
584 lhs_offset += z * lhs_stride_z;
585
586#endif // defined(REINTERPRET_INPUT_AS_3D)
587
588 // Initialize the accumulators
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000589 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 +0000590
591 for(int i = 0; i < K; i += K0)
592 {
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000593 // Load values from LHS matrix
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000594 LOAD_BLOCK(M0, K0, DATA_TYPE, a, lhs_ptr, lhs_offset, lhs_stride_y, zlhs);
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000595
596 // Load values from RHS matrix
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000597 LOAD_BLOCK(N0, K0, DATA_TYPE, b, rhs_ptr, rhs_offset, RHS_STEP_X, zrhs);
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000598
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100599 // Partial matrix multiplication M0,N0,K0
600 ARM_MM_K0XN0XM0(M0, N0, K0, a, b, c);
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000601
602 lhs_offset += K0;
603 rhs_offset += N0 * RHS_STEP_X * RHS_STEP_LOOP;
604 }
605
606 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + (x * (uint)N0) * sizeof(int) + (y * (uint)M0 * dst_stride_y);
607
608 REPEAT_VAR_INIT_TO_CONST(8, uint, zout, 0); //uint zout0=0,zout1=0,zout2=0,... zout7=0;
609
610#if defined(REINTERPRET_OUTPUT_AS_3D)
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000611 // The plane (zout) is calculated dividing M (y * M0) by HEIGHT_GEMM3D
Gian Marco Iodice9ae06d42020-10-22 16:37:12 +0100612 CALCULATE_Z_OFFSET(M0, uint, zout, y * M0, HEIGHT_GEMM3D, DEPTH_GEMM3D, dst_cross_plane_pad, dst_stride_y);
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000613
614 // Add offset for batched GEMM. The batches will be in the fourth dimension and for this reason we
615 // multiply dst_stride_z by DEPTH_GEMM3D
616 dst_addr += z * dst_stride_z * DEPTH_GEMM3D;
617
618#else // defined(REINTERPRET_OUTPUT_AS_3D)
619
620 // Add offset for batched GEMM
621 dst_addr += z * dst_stride_z;
622
623#endif // defined(REINTERPRET_OUTPUT_AS_3D)
624
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100625 // Convert and store output block
626 CONVERT_STORE_BLOCK(M0, N0, int, c, dst_addr, dst_stride_y, zout);
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000627
628#undef RHS_BLOCK_SIZE
629#undef RHS_OFFSET_X
630#undef RHS_STEP_X
631}
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000632
633#if defined(RESULT_OFFSET) && defined(RESULT_SHIFT) && defined(RESULT_MULTIPLIER)
634/** This OpenCL kernel computes the matrix multiplication between 2 matrices with fused output stage using fixed-point arithmetic.
635 * The LHS matrix is NOT reshaped
636 * The RHS matrix is reshaped with @ref CLGEMMReshapeRHSMatrixKernel and the block K0xN0 is transposed
637 *
638 * @note The input data type must be passed at compile time using -DDATA_TYPE (i.e. -DDATA_TYPE=uchar)
639 * @note The accumulator data type must be passed at compile time using -DACC_DATA_TYPE (i.e. -DACC_DATA_TYPE=uint)
640 * @note The number of columns of LHS matrix must be passed at compile time using -DK (i.e. -DK=64)
641 * @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).
642 * @note The number of M0 rows to process must be passed at compile time using -DM0 (i.e. -DM0=2)
643 * @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)
644 * @note If the K0xN0 blocks in the reshaped RHS matrix have been interleaved, the option -DRHS_INTERLEAVE must passed at compile time.
645 * @note Only the following configurations of M0, N0 and K0 are currently supported:
646 * - M0 = 1, 2, 3, 4, 5, 6, 7, 8
647 * - N0 = 2, 3, 4, 8, 16
648 * - K0 = 2, 3, 4, 8, 16
649 * - H0 >= 1
650 *
651 * @note In case the input or output have to be reinterpreted as a 3D tensor, the following information must be passed at compile time:
652 * -# REINTERPRET_INPUT_AS_3D: To reinterpret the input as 3D
653 * -# REINTERPRET_OUTPUT_AS_3D: To reinterpret the output as 3D
654 * -# HEIGHT_GEMM3D: The height of the output in case it has to be reinterpreted as a 3D tensor.
655 * -# DEPTH_GEMM3D: The depth of the output in case it has to be reinterpreted as a 3D tensor
656 * (HEIGHT_GEMM3D * DEPTH_GEMM3D) = columns LHS matrix
657 *
658 * @note The offset, scalar scale factor and number of bits to shift right of output tensor must be passed at compile time using -DRESULT_OFFSET, -RESULT_MULTIPLIER and -DRESULT_SHIFT
659 * @note In case the addition of int32 biases is required, -DADD_BIAS should be passed at compile time
660 * @note The output datatype should be passed at compile time using -DOUTPUT_DATA_TYPE
661 * @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.
662 * These values can be used to implement "rectified linear unit" activation functions
663 * @note In case of per-channel quantization of matrix B, -DPER_CHANNEL_QUANTIZATION must be passed at compile time.
664 *
665 * @param[in] lhs_ptr Pointer to the LHS reshaped matrix. Supported data type: QASYMM8/QASYMM8_SIGNED
666 * @param[in] lhs_stride_x Stride of the LHS reshaped matrix in X dimension (in bytes)
667 * @param[in] lhs_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
668 * @param[in] lhs_stride_y Stride of the LHS reshaped matrix in Y dimension (in bytes)
669 * @param[in] lhs_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
670 * @param[in] lhs_offset_first_element_in_bytes The offset of the first element in the LHS reshaped matrix
671 * @param[in] rhs_ptr Pointer to the RHS reshaped matrix. Supported data type: same as @p lhs_ptr
672 * @param[in] rhs_stride_x Stride of the RHS reshaped matrix in X dimension (in bytes)
673 * @param[in] rhs_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
674 * @param[in] rhs_stride_y Stride of the RHS reshaped matrix in Y dimension (in bytes)
675 * @param[in] rhs_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
676 * @param[in] rhs_offset_first_element_in_bytes The offset of the first element in the RHS reshaped matrix
677 * @param[out] dst_ptr Pointer to the destination matrix Supported data type: same as @p lhs_ptr
678 * @param[in] dst_stride_x Stride of the destination matrix in X dimension (in bytes)
679 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
680 * @param[in] dst_stride_y Stride of the destination matrix in Y dimension (in bytes)
681 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
682 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination matrix
683 * @param[in] lhs_stride_z Stride of the LHS reshaped matrix in Z dimension (in bytes)
684 * @param[in] rhs_stride_z Stride of the RHS reshaped matrix in Z dimension (in bytes)
685 * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes)
686 * @param[in] lhs_cross_plane_pad (Optional) Bottom paddings for LHS matrix in unit of elements (only if defined REINTERPRET_INPUT_AS_3D)
687 * @param[in] dst_cross_plane_pad (Optional) Bottom paddings for the output matrix in unit of elements (only if defined REINTERPRET_OUTPUT_AS_3D)
688 * @param[in] sum_col_ptr (Optional) Pointer to the source tensor. Supported data type: S32
689 * @param[in] sum_col_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
690 * @param[in] sum_col_step_x (Optional) sum_col_stride_x * number of elements along X processed per workitem(in bytes)
691 * @param[in] sum_col_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
692 * @param[in] sum_col_step_y (Optional) sum_col_stride_y * number of elements along Y processed per workitem(in bytes)
693 * @param[in] sum_col_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
694 * @param[in] sum_row_ptr (Optional) Pointer to the source tensor. Supported data type: S32
695 * @param[in] sum_row_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
696 * @param[in] sum_row_step_x (Optional) sum_row_stride_x * number of elements along X processed per workitem(in bytes)
697 * @param[in] sum_row_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
698 * @param[in] sum_row_step_y (Optional) sum_row_stride_y * number of elements along Y processed per workitem(in bytes)
699 * @param[in] sum_row_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
700 * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: S32
701 * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes)
702 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
703 * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases tensor
704 * @param[in] result_multipliers_ptr (Optional) Pointer to the output multipliers vector for per-channel quantization. Supported data types: S32
705 * @param[in] result_multipliers_stride_x (Optional) Stride of the output multipliers vector in X dimension (in bytes)
706 * @param[in] result_multipliers_step_x (Optional) output_multipliers_stride_x * number of elements along X processed per workitem(in bytes)
707 * @param[in] result_multipliers_offset_first_element_in_bytes (Optional) The offset of the first element in the output multipliers vector
708 * @param[in] result_shifts_ptr (Optional) Pointer to the output shifts vector for per-channel quantization. Supported data types: S32
709 * @param[in] result_shifts_stride_x (Optional) Stride of the output shifts vector in X dimension (in bytes)
710 * @param[in] result_shifts_step_x (Optional) output_shifts_stride_x * number of elements along X processed per workitem(in bytes)
711 * @param[in] result_shifts_offset_first_element_in_bytes (Optional) The offset of the first element in the output shifts vector
712 */
713__kernel void gemmlowp_mm_reshaped_only_rhs_t_fused_output_stage_fixedpoint(IMAGE_DECLARATION(lhs),
714 IMAGE_DECLARATION(rhs),
715 IMAGE_DECLARATION(dst),
716 uint lhs_stride_z,
717 uint rhs_stride_z,
718 uint dst_stride_z
719#if defined(REINTERPRET_INPUT_AS_3D)
720 ,
721 uint lhs_cross_plane_pad
722#endif // REINTERPRET_INPUT_AS_3D
723#if defined(REINTERPRET_OUTPUT_AS_3D)
724 ,
725 uint dst_cross_plane_pad
726#endif // REINTERPRET_OUTPUT_AS_3D
727#if defined(A_OFFSET)
728 ,
729 IMAGE_DECLARATION(sum_col)
730#endif // defined(A_OFFSET)
731#if defined(B_OFFSET)
732 ,
733 IMAGE_DECLARATION(sum_row)
734#endif // defined(B_OFFSET)
735#if defined(ADD_BIAS)
736 ,
737 VECTOR_DECLARATION(biases)
738#endif // defined(ADD_BIAS)
739#if defined(PER_CHANNEL_QUANTIZATION)
740 ,
741 VECTOR_DECLARATION(result_multipliers),
742 VECTOR_DECLARATION(result_shifts)
743#endif // defined(PER_CHANNEL_QUANTIZATION)
744 )
745{
746 // Block size
747#define RHS_BLOCK_SIZE ((K0) * (N0))
748
749 // RHS offset and step X
750#if defined(RHS_INTERLEAVE)
751#define RHS_OFFSET_X (K0)
752#define RHS_STEP_X ((K0) * (H0))
753#define RHS_STEP_LOOP (1)
754#else // defined(RHS_INTERLEAVE)
755#define RHS_OFFSET_X (RHS_BLOCK_SIZE)
756#define RHS_STEP_X (K0)
757#define RHS_STEP_LOOP (H0)
758#endif // defined(RHS_INTERLEAVE)
759
760 uint x = get_global_id(0);
761 uint y = get_global_id(1);
762 uint z = get_global_id(2);
763
764#if defined(DUMMY_WORK_ITEMS)
765 if((x * N0 >= N) || (y * M0 >= M))
766 {
767 return;
768 }
769#endif // defined(DUMMY_WORK_ITEMS)
770
771 // Compute LHS matrix address
772 uint lhs_offset = lhs_offset_first_element_in_bytes + y * M0 * (uint)lhs_stride_y;
773
774 // Compute RHS matrix address
775 uint rhs_offset = rhs_offset_first_element_in_bytes + (x % H0) * (uint)RHS_OFFSET_X + (x / (uint)H0) * rhs_stride_y;
776
777#if defined(MATRIX_B_DEPTH)
778 // Do not slide matrix B if the matrix B has 3 dimensions and matrix A more than 3
779 rhs_offset += (z % MATRIX_B_DEPTH) * rhs_stride_z;
780#else // defined(MATRIX_B_DEPTH)
781 rhs_offset += z * rhs_stride_z;
782#endif // defined(MATRIX_B_DEPTH)
783
784 REPEAT_VAR_INIT_TO_CONST(8, uint, zlhs, 0); //uint zout0=0,zout1=0,zout2=0,... zout7=0;
785 REPEAT_VAR_INIT_TO_CONST(16, uint, zrhs, 0);
786
787#if defined(REINTERPRET_INPUT_AS_3D)
788 // The plane (zlhs) is calculated dividing M (y * M0) by HEIGHT_GEMM3D
Gian Marco Iodice9ae06d42020-10-22 16:37:12 +0100789 CALCULATE_Z_OFFSET(M0, uint, zlhs, y * M0, HEIGHT_GEMM3D, DEPTH_GEMM3D, lhs_cross_plane_pad, lhs_stride_y);
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000790
791 // Add offset for batched GEMM. The batches will be in the fourth dimension and for this reason we
792 // multiply lhs_stride_z by DEPTH_GEMM3D
793 lhs_offset += z * lhs_stride_z * DEPTH_GEMM3D;
794
795#else // defined(REINTERPRET_INPUT_AS_3D)
796
797 // Add offset for batched GEMM
798 lhs_offset += z * lhs_stride_z;
799
800#endif // defined(REINTERPRET_INPUT_AS_3D)
801
802 // Initialize the accumulators
803 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;
804
805 for(int i = 0; i < K; i += K0)
806 {
807 // Load values from LHS matrix
808 LOAD_BLOCK(M0, K0, DATA_TYPE, a, lhs_ptr, lhs_offset, lhs_stride_y, zlhs);
809
810 // Load values from RHS matrix
811 LOAD_BLOCK(N0, K0, DATA_TYPE, b, rhs_ptr, rhs_offset, RHS_STEP_X, zrhs);
812
813 // Partial matrix multiplication M0,N0,K0
814 ARM_MM_K0XN0XM0(M0, N0, K0, a, b, c);
815
816 lhs_offset += K0;
817 rhs_offset += N0 * RHS_STEP_X * RHS_STEP_LOOP;
818 }
819
820 // Result of MM is of type DATA_TYPE
821 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + (x * (uint)N0) * sizeof(DATA_TYPE) + (y * (uint)M0 * dst_stride_y);
822
823 REPEAT_VAR_INIT_TO_CONST(8, uint, zout, 0); //uint zout0=0,zout1=0,zout2=0,... zout7=0;
824
825#if defined(REINTERPRET_OUTPUT_AS_3D)
826 // The plane (zout) is calculated dividing M (y * M0) by HEIGHT_GEMM3D
Gian Marco Iodice9ae06d42020-10-22 16:37:12 +0100827 CALCULATE_Z_OFFSET(M0, uint, zout, y * M0, HEIGHT_GEMM3D, DEPTH_GEMM3D, dst_cross_plane_pad, dst_stride_y);
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000828
829 // Add offset for batched GEMM. The batches will be in the fourth dimension and for this reason we
830 // multiply dst_stride_z by DEPTH_GEMM3D
831 dst_addr += z * dst_stride_z * DEPTH_GEMM3D;
832
833#else // defined(REINTERPRET_OUTPUT_AS_3D)
834
835 // Add offset for batched GEMM
836 dst_addr += z * dst_stride_z;
837
838#endif // defined(REINTERPRET_OUTPUT_AS_3D)
839
840 // Convert result of matrix multiplication to S32
841 REPEAT_VAR_INIT_CONVERT_SAT(M0, VEC_DATA_TYPE(int, N0), c, c_int);
842
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000843 // Offset contribution: c += (A_OFFSET * sum_col) + (B_OFFSET * sum_row) + K_OFFSET;
844 REPEAT_VAR_INIT_TO_CONST(M0, VEC_DATA_TYPE(int, N0), offset_s32_, K_OFFSET);
845
846#if defined(A_OFFSET)
847 // Compute the offset contribution due to A_OFFSET
848 __global uchar *sum_col_addr = sum_col_ptr + sum_col_offset_first_element_in_bytes + (x * (uint)N0) * sizeof(int);
849
850#if defined(SUM_COL_HAS_BATCHES)
851 sum_col_addr += z * sum_col_stride_y;
852#endif // defined(SUM_COL_HAS_BATCHES)
853 VEC_DATA_TYPE(int, N0)
854 a_offset_s32 = VLOAD(N0)(0, (__global int *)sum_col_addr);
855 a_offset_s32 *= (VEC_DATA_TYPE(int, N0))A_OFFSET;
856
857 REPEAT_ADD_VECTOR_TO_VAR(M0, offset_s32_, a_offset_s32);
858#endif // defined(A_OFFSET)
859
860#if defined(B_OFFSET)
861 // Compute the offset contribution due to B_OFFSET
Gian Marco Iodice27423f02020-08-12 14:12:28 +0100862 // Note: The sum_row tensor is generated through CLGEMMLowpMatrixAReductionKernel which
863 // does not introduce paddings. For this reason is safe to access the tensor in this manner
864 // without considering that the coordinate "y" could come from an input 3D tensor
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000865 __global uchar *sum_row_addr = sum_row_ptr + sum_row_offset_first_element_in_bytes + (y * (uint)M0) * sizeof(int) + z * sum_row_stride_y;
866
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000867 LOAD_SCALAR_AS_VECTOR(M0, N0, int, b_offset_s32_, sum_row_addr, 0, sum_row_stride_x);
868
869 REPEAT_MLA_VAR_WITH_CONST_VEC(M0, offset_s32_, b_offset_s32_, (VEC_DATA_TYPE(int, N0))B_OFFSET);
870#endif // defined(B_OFFSET)
871
872#if defined(ADD_BIAS)
873 // Add bias
874 __global uchar *bias_addr = biases_ptr + biases_offset_first_element_in_bytes + (x * (uint)N0) * sizeof(int);
875
876 VEC_DATA_TYPE(int, N0)
877 bias_values = VLOAD(N0)(0, (__global int *)bias_addr);
878 REPEAT_ADD_VECTOR_TO_VAR(M0, offset_s32_, bias_values);
879#endif // defined(ADD_BIAS)
880
881 REPEAT_ADD_TWO_VARS(M0, c_int, offset_s32_);
882
883 // Multiply by result_mult_int and shift
884#if defined(PER_CHANNEL_QUANTIZATION)
885 __global uchar *result_multipliers_addr = result_multipliers_ptr + result_multipliers_offset_first_element_in_bytes + (x * (uint)N0) * sizeof(int);
886 __global uchar *result_shifts_addr = result_shifts_ptr + result_shifts_offset_first_element_in_bytes + (x * (uint)N0) * sizeof(int);
887
888 VEC_DATA_TYPE(int, N0)
889 res_mul = VLOAD(N0)(0, (__global int *)result_multipliers_addr);
890 VEC_DATA_TYPE(int, N0)
891 res_shift = VLOAD(N0)(0, (__global int *)result_shifts_addr);
892
893 REPEAT_ASYMM_MULT_BY_QUANT_MULTIPLIER_PER_CHANNEL(M0, N0, c_int, res_mul, res_shift);
894#else // defined(PER_CHANNEL_QUANTIZATION)
895
896#if RESULT_SHIFT < 0
897 REPEAT_ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(M0, N0, c_int, RESULT_MULTIPLIER, RESULT_SHIFT);
898#else // RESULT_SHIFT >= 0
899 REPEAT_ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(M0, N0, c_int, RESULT_MULTIPLIER, RESULT_SHIFT);
900#endif // RESULT_SHIFT < 0
901
902#endif // defined(PER_CHANNEL_QUANTIZATION)
903
904 // Add the offset terms to GEMM's result
905 REPEAT_ADD_CONST_TO_VAR(M0, VEC_DATA_TYPE(int, N0), c_int, RESULT_OFFSET);
906
907#if defined(MIN_BOUND)
908 REPEAT_MAX_CONST_VAR(M0, VEC_DATA_TYPE(int, N0), c_int, MIN_BOUND);
909#endif // defined(MIN_BOUND)
910#if defined(MAX_BOUND)
911 REPEAT_MIN_CONST_VAR(M0, VEC_DATA_TYPE(int, N0), c_int, MAX_BOUND);
912#endif // defined(MAX_BOUND)
913
914 // Convert and store output block (does convert saturate)
915 CONVERT_STORE_BLOCK(M0, N0, DATA_TYPE, c_int, dst_addr, dst_stride_y, zout);
916
917#undef RHS_BLOCK_SIZE
918#undef RHS_OFFSET_X
919#undef RHS_STEP_X
920}
921#endif // defined(RESULT_OFFSET) && defined(RESULT_SHIFT) && defined(RESULT_MULTIPLIER)
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000922#endif // defined(M0) && defined(N0) && defined(K0) && defined(H0) && defined(DATA_TYPE) && defined(K)
923
SiCong Lied5fb392020-10-20 18:07:27 +0100924#if defined(M0) && defined(N0) && defined(K0) && defined(K) && defined(PARTIAL_STORE_M0) && defined(PARTIAL_STORE_N0)
Gian Marco Iodicee7510622019-06-03 17:28:17 +0100925
926/** This OpenCL kernel computes the matrix multiplication between 2 matrices.
927 * The LHS matrix is NOT reshaped
928 * The RHS matrix is NOT reshaped
929 *
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000930 * @note The input data type must be passed at compile time using -DDATA_TYPE (i.e. -DDATA_TYPE=uchar)
931 * @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 +0100932 * @note The number of columns of LHS matrix must be passed at compile time using -DK (i.e. -DK=64)
933 * @note The number of M0 rows to process must be passed at compile time using -DM0 (i.e. -DM0=2)
934 * @note The number of N0 columns to process must be passed at compile time using -DN0 (i.e. -DN0=2)
935 * @note The number of K0 partial accumulations must be passed at compile time using -DK0 (i.e., -DK0=2)
936 * @note Only the following configurations of M0, N0 and K0 are currently supported:
937 * - M0 = 1, 2, 3, 4, 5, 6, 7, 8
938 * - N0 = 2, 3, 4, 8, 16
939 * - K0 = 2, 3, 4, 8, 16
940 *
941 * @note In case the input or output have to be reinterpreted as a 3D tensor, the following information must be passed at compile time:
942 * -# REINTERPRET_INPUT_AS_3D: To reinterpret the input as 3D
943 * -# REINTERPRET_OUTPUT_AS_3D: To reinterpret the output as 3D
944 * -# HEIGHT_GEMM3D: The height of the output in case it has to be reinterpreted as a 3D tensor.
945 * -# DEPTH_GEMM3D: The depth of the output in case it has to be reinterpreted as a 3D tensor
946 * (HEIGHT_GEMM3D * DEPTH_GEMM3D) = columns LHS matrix
947 *
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000948 * @param[in] lhs_ptr Pointer to the LHS reshaped matrix. Supported data type: QASYMM8
Gian Marco Iodicee7510622019-06-03 17:28:17 +0100949 * @param[in] lhs_stride_x Stride of the LHS reshaped matrix in X dimension (in bytes)
950 * @param[in] lhs_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
951 * @param[in] lhs_stride_y Stride of the LHS reshaped matrix in Y dimension (in bytes)
952 * @param[in] lhs_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
953 * @param[in] lhs_offset_first_element_in_bytes The offset of the first element in the LHS reshaped matrix
954 * @param[in] rhs_ptr Pointer to the RHS reshaped matrix. Supported data type: same as @p lhs_ptr
955 * @param[in] rhs_stride_x Stride of the RHS reshaped matrix in X dimension (in bytes)
956 * @param[in] rhs_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
957 * @param[in] rhs_stride_y Stride of the RHS reshaped matrix in Y dimension (in bytes)
958 * @param[in] rhs_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
959 * @param[in] rhs_offset_first_element_in_bytes The offset of the first element in the RHS reshaped matrix
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000960 * @param[out] dst_ptr Pointer to the destination matrix Supported data type: S32
Gian Marco Iodicee7510622019-06-03 17:28:17 +0100961 * @param[in] dst_stride_x Stride of the destination matrix in X dimension (in bytes)
962 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
963 * @param[in] dst_stride_y Stride of the destination matrix in Y dimension (in bytes)
964 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
965 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination matrix
966 * @param[in] lhs_stride_z Stride of the LHS reshaped matrix in Z dimension (in bytes)
967 * @param[in] rhs_stride_z Stride of the RHS reshaped matrix in Z dimension (in bytes)
968 * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes)
969 * @param[in] lhs_cross_plane_pad (Optional) Bottom paddings for LHS matrix in unit of elements (only if defined REINTERPRET_INPUT_AS_3D)
970 * @param[in] dst_cross_plane_pad (Optional) Bottom paddings for the output matrix in unit of elements (only if defined REINTERPRET_OUTPUT_AS_3D)
971 */
972__kernel void gemmlowp_mm_native(IMAGE_DECLARATION(lhs),
973 IMAGE_DECLARATION(rhs),
974 IMAGE_DECLARATION(dst),
975 uint lhs_stride_z,
976 uint rhs_stride_z,
977 uint dst_stride_z
978#if defined(REINTERPRET_INPUT_AS_3D)
979 ,
980 uint lhs_cross_plane_pad
981#endif // REINTERPRET_INPUT_AS_3D
982#if defined(REINTERPRET_OUTPUT_AS_3D)
983 ,
984 uint dst_cross_plane_pad
985#endif // REINTERPRET_OUTPUT_AS_3D
986 )
987{
988 uint x = get_global_id(0);
989 uint y = get_global_id(1);
990 uint z = get_global_id(2);
991
992#if defined(DUMMY_WORK_ITEMS)
993 if((x * N0 >= N) || (y * M0 >= M))
994 {
995 return;
996 }
997#endif // defined(DUMMY_WORK_ITEMS)
998
999 // Compute LHS matrix address
morgolockcf343e32020-10-12 14:00:43 +01001000 uint lhs_offset = lhs_offset_first_element_in_bytes + COMPUTE_M0_START_ROW(y, M0, PARTIAL_STORE_M0) * (uint)lhs_stride_y;
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001001
1002 // Compute RHS matrix address
morgolockcf343e32020-10-12 14:00:43 +01001003 uint rhs_offset = rhs_offset_first_element_in_bytes + x * N0 * sizeof(DATA_TYPE);
1004
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001005#if defined(MATRIX_B_DEPTH)
1006 // Do not slide matrix B if the matrix B has 3 dimensions and matrix A more than 3
1007 rhs_offset += (z % MATRIX_B_DEPTH) * rhs_stride_z;
1008#else // defined(MATRIX_B_DEPTH)
1009 rhs_offset += z * rhs_stride_z;
1010#endif // defined(MATRIX_B_DEPTH)
1011
1012 REPEAT_VAR_INIT_TO_CONST(8, uint, zlhs, 0);
1013 REPEAT_VAR_INIT_TO_CONST(16, uint, zrhs, 0);
1014
1015#if defined(REINTERPRET_INPUT_AS_3D)
1016 // The plane (zlhs) is calculated dividing M (y * M0) by HEIGHT_GEMM3D
Gian Marco Iodice9ae06d42020-10-22 16:37:12 +01001017 CALCULATE_Z_OFFSET(M0, uint, zlhs, COMPUTE_M0_START_ROW(y, M0, PARTIAL_STORE_M0), HEIGHT_GEMM3D, DEPTH_GEMM3D, lhs_cross_plane_pad, lhs_stride_y);
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001018
1019 // Add offset for batched GEMM. The batches will be in the fourth dimension and for this reason we
1020 // multiply lhs_stride_z by DEPTH_GEMM3D
1021 lhs_offset += z * lhs_stride_z * DEPTH_GEMM3D;
1022
1023#else // defined(REINTERPRET_INPUT_AS_3D)
1024
1025 // Add offset for batched GEMM
1026 lhs_offset += z * lhs_stride_z;
1027
1028#endif // defined(REINTERPRET_INPUT_AS_3D)
1029
1030 // Initialize the accumulators
Michele Di Giorgiof9179d32019-11-27 16:17:30 +00001031 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 +01001032
1033 int i = 0;
1034
1035 for(; i <= (K - K0); i += K0)
1036 {
1037 // Load values from LHS matrix
Michele Di Giorgiof9179d32019-11-27 16:17:30 +00001038 LOAD_BLOCK(M0, K0, DATA_TYPE, a, lhs_ptr, lhs_offset, lhs_stride_y, zlhs);
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001039
1040 // Load values from RHS matrix
Michele Di Giorgiof9179d32019-11-27 16:17:30 +00001041 LOAD_BLOCK(K0, N0, DATA_TYPE, b, rhs_ptr, rhs_offset, rhs_stride_y, zrhs);
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001042
SiCong Li738893e2020-05-01 12:55:16 +01001043 // Partial matrix multiplication M0,N0,K0
1044#if(GPU_ARCH == GPU_ARCH_MIDGARD)
1045 ARM_MM_NATIVE_N0XK0XM0(VEC_DATA_TYPE(ACC_DATA_TYPE, N0), M0, K0, a, b, c);
1046#else // GPU_ARCH == GPU_ARCH_MIDGARD
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001047 // Transpose the values from RHS matrix
Michele Di Giorgiof9179d32019-11-27 16:17:30 +00001048 TRANSPOSE_K0XN0(K0, N0, b_t, b, DATA_TYPE);
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001049
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001050 ARM_MM_K0XN0XM0(M0, N0, K0, a, b_t, c);
SiCong Li738893e2020-05-01 12:55:16 +01001051#endif // GPU_ARCH == GPU_ARCH_MIDGARD
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001052
1053 // Update the offset
1054 lhs_offset += K0;
1055 rhs_offset += K0 * rhs_stride_y;
1056 }
1057
1058 // Left-over for loop
1059 for(; i < K; ++i)
1060 {
1061 // Load values from LHS matrix
Michele Di Giorgiof9179d32019-11-27 16:17:30 +00001062 LOAD_BLOCK(M0, 1, DATA_TYPE, a, lhs_ptr, lhs_offset, lhs_stride_y, zlhs);
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001063
1064 // Load values from RHS matrix
Michele Di Giorgiof9179d32019-11-27 16:17:30 +00001065 LOAD_BLOCK(1, N0, DATA_TYPE, b, rhs_ptr, rhs_offset, rhs_stride_y, zrhs);
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001066
SiCong Li738893e2020-05-01 12:55:16 +01001067 // Partial matrix multiplication M0,N0,1
1068#if(GPU_ARCH == GPU_ARCH_MIDGARD)
1069 ARM_MM_NATIVE_N0XK0XM0(VEC_DATA_TYPE(ACC_DATA_TYPE, N0), M0, 1, a, b, c);
1070#else // GPU_ARCH == GPU_ARCH_MIDGARD
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001071 // Transpose the values from RHS matrix
Michele Di Giorgiof9179d32019-11-27 16:17:30 +00001072 TRANSPOSE_K0XN0(1, N0, b_t, b, DATA_TYPE);
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001073
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001074 ARM_MM_K0XN0XM0(M0, N0, 1, a, b_t, c);
SiCong Li738893e2020-05-01 12:55:16 +01001075#endif // GPU_ARCH == GPU_ARCH_MIDGARD
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001076
1077 // Update the offset
1078 lhs_offset += 1;
1079 rhs_offset += rhs_stride_y;
1080 }
1081
morgolockcf343e32020-10-12 14:00:43 +01001082 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + (x * (uint)N0 * sizeof(int)) + (COMPUTE_M0_START_ROW(y, M0, PARTIAL_STORE_M0) * dst_stride_y);
1083
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001084 REPEAT_VAR_INIT_TO_CONST(M0, uint, zout, 0); //uint zout0=0,zout1=0,zout2=0,... zout7=0;
1085
1086#if defined(REINTERPRET_OUTPUT_AS_3D)
1087 // The plane (zout) is calculated dividing M (y * M0) by HEIGHT_GEMM3D
Gian Marco Iodice9ae06d42020-10-22 16:37:12 +01001088 CALCULATE_Z_OFFSET(M0, uint, zout, COMPUTE_M0_START_ROW(y, M0, PARTIAL_STORE_M0), HEIGHT_GEMM3D, DEPTH_GEMM3D, dst_cross_plane_pad, dst_stride_y);
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001089
1090 // Add offset for batched GEMM. The batches will be in the fourth dimension and for this reason we
1091 // multiply dst_stride_z by DEPTH_GEMM3D
1092 dst_addr += z * dst_stride_z * DEPTH_GEMM3D;
1093
1094#else // defined(REINTERPRET_OUTPUT_AS_3D)
1095
1096 // Add offset for batched GEMM
1097 dst_addr += z * dst_stride_z;
1098
1099#endif // defined(REINTERPRET_OUTPUT_AS_3D)
morgolockcf343e32020-10-12 14:00:43 +01001100 const bool cond_y = y == 0;
1101 const bool cond_x = ((x + 1) * N0 >= N);
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001102
SiCong Lied5fb392020-10-20 18:07:27 +01001103 // Store output block
Giorgio Arena1e2af2a2020-10-15 17:39:41 +01001104 STORE_BLOCK_BOUNDARY_AWARE(M0, N0, int, c, dst_addr, dst_stride_y, zout, PARTIAL_STORE_M0, PARTIAL_STORE_N0, cond_y, cond_x);
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001105}
SiCong Lied5fb392020-10-20 18:07:27 +01001106#endif // defined(M0) && defined(N0) && defined(K0) && defined(K) && defined(PARTIAL_STORE_M0) && defined(PARTIAL_STORE_N0)
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001107
Gian Marco05288a22017-11-21 10:57:50 +00001108#if defined(COLS_A)
1109/** OpenCL kernel used to compute the row-vectors of sums of all the entries in each row of Matrix A.
Michele Di Giorgiof64d3362020-04-03 12:40:10 +01001110 * It is also possible to multiply each reduced row by a scalar value, if SCALAR is passed at compile time.
Gian Marco05288a22017-11-21 10:57:50 +00001111 *
1112 * @note This stage is needed to handle the offset of matrix product
1113 * https://github.com/google/gemmlowp/blob/master/doc/low-precision.md
1114 *
1115 * @attention The number of matrix A columns needs to be passed at compile time using -DCOLS_A
Manuel Bottini959c26d2019-12-02 16:22:35 +00001116 * @note The input data type must be passed at compile time using -DDATA_TYPE (i.e. -DDATA_TYPE=uchar)
Michele Di Giorgioe7b333e2020-01-15 10:30:51 +00001117 * @note The data type for the accumulation must be passed at compile time using -DACC_DATA_TYPE (i.e. -DACC_DATA_TYPE=uint)
Michele Di Giorgiof64d3362020-04-03 12:40:10 +01001118 * @note In case of scaling the scalar value must be passed at compile time using -DSCALAR (e.g. -DSCALAR=3)
Gian Marco05288a22017-11-21 10:57:50 +00001119 *
Michele Di Giorgiof6f78762020-07-06 11:27:21 +01001120 * @param[in] src_ptr Pointer to the source tensor. Supported data type: QASYMM8/QASYMM8_SIGNED/QSYMM8
Gian Marco05288a22017-11-21 10:57:50 +00001121 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
1122 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
1123 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
1124 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
1125 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
1126 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1127 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
1128 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: S32
1129 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
1130 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
1131 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
1132 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
1133 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
1134 */
1135__kernel void gemmlowp_matrix_a_reduction(TENSOR3D_DECLARATION(src),
1136 IMAGE_DECLARATION(dst))
1137{
1138 // Compute source and destination addresses
1139 Tensor3D src = CONVERT_TO_TENSOR3D_STRUCT(src);
1140 Image dst = CONVERT_TO_IMAGE_STRUCT(dst);
1141
Michele Di Giorgioe7b333e2020-01-15 10:30:51 +00001142 VEC_DATA_TYPE(ACC_DATA_TYPE, 4)
1143 sum_row_32 = (VEC_DATA_TYPE(ACC_DATA_TYPE, 4))0;
1144 ACC_DATA_TYPE sum_row = 0;
Gian Marco05288a22017-11-21 10:57:50 +00001145
Manuel Bottini959c26d2019-12-02 16:22:35 +00001146 __global const DATA_TYPE *matrix_a = (__global const DATA_TYPE *)(src.ptr + get_global_id(0) * src_stride_y + get_global_id(1) * src_stride_z);
Gian Marco05288a22017-11-21 10:57:50 +00001147
1148 int i = 0;
1149
1150 // This for loop performs 16 accumulations
1151 for(; i <= ((int)COLS_A - 16); i += 16)
1152 {
Manuel Bottini959c26d2019-12-02 16:22:35 +00001153 const VEC_DATA_TYPE(DATA_TYPE, 16) a0 = vload16(0, matrix_a + i);
Gian Marco05288a22017-11-21 10:57:50 +00001154
Michele Di Giorgioe7b333e2020-01-15 10:30:51 +00001155 sum_row_32 += CONVERT(a0.s0123, VEC_DATA_TYPE(ACC_DATA_TYPE, 4)) + CONVERT(a0.s4567, VEC_DATA_TYPE(ACC_DATA_TYPE, 4)) + CONVERT(a0.s89AB, VEC_DATA_TYPE(ACC_DATA_TYPE, 4)) + CONVERT(a0.sCDEF,
1156 VEC_DATA_TYPE(ACC_DATA_TYPE, 4));
Gian Marco05288a22017-11-21 10:57:50 +00001157 }
1158
1159 // This for loop performs the leftover accumulations
1160 for(; i < COLS_A; ++i)
1161 {
Michele Di Giorgioe7b333e2020-01-15 10:30:51 +00001162 sum_row += (ACC_DATA_TYPE)matrix_a[i];
Gian Marco05288a22017-11-21 10:57:50 +00001163 }
1164
Manuel Bottini959c26d2019-12-02 16:22:35 +00001165 sum_row += sum_row_32.s0 + sum_row_32.s1 + sum_row_32.s2 + sum_row_32.s3;
Gian Marco05288a22017-11-21 10:57:50 +00001166
Michele Di Giorgiof64d3362020-04-03 12:40:10 +01001167#if defined(SCALAR)
1168 sum_row *= (int)SCALAR;
1169#endif // defined(SCALAR)
Gian Marco05288a22017-11-21 10:57:50 +00001170 *((__global int *)dst.ptr) = (int)sum_row;
1171}
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001172
1173#if defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8)
Michele Di Giorgiof64d3362020-04-03 12:40:10 +01001174/** 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.
1175 * It is also possible to multiply each reduced row by a scalar value, if SCALAR is passed at compile time.
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001176 *
1177 * @note This stage is needed to handle the offset of matrix product
1178 * https://github.com/google/gemmlowp/blob/master/doc/low-precision.md
1179 *
1180 * @attention The number of matrix A columns needs to be passed at compile time using -DCOLS_A
Manuel Bottini959c26d2019-12-02 16:22:35 +00001181 * @note The input data type must be passed at compile time using -DDATA_TYPE (i.e. -DDATA_TYPE=uchar)
Michele Di Giorgioe7b333e2020-01-15 10:30:51 +00001182 * @note The data type for the accumulation must be passed at compile time using -DACC_DATA_TYPE (i.e. -DACC_DATA_TYPE=uint)
Michele Di Giorgiof64d3362020-04-03 12:40:10 +01001183 * @note In case of scaling the scalar value must be passed at compile time using -DSCALAR (e.g. -DSCALAR=3)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001184 *
Michele Di Giorgiof6f78762020-07-06 11:27:21 +01001185 * @param[in] src_ptr Pointer to the source tensor. Supported data type: QASYMM8/QASYMM8_SIGNED/QSYMM8
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001186 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
1187 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
1188 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
1189 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
1190 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
1191 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1192 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
1193 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: S32
1194 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
1195 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
1196 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
1197 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
1198 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
1199 */
1200__kernel void gemmlowp_matrix_a_reduction_dot8(TENSOR3D_DECLARATION(src),
1201 IMAGE_DECLARATION(dst))
1202{
1203 // Compute source and destination addresses
1204 Tensor3D src = CONVERT_TO_TENSOR3D_STRUCT(src);
1205 Image dst = CONVERT_TO_IMAGE_STRUCT(dst);
1206
Michele Di Giorgioe7b333e2020-01-15 10:30:51 +00001207 ACC_DATA_TYPE sum_row = 0;
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001208
Manuel Bottini959c26d2019-12-02 16:22:35 +00001209 __global const DATA_TYPE *matrix_a = (__global const DATA_TYPE *)(src.ptr + get_global_id(0) * src_stride_y + get_global_id(1) * src_stride_z);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001210
1211 int i = 0;
1212
1213 // This for loop performs 16 accumulations
1214 for(; i <= ((int)COLS_A - 32); i += 32)
1215 {
Manuel Bottini959c26d2019-12-02 16:22:35 +00001216 VEC_DATA_TYPE(DATA_TYPE, 16)
1217 a0 = vload16(0, matrix_a + i);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001218
Manuel Bottini959c26d2019-12-02 16:22:35 +00001219 sum_row += arm_dot(a0.s0123, (VEC_DATA_TYPE(DATA_TYPE, 4))(1));
1220 sum_row += arm_dot(a0.s4567, (VEC_DATA_TYPE(DATA_TYPE, 4))(1));
1221 sum_row += arm_dot(a0.s89AB, (VEC_DATA_TYPE(DATA_TYPE, 4))(1));
1222 sum_row += arm_dot(a0.sCDEF, (VEC_DATA_TYPE(DATA_TYPE, 4))(1));
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001223
Manuel Bottini959c26d2019-12-02 16:22:35 +00001224 a0 = vload16(1, matrix_a + i);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001225
Manuel Bottini959c26d2019-12-02 16:22:35 +00001226 sum_row += arm_dot(a0.s0123, (VEC_DATA_TYPE(DATA_TYPE, 4))(1));
1227 sum_row += arm_dot(a0.s4567, (VEC_DATA_TYPE(DATA_TYPE, 4))(1));
1228 sum_row += arm_dot(a0.s89AB, (VEC_DATA_TYPE(DATA_TYPE, 4))(1));
1229 sum_row += arm_dot(a0.sCDEF, (VEC_DATA_TYPE(DATA_TYPE, 4))(1));
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001230 }
1231
1232 // This for loop performs the leftover accumulations
1233 for(; i < COLS_A; ++i)
1234 {
Michele Di Giorgioe7b333e2020-01-15 10:30:51 +00001235 sum_row += (ACC_DATA_TYPE)matrix_a[i];
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001236 }
1237
Michele Di Giorgiof64d3362020-04-03 12:40:10 +01001238#if defined(SCALAR)
1239 sum_row *= (int)SCALAR;
1240#endif // defined(SCALAR)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001241 *((__global int *)dst.ptr) = (int)sum_row;
1242}
1243#endif // defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8)
Gian Marco05288a22017-11-21 10:57:50 +00001244#endif // defined(COLS_A)
1245
Michele Di Giorgioaae34102020-10-19 15:31:45 +01001246#if defined(COLS_B) && defined(ROWS_B) && defined(VEC_SIZE) && defined(VEC_SIZE_LEFTOVER)
Gian Marco05288a22017-11-21 10:57:50 +00001247/** OpenCL kernel used to compute the row-vectors of sums of all the entries in each column of Matrix B.
Michele Di Giorgiof64d3362020-04-03 12:40:10 +01001248 * It is also possible to multiply each reduced column by a scalar value, if SCALAR is passed at compile time.
Gian Marco05288a22017-11-21 10:57:50 +00001249 *
1250 * @note This stage is needed to handle the offset of matrix product
1251 * https://github.com/google/gemmlowp/blob/master/doc/low-precision.md
1252 *
1253 * @attention The number of matrix B columns and rows needs to be passed at compile time using -DCOLS_B and -DROWS_B
Manuel Bottini959c26d2019-12-02 16:22:35 +00001254 * @note The input data type must be passed at compile time using -DDATA_TYPE (i.e. -DDATA_TYPE=uchar)
Michele Di Giorgioe7b333e2020-01-15 10:30:51 +00001255 * @note The data type for the accumulation must be passed at compile time using -DACC_DATA_TYPE (i.e. -DACC_DATA_TYPE=uint)
Michele Di Giorgiof64d3362020-04-03 12:40:10 +01001256 * @note In case of scaling the scalar value must be passed at compile time using -DSCALAR (i.e. -DSCALAR=3)
Michele Di Giorgioaae34102020-10-19 15:31:45 +01001257 * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size. e.g. -DVEC_SIZE=16
1258 * @note Leftover vector size has to be passed at compile time using -DVEC_SIZE_LEFTOVER. e.g. -DVEC_SIZE_LEFTOVER=3. It is defined as the remainder between the input's first dimension and VEC_SIZE
Gian Marco05288a22017-11-21 10:57:50 +00001259 *
Michele Di Giorgiof6f78762020-07-06 11:27:21 +01001260 * @param[in] src_ptr Pointer to the source tensor. Supported data type: QASYMM8/QASYMM8_SIGNED/QSYMM8/QSYMM8_PER_CHANNEL
Gian Marco05288a22017-11-21 10:57:50 +00001261 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
1262 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
1263 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
1264 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
1265 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
1266 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1267 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
1268 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: S32
1269 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
1270 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
1271 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
1272 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
1273 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
1274 */
1275__kernel void gemmlowp_matrix_b_reduction(TENSOR3D_DECLARATION(src),
1276 IMAGE_DECLARATION(dst))
1277{
1278 // Compute source and destination addresses
Michele Di Giorgioaae34102020-10-19 15:31:45 +01001279 const uint x_offs = max((int)(get_global_id(0) * VEC_SIZE - (VEC_SIZE - VEC_SIZE_LEFTOVER) % VEC_SIZE), 0);
1280 const uint y = get_global_id(1);
Gian Marco05288a22017-11-21 10:57:50 +00001281
Michele Di Giorgioaae34102020-10-19 15:31:45 +01001282 __global const DATA_TYPE *matrix_b = (__global const DATA_TYPE *)(src_ptr + src_offset_first_element_in_bytes + x_offs * sizeof(DATA_TYPE) + y * src_step_y + y * src_stride_z);
1283 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x_offs * sizeof(int) + y * dst_stride_y;
Gian Marco05288a22017-11-21 10:57:50 +00001284
Michele Di Giorgioaae34102020-10-19 15:31:45 +01001285 VEC_DATA_TYPE(ACC_DATA_TYPE, VEC_SIZE)
Michele Di Giorgioed902bc2020-10-22 12:05:09 +01001286 sum_col_32 = (VEC_DATA_TYPE(ACC_DATA_TYPE, VEC_SIZE))0;
Gian Marco05288a22017-11-21 10:57:50 +00001287
1288 int i = 0;
1289 // This for loop performs 4 accumulations
1290 for(; i <= ((int)ROWS_B - 4); i += 4)
1291 {
Michele Di Giorgioaae34102020-10-19 15:31:45 +01001292 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
1293 b0 = VLOAD(VEC_SIZE)(0, matrix_b + 0 * src_stride_y);
1294 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
1295 b1 = VLOAD(VEC_SIZE)(0, matrix_b + 1 * src_stride_y);
1296 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
1297 b2 = VLOAD(VEC_SIZE)(0, matrix_b + 2 * src_stride_y);
1298 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
1299 b3 = VLOAD(VEC_SIZE)(0, matrix_b + 3 * src_stride_y);
Gian Marco05288a22017-11-21 10:57:50 +00001300
Michele Di Giorgioed902bc2020-10-22 12:05:09 +01001301 sum_col_32 += CONVERT(b0, VEC_DATA_TYPE(ACC_DATA_TYPE, VEC_SIZE)) + CONVERT(b1, VEC_DATA_TYPE(ACC_DATA_TYPE, VEC_SIZE)) + CONVERT(b2, VEC_DATA_TYPE(ACC_DATA_TYPE, VEC_SIZE)) + CONVERT(b3,
1302 VEC_DATA_TYPE(ACC_DATA_TYPE, VEC_SIZE));
Gian Marco05288a22017-11-21 10:57:50 +00001303
1304 matrix_b += 4 * src_stride_y;
1305 }
1306
1307 // This for loop perfoms the leftover accumulations
1308 for(; i < (int)ROWS_B; ++i)
1309 {
Michele Di Giorgioaae34102020-10-19 15:31:45 +01001310 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
1311 b0 = VLOAD(VEC_SIZE)(0, matrix_b);
Gian Marco05288a22017-11-21 10:57:50 +00001312
Michele Di Giorgioed902bc2020-10-22 12:05:09 +01001313 sum_col_32 += CONVERT(b0, VEC_DATA_TYPE(ACC_DATA_TYPE, VEC_SIZE));
Gian Marco05288a22017-11-21 10:57:50 +00001314
1315 matrix_b += src_stride_y;
1316 }
1317
Michele Di Giorgiof64d3362020-04-03 12:40:10 +01001318#if defined(SCALAR)
Michele Di Giorgioed902bc2020-10-22 12:05:09 +01001319 sum_col_32 *= (VEC_DATA_TYPE(ACC_DATA_TYPE, VEC_SIZE))SCALAR;
Michele Di Giorgiof64d3362020-04-03 12:40:10 +01001320#endif // defined(SCALAR)
Michele Di Giorgioed902bc2020-10-22 12:05:09 +01001321 VEC_DATA_TYPE(int, VEC_SIZE)
1322 res0 = CONVERT(sum_col_32, VEC_DATA_TYPE(int, VEC_SIZE));
1323
1324 STORE_VECTOR_SELECT(res, int, dst_addr, VEC_SIZE, VEC_SIZE_LEFTOVER, VEC_SIZE_LEFTOVER != 0 && get_global_id(0) == 0)
Gian Marco05288a22017-11-21 10:57:50 +00001325}
Michele Di Giorgioaae34102020-10-19 15:31:45 +01001326#endif // defined(COLS_B) && defined(ROWS_B) && defined(VEC_SIZE) && defined(VEC_SIZE_LEFTOVER)
Gian Marco05288a22017-11-21 10:57:50 +00001327
Michele Di Giorgioe7b333e2020-01-15 10:30:51 +00001328#endif // defined(DATA_TYPE) && defined(ACC_DATA_TYPE)
1329
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001330#if defined(K_OFFSET) && defined(VEC_SIZE) && defined(VEC_SIZE_LEFTOVER)
1331
1332#define VEC_INT VEC_DATA_TYPE(int, VEC_SIZE)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001333
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001334/* Helper function used to calculate the offset contribution after matrix multiplication.
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001335 *
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001336 * This kernel takes a final int32 accumulator value (the output of matrix multiplication),
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001337 * and calculates the offset contribution of matrix A and matrix B.
1338 *
1339 * @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)
1340 * @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)
1341 * @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)
1342 * @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
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001343 * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size. e.g. -DVEC_SIZE=16
1344 * @note Leftover vector size has to be passed at compile time using -DVEC_SIZE_LEFTOVER. e.g. -DVEC_SIZE_LEFTOVER=3. It is defined as the remainder between the input's first dimension and VEC_SIZE
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001345 *
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001346 * @param[in] x max((int)(get_global_id(0) * VEC_SIZE - (VEC_SIZE - VEC_SIZE_LEFTOVER) % VEC_SIZE), 0)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001347 * @param[in] y get_global_id(1)
1348 * @param[in] z get_global_id(2)
1349 * @param[in] sum_col_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr
1350 * @param[in] sum_col_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
1351 * @param[in] sum_col_step_x (Optional) sum_col_stride_x * number of elements along X processed per workitem(in bytes)
1352 * @param[in] sum_col_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
1353 * @param[in] sum_col_step_y (Optional) sum_col_stride_y * number of elements along Y processed per workitem(in bytes)
1354 * @param[in] sum_col_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
1355 * @param[in] sum_row_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr
1356 * @param[in] sum_row_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
1357 * @param[in] sum_row_step_x (Optional) sum_row_stride_x * number of elements along X processed per workitem(in bytes)
1358 * @param[in] sum_row_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
1359 * @param[in] sum_row_step_y (Optional) sum_row_stride_y * number of elements along Y processed per workitem(in bytes)
1360 * @param[in] sum_row_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
1361 * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: same as @p src_ptr
1362 * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes)
1363 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
1364 * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases tensor
1365 */
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001366inline VEC_INT offset_contribution(
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001367 int x,
1368 int y,
1369 int z
1370#if defined(A_OFFSET)
1371 ,
1372 IMAGE_DECLARATION(sum_col)
1373#endif // defined(A_OFFSET)
1374#if defined(B_OFFSET)
1375 ,
1376 IMAGE_DECLARATION(sum_row)
1377#endif // defined(B_OFFSET)
1378#if defined(ADD_BIAS)
1379 ,
1380 VECTOR_DECLARATION(biases)
1381#endif // defined(ADD_BIAS)
1382)
1383{
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001384 VEC_INT a_offset_s32 = (VEC_INT)0;
1385 VEC_INT b_offset_s32 = (VEC_INT)0;
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001386
1387 int batch_id = z;
1388#if defined(DEPTH_INPUT3D)
1389 batch_id /= (int)DEPTH_INPUT3D;
1390#endif // defined(DEPTH_INPUT3D)
1391
1392#if defined(A_OFFSET)
1393 // Compute the offset contribution due to A_OFFSET
1394 __global uchar *sum_col_addr = sum_col_ptr + sum_col_offset_first_element_in_bytes + x * sizeof(int);
1395
1396 // Compute the offset contribution due to A_OFFSET
1397#if defined(SUM_COL_HAS_BATCHES)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001398 a_offset_s32 = VLOAD(VEC_SIZE)(0, (__global int *)(sum_col_addr + batch_id * sum_col_stride_y));
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001399#else // defined(SUM_COL_HAS_BATCHES)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001400 a_offset_s32 = VLOAD(VEC_SIZE)(0, (__global int *)sum_col_addr);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001401#endif // defined(SUM_COL_HAS_BATCHES)
1402
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001403 a_offset_s32 *= (VEC_INT)A_OFFSET;
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001404#endif // defined(A_OFFSET)
1405
1406#if defined(B_OFFSET)
1407 // Compute the offset contribution due to A_OFFSET
1408 __global uchar *sum_row_addr = sum_row_ptr + sum_row_offset_first_element_in_bytes + y * sizeof(int);
1409
1410 // Compute the offset contribution due to B_OFFSET
1411#if defined(HEIGHT_INPUT3D) && defined(DEPTH_INPUT3D)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001412 b_offset_s32 = (VEC_INT) * (((__global int *)(sum_row_addr + batch_id * sum_row_stride_y)) + (z % (int)DEPTH_INPUT3D) * (int)HEIGHT_INPUT3D);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001413#else // defined(HEIGHT_INPUT3D) && defined(DEPTH_INPUT3D)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001414 b_offset_s32 = (VEC_INT) * (((__global int *)(sum_row_addr + batch_id * sum_row_stride_y)));
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001415#endif // defined(HEIGHT_INPUT3D) && defined(DEPTH_INPUT3D)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001416 b_offset_s32 *= (VEC_INT)B_OFFSET;
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001417#endif // defined(B_OFFSET)
1418
1419#if defined(ADD_BIAS)
1420 // Add bias
1421 __global uchar *bias_addr = biases_ptr + biases_offset_first_element_in_bytes + x * sizeof(int);
1422
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001423 VEC_INT biases_values = VLOAD(VEC_SIZE)(0, (__global int *)bias_addr);
1424 b_offset_s32 += (VEC_INT)biases_values;
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001425#endif // defined(ADD_BIAS)
1426
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001427 return (VEC_INT)K_OFFSET + a_offset_s32 + b_offset_s32;
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001428}
1429
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001430/* OpenCL kernel used to add the offset contribution after matrix multiplication. The computation is performed in-place
Gian Marco05288a22017-11-21 10:57:50 +00001431 *
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001432 * This kernel takes a final int32 accumulator value (the output of matrix multiplication),
Gian Marco05288a22017-11-21 10:57:50 +00001433 * and adds to it the offset contribution of matrix A and matrix B in-place.
1434 *
1435 * @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)
1436 * @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)
1437 * @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 +07001438 * @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
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001439 * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size. e.g. -DVEC_SIZE=16
1440 * @note Leftover vector size has to be passed at compile time using -DVEC_SIZE_LEFTOVER. e.g. -DVEC_SIZE_LEFTOVER=3. It is defined as the remainder between the input's first dimension and VEC_SIZE
Gian Marco05288a22017-11-21 10:57:50 +00001441 *
1442 * The final result is:
1443 *
1444 * mm_result[i][k] = mm_result[i][k] +
1445 * (sum_col[k] * A_OFFSET) +
1446 * (sum_row[i] * B_OFFSET) +
1447 * (K_OFFSET)
1448 *
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +01001449 * @param[in] mm_result_ptr Pointer to the source tensor. Supported data type: S32
1450 * @param[in] mm_result_stride_x Stride of the source tensor in X dimension (in bytes)
1451 * @param[in] mm_result_step_x mm_result_stride_x * number of elements along X processed per workitem(in bytes)
1452 * @param[in] mm_result_stride_y Stride of the source tensor in Y dimension (in bytes)
1453 * @param[in] mm_result_step_y mm_result_stride_y * number of elements along Y processed per workitem(in bytes)
1454 * @param[in] mm_result_stride_z Stride of the source tensor in Z dimension (in bytes)
1455 * @param[in] mm_result_step_z mm_result_stride_z * number of elements along Z processed per workitem(in bytes)
1456 * @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 +01001457 * @param[in] sum_col_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr
1458 * @param[in] sum_col_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
1459 * @param[in] sum_col_step_x (Optional) sum_col_stride_x * number of elements along X processed per workitem(in bytes)
1460 * @param[in] sum_col_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
1461 * @param[in] sum_col_step_y (Optional) sum_col_stride_y * number of elements along Y processed per workitem(in bytes)
1462 * @param[in] sum_col_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
1463 * @param[in] sum_row_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr
1464 * @param[in] sum_row_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
1465 * @param[in] sum_row_step_x (Optional) sum_row_stride_x * number of elements along X processed per workitem(in bytes)
1466 * @param[in] sum_row_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
1467 * @param[in] sum_row_step_y (Optional) sum_row_stride_y * number of elements along Y processed per workitem(in bytes)
1468 * @param[in] sum_row_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
1469 * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: same as @p src_ptr
1470 * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes)
1471 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
1472 * @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 +00001473 */
1474__kernel void gemmlowp_offset_contribution(TENSOR3D_DECLARATION(mm_result)
1475#if defined(A_OFFSET)
1476 ,
1477 IMAGE_DECLARATION(sum_col)
1478#endif // defined(A_OFFSET)
1479#if defined(B_OFFSET)
1480 ,
1481 IMAGE_DECLARATION(sum_row)
1482#endif // defined(B_OFFSET)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001483#if defined(ADD_BIAS)
1484 ,
1485 VECTOR_DECLARATION(biases)
1486#endif // defined(ADD_BIAS))
Gian Marco05288a22017-11-21 10:57:50 +00001487 )
1488{
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001489 const int x = max((int)(get_global_id(0) * VEC_SIZE - (VEC_SIZE - VEC_SIZE_LEFTOVER) % VEC_SIZE), 0);
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +01001490 const int y = get_global_id(1);
1491 const int z = get_global_id(2);
1492
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001493 // Compute offset contribution
Michele Di Giorgio410bca42020-10-22 11:07:33 +01001494 VEC_INT offset_term_s32 = offset_contribution(
1495 x, y, z
Gian Marco05288a22017-11-21 10:57:50 +00001496#if defined(A_OFFSET)
Michele Di Giorgio410bca42020-10-22 11:07:33 +01001497 ,
1498 sum_col_ptr,
1499 sum_col_stride_x,
1500 sum_col_step_x,
1501 sum_col_stride_y,
1502 sum_col_step_y,
1503 sum_col_offset_first_element_in_bytes
Gian Marco05288a22017-11-21 10:57:50 +00001504#endif // defined(A_OFFSET)
Gian Marco05288a22017-11-21 10:57:50 +00001505#if defined(B_OFFSET)
Michele Di Giorgio410bca42020-10-22 11:07:33 +01001506 ,
1507 sum_row_ptr,
1508 sum_row_stride_x,
1509 sum_row_step_x,
1510 sum_row_stride_y,
1511 sum_row_step_y,
1512 sum_row_offset_first_element_in_bytes
Gian Marco05288a22017-11-21 10:57:50 +00001513#endif // defined(B_OFFSET)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001514#if defined(ADD_BIAS)
Michele Di Giorgio410bca42020-10-22 11:07:33 +01001515 ,
1516 biases_ptr,
1517 biases_stride_x,
1518 biases_step_x,
1519 biases_offset_first_element_in_bytes
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001520#endif // defined(ADD_BIAS)
Michele Di Giorgio410bca42020-10-22 11:07:33 +01001521 );
Gian Marco05288a22017-11-21 10:57:50 +00001522
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001523 __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 +00001524
Michele Di Giorgio410bca42020-10-22 11:07:33 +01001525 VEC_INT in_s32_0 = VLOAD(VEC_SIZE)(0, (__global int *)mm_result_addr);
Gian Marco05288a22017-11-21 10:57:50 +00001526
1527 // Add the offset terms to GEMM's result
Michele Di Giorgio410bca42020-10-22 11:07:33 +01001528 in_s32_0 += offset_term_s32;
Gian Marco05288a22017-11-21 10:57:50 +00001529
1530 // Store the result with the offset contribution
Michele Di Giorgio410bca42020-10-22 11:07:33 +01001531 STORE_VECTOR_SELECT(in_s32_, int, mm_result_addr, VEC_SIZE, VEC_SIZE_LEFTOVER, VEC_SIZE_LEFTOVER != 0 && get_global_id(0) == 0)
Gian Marco05288a22017-11-21 10:57:50 +00001532}
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001533
Michele Di Giorgiob54ba282020-01-14 15:31:55 +00001534#if defined(RESULT_OFFSET) && defined(RESULT_MULTIPLIER) && defined(RESULT_SHIFT) && defined(OUTPUT_DATA_TYPE)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001535/* OpenCL kernel used to add the offset contribution after @ref CLGEMMLowpMatrixMultiplyKernel and it quantizes down to uint8.
1536 *
1537 * 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.
1538 *
1539 *
1540 * @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)
1541 * @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)
1542 * @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)
1543 * @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
1544 *
1545 * The result before the output stage is:
1546 *
1547 * mm_result[i][k] = mm_result[i][k] +
1548 * (sum_col[k] * A_OFFSET) +
1549 * (sum_row[i] * B_OFFSET) +
1550 * (K_OFFSET)
1551 *
Manuel Bottini959c26d2019-12-02 16:22:35 +00001552 * This result is quantized down to uint8/int8 using the output stage. The output stage computes the following operations:
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001553 *
1554 * -# Add offset terms to final result
1555 * -# Multiply each entry of result by result_mult_int
1556 * -# Add bias to final result (if -DADD_BIAS is passed at compile time)
1557 * -# Shift the int32 accumulator by result_shift
1558 * -# Clamp the value between the specified min and max bounds (if -DMIN_BOUND and/or -DMAX_BOUND are passed at compile time)
Manuel Bottini959c26d2019-12-02 16:22:35 +00001559 * -# Clamp the resulting int32 values:
1560 * - to the [0..255] range and cast to QASYMM8.
1561 * - to the [-128..127] range and cast to QASYMM8_SIGNED.
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001562 *
1563 * @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
1564 *
1565 * @note In case the addition of int32 biases is required, -DADD_BIAS should be passed at compile time
Manuel Bottini959c26d2019-12-02 16:22:35 +00001566 * @note The output datatype should be passed at compile time using -DOUTPUT_DATA_TYPE
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001567 * @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.
1568 * These values can be used to implement "rectified linear unit" activation functions
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001569 * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size. e.g. -DVEC_SIZE=16
1570 * @note Leftover vector size has to be passed at compile time using -DVEC_SIZE_LEFTOVER. e.g. -DVEC_SIZE_LEFTOVER=3. It is defined as the remainder between the input's first dimension and VEC_SIZE
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001571 *
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001572 * @param[in] mm_result_ptr Pointer to the source tensor. Supported data type: S32
1573 * @param[in] mm_result_stride_x Stride of the source tensor in X dimension (in bytes)
1574 * @param[in] mm_result_step_x mm_result_stride_x * number of elements along X processed per workitem(in bytes)
1575 * @param[in] mm_result_stride_y Stride of the source tensor in Y dimension (in bytes)
1576 * @param[in] mm_result_step_y mm_result_stride_y * number of elements along Y processed per workitem(in bytes)
1577 * @param[in] mm_result_stride_z Stride of the source tensor in Z dimension (in bytes)
1578 * @param[in] mm_result_step_z mm_result_stride_z * number of elements along Z processed per workitem(in bytes)
1579 * @param[in] mm_result_offset_first_element_in_bytes The offset of the first element in the source tensor
1580 * @param[in] sum_col_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr
1581 * @param[in] sum_col_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
1582 * @param[in] sum_col_step_x (Optional) sum_col_stride_x * number of elements along X processed per workitem(in bytes)
1583 * @param[in] sum_col_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
1584 * @param[in] sum_col_step_y (Optional) sum_col_stride_y * number of elements along Y processed per workitem(in bytes)
1585 * @param[in] sum_col_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
1586 * @param[in] sum_row_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr
1587 * @param[in] sum_row_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
1588 * @param[in] sum_row_step_x (Optional) sum_row_stride_x * number of elements along X processed per workitem(in bytes)
1589 * @param[in] sum_row_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
1590 * @param[in] sum_row_step_y (Optional) sum_row_stride_y * number of elements along Y processed per workitem(in bytes)
1591 * @param[in] sum_row_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
1592 * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: same as @p src_ptr
1593 * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes)
1594 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
1595 * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases tensor
Manuel Bottini959c26d2019-12-02 16:22:35 +00001596 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: QASYMM8/QASYMM8_SIGNED
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001597 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
1598 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
1599 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
1600 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
1601 * @param[in] dst_stride_z Stride of the source tensor in Z dimension (in bytes)
1602 * @param[in] dst_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1603 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
1604 * @param[in] result_multipliers_ptr (Optional) Pointer to the output multipliers vector for per-channel quantization. Supported data types: S32
1605 * @param[in] result_multipliers_stride_x (Optional) Stride of the output multipliers vector in X dimension (in bytes)
1606 * @param[in] result_multipliers_step_x (Optional) output_multipliers_stride_x * number of elements along X processed per workitem(in bytes)
1607 * @param[in] result_multipliers_offset_first_element_in_bytes (Optional) The offset of the first element in the output multipliers vector
1608 * @param[in] result_shifts_ptr (Optional) Pointer to the output shifts vector for per-channel quantization. Supported data types: S32
1609 * @param[in] result_shifts_stride_x (Optional) Stride of the output shifts vector in X dimension (in bytes)
1610 * @param[in] result_shifts_step_x (Optional) output_shifts_stride_x * number of elements along X processed per workitem(in bytes)
1611 * @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 +01001612 */
1613__kernel void gemmlowp_offset_contribution_quantize_down(TENSOR3D_DECLARATION(mm_result)
1614#if defined(A_OFFSET)
1615 ,
1616 IMAGE_DECLARATION(sum_col)
1617#endif // defined(A_OFFSET)
1618#if defined(B_OFFSET)
1619 ,
1620 IMAGE_DECLARATION(sum_row)
1621#endif // defined(B_OFFSET)
1622 ,
1623#if defined(ADD_BIAS)
1624 VECTOR_DECLARATION(biases),
1625#endif // defined(ADD_BIAS)
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001626 TENSOR3D_DECLARATION(dst)
1627#if defined(PER_CHANNEL_QUANTIZATION)
1628 ,
1629 VECTOR_DECLARATION(result_multipliers),
1630 VECTOR_DECLARATION(result_shifts)
1631#endif // defined(PER_CHANNEL_QUANTIZATION)
1632 )
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001633{
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001634 const int x = max((int)(get_global_id(0) * VEC_SIZE - (VEC_SIZE - VEC_SIZE_LEFTOVER) % VEC_SIZE), 0);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001635 const int y = get_global_id(1);
1636 const int z = get_global_id(2);
1637
1638 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x + y * dst_stride_y + z * dst_stride_z;
1639
1640 // Compute offset contribution
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001641 VEC_INT offset_term_s32 = offset_contribution(
1642 x, y, z
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001643#if defined(A_OFFSET)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001644 ,
1645 sum_col_ptr,
1646 sum_col_stride_x,
1647 sum_col_step_x,
1648 sum_col_stride_y,
1649 sum_col_step_y,
1650 sum_col_offset_first_element_in_bytes
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001651#endif // defined(A_OFFSET)
1652#if defined(B_OFFSET)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001653 ,
1654 sum_row_ptr,
1655 sum_row_stride_x,
1656 sum_row_step_x,
1657 sum_row_stride_y,
1658 sum_row_step_y,
1659 sum_row_offset_first_element_in_bytes
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001660#endif // defined(B_OFFSET)
1661#if defined(ADD_BIAS)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001662 ,
1663 biases_ptr,
1664 biases_stride_x,
1665 biases_step_x,
1666 biases_offset_first_element_in_bytes
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001667#endif // defined(ADD_BIAS)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001668 );
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001669
1670 __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;
1671
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001672 VEC_INT in_s32 = VLOAD(VEC_SIZE)(0, (__global int *)mm_result_addr);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001673
1674 // Add the offset terms to GEMM's result
1675 in_s32 += offset_term_s32;
1676
1677 // -------------- OUTPUT STAGE
1678
1679 // Add the offset terms to GEMM's result
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001680 in_s32 += (VEC_INT)RESULT_OFFSET;
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001681
1682 // Multiply by result_mult_int and shift
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001683#if defined(PER_CHANNEL_QUANTIZATION)
1684 __global uchar *result_multipliers_addr = result_multipliers_ptr + result_multipliers_offset_first_element_in_bytes + x * sizeof(int);
1685 __global uchar *result_shifts_addr = result_shifts_ptr + result_shifts_offset_first_element_in_bytes + x * sizeof(int);
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001686 VEC_INT result_multipliers_values = VLOAD(VEC_SIZE)(0, (__global int *)result_multipliers_addr);
1687 VEC_INT result_shifts_values = VLOAD(VEC_SIZE)(0, (__global int *)result_shifts_addr);
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001688
1689 in_s32 *= result_multipliers_values;
1690 in_s32 >>= result_shifts_values;
1691#else // defined(PER_CHANNEL_QUANTIZATION)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001692 in_s32 *= RESULT_MULTIPLIER;
1693
1694 in_s32 >>= RESULT_SHIFT;
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001695#endif // defined(PER_CHANNEL_QUANTIZATION)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001696
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001697 VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE)
1698 res0 = CONVERT_SAT(in_s32, VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE));
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001699
1700#if defined(MIN_BOUND)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001701 res0 = max(res0, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE))MIN_BOUND);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001702#endif // defined(MIN_BOUND)
1703#if defined(MAX_BOUND)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001704 res0 = min(res0, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE))MAX_BOUND);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001705#endif // defined(MAX_BOUND)
1706
1707 // Store the result
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001708 STORE_VECTOR_SELECT(res, OUTPUT_DATA_TYPE, dst_addr, VEC_SIZE, VEC_SIZE_LEFTOVER, VEC_SIZE_LEFTOVER != 0 && get_global_id(0) == 0)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001709}
1710
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001711/* 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 +01001712 *
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001713 * 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 +01001714 *
1715 *
1716 * @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)
1717 * @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)
1718 * @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)
1719 * @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
1720 *
1721 * The result before the output stage is:
1722 *
1723 * mm_result[i][k] = mm_result[i][k] +
1724 * (sum_col[k] * A_OFFSET) +
1725 * (sum_row[i] * B_OFFSET) +
1726 * (K_OFFSET)
1727 *
Manuel Bottini959c26d2019-12-02 16:22:35 +00001728 * This result is quantized down to uint8/int8 using the output stage. The output stage computes the following operations:
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001729 *
1730 * -# Compute fixed point multiplication between each entry of input by result_fixedpoint_multiplier
1731 * -# Add bias to final result if bias tensor is not a nullptr
1732 * -# Round to nearest division by a power-of-two using result_shift
1733 * -# Add offset to each result
1734 * -# Clamp the value between the specified min and max bounds
Manuel Bottini959c26d2019-12-02 16:22:35 +00001735 * -# Clamp the resulting int32 values:
1736 * - to the [0..255] range and cast to QASYMM8.
1737 * - to the [-128..127] range and cast to QASYMM8_SIGNED.
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001738 *
1739 * @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
1740 *
1741 * @note In case the addition of int32 biases is required, -DADD_BIAS should be passed at compile time
Manuel Bottini959c26d2019-12-02 16:22:35 +00001742 * @note The output datatype should be passed at compile time using -DOUTPUT_DATA_TYPE
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001743 * @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.
1744 * These values can be used to implement "rectified linear unit" activation functions
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001745 * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size. e.g. -DVEC_SIZE=16
1746 * @note Leftover vector size has to be passed at compile time using -DVEC_SIZE_LEFTOVER. e.g. -DVEC_SIZE_LEFTOVER=3. It is defined as the remainder between the input's first dimension and VEC_SIZE
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001747 *
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001748 * @param[in] mm_result_ptr Pointer to the source tensor. Supported data type: S32
1749 * @param[in] mm_result_stride_x Stride of the source tensor in X dimension (in bytes)
1750 * @param[in] mm_result_step_x mm_result_stride_x * number of elements along X processed per workitem(in bytes)
1751 * @param[in] mm_result_stride_y Stride of the source tensor in Y dimension (in bytes)
1752 * @param[in] mm_result_step_y mm_result_stride_y * number of elements along Y processed per workitem(in bytes)
1753 * @param[in] mm_result_stride_z Stride of the source tensor in Z dimension (in bytes)
1754 * @param[in] mm_result_step_z mm_result_stride_z * number of elements along Z processed per workitem(in bytes)
1755 * @param[in] mm_result_offset_first_element_in_bytes The offset of the first element in the source tensor
1756 * @param[in] sum_col_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr
1757 * @param[in] sum_col_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
1758 * @param[in] sum_col_step_x (Optional) sum_col_stride_x * number of elements along X processed per workitem(in bytes)
1759 * @param[in] sum_col_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
1760 * @param[in] sum_col_step_y (Optional) sum_col_stride_y * number of elements along Y processed per workitem(in bytes)
1761 * @param[in] sum_col_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
1762 * @param[in] sum_row_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr
1763 * @param[in] sum_row_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
1764 * @param[in] sum_row_step_x (Optional) sum_row_stride_x * number of elements along X processed per workitem(in bytes)
1765 * @param[in] sum_row_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
1766 * @param[in] sum_row_step_y (Optional) sum_row_stride_y * number of elements along Y processed per workitem(in bytes)
1767 * @param[in] sum_row_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
1768 * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: same as @p src_ptr
1769 * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes)
1770 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
1771 * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases tensor
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001772 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: QASYMM8/QASYMM8_SIGNED
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001773 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
1774 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
1775 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
1776 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
1777 * @param[in] dst_stride_z Stride of the source tensor in Z dimension (in bytes)
1778 * @param[in] dst_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1779 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
1780 * @param[in] result_multipliers_ptr (Optional) Pointer to the output multipliers vector for per-channel quantization. Supported data types: S32
1781 * @param[in] result_multipliers_stride_x (Optional) Stride of the output multipliers vector in X dimension (in bytes)
1782 * @param[in] result_multipliers_step_x (Optional) output_multipliers_stride_x * number of elements along X processed per workitem(in bytes)
1783 * @param[in] result_multipliers_offset_first_element_in_bytes (Optional) The offset of the first element in the output multipliers vector
1784 * @param[in] result_shifts_ptr (Optional) Pointer to the output shifts vector for per-channel quantization. Supported data types: S32
1785 * @param[in] result_shifts_stride_x (Optional) Stride of the output shifts vector in X dimension (in bytes)
1786 * @param[in] result_shifts_step_x (Optional) output_shifts_stride_x * number of elements along X processed per workitem(in bytes)
1787 * @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 +01001788 */
1789__kernel void gemmlowp_offset_contribution_quantize_down_fixedpoint(TENSOR3D_DECLARATION(mm_result)
1790#if defined(A_OFFSET)
1791 ,
1792 IMAGE_DECLARATION(sum_col)
1793#endif // defined(A_OFFSET)
1794#if defined(B_OFFSET)
1795 ,
1796 IMAGE_DECLARATION(sum_row)
1797#endif // defined(B_OFFSET)
1798 ,
1799#if defined(ADD_BIAS)
1800 VECTOR_DECLARATION(biases),
1801#endif // defined(ADD_BIAS)
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001802 TENSOR3D_DECLARATION(dst)
1803#if defined(PER_CHANNEL_QUANTIZATION)
1804 ,
1805 VECTOR_DECLARATION(result_multipliers),
1806 VECTOR_DECLARATION(result_shifts)
1807#endif // defined(PER_CHANNEL_QUANTIZATION)
1808 )
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001809{
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001810 const int x = max((int)(get_global_id(0) * VEC_SIZE - (VEC_SIZE - VEC_SIZE_LEFTOVER) % VEC_SIZE), 0);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001811 const int y = get_global_id(1);
1812 const int z = get_global_id(2);
1813
1814 // Compute offset contribution
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001815 VEC_INT offset_term_s32 = offset_contribution(
1816 x, y, z
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001817#if defined(A_OFFSET)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001818 ,
1819 sum_col_ptr,
1820 sum_col_stride_x,
1821 sum_col_step_x,
1822 sum_col_stride_y,
1823 sum_col_step_y,
1824 sum_col_offset_first_element_in_bytes
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001825#endif // defined(A_OFFSET)
1826#if defined(B_OFFSET)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001827 ,
1828 sum_row_ptr,
1829 sum_row_stride_x,
1830 sum_row_step_x,
1831 sum_row_stride_y,
1832 sum_row_step_y,
1833 sum_row_offset_first_element_in_bytes
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001834#endif // defined(B_OFFSET)
1835#if defined(ADD_BIAS)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001836 ,
1837 biases_ptr,
1838 biases_stride_x,
1839 biases_step_x,
1840 biases_offset_first_element_in_bytes
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001841#endif // defined(ADD_BIAS)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001842 );
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001843
1844 __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;
1845
1846 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x + y * dst_stride_y + z * dst_stride_z;
1847
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001848 VEC_INT in_s32 = VLOAD(VEC_SIZE)(0, (__global int *)mm_result_addr);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001849
1850 // Add the offset terms to GEMM's result
1851 in_s32 += offset_term_s32;
1852
1853 // -------------- OUTPUT STAGE
1854
1855 // Multiply by result_mult_int and shift
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001856#if defined(PER_CHANNEL_QUANTIZATION)
1857 __global uchar *result_multipliers_addr = result_multipliers_ptr + result_multipliers_offset_first_element_in_bytes + x * sizeof(int);
1858 __global uchar *result_shifts_addr = result_shifts_ptr + result_shifts_offset_first_element_in_bytes + x * sizeof(int);
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001859 VEC_INT result_multipliers_values = VLOAD(VEC_SIZE)(0, (__global int *)result_multipliers_addr);
1860 VEC_INT result_shifts_values = VLOAD(VEC_SIZE)(0, (__global int *)result_shifts_addr);
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001861
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001862 VEC_INT in_s32_shift_lt0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(in_s32, result_multipliers_values, result_shifts_values, VEC_SIZE);
1863 VEC_INT in_s32_shift_gt0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(in_s32, result_multipliers_values, result_shifts_values, VEC_SIZE);
1864 in_s32 = select(in_s32_shift_lt0, in_s32_shift_gt0, result_shifts_values >= 0);
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +01001865#else // defined(PER_CHANNEL_QUANTIZATION)
1866
1867#if RESULT_SHIFT < 0
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001868 in_s32 = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(in_s32, RESULT_MULTIPLIER, RESULT_SHIFT, VEC_SIZE);
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +01001869#else // RESULT_SHIFT >= 0
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001870 in_s32 = ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(in_s32, RESULT_MULTIPLIER, RESULT_SHIFT, VEC_SIZE);
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +01001871#endif // RESULT_SHIFT < 0
1872
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001873#endif // defined(PER_CHANNEL_QUANTIZATION)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001874
1875 // Add the offset terms to GEMM's result
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001876 in_s32 += (VEC_INT)RESULT_OFFSET;
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001877
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001878 VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE)
1879 res0 = CONVERT_SAT(in_s32, VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE));
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001880
1881#if defined(MIN_BOUND)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001882 res0 = max(res0, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE))MIN_BOUND);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001883#endif // defined(MIN_BOUND)
1884#if defined(MAX_BOUND)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001885 res0 = min(res0, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE))MAX_BOUND);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001886#endif // defined(MAX_BOUND)
1887
1888 // Store the result
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001889 STORE_VECTOR_SELECT(res, OUTPUT_DATA_TYPE, dst_addr, VEC_SIZE, VEC_SIZE_LEFTOVER, VEC_SIZE_LEFTOVER != 0 && get_global_id(0) == 0)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001890}
Michele Di Giorgiob54ba282020-01-14 15:31:55 +00001891#endif // defined(RESULT_OFFSET) && defined(RESULT_MULTIPLIER) && defined(RESULT_SHIFT) && defined(OUTPUT_DATA_TYPE)
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001892
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001893#undef VEC_INT
1894
1895#endif // defined(K_OFFSET) && defined(VEC_SIZE) && defined(VEC_SIZE_LEFTOVER)
Gian Marco05288a22017-11-21 10:57:50 +00001896
1897#if defined(RESULT_OFFSET) && defined(RESULT_MULT_INT) && defined(RESULT_SHIFT)
Luca Foschiani689c9682020-02-26 14:30:14 +00001898/** This OpenCL kernel is used to quantize down the int32 accumulator values of GEMMLowp to QASYMM8/QASYMM8_SIGNED
Gian Marco05288a22017-11-21 10:57:50 +00001899 *
Luca Foschiani689c9682020-02-26 14:30:14 +00001900 * This kernel takes a final int32 accumulator value and processes it to obtain the final QASYMM8/QASYMM8_SIGNED value.
Gian Marco05288a22017-11-21 10:57:50 +00001901 * The following computations will be performed by the kernel:
1902 *
1903 * -# Add offset terms to final result
1904 * -# Multiply each entry of result by result_mult_int
1905 * -# Add bias to final result (if -DADD_BIAS is passed at compile time)
1906 * -# Shift the int32 accumulator by result_shift
1907 * -# Clamp the value between the specified min and max bounds (if -DMIN_BOUND and/or -DMAX_BOUND are passed at compile time)
Luca Foschiani689c9682020-02-26 14:30:14 +00001908 * -# Clamp the resulting int32 values:
1909 * -# - to the [0..255] range and cast to QASYMM8.
1910 * -# - to the [-128..127] range and cast to QASYMM8_SIGNED.
Gian Marco05288a22017-11-21 10:57:50 +00001911 *
1912 * @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
1913 *
1914 * @note In case the addition of int32 biases is required, -DADD_BIAS should be passed at compile time
Luca Foschiani689c9682020-02-26 14:30:14 +00001915 * @note The output datatype should be passed at compile time using -DOUTPUT_DATA_TYPE
Gian Marco05288a22017-11-21 10:57:50 +00001916 * @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.
1917 * These values can be used to implement "rectified linear unit" activation functions
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01001918 * @note Leftover vector size has to be passed at compile time using -DVEC_SIZE_LEFTOVER. e.g. -DVEC_SIZE_LEFTOVER=3. It is defined as the remainder between the input's first dimension and VEC_SIZE
Gian Marco05288a22017-11-21 10:57:50 +00001919 *
1920 * @param[in] src_ptr Pointer to the source tensor. Supported data type: S32
1921 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
1922 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
1923 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
1924 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
1925 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
1926 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1927 * @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 +01001928 * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: same as @p src_ptr
1929 * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes)
1930 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
1931 * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases tensor
Luca Foschiani689c9682020-02-26 14:30:14 +00001932 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: QASYMM8/QASYMM8_SIGNED
Gian Marco05288a22017-11-21 10:57:50 +00001933 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
1934 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
1935 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
1936 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
1937 * @param[in] dst_stride_z Stride of the source tensor in Z dimension (in bytes)
1938 * @param[in] dst_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1939 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
1940 */
1941__kernel void gemmlowp_output_stage_quantize_down(TENSOR3D_DECLARATION(src),
1942#if defined(ADD_BIAS)
1943 VECTOR_DECLARATION(biases),
1944#endif // defined(ADD_BIAS)
1945 TENSOR3D_DECLARATION(dst))
1946{
1947 // Compute source and destination addresses
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01001948 int x = max((int)(get_global_id(0) * VEC_SIZE - (VEC_SIZE - VEC_SIZE_LEFTOVER) % VEC_SIZE), 0);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001949 int y = get_global_id(1);
1950 int z = get_global_id(2);
Gian Marco05288a22017-11-21 10:57:50 +00001951
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001952 __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 +00001953
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001954 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x + y * dst_stride_y + z * dst_stride_z;
1955
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01001956 VEC_DATA_TYPE(int, VEC_SIZE)
1957 input_values = VLOAD(VEC_SIZE)(0, (__global int *)src_addr);
Gian Marco58c57942017-11-28 09:10:03 +00001958
Gian Marco05288a22017-11-21 10:57:50 +00001959#if defined(ADD_BIAS)
1960 // Add bias
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001961 __global uchar *bias_addr = biases_ptr + biases_offset_first_element_in_bytes + x * sizeof(int);
1962
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01001963 VEC_DATA_TYPE(int, VEC_SIZE)
1964 biases_values = VLOAD(VEC_SIZE)(0, (__global int *)bias_addr);
1965 input_values += biases_values;
Gian Marco05288a22017-11-21 10:57:50 +00001966#endif // defined(ADD_BIAS)
1967
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001968 // Add the offset terms to GEMM's result
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01001969 input_values += (VEC_DATA_TYPE(int, VEC_SIZE))RESULT_OFFSET;
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001970
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +00001971 // Multiply by result_mult_int and shift
Gian Marco58c57942017-11-28 09:10:03 +00001972 input_values *= RESULT_MULT_INT;
Gian Marco05288a22017-11-21 10:57:50 +00001973
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +01001974#if RESULT_SHIFT < 0
1975 input_values >>= -RESULT_SHIFT;
1976#else // RESULT_SHIFT >= 0
Gian Marco58c57942017-11-28 09:10:03 +00001977 input_values >>= RESULT_SHIFT;
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +01001978#endif // RESULT_SHIFT < 0
Gian Marco05288a22017-11-21 10:57:50 +00001979
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01001980 VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE)
1981 res0 = CONVERT_SAT(input_values, VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE));
Gian Marco05288a22017-11-21 10:57:50 +00001982
1983#if defined(MIN_BOUND)
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01001984 res0 = max(res0, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE))MIN_BOUND);
Gian Marco05288a22017-11-21 10:57:50 +00001985#endif // defined(MIN_BOUND)
1986#if defined(MAX_BOUND)
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01001987 res0 = min(res0, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE))MAX_BOUND);
Gian Marco05288a22017-11-21 10:57:50 +00001988#endif // defined(MAX_BOUND)
1989
1990 // Store the result
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01001991 STORE_VECTOR_SELECT(res, OUTPUT_DATA_TYPE, dst_addr, VEC_SIZE, VEC_SIZE_LEFTOVER, VEC_SIZE_LEFTOVER != 0 && get_global_id(0) == 0)
Gian Marco05288a22017-11-21 10:57:50 +00001992}
Gian Marco58c57942017-11-28 09:10:03 +00001993#endif // defined(RESULT_OFFSET) && defined(RESULT_MULT_INT) && defined(RESULT_SHIFT)
1994
1995#if defined(RESULT_OFFSET_AFTER_SHIFT) && defined(RESULT_FIXEDPOINT_MULTIPLIER) && defined(RESULT_SHIFT)
Manuel Bottini959c26d2019-12-02 16:22:35 +00001996/** This OpenCL kernel is used to quantize down the int32 accumulator values of GEMMLowp to QASYMM8/QASYMM8_SIGNED
Gian Marco58c57942017-11-28 09:10:03 +00001997 *
Manuel Bottini959c26d2019-12-02 16:22:35 +00001998 * This kernel takes a final int32 accumulator value (the output of matrix multiplication), and processes it to obtain the final QASYMM8/QASYMM8_SIGNED value.
Gian Marco58c57942017-11-28 09:10:03 +00001999 * The following computations will be performed by the kernel:
2000 *
2001 * -# Compute fixed point multiplication between each entry of input by result_fixedpoint_multiplier
2002 * -# Add bias to final result if bias tensor is not a nullptr
2003 * -# Round to nearest division by a power-of-two using result_shift
2004 * -# Add offset to each result
2005 * -# Clamp the value between the specified min and max bounds
Manuel Bottini1f332d42019-11-29 17:25:25 +00002006 * -# Clamp the resulting int32 values:
2007 * - to the [0..255] range and cast to QASYMM8.
2008 * - to the [-128..127] range and cast to QASYMM8_SIGNED.
Gian Marco58c57942017-11-28 09:10:03 +00002009 *
Gian Marco Iodice4b908652018-10-18 10:21:02 +01002010 * @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 +00002011 *
2012 * @note In case the addition of int32 biases is required, -DADD_BIAS should be passed at compile time
Manuel Bottini1f332d42019-11-29 17:25:25 +00002013 * @note The output datatype should be passed at compile time using -DOUTPUT_DATA_TYPE
Gian Marco58c57942017-11-28 09:10:03 +00002014 * @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.
2015 * These values can be used to implement "rectified linear unit" activation functions
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002016 * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size. e.g. -DVEC_SIZE=16
2017 * @note Leftover vector size has to be passed at compile time using -DVEC_SIZE_LEFTOVER. e.g. -DVEC_SIZE_LEFTOVER=3. It is defined as the remainder between the input's first dimension and VEC_SIZE
Gian Marco58c57942017-11-28 09:10:03 +00002018 *
2019 * @param[in] src_ptr Pointer to the source tensor. Supported data type: S32
2020 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
2021 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
2022 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
2023 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
2024 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
2025 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
2026 * @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 +01002027 * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: same as @p src_ptr
2028 * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes)
2029 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
2030 * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases tensor
Sheri Zhang0cdbda52020-02-25 15:57:21 +00002031 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: QASYMM8/QASYMM8_SIGNED
Gian Marco58c57942017-11-28 09:10:03 +00002032 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
2033 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
2034 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
2035 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
2036 * @param[in] dst_stride_z Stride of the source tensor in Z dimension (in bytes)
2037 * @param[in] dst_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
2038 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
2039 */
2040__kernel void gemmlowp_output_stage_quantize_down_fixedpoint(TENSOR3D_DECLARATION(src),
2041#if defined(ADD_BIAS)
2042 VECTOR_DECLARATION(biases),
2043#endif // defined(ADD_BIAS)
2044 TENSOR3D_DECLARATION(dst))
2045{
2046 // Compute source and destination addresses
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002047 int x = max((int)(get_global_id(0) * VEC_SIZE - (VEC_SIZE - VEC_SIZE_LEFTOVER) % VEC_SIZE), 0);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01002048 int y = get_global_id(1);
2049 int z = get_global_id(2);
Georgios Pinitas932491f2018-09-21 16:33:15 +01002050
Gian Marco Iodice4b908652018-10-18 10:21:02 +01002051 __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 +00002052
Gian Marco Iodice4b908652018-10-18 10:21:02 +01002053 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x + y * dst_stride_y + z * dst_stride_z;
2054
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002055 VEC_DATA_TYPE(int, VEC_SIZE)
2056 input_values = VLOAD(VEC_SIZE)(0, (__global int *)src_addr);
Gian Marco58c57942017-11-28 09:10:03 +00002057
2058#if defined(ADD_BIAS)
2059 // Add bias
Gian Marco Iodice4b908652018-10-18 10:21:02 +01002060 __global uchar *bias_addr = biases_ptr + biases_offset_first_element_in_bytes + x * sizeof(int);
2061
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002062 VEC_DATA_TYPE(int, VEC_SIZE)
2063 biases_values = VLOAD(VEC_SIZE)(0, (__global int *)bias_addr);
2064 input_values += biases_values;
Gian Marco58c57942017-11-28 09:10:03 +00002065#endif // defined(ADD_BIAS)
2066
2067 // Multiply by result_mult_int and shift
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +01002068#if RESULT_SHIFT < 0
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002069 input_values = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(input_values, RESULT_FIXEDPOINT_MULTIPLIER, RESULT_SHIFT, VEC_SIZE);
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +01002070#else // RESULT_SHIFT >= 0
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002071 input_values = ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(input_values, RESULT_FIXEDPOINT_MULTIPLIER, RESULT_SHIFT, VEC_SIZE);
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +01002072#endif // RESULT_SHIFT < 0
Gian Marco58c57942017-11-28 09:10:03 +00002073
2074 // Add the offset terms to GEMM's result
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002075 input_values += (VEC_DATA_TYPE(int, VEC_SIZE))RESULT_OFFSET_AFTER_SHIFT;
Gian Marco58c57942017-11-28 09:10:03 +00002076
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002077 VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE)
2078 res0 = CONVERT_SAT(input_values, VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE));
Gian Marco58c57942017-11-28 09:10:03 +00002079
2080#if defined(MIN_BOUND)
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002081 res0 = max(res0, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE))MIN_BOUND);
Gian Marco58c57942017-11-28 09:10:03 +00002082#endif // defined(MIN_BOUND)
2083#if defined(MAX_BOUND)
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002084 res0 = min(res0, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE))MAX_BOUND);
Gian Marco58c57942017-11-28 09:10:03 +00002085#endif // defined(MAX_BOUND)
2086
2087 // Store the result
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002088 STORE_VECTOR_SELECT(res, OUTPUT_DATA_TYPE, dst_addr, VEC_SIZE, VEC_SIZE_LEFTOVER, VEC_SIZE_LEFTOVER != 0 && get_global_id(0) == 0)
Gian Marco58c57942017-11-28 09:10:03 +00002089}
Chunosov5124be52017-11-22 20:42:13 +07002090#endif // defined(RESULT_OFFSET_AFTER_SHIFT) && defined(RESULT_FIXEDPOINT_MULTIPLIER) && defined(RESULT_SHIFT)
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002091
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002092#if defined(RESULT_FIXEDPOINT_MULTIPLIER) && defined(RESULT_SHIFT)
2093
Michalis Spyrou51146c52019-07-12 14:42:29 +01002094/** This OpenCL kernel is used to quantize down the int32 accumulator values of GEMMLowp to QSYMM16
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002095 *
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00002096 * 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 +01002097 * The following computations will be performed by the kernel:
2098 *
2099 * -# Compute fixed point multiplication between each entry of input by result_fixedpoint_multiplier
2100 * -# Add bias to final result if bias tensor is not a nullptr
2101 * -# Round to nearest division by a power-of-two using result_shift
2102 * -# Add offset to each result
2103 * -# Clamp the value between the specified min and max bounds
2104 * -# Clamp the resulting int32 values to the [-32768..32767] range and cast to QSYMM16.
2105 *
2106 * @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
2107 *
2108 * @note In case the addition of int32 biases is required, -DADD_BIAS should be passed at compile time
2109 * @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.
2110 * These values can be used to implement "rectified linear unit" activation functions
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002111 * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size. e.g. -DVEC_SIZE=16
2112 * @note Leftover vector size has to be passed at compile time using -DVEC_SIZE_LEFTOVER. e.g. -DVEC_SIZE_LEFTOVER=3. It is defined as the remainder between the input's first dimension and VEC_SIZE
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002113 *
2114 * @param[in] src_ptr Pointer to the source tensor. Supported data type: S32
2115 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
2116 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
2117 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
2118 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
2119 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
2120 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
2121 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
2122 * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: same as @p src_ptr
2123 * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes)
2124 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
2125 * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases tensor
Sheri Zhangb18252d2020-04-07 11:04:57 +01002126 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: QSYMM16
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002127 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
2128 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
2129 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
2130 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
2131 * @param[in] dst_stride_z Stride of the source tensor in Z dimension (in bytes)
2132 * @param[in] dst_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
2133 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
2134 */
2135__kernel void gemmlowp_output_stage_quantize_down_fixedpoint_qsymm16(TENSOR3D_DECLARATION(src),
2136#if defined(ADD_BIAS)
2137 VECTOR_DECLARATION(biases),
2138#endif // defined(ADD_BIAS)
2139 TENSOR3D_DECLARATION(dst))
2140{
2141 // Compute source and destination addresses
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002142 int x = max((int)(get_global_id(0) * VEC_SIZE - (VEC_SIZE - VEC_SIZE_LEFTOVER) % VEC_SIZE), 0);
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002143 int y = get_global_id(1);
2144 int z = get_global_id(2);
2145
Michalis Spyrou51146c52019-07-12 14:42:29 +01002146 __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 +01002147
Michele Di Giorgioba14c922020-10-12 13:27:57 +01002148 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x * sizeof(short) + y * dst_stride_y + z * dst_stride_z;
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002149
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002150 VEC_DATA_TYPE(int, VEC_SIZE)
2151 input_values = VLOAD(VEC_SIZE)(0, (__global int *)src_addr);
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002152
2153#if defined(ADD_BIAS)
2154 // Add bias
Michalis Spyrou51146c52019-07-12 14:42:29 +01002155 __global uchar *bias_addr = biases_ptr + biases_offset_first_element_in_bytes + x * sizeof(int);
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002156
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002157 VEC_DATA_TYPE(int, VEC_SIZE)
2158 biases_values = VLOAD(VEC_SIZE)(0, (__global int *)bias_addr);
2159 input_values += biases_values;
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002160#endif // defined(ADD_BIAS)
2161
2162 // Multiply by result_mult_int and shift
Manuel Bottini07263982019-10-17 18:37:26 +01002163#if RESULT_SHIFT < 0
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002164 input_values = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(input_values, RESULT_FIXEDPOINT_MULTIPLIER, RESULT_SHIFT, VEC_SIZE);
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00002165#else // RESULT_SHIFT >= 0
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002166 input_values = ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(input_values, RESULT_FIXEDPOINT_MULTIPLIER, RESULT_SHIFT, VEC_SIZE);
Manuel Bottini07263982019-10-17 18:37:26 +01002167#endif // RESULT_SHIFT < 0
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002168
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002169 VEC_DATA_TYPE(short, VEC_SIZE)
2170 res0 = CONVERT_SAT(input_values, VEC_DATA_TYPE(short, VEC_SIZE));
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002171
2172#if defined(MIN_BOUND)
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002173 res0 = max(res0, (VEC_DATA_TYPE(short, VEC_SIZE))MIN_BOUND);
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002174#endif // defined(MIN_BOUND)
2175#if defined(MAX_BOUND)
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002176 res0 = min(res0, (VEC_DATA_TYPE(short, VEC_SIZE))MAX_BOUND);
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002177#endif // defined(MAX_BOUND)
2178
2179 // Store the result
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002180 STORE_VECTOR_SELECT(res, short, dst_addr, VEC_SIZE, VEC_SIZE_LEFTOVER, VEC_SIZE_LEFTOVER != 0 && get_global_id(0) == 0)
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002181}
2182#endif // defined(RESULT_FIXEDPOINT_MULTIPLIER) && defined(RESULT_SHIFT)
2183
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002184#if defined(REAL_MULTIPLIER) && defined(OUTPUT_OFFSET)
Sheri Zhang1b14c752020-03-09 14:29:52 +00002185/** This OpenCL kernel is used to quantize down the int32 accumulator values of GEMMLowp to QASYMM8/QASYMM8_SIGNED
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002186 *
Sheri Zhang1b14c752020-03-09 14:29:52 +00002187 * This kernel takes a final int32 accumulator value (the output of matrix multiplication), and processes it to obtain the final QASYMM8/QASYMM8_SIGNED value.
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002188 * The following computations will be performed by the kernel:
2189 *
2190 * -# Compute fixed point multiplication between each entry of input by result_fixedpoint_multiplier
2191 * -# Add bias to final result if bias tensor is not a nullptr
2192 * -# Requantize
2193 * -# Add offset to each result
2194 * -# Clamp the value between the specified min and max bounds
Sheri Zhang1b14c752020-03-09 14:29:52 +00002195 * -# Clamp the resulting int32 values:
2196 * - to the [0..255] range and cast to QASYMM8.
2197 * - to the [-128..127] range and cast to QASYMM8_SIGNED.
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002198 *
2199 * @attention The offset and scalar scale factor must be passed at compile time using -DRESULT_OFFSET, -DREAL_MULTIPLIER
2200 *
2201 * @note In case the addition of int32 biases is required, -DADD_BIAS should be passed at compile time
Sheri Zhang1b14c752020-03-09 14:29:52 +00002202 * @note The output datatype should be passed at compile time using -DOUTPUT_DATA_TYPE
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002203 * @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.
2204 * These values can be used to implement "rectified linear unit" activation functions
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002205 * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size. e.g. -DVEC_SIZE=16
2206 * @note Leftover vector size has to be passed at compile time using -DVEC_SIZE_LEFTOVER. e.g. -DVEC_SIZE_LEFTOVER=3. It is defined as the remainder between the input's first dimension and VEC_SIZE
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002207 *
2208 * @param[in] src_ptr Pointer to the source tensor. Supported data type: S32
2209 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
2210 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
2211 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
2212 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
2213 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
2214 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
2215 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
2216 * @param[in] biases_ptr Pointer to the biases tensor. Supported data type: same as @p src_ptr
2217 * @param[in] biases_stride_x Stride of the biases tensor in X dimension (in bytes)
2218 * @param[in] biases_step_x biases_stride_x * number of elements along X processed per workitem(in bytes)
2219 * @param[in] biases_offset_first_element_in_bytes The offset of the first element in the biases tensor
2220 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: QASYMM8
2221 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
2222 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
2223 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
2224 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
2225 * @param[in] dst_stride_z Stride of the source tensor in Z dimension (in bytes)
2226 * @param[in] dst_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
2227 * @param[in] dst_stride_w Stride of the source tensor in W dimension (in bytes)
2228 * @param[in] dst_step_w src_stride_w * number of elements along W processed per workitem(in bytes)
2229 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
2230 */
2231__kernel void gemmlowp_output_stage_quantize_down_float(TENSOR3D_DECLARATION(src),
2232#if defined(ADD_BIAS)
2233 VECTOR_DECLARATION(biases),
2234#endif // defined(ADD_BIAS)
2235#if defined(DST_HEIGHT)
2236 TENSOR4D_DECLARATION(dst))
2237#else // defined(DST_HEIGHT)
2238 TENSOR3D_DECLARATION(dst))
2239#endif // defined(DST_HEIGHT)
2240{
2241 // Compute source and destination addresses
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002242 int x = max((int)(get_global_id(0) * VEC_SIZE - (VEC_SIZE - VEC_SIZE_LEFTOVER) % VEC_SIZE), 0);
Gian Marco Iodice0c54a622018-10-30 12:20:03 +00002243 int y = get_global_id(1);
2244 int z = get_global_id(2);
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002245
Gian Marco Iodice0c54a622018-10-30 12:20:03 +00002246 __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 +01002247
Gian Marco Iodice0c54a622018-10-30 12:20:03 +00002248 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x + y * dst_stride_y + z * dst_stride_z;
2249
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002250 VEC_DATA_TYPE(int, VEC_SIZE)
2251 input_values = VLOAD(VEC_SIZE)(0, (__global int *)src_addr);
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002252
2253#if defined(ADD_BIAS)
2254 // Add bias
Gian Marco Iodice0c54a622018-10-30 12:20:03 +00002255 __global uchar *bias_addr = biases_ptr + biases_offset_first_element_in_bytes + x * sizeof(int);
2256
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002257 VEC_DATA_TYPE(int, VEC_SIZE)
2258 biases_values = VLOAD(VEC_SIZE)(0, (__global int *)bias_addr);
Gian Marco Iodice0c54a622018-10-30 12:20:03 +00002259 input_values += (int4)biases_values;
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002260#endif // defined(ADD_BIAS)
2261
2262 // Convert to float
Sheri Zhang1b14c752020-03-09 14:29:52 +00002263 float4 input_values_f = convert_float4(input_values);
2264 input_values_f = round(input_values_f * (float)REAL_MULTIPLIER + (float)OUTPUT_OFFSET);
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002265
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002266 VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE)
2267 res0 = CONVERT_SAT(input_values_f, VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE));
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002268
2269#if defined(MIN_BOUND)
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002270 res0 = max(res0, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE))MIN_BOUND);
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002271#endif // defined(MIN_BOUND)
2272#if defined(MAX_BOUND)
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002273 res0 = min(res0, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE))MAX_BOUND);
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002274#endif // defined(MAX_BOUND)
2275
2276 // Store the result
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002277 STORE_VECTOR_SELECT(res, OUTPUT_DATA_TYPE, dst_addr, VEC_SIZE, VEC_SIZE_LEFTOVER, VEC_SIZE_LEFTOVER != 0 && get_global_id(0) == 0)
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002278}
Gian Marco Iodice27423f02020-08-12 14:12:28 +01002279#endif // defined(REAL_MULTIPLIER) && defined(OUTPUT_OFFSET)