blob: 89cbe4440e3d9198685c3d16d0db44e6cf0c49b3 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michalis Spyrouef6ec502020-07-31 11:38:36 +01002 * Copyright (c) 2017-2020 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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 */
24#include "helpers.h"
25
Michalis Spyrou172e5702017-06-26 14:18:47 +010026#define ADD_OP(a, b) ((a) + (b))
27#define SUB_OP(a, b) ((a) - (b))
28#define MUL_OP(a, b) ((a) * (b))
29#define INVSQRT_OP(a) rsqrt((a))
30#define SQCVT_SAT(a) (a)
31
Usama Arif6a98a6e2019-05-10 17:07:27 +010032#if defined(VEC_SIZE) && defined(DATA_TYPE) && defined(ACTIVATION_TYPE)
33#include "activation_float_helpers.h"
Giorgio Arena11674872018-02-07 15:38:12 +000034
Anthony Barbier6ff3b192017-09-04 18:44:23 +010035/** Apply batch normalization.
36 *
Usama Arif6a98a6e2019-05-10 17:07:27 +010037 * @note It is possible to select the activation function to apply using -DACTIVATION_TYPE e.g. -DACTIVATION_TYPE=relu
38 * @note A, B variables required by some activation functions are set using -DA_VAL= and -DB_VAL= respectively
39 *
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010040 * @param[in] input_ptr Pointer to the first source tensor. Supported data types: F16/F32
Anthony Barbier6ff3b192017-09-04 18:44:23 +010041 * @param[in] input_stride_x Stride of the first source tensor in X dimension (in bytes)
42 * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes)
43 * @param[in] input_stride_y Stride of the first source tensor in Y dimension (in bytes)
44 * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes)
45 * @param[in] input_stride_z Stride of the first source tensor in Z dimension (in bytes)
46 * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes)
47 * @param[in] input_offset_first_element_in_bytes The offset of the first element in the first source tensor
Michalis Spyrou172e5702017-06-26 14:18:47 +010048 * @param[out] output_ptr Pointer to the destination tensor. Supported data types: same as @p input_ptr
Anthony Barbier6ff3b192017-09-04 18:44:23 +010049 * @param[in] output_stride_x Stride of the destination tensor in X dimension (in bytes)
50 * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes)
51 * @param[in] output_stride_y Stride of the destination tensor in Y dimension (in bytes)
52 * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes)
53 * @param[in] output_stride_z Stride of the destination tensor in Z dimension (in bytes)
54 * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes)
55 * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination tensor
Michalis Spyrou172e5702017-06-26 14:18:47 +010056 * @param[in] mean_ptr Pointer to the mean source tensor. Supported data types: same as @p input_ptr
Anthony Barbier6ff3b192017-09-04 18:44:23 +010057 * @param[in] mean_stride_x Stride of the mean source tensor in X dimension (in bytes)
58 * @param[in] mean_step_x mean_stride_x * number of elements along X processed per workitem(in bytes)
59 * @param[in] mean_offset_first_element_in_bytes The offset of the first element in the mean source tensor
Michalis Spyrou172e5702017-06-26 14:18:47 +010060 * @param[in] var_ptr Pointer to the var tensor. Supported data types: same as @p input_ptr
Anthony Barbier6ff3b192017-09-04 18:44:23 +010061 * @param[in] var_stride_x Stride of the var tensor in X dimension (in bytes)
62 * @param[in] var_step_x var_stride_x * number of elements along X processed per workitem(in bytes)
63 * @param[in] var_offset_first_element_in_bytes The offset of the first element in the var source tensor
Michalis Spyrou172e5702017-06-26 14:18:47 +010064 * @param[in] beta_ptr Pointer to the beta source tensor. Supported data types: same as @p input_ptr
Anthony Barbier6ff3b192017-09-04 18:44:23 +010065 * @param[in] beta_stride_x Stride of the beta source tensor in X dimension (in bytes)
66 * @param[in] beta_step_x beta_stride_x * number of elements along X processed per workitem(in bytes)
67 * @param[in] beta_offset_first_element_in_bytes The offset of the first element in the beta source tensor
Michalis Spyrou172e5702017-06-26 14:18:47 +010068 * @param[in] gamma_ptr Pointer to the gamma source tensor. Supported data types: same as @p input_ptr
Anthony Barbier6ff3b192017-09-04 18:44:23 +010069 * @param[in] gamma_stride_x Stride of the gamma source tensor in X dimension (in bytes)
70 * @param[in] gamma_step_x gamma_stride_x * number of elements along X processed per workitem(in bytes)
71 * @param[in] gamma_offset_first_element_in_bytes The offset of the first element in the gamma source tensor
72 * @param[in] epsilon Epsilon parameter in the batch normalization equation
73 */
Michele Di Giorgiobf3c6622018-03-08 11:52:27 +000074__kernel void batchnormalization_layer_nchw(TENSOR3D_DECLARATION(input),
Georgios Pinitas409ee0a2017-08-18 10:16:09 +010075#ifndef IN_PLACE
Michele Di Giorgiobf3c6622018-03-08 11:52:27 +000076 TENSOR3D_DECLARATION(output),
Georgios Pinitas409ee0a2017-08-18 10:16:09 +010077#endif /* not IN_PLACE */
Michele Di Giorgiobf3c6622018-03-08 11:52:27 +000078 VECTOR_DECLARATION(mean),
79 VECTOR_DECLARATION(var),
Michele Di Giorgio4d336302018-03-02 09:43:54 +000080#ifndef USE_DEFAULT_BETA
Michele Di Giorgiobf3c6622018-03-08 11:52:27 +000081 VECTOR_DECLARATION(beta),
Michele Di Giorgio4d336302018-03-02 09:43:54 +000082#endif /* USE_DEFAULT_BETA */
83#ifndef USE_DEFAULT_GAMMA
Michele Di Giorgiobf3c6622018-03-08 11:52:27 +000084 VECTOR_DECLARATION(gamma),
Michele Di Giorgio4d336302018-03-02 09:43:54 +000085#endif /* USE_DEFAULT_GAMMA */
Michele Di Giorgiobf3c6622018-03-08 11:52:27 +000086 float epsilon)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010087{
Georgios Pinitas409ee0a2017-08-18 10:16:09 +010088 Tensor3D in = CONVERT_TO_TENSOR3D_STRUCT(input);
89#ifdef IN_PLACE
90 Tensor3D out = in;
91#else /* IN_PLACE */
92 Tensor3D out = CONVERT_TO_TENSOR3D_STRUCT(output);
93#endif /* IN_PLACE */
Michele Di Giorgio4d336302018-03-02 09:43:54 +000094 Vector mean = CONVERT_TO_VECTOR_STRUCT(mean);
95 Vector var = CONVERT_TO_VECTOR_STRUCT(var);
96#ifndef USE_DEFAULT_BETA
97 Vector beta = CONVERT_TO_VECTOR_STRUCT(beta);
98#endif /* USE_DEFAULT_BETA */
99#ifndef USE_DEFAULT_GAMMA
Georgios Pinitas409ee0a2017-08-18 10:16:09 +0100100 Vector gamma = CONVERT_TO_VECTOR_STRUCT(gamma);
Michele Di Giorgio4d336302018-03-02 09:43:54 +0000101#endif /* USE_DEFAULT_GAMMA */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100102
Michalis Spyrou172e5702017-06-26 14:18:47 +0100103 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
Gian Marco Iodice349feef2017-09-28 11:21:29 +0100104 data = 0;
Michalis Spyrou172e5702017-06-26 14:18:47 +0100105 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
106 denominator = 0;
107 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
108 numerator = 0;
109 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
110 x_bar = 0;
111 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
Michele Di Giorgio4d336302018-03-02 09:43:54 +0000112 res = 0;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100113
114 const int current_slice = get_global_id(2);
115
Gian Marco Iodice349feef2017-09-28 11:21:29 +0100116 data = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)in.ptr);
Michalis Spyrou172e5702017-06-26 14:18:47 +0100117 denominator = *((__global DATA_TYPE *)(var.ptr + current_slice * var.stride_x));
Gian Marco Iodice349feef2017-09-28 11:21:29 +0100118 denominator = INVSQRT_OP(ADD_OP(denominator, ((VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE))SQCVT_SAT(epsilon))));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100119
120 // Calculate x bar and store results
Michalis Spyrou172e5702017-06-26 14:18:47 +0100121 numerator = *((__global DATA_TYPE *)(mean.ptr + current_slice * mean.stride_x));
Gian Marco Iodice349feef2017-09-28 11:21:29 +0100122 numerator = SUB_OP(data, numerator);
Michalis Spyrou172e5702017-06-26 14:18:47 +0100123 x_bar = MUL_OP(numerator, denominator);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100124
Michele Di Giorgio4d336302018-03-02 09:43:54 +0000125#ifndef USE_DEFAULT_GAMMA
Giorgio Arena11674872018-02-07 15:38:12 +0000126 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
Michele Di Giorgio4d336302018-03-02 09:43:54 +0000127 gamma_vec = *((__global DATA_TYPE *)(gamma.ptr + current_slice * gamma.stride_x));
128
129 res = MUL_OP(gamma_vec, x_bar);
130#else /* USE_DEFAULT_GAMMA */
131 // gamma is equal to 1, no need to perform multiplications
Sheri Zhang141c31a2020-10-08 12:35:28 +0100132 res = x_bar;
Michele Di Giorgio4d336302018-03-02 09:43:54 +0000133#endif /* USE_DEFAULT_GAMMA */
134
135#ifndef USE_DEFAULT_BETA
136 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
137 beta_vec = *((__global DATA_TYPE *)(beta.ptr + current_slice * beta.stride_x));
138 // beta is not zero, hence we need to perform the addition
139 res = ADD_OP(res, beta_vec);
140#endif /* USE_DEFAULT_BETA */
Giorgio Arena11674872018-02-07 15:38:12 +0000141
Giorgio Arenad056e572020-10-12 11:53:51 +0100142 res = ACTIVATION(ACTIVATION_TYPE, DATA_TYPE, VEC_SIZE, res, A_VAL, B_VAL);
Giorgio Arena11674872018-02-07 15:38:12 +0000143
Michalis Spyrou172e5702017-06-26 14:18:47 +0100144 VSTORE(VEC_SIZE)
Giorgio Arena11674872018-02-07 15:38:12 +0000145 (res, 0, (__global DATA_TYPE *)out.ptr);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100146}
Giorgio Arena11674872018-02-07 15:38:12 +0000147
Michele Di Giorgiobf3c6622018-03-08 11:52:27 +0000148/** Apply batch normalization on tensors with NHWC format.
149 *
Usama Arif6a98a6e2019-05-10 17:07:27 +0100150 * @note It is possible to select the activation function to apply using -DACTIVATION_TYPE e.g. -DACTIVATION_TYPE=relu
151 * @note A, B variables required by some activation functions are set using -DA_VAL= and -DB_VAL= respectively
152 *
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100153 * @param[in] input_ptr Pointer to the first source tensor. Supported data types: F16/F32
Michele Di Giorgiobf3c6622018-03-08 11:52:27 +0000154 * @param[in] input_stride_x Stride of the first source tensor in X dimension (in bytes)
155 * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes)
156 * @param[in] input_stride_y Stride of the first source tensor in Y dimension (in bytes)
157 * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes)
158 * @param[in] input_stride_z Stride of the first source tensor in Z dimension (in bytes)
159 * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes)
160 * @param[in] input_offset_first_element_in_bytes The offset of the first element in the first source tensor
161 * @param[out] output_ptr Pointer to the destination tensor. Supported data types: same as @p input_ptr
162 * @param[in] output_stride_x Stride of the destination tensor in X dimension (in bytes)
163 * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes)
164 * @param[in] output_stride_y Stride of the destination tensor in Y dimension (in bytes)
165 * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes)
166 * @param[in] output_stride_z Stride of the destination tensor in Z dimension (in bytes)
167 * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes)
168 * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination tensor
169 * @param[in] mean_ptr Pointer to the mean source tensor. Supported data types: same as @p input_ptr
170 * @param[in] mean_stride_x Stride of the mean source tensor in X dimension (in bytes)
171 * @param[in] mean_step_x mean_stride_x * number of elements along X processed per workitem(in bytes)
172 * @param[in] mean_offset_first_element_in_bytes The offset of the first element in the mean source tensor
173 * @param[in] var_ptr Pointer to the var tensor. Supported data types: same as @p input_ptr
174 * @param[in] var_stride_x Stride of the var tensor in X dimension (in bytes)
175 * @param[in] var_step_x var_stride_x * number of elements along X processed per workitem(in bytes)
176 * @param[in] var_offset_first_element_in_bytes The offset of the first element in the var source tensor
177 * @param[in] beta_ptr Pointer to the beta source tensor. Supported data types: same as @p input_ptr
178 * @param[in] beta_stride_x Stride of the beta source tensor in X dimension (in bytes)
179 * @param[in] beta_step_x beta_stride_x * number of elements along X processed per workitem(in bytes)
180 * @param[in] beta_offset_first_element_in_bytes The offset of the first element in the beta source tensor
181 * @param[in] gamma_ptr Pointer to the gamma source tensor. Supported data types: same as @p input_ptr
182 * @param[in] gamma_stride_x Stride of the gamma source tensor in X dimension (in bytes)
183 * @param[in] gamma_step_x gamma_stride_x * number of elements along X processed per workitem(in bytes)
184 * @param[in] gamma_offset_first_element_in_bytes The offset of the first element in the gamma source tensor
185 * @param[in] epsilon Epsilon parameter in the batch normalization equation
186 */
187__kernel void batchnormalization_layer_nhwc(TENSOR3D_DECLARATION(input),
188#ifndef IN_PLACE
189 TENSOR3D_DECLARATION(output),
190#endif /* not IN_PLACE */
191 VECTOR_DECLARATION(mean),
192 VECTOR_DECLARATION(var),
193#ifndef USE_DEFAULT_BETA
194 VECTOR_DECLARATION(beta),
195#endif /* USE_DEFAULT_BETA */
196#ifndef USE_DEFAULT_GAMMA
197 VECTOR_DECLARATION(gamma),
198#endif /* USE_DEFAULT_GAMMA */
199 float epsilon)
200{
Sheri Zhang141c31a2020-10-08 12:35:28 +0100201 uint x_offs = max((int)(get_global_id(0) * VEC_SIZE * sizeof(DATA_TYPE) - (VEC_SIZE - VEC_SIZE_LEFTOVER) % VEC_SIZE * sizeof(DATA_TYPE)), 0);
202
203 __global uchar *input_addr = input_ptr + input_offset_first_element_in_bytes + x_offs + get_global_id(1) * input_stride_y + get_global_id(2) * input_stride_z;
Michele Di Giorgiobf3c6622018-03-08 11:52:27 +0000204#ifdef IN_PLACE
Sheri Zhang141c31a2020-10-08 12:35:28 +0100205 __global uchar *output_addr = input_ptr;
Michele Di Giorgiobf3c6622018-03-08 11:52:27 +0000206#else /* IN_PLACE */
Sheri Zhang141c31a2020-10-08 12:35:28 +0100207 __global uchar *output_addr = output_ptr + output_offset_first_element_in_bytes + x_offs + get_global_id(1) * output_stride_y + get_global_id(2) * output_stride_z;
Michele Di Giorgiobf3c6622018-03-08 11:52:27 +0000208#endif /* IN_PLACE */
Sheri Zhang141c31a2020-10-08 12:35:28 +0100209 __global uchar *mean_addr = mean_ptr + mean_offset_first_element_in_bytes + x_offs;
210 __global uchar *var_addr = var_ptr + var_offset_first_element_in_bytes + x_offs;
Michele Di Giorgiobf3c6622018-03-08 11:52:27 +0000211#ifndef USE_DEFAULT_BETA
Sheri Zhang141c31a2020-10-08 12:35:28 +0100212 __global uchar *beta_addr = beta_ptr + beta_offset_first_element_in_bytes + x_offs;
Michele Di Giorgiobf3c6622018-03-08 11:52:27 +0000213#endif /* USE_DEFAULT_BETA */
214#ifndef USE_DEFAULT_GAMMA
Sheri Zhang141c31a2020-10-08 12:35:28 +0100215 __global uchar *gamma_addr = gamma_ptr + gamma_offset_first_element_in_bytes + x_offs;
Michele Di Giorgiobf3c6622018-03-08 11:52:27 +0000216#endif /* USE_DEFAULT_GAMMA */
217
218 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
219 data = 0;
220 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
221 denominator = 0;
222 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
223 numerator = 0;
224 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
225 x_bar = 0;
226 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
Sheri Zhang141c31a2020-10-08 12:35:28 +0100227 res0 = 0;
Michele Di Giorgiobf3c6622018-03-08 11:52:27 +0000228
Sheri Zhang141c31a2020-10-08 12:35:28 +0100229 data = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)input_addr);
230 denominator = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)var_addr);
Michele Di Giorgiobf3c6622018-03-08 11:52:27 +0000231 denominator = INVSQRT_OP(ADD_OP(denominator, ((VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE))SQCVT_SAT(epsilon))));
232
233 // Calculate x bar and store results
Sheri Zhang141c31a2020-10-08 12:35:28 +0100234 numerator = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)mean_addr);
Michele Di Giorgiobf3c6622018-03-08 11:52:27 +0000235 numerator = SUB_OP(data, numerator);
236 x_bar = MUL_OP(numerator, denominator);
237
238#ifndef USE_DEFAULT_GAMMA
239 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
Sheri Zhang141c31a2020-10-08 12:35:28 +0100240 gamma_vec = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)gamma_addr);
Michele Di Giorgiobf3c6622018-03-08 11:52:27 +0000241
Sheri Zhang141c31a2020-10-08 12:35:28 +0100242 res0 = MUL_OP(gamma_vec, x_bar);
Michele Di Giorgiobf3c6622018-03-08 11:52:27 +0000243#else /* USE_DEFAULT_GAMMA */
244 // gamma is equal to 1, no need to perform multiplications
Sheri Zhang141c31a2020-10-08 12:35:28 +0100245 res0 = x_bar;
Michele Di Giorgiobf3c6622018-03-08 11:52:27 +0000246#endif /* USE_DEFAULT_GAMMA */
247
248#ifndef USE_DEFAULT_BETA
249 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
Sheri Zhang141c31a2020-10-08 12:35:28 +0100250 beta_vec = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)beta_addr);
Michele Di Giorgiobf3c6622018-03-08 11:52:27 +0000251 // beta is not zero, hence we need to perform the addition
Sheri Zhang141c31a2020-10-08 12:35:28 +0100252 res0 = ADD_OP(res0, beta_vec);
Michele Di Giorgiobf3c6622018-03-08 11:52:27 +0000253#endif /* USE_DEFAULT_BETA */
254
Sheri Zhang141c31a2020-10-08 12:35:28 +0100255 res0 = ACTIVATION(ACTIVATION_TYPE, DATA_TYPE, VEC_SIZE, res0, A_VAL, B_VAL);
Michele Di Giorgiobf3c6622018-03-08 11:52:27 +0000256
Sheri Zhang141c31a2020-10-08 12:35:28 +0100257 STORE_VECTOR_SELECT(res, DATA_TYPE, output_addr, VEC_SIZE, VEC_SIZE_LEFTOVER, VEC_SIZE_LEFTOVER != 0 && get_global_id(0) == 0)
Michele Di Giorgiobf3c6622018-03-08 11:52:27 +0000258}
Usama Arif6a98a6e2019-05-10 17:07:27 +0100259#endif /* defined(VEC_SIZE) && defined(DATA_TYPE) && defined(DATA_TYPE)*/
Georgios Pinitasc9369172018-09-26 11:25:40 +0100260
Manuel Bottini2732cca2019-05-28 11:44:41 +0100261#if defined(DATA_TYPE) && defined(EPSILON)
262/** OpenCL kernel to fuse the weights of convolution or depthwise convolution layer with batch normalization when the data layout is either NCHW or NHWC
Georgios Pinitasc9369172018-09-26 11:25:40 +0100263 *
Gian Marco Iodice761c8d02019-06-10 14:46:49 +0100264 * @note The input weights tensor is assumed 4D with the OFMs in the fourth dimension
265 * @note Data type should be passed at compile time using the -DDATA_TYPE, e.g. -DDATA_TYPE=float
Manuel Bottini2732cca2019-05-28 11:44:41 +0100266 * @note The third dimension of the input tensor should be passed at compile time when weights belong to a convolution layer using -DDIM2=size. e.g. -DDIM2=16.
267 * For depthwise convolution weight do not pass DIM2
268 * @note Data layout NHWC should be passed at compile time with -DNHWC. For data layout NCHW it is not required to pass any parameter
Gian Marco Iodice761c8d02019-06-10 14:46:49 +0100269 * @note Batch normalization epsilon parameter should be passed at compile time using -DEPSILON=value. e.g. -DEPSILON=0.001f
Georgios Pinitasc9369172018-09-26 11:25:40 +0100270 *
Gian Marco Iodice761c8d02019-06-10 14:46:49 +0100271 * @param[in] w_ptr Pointer to the weights tensor. Supported data types: F16/F32
272 * @param[in] w_stride_x Stride of the weights tensor in X dimension (in bytes)
273 * @param[in] w_step_x w_stride_x * number of elements along X processed per workitem(in bytes)
274 * @param[in] w_stride_y Stride of the weights tensor in Y dimension (in bytes)
275 * @param[in] w_step_y w_stride_y * number of elements along Y processed per workitem(in bytes)
276 * @param[in] w_stride_z Stride of the weights tensor in Z dimension (in bytes)
277 * @param[in] w_step_z w_stride_z * number of elements along Z processed per workitem(in bytes)
278 * @param[in] w_offset_first_element_in_bytes The offset of the first element in the weights tensor
279 * @param[in] b_ptr (Optional) Pointer to the bias tensor. Supported data types: same as @p w_ptr
280 * @param[in] b_stride_x (Optional) Stride of the bias tensor in X dimension (in bytes)
281 * @param[in] b_step_x (Optional) b_stride_x * number of elements along X processed per workitem(in bytes)
282 * @param[in] b_stride_y (Optional) Stride of the bias tensor in Y dimension (in bytes)
283 * @param[in] b_step_y (Optional) b_stride_y * number of elements along Y processed per workitem(in bytes)
284 * @param[in] b_stride_z (Optional) Stride of the bias tensor in Z dimension (in bytes)
285 * @param[in] b_step_z (Optional) b_stride_z * number of elements along Z processed per workitem(in bytes)
286 * @param[in] b_offset_first_element_in_bytes (Optional) The offset of the first element in the bias tensor
287 * @param[in] mean_ptr Pointer to the mean source tensor. Supported data types: same as @p w_ptr
288 * @param[in] mean_stride_x Stride of the mean source tensor in X dimension (in bytes)
289 * @param[in] mean_step_x mean_stride_x * number of elements along X processed per workitem(in bytes)
290 * @param[in] mean_offset_first_element_in_bytes The offset of the first element in the mean source tensor
291 * @param[in] var_ptr Pointer to the var tensor. Supported data types: same as @p w_ptr
292 * @param[in] var_stride_x Stride of the var tensor in X dimension (in bytes)
293 * @param[in] var_step_x var_stride_x * number of elements along X processed per workitem(in bytes)
294 * @param[in] var_offset_first_element_in_bytes The offset of the first element in the var source tensor
295 * @param[out] w_fused_ptr (Optional) Pointer to the destination weights tensors. Supported data types: same as @p w_ptr
296 * @param[in] w_fused_stride_x (Optional) Stride of the destination weights tensor in X dimension (in bytes)
297 * @param[in] w_fused_step_x (Optional) w_fused_stride_x * number of elements along X processed per workitem(in bytes)
298 * @param[in] w_fused_stride_y (Optional) Stride of the destination weights tensor in Y dimension (in bytes)
299 * @param[in] w_fused_step_y (Optional) w_fused_stride_y * number of elements along Y processed per workitem(in bytes)
300 * @param[in] w_fused_stride_z (Optional) Stride of the destination weights tensor in Z dimension (in bytes)
301 * @param[in] w_fused_step_z (Optional) w_fused_stride_z * number of elements along Z processed per workitem(in bytes)
302 * @param[in] w_fused_offset_first_element_in_bytes (Optional) The offset of the first element in the destination weights tensor
303 * @param[in] b_fused_ptr (Optional) Pointer to the destination bias tensor. Supported data types: same as @p w_ptr
304 * @param[in] b_fused_stride_x (Optional) Stride of the destination bias tensor in X dimension (in bytes)
305 * @param[in] b_fused_step_x (Optional) b_fused_stride_x * number of elements along X processed per workitem(in bytes)
306 * @param[in] b_fused_offset_first_element_in_bytes (Optional) The offset of the first element in the destination bias tensor
307 * @param[in] beta_ptr (Optional) Pointer to the beta source tensor. Supported data types: same as @p w_ptr
308 * @param[in] beta_stride_x (Optional) Stride of the beta source tensor in X dimension (in bytes)
309 * @param[in] beta_step_x (Optional) beta_stride_x * number of elements along X processed per workitem(in bytes)
310 * @param[in] beta_offset_first_element_in_bytes (Optional) The offset of the first element in the beta source tensor
311 * @param[in] gamma_ptr (Optional) Pointer to the gamma source tensor. Supported data types: same as @p w_ptr
312 * @param[in] gamma_stride_x (Optional) Stride of the gamma source tensor in X dimension (in bytes)
313 * @param[in] gamma_step_x (Optional) gamma_stride_x * number of elements along X processed per workitem(in bytes)
314 * @param[in] gamma_offset_first_element_in_bytes (Optional) The offset of the first element in the gamma source tensor
Georgios Pinitasc9369172018-09-26 11:25:40 +0100315 */
Manuel Bottini2732cca2019-05-28 11:44:41 +0100316__kernel void fuse_batchnormalization_layer(TENSOR3D_DECLARATION(w),
Gian Marco Iodice761c8d02019-06-10 14:46:49 +0100317#if defined(BIAS)
Manuel Bottini2732cca2019-05-28 11:44:41 +0100318 VECTOR_DECLARATION(b),
Gian Marco Iodice761c8d02019-06-10 14:46:49 +0100319#endif // defined(BIAS)
Manuel Bottini2732cca2019-05-28 11:44:41 +0100320 VECTOR_DECLARATION(mean),
321 VECTOR_DECLARATION(var)
Georgios Pinitasc9369172018-09-26 11:25:40 +0100322#ifndef IN_PLACE_W
Manuel Bottini2732cca2019-05-28 11:44:41 +0100323 ,
324 TENSOR3D_DECLARATION(w_fused)
Gian Marco Iodice761c8d02019-06-10 14:46:49 +0100325#endif // ifndef IN_PLACE_W
Georgios Pinitasc9369172018-09-26 11:25:40 +0100326#ifndef IN_PLACE_B
Manuel Bottini2732cca2019-05-28 11:44:41 +0100327 ,
328 VECTOR_DECLARATION(b_fused)
Gian Marco Iodice761c8d02019-06-10 14:46:49 +0100329#endif // ifndef IN_PLACE_B
330#if defined(BETA)
Manuel Bottini2732cca2019-05-28 11:44:41 +0100331 ,
332 VECTOR_DECLARATION(beta)
Gian Marco Iodice761c8d02019-06-10 14:46:49 +0100333#endif // defined(BETA)
334#if defined(GAMMA)
Manuel Bottini2732cca2019-05-28 11:44:41 +0100335 ,
336 VECTOR_DECLARATION(gamma)
Gian Marco Iodice761c8d02019-06-10 14:46:49 +0100337#endif // defined(GAMMA)
Manuel Bottini2732cca2019-05-28 11:44:41 +0100338 )
Georgios Pinitasc9369172018-09-26 11:25:40 +0100339{
Manuel Bottini2732cca2019-05-28 11:44:41 +0100340 int x = get_global_id(0);
341 int y = get_global_id(1);
342 int z = get_global_id(2);
343
344#if defined(DIM2)
Gian Marco Iodice761c8d02019-06-10 14:46:49 +0100345 int c0 = z % DIM2;
346 int c1 = z / DIM2;
Manuel Bottini2732cca2019-05-28 11:44:41 +0100347#else // ! defined(DIM2)
Michalis Spyrouef6ec502020-07-31 11:38:36 +0100348 int c0 = 0;
Manuel Bottini2732cca2019-05-28 11:44:41 +0100349#if defined(NHWC)
Michalis Spyrouef6ec502020-07-31 11:38:36 +0100350 int c1 = x;
Manuel Bottini2732cca2019-05-28 11:44:41 +0100351#else // defined(NHWC)
352 int c1 = z;
353#endif // defined(NHWC)
354#endif // defined(DIM2)
Georgios Pinitasc9369172018-09-26 11:25:40 +0100355
Gian Marco Iodice761c8d02019-06-10 14:46:49 +0100356 int w_offset = x * sizeof(DATA_TYPE) + y * w_stride_y + z * w_stride_z;
357 int v_offset = c1 * sizeof(DATA_TYPE);
Georgios Pinitasc9369172018-09-26 11:25:40 +0100358
Gian Marco Iodice761c8d02019-06-10 14:46:49 +0100359 DATA_TYPE w_old = 0.0f;
360 DATA_TYPE b_old = 0.0f;
361 DATA_TYPE w_new = 0.0f;
362 DATA_TYPE b_new = 0.0f;
363 DATA_TYPE gamma = 1.0f;
364 DATA_TYPE mean = 0.0f;
365 DATA_TYPE var = 1.0f;
366 DATA_TYPE beta = 0.0f;
giuros01acce5042019-02-21 17:32:34 +0000367
Gian Marco Iodice761c8d02019-06-10 14:46:49 +0100368 w_old = *((__global DATA_TYPE *)(w_ptr + w_offset + w_offset_first_element_in_bytes));
369 var = *((__global DATA_TYPE *)(var_ptr + v_offset + var_offset_first_element_in_bytes));
370 mean = *((__global DATA_TYPE *)(mean_ptr + v_offset + mean_offset_first_element_in_bytes));
Georgios Pinitasc9369172018-09-26 11:25:40 +0100371
Gian Marco Iodice761c8d02019-06-10 14:46:49 +0100372#if defined(GAMMA)
373 gamma = *((__global DATA_TYPE *)(gamma_ptr + v_offset + gamma_offset_first_element_in_bytes));
374#endif // defined(GAMMA)
Georgios Pinitasc9369172018-09-26 11:25:40 +0100375
Gian Marco Iodice761c8d02019-06-10 14:46:49 +0100376 // Compute new weight
377 w_new = (gamma * w_old) / (sqrt(var + EPSILON));
Georgios Pinitasc9369172018-09-26 11:25:40 +0100378
Gian Marco Iodice761c8d02019-06-10 14:46:49 +0100379#if defined(IN_PLACE_W)
380 *((__global DATA_TYPE *)(w_ptr + w_offset + w_offset_first_element_in_bytes)) = w_new;
381#else // defined(IN_PLACE_W)
Manuel Bottini2732cca2019-05-28 11:44:41 +0100382 *((__global DATA_TYPE *)(w_fused_ptr + w_offset + w_fused_offset_first_element_in_bytes)) = w_new;
Gian Marco Iodice761c8d02019-06-10 14:46:49 +0100383#endif // defined(IN_PLACE_W)
Georgios Pinitasc9369172018-09-26 11:25:40 +0100384
Gian Marco Iodice761c8d02019-06-10 14:46:49 +0100385 // Compute bias
Manuel Bottini2732cca2019-05-28 11:44:41 +0100386#if !defined(DIM2) && defined(NHWC)
387 if(z == 0 && y == 0)
Michalis Spyrouef6ec502020-07-31 11:38:36 +0100388#else // !defined(DIM2) && defined(NHWC)
Gian Marco Iodice761c8d02019-06-10 14:46:49 +0100389 if(x == 0 && y == 0 && c0 == 0)
Manuel Bottini2732cca2019-05-28 11:44:41 +0100390#endif // !defined(DIM2) && defined(NHWC)
Gian Marco Iodice761c8d02019-06-10 14:46:49 +0100391 {
392#if defined(BIAS)
393 b_old = *((__global DATA_TYPE *)(b_ptr + v_offset + b_offset_first_element_in_bytes));
394#endif // defined(BIAS)
395#if defined(BETA)
396 beta = *((__global DATA_TYPE *)(beta_ptr + v_offset + beta_offset_first_element_in_bytes));
397#endif // defined(BETA)
Georgios Pinitasc9369172018-09-26 11:25:40 +0100398
Gian Marco Iodice761c8d02019-06-10 14:46:49 +0100399 b_new = ((gamma * (b_old - mean)) / (sqrt(var + EPSILON))) + beta;
Georgios Pinitasc9369172018-09-26 11:25:40 +0100400
Gian Marco Iodice761c8d02019-06-10 14:46:49 +0100401#if defined(BIAS)
Georgios Pinitasc9369172018-09-26 11:25:40 +0100402
Gian Marco Iodice761c8d02019-06-10 14:46:49 +0100403#if defined(IN_PLACE_B)
404 *((__global DATA_TYPE *)(b_ptr + v_offset + b_offset_first_element_in_bytes)) = b_new;
405#else // defined(IN_PLACE_B)
406 *((__global DATA_TYPE *)(b_fused_ptr + v_offset + b_fused_offset_first_element_in_bytes)) = b_new;
407#endif // defined(IN_PLACE_B)
Georgios Pinitasc9369172018-09-26 11:25:40 +0100408
Gian Marco Iodice761c8d02019-06-10 14:46:49 +0100409#else // defined(BIAS)
410
411#ifndef IN_PLACE_B
412 *((__global DATA_TYPE *)(b_fused_ptr + v_offset + b_fused_offset_first_element_in_bytes)) = b_new;
413#endif // ifndef IN_PLACE_B
414
415#endif // defined(BIAS)
416 }
Georgios Pinitasc9369172018-09-26 11:25:40 +0100417}
Manuel Bottini2732cca2019-05-28 11:44:41 +0100418#endif // defined(DATA_TYPE) && defined(EPSILON)