blob: 0bb6cd7c5f70d1b2438c26ca73d7f039ae663268 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
giuros01acce5042019-02-21 17:32:34 +00002 * Copyright (c) 2017-2019 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
Georgios Pinitasc9369172018-09-26 11:25:40 +010032#if defined(VEC_SIZE) && defined(DATA_TYPE)
33
Giorgio Arena99ac60b2018-02-16 15:17:23 +000034#if defined(FUSED_ACTIVATION)
Usama Arife2428a02019-05-09 11:03:17 +010035#define SELECT_TYPE VEC_DATA_TYPE(SELECT_DATA_TYPE, VEC_SIZE)
36#define TYPE VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
37#include "activation_helpers.h"
Giorgio Arena99ac60b2018-02-16 15:17:23 +000038#define ACTIVATION_FUNC(x) ACTIVATION_OP(FUSED_ACTIVATION, x)
39#else /* defined(FUSED_ACTIVATION) */
Giorgio Arena11674872018-02-07 15:38:12 +000040#define ACTIVATION_FUNC(x) (x)
Giorgio Arena99ac60b2018-02-16 15:17:23 +000041#endif /* defined(FUSED_ACTIVATION) */
Giorgio Arena11674872018-02-07 15:38:12 +000042
Anthony Barbier6ff3b192017-09-04 18:44:23 +010043/** Apply batch normalization.
44 *
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010045 * @param[in] input_ptr Pointer to the first source tensor. Supported data types: F16/F32
Anthony Barbier6ff3b192017-09-04 18:44:23 +010046 * @param[in] input_stride_x Stride of the first source tensor in X dimension (in bytes)
47 * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes)
48 * @param[in] input_stride_y Stride of the first source tensor in Y dimension (in bytes)
49 * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes)
50 * @param[in] input_stride_z Stride of the first source tensor in Z dimension (in bytes)
51 * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes)
52 * @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 +010053 * @param[out] output_ptr Pointer to the destination tensor. Supported data types: same as @p input_ptr
Anthony Barbier6ff3b192017-09-04 18:44:23 +010054 * @param[in] output_stride_x Stride of the destination tensor in X dimension (in bytes)
55 * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes)
56 * @param[in] output_stride_y Stride of the destination tensor in Y dimension (in bytes)
57 * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes)
58 * @param[in] output_stride_z Stride of the destination tensor in Z dimension (in bytes)
59 * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes)
60 * @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 +010061 * @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 +010062 * @param[in] mean_stride_x Stride of the mean source tensor in X dimension (in bytes)
63 * @param[in] mean_step_x mean_stride_x * number of elements along X processed per workitem(in bytes)
64 * @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 +010065 * @param[in] var_ptr Pointer to the var tensor. Supported data types: same as @p input_ptr
Anthony Barbier6ff3b192017-09-04 18:44:23 +010066 * @param[in] var_stride_x Stride of the var tensor in X dimension (in bytes)
67 * @param[in] var_step_x var_stride_x * number of elements along X processed per workitem(in bytes)
68 * @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 +010069 * @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 +010070 * @param[in] beta_stride_x Stride of the beta source tensor in X dimension (in bytes)
71 * @param[in] beta_step_x beta_stride_x * number of elements along X processed per workitem(in bytes)
72 * @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 +010073 * @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 +010074 * @param[in] gamma_stride_x Stride of the gamma source tensor in X dimension (in bytes)
75 * @param[in] gamma_step_x gamma_stride_x * number of elements along X processed per workitem(in bytes)
76 * @param[in] gamma_offset_first_element_in_bytes The offset of the first element in the gamma source tensor
77 * @param[in] epsilon Epsilon parameter in the batch normalization equation
78 */
Michele Di Giorgiobf3c6622018-03-08 11:52:27 +000079__kernel void batchnormalization_layer_nchw(TENSOR3D_DECLARATION(input),
Georgios Pinitas409ee0a2017-08-18 10:16:09 +010080#ifndef IN_PLACE
Michele Di Giorgiobf3c6622018-03-08 11:52:27 +000081 TENSOR3D_DECLARATION(output),
Georgios Pinitas409ee0a2017-08-18 10:16:09 +010082#endif /* not IN_PLACE */
Michele Di Giorgiobf3c6622018-03-08 11:52:27 +000083 VECTOR_DECLARATION(mean),
84 VECTOR_DECLARATION(var),
Michele Di Giorgio4d336302018-03-02 09:43:54 +000085#ifndef USE_DEFAULT_BETA
Michele Di Giorgiobf3c6622018-03-08 11:52:27 +000086 VECTOR_DECLARATION(beta),
Michele Di Giorgio4d336302018-03-02 09:43:54 +000087#endif /* USE_DEFAULT_BETA */
88#ifndef USE_DEFAULT_GAMMA
Michele Di Giorgiobf3c6622018-03-08 11:52:27 +000089 VECTOR_DECLARATION(gamma),
Michele Di Giorgio4d336302018-03-02 09:43:54 +000090#endif /* USE_DEFAULT_GAMMA */
Michele Di Giorgiobf3c6622018-03-08 11:52:27 +000091 float epsilon)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010092{
Georgios Pinitas409ee0a2017-08-18 10:16:09 +010093 Tensor3D in = CONVERT_TO_TENSOR3D_STRUCT(input);
94#ifdef IN_PLACE
95 Tensor3D out = in;
96#else /* IN_PLACE */
97 Tensor3D out = CONVERT_TO_TENSOR3D_STRUCT(output);
98#endif /* IN_PLACE */
Michele Di Giorgio4d336302018-03-02 09:43:54 +000099 Vector mean = CONVERT_TO_VECTOR_STRUCT(mean);
100 Vector var = CONVERT_TO_VECTOR_STRUCT(var);
101#ifndef USE_DEFAULT_BETA
102 Vector beta = CONVERT_TO_VECTOR_STRUCT(beta);
103#endif /* USE_DEFAULT_BETA */
104#ifndef USE_DEFAULT_GAMMA
Georgios Pinitas409ee0a2017-08-18 10:16:09 +0100105 Vector gamma = CONVERT_TO_VECTOR_STRUCT(gamma);
Michele Di Giorgio4d336302018-03-02 09:43:54 +0000106#endif /* USE_DEFAULT_GAMMA */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100107
Michalis Spyrou172e5702017-06-26 14:18:47 +0100108 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
Gian Marco Iodice349feef2017-09-28 11:21:29 +0100109 data = 0;
Michalis Spyrou172e5702017-06-26 14:18:47 +0100110 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
111 denominator = 0;
112 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
113 numerator = 0;
114 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
115 x_bar = 0;
116 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
Michele Di Giorgio4d336302018-03-02 09:43:54 +0000117 res = 0;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100118
119 const int current_slice = get_global_id(2);
120
Gian Marco Iodice349feef2017-09-28 11:21:29 +0100121 data = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)in.ptr);
Michalis Spyrou172e5702017-06-26 14:18:47 +0100122 denominator = *((__global DATA_TYPE *)(var.ptr + current_slice * var.stride_x));
Gian Marco Iodice349feef2017-09-28 11:21:29 +0100123 denominator = INVSQRT_OP(ADD_OP(denominator, ((VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE))SQCVT_SAT(epsilon))));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100124
125 // Calculate x bar and store results
Michalis Spyrou172e5702017-06-26 14:18:47 +0100126 numerator = *((__global DATA_TYPE *)(mean.ptr + current_slice * mean.stride_x));
Gian Marco Iodice349feef2017-09-28 11:21:29 +0100127 numerator = SUB_OP(data, numerator);
Michalis Spyrou172e5702017-06-26 14:18:47 +0100128 x_bar = MUL_OP(numerator, denominator);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100129
Michele Di Giorgio4d336302018-03-02 09:43:54 +0000130#ifndef USE_DEFAULT_GAMMA
Giorgio Arena11674872018-02-07 15:38:12 +0000131 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
Michele Di Giorgio4d336302018-03-02 09:43:54 +0000132 gamma_vec = *((__global DATA_TYPE *)(gamma.ptr + current_slice * gamma.stride_x));
133
134 res = MUL_OP(gamma_vec, x_bar);
135#else /* USE_DEFAULT_GAMMA */
136 // gamma is equal to 1, no need to perform multiplications
Michele Di Giorgiobf3c6622018-03-08 11:52:27 +0000137 res = x_bar;
Michele Di Giorgio4d336302018-03-02 09:43:54 +0000138#endif /* USE_DEFAULT_GAMMA */
139
140#ifndef USE_DEFAULT_BETA
141 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
142 beta_vec = *((__global DATA_TYPE *)(beta.ptr + current_slice * beta.stride_x));
143 // beta is not zero, hence we need to perform the addition
144 res = ADD_OP(res, beta_vec);
145#endif /* USE_DEFAULT_BETA */
Giorgio Arena11674872018-02-07 15:38:12 +0000146
147 res = ACTIVATION_FUNC(res);
148
Michalis Spyrou172e5702017-06-26 14:18:47 +0100149 VSTORE(VEC_SIZE)
Giorgio Arena11674872018-02-07 15:38:12 +0000150 (res, 0, (__global DATA_TYPE *)out.ptr);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100151}
Giorgio Arena11674872018-02-07 15:38:12 +0000152
Michele Di Giorgiobf3c6622018-03-08 11:52:27 +0000153/** Apply batch normalization on tensors with NHWC format.
154 *
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100155 * @param[in] input_ptr Pointer to the first source tensor. Supported data types: F16/F32
Michele Di Giorgiobf3c6622018-03-08 11:52:27 +0000156 * @param[in] input_stride_x Stride of the first source tensor in X dimension (in bytes)
157 * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes)
158 * @param[in] input_stride_y Stride of the first source tensor in Y dimension (in bytes)
159 * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes)
160 * @param[in] input_stride_z Stride of the first source tensor in Z dimension (in bytes)
161 * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes)
162 * @param[in] input_offset_first_element_in_bytes The offset of the first element in the first source tensor
163 * @param[out] output_ptr Pointer to the destination tensor. Supported data types: same as @p input_ptr
164 * @param[in] output_stride_x Stride of the destination tensor in X dimension (in bytes)
165 * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes)
166 * @param[in] output_stride_y Stride of the destination tensor in Y dimension (in bytes)
167 * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes)
168 * @param[in] output_stride_z Stride of the destination tensor in Z dimension (in bytes)
169 * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes)
170 * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination tensor
171 * @param[in] mean_ptr Pointer to the mean source tensor. Supported data types: same as @p input_ptr
172 * @param[in] mean_stride_x Stride of the mean source tensor in X dimension (in bytes)
173 * @param[in] mean_step_x mean_stride_x * number of elements along X processed per workitem(in bytes)
174 * @param[in] mean_offset_first_element_in_bytes The offset of the first element in the mean source tensor
175 * @param[in] var_ptr Pointer to the var tensor. Supported data types: same as @p input_ptr
176 * @param[in] var_stride_x Stride of the var tensor in X dimension (in bytes)
177 * @param[in] var_step_x var_stride_x * number of elements along X processed per workitem(in bytes)
178 * @param[in] var_offset_first_element_in_bytes The offset of the first element in the var source tensor
179 * @param[in] beta_ptr Pointer to the beta source tensor. Supported data types: same as @p input_ptr
180 * @param[in] beta_stride_x Stride of the beta source tensor in X dimension (in bytes)
181 * @param[in] beta_step_x beta_stride_x * number of elements along X processed per workitem(in bytes)
182 * @param[in] beta_offset_first_element_in_bytes The offset of the first element in the beta source tensor
183 * @param[in] gamma_ptr Pointer to the gamma source tensor. Supported data types: same as @p input_ptr
184 * @param[in] gamma_stride_x Stride of the gamma source tensor in X dimension (in bytes)
185 * @param[in] gamma_step_x gamma_stride_x * number of elements along X processed per workitem(in bytes)
186 * @param[in] gamma_offset_first_element_in_bytes The offset of the first element in the gamma source tensor
187 * @param[in] epsilon Epsilon parameter in the batch normalization equation
188 */
189__kernel void batchnormalization_layer_nhwc(TENSOR3D_DECLARATION(input),
190#ifndef IN_PLACE
191 TENSOR3D_DECLARATION(output),
192#endif /* not IN_PLACE */
193 VECTOR_DECLARATION(mean),
194 VECTOR_DECLARATION(var),
195#ifndef USE_DEFAULT_BETA
196 VECTOR_DECLARATION(beta),
197#endif /* USE_DEFAULT_BETA */
198#ifndef USE_DEFAULT_GAMMA
199 VECTOR_DECLARATION(gamma),
200#endif /* USE_DEFAULT_GAMMA */
201 float epsilon)
202{
203 Tensor3D in = CONVERT_TO_TENSOR3D_STRUCT(input);
204#ifdef IN_PLACE
205 Tensor3D out = in;
206#else /* IN_PLACE */
207 Tensor3D out = CONVERT_TO_TENSOR3D_STRUCT(output);
208#endif /* IN_PLACE */
209 Vector mean = CONVERT_TO_VECTOR_STRUCT(mean);
210 Vector var = CONVERT_TO_VECTOR_STRUCT(var);
211#ifndef USE_DEFAULT_BETA
212 Vector beta = CONVERT_TO_VECTOR_STRUCT(beta);
213#endif /* USE_DEFAULT_BETA */
214#ifndef USE_DEFAULT_GAMMA
215 Vector gamma = CONVERT_TO_VECTOR_STRUCT(gamma);
216#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)
227 res = 0;
228
229 const int current_slice = get_global_id(0);
230
231 data = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)in.ptr);
232 denominator = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(var.ptr + current_slice * VEC_SIZE * var.stride_x));
233 denominator = INVSQRT_OP(ADD_OP(denominator, ((VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE))SQCVT_SAT(epsilon))));
234
235 // Calculate x bar and store results
236 numerator = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(mean.ptr + current_slice * VEC_SIZE * mean.stride_x));
237 numerator = SUB_OP(data, numerator);
238 x_bar = MUL_OP(numerator, denominator);
239
240#ifndef USE_DEFAULT_GAMMA
241 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
242 gamma_vec = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(gamma.ptr + current_slice * VEC_SIZE * gamma.stride_x));
243
244 res = MUL_OP(gamma_vec, x_bar);
245#else /* USE_DEFAULT_GAMMA */
246 // gamma is equal to 1, no need to perform multiplications
247 res = x_bar;
248#endif /* USE_DEFAULT_GAMMA */
249
250#ifndef USE_DEFAULT_BETA
251 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
252 beta_vec = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(beta.ptr + current_slice * VEC_SIZE * beta.stride_x));
253 // beta is not zero, hence we need to perform the addition
254 res = ADD_OP(res, beta_vec);
255#endif /* USE_DEFAULT_BETA */
256
257 res = ACTIVATION_FUNC(res);
258
259 VSTORE(VEC_SIZE)
260 (res, 0, (__global DATA_TYPE *)out.ptr);
261}
Michele Di Giorgio4d336302018-03-02 09:43:54 +0000262#endif /* defined(VEC_SIZE) && defined(DATA_TYPE) */
Georgios Pinitasc9369172018-09-26 11:25:40 +0100263
264#if defined(NUM_CHANNELS) && defined(DATA_TYPE) && defined(EPSILON)
265/** Fuse batchnorm parameters to convolution layer parameters
266 *
267 * @attention Data type should be passed using the -DDATA_TYPE compile flag, e.g. -DDATA_TYPE=float
268 * @attention Input tensor depth should be given as a preprocessor argument using -DNUM_CHANNELS=size. e.g. -DNUM_CHANNELS=16
269 * @attention Batch normalization epsilon parameter should be given as a preprocessor argument with -DEPSILON=value. e.g. -DEPSILON=0.001f
270 *
271 * @param[in] conv_w_ptr Pointer to the source tensor. Supported data types: F16/F32
272 * @param[in] conv_w_stride_x Stride of the source tensor in X dimension (in bytes)
273 * @param[in] conv_w_step_x input_stride_x * number of elements along X processed per workitem(in bytes)
274 * @param[in] conv_w_stride_y Stride of the source tensor in Y dimension (in bytes)
275 * @param[in] conv_w_step_y input_stride_y * number of elements along Y processed per workitem(in bytes)
276 * @param[in] conv_w_stride_z Stride of the source tensor in Z dimension (in bytes)
277 * @param[in] conv_w_step_z input_stride_z * number of elements along Z processed per workitem(in bytes)
Georgios Pinitasc55beee2018-10-23 15:23:23 +0100278 * @param[in] conv_w_stride_w Stride of the source tensor in W dimension (in bytes)
279 * @param[in] conv_w_step_w input_stride_w * number of elements along W processed per workitem(in bytes)
Georgios Pinitasc9369172018-09-26 11:25:40 +0100280 * @param[in] conv_w_offset_first_element_in_bytes The offset of the first element in the source tensor
281 * @param[in] bn_mean_ptr Pointer to the mean source tensor. Supported data types: same as @p input_ptr
282 * @param[in] bn_mean_stride_x Stride of the mean source tensor in X dimension (in bytes)
283 * @param[in] bn_mean_step_x bn_mean_stride_x * number of elements along X processed per workitem(in bytes)
284 * @param[in] bn_mean_offset_first_element_in_bytes The offset of the first element in the mean source tensor
285 * @param[in] bn_var_ptr Pointer to the var tensor. Supported data types: same as @p input_ptr
286 * @param[in] bn_var_stride_x Stride of the var tensor in X dimension (in bytes)
287 * @param[in] bn_var_step_x bn_var_stride_x * number of elements along X processed per workitem(in bytes)
288 * @param[in] bn_var_offset_first_element_in_bytes The offset of the first element in the var source tensor
289 * @param[out] fused_w_ptr Pointer to the destination weights tensors. Supported data types: same as @p input_ptr
290 * @param[in] fused_w_stride_x Stride of the destination tensor in X dimension (in bytes)
291 * @param[in] fused_w_step_x fused_w_stride_x * number of elements along X processed per workitem(in bytes)
292 * @param[in] fused_w_stride_y Stride of the destination tensor in Y dimension (in bytes)
293 * @param[in] fused_w_step_y fused_w_stride_y * number of elements along Y processed per workitem(in bytes)
294 * @param[in] fused_w_stride_z Stride of the destination tensor in Z dimension (in bytes)
295 * @param[in] fused_w_step_z fused_w_stride_z * number of elements along Z processed per workitem(in bytes)
296 * @param[in] fused_w_stride_w Stride of the destination tensor in W dimension (in bytes)
297 * @param[in] fused_w_step_w fused_w_stride_w * number of elements along W processed per workitem(in bytes)
298 * @param[in] fused_w_offset_first_element_in_bytes The offset of the first element in the destination tensor
299 * @param[in] fused_b_ptr Pointer to the destination bias tensor. Supported data types: same as @p input_ptr
300 * @param[in] fused_b_stride_x Stride of the bias source tensor in X dimension (in bytes)
301 * @param[in] fused_b_step_x fused_b_stride_x * number of elements along X processed per workitem(in bytes)
302 * @param[in] fused_b_offset_first_element_in_bytes The offset of the first element in the destination tensor
303 * @param[in] conv_b_ptr Pointer to the source bias tensor. Supported data types: same as @p input_ptr
304 * @param[in] conv_b_stride_x Stride of the beta source tensor in X dimension (in bytes)
305 * @param[in] conv_b_step_x conv_b_beta_stride_x * number of elements along X processed per workitem(in bytes)
306 * @param[in] conv_b_offset_first_element_in_bytes The offset of the first element in the source bias tensor
307 * @param[in] bn_beta_ptr Pointer to the beta source tensor. Supported data types: same as @p input_ptr
308 * @param[in] bn_beta_stride_x Stride of the beta source tensor in X dimension (in bytes)
309 * @param[in] bn_beta_step_x bn_beta_stride_x * number of elements along X processed per workitem(in bytes)
310 * @param[in] bn_beta_offset_first_element_in_bytes The offset of the first element in the beta source tensor
311 * @param[in] bn_gamma_ptr Pointer to the gamma source tensor. Supported data types: same as @p input_ptr
312 * @param[in] bn_gamma_stride_x Stride of the gamma source tensor in X dimension (in bytes)
313 * @param[in] bn_gamma_step_x bn_gamma_stride_x * number of elements along X processed per workitem(in bytes)
314 * @param[in] bn_gamma_offset_first_element_in_bytes The offset of the first element in the gamma source tensor
315 * @param[in] epsilon Epsilon parameter in the batch normalization equation
316 */
317__kernel void fuse_batchnormalization_layer(TENSOR4D_DECLARATION(conv_w),
318 VECTOR_DECLARATION(bn_mean),
319 VECTOR_DECLARATION(bn_var)
320#ifndef IN_PLACE_W
321 ,
322 TENSOR4D_DECLARATION(fused_w)
323#endif /* not IN_PLACE_W */
324#ifndef IN_PLACE_B
325 ,
326 VECTOR_DECLARATION(fused_b)
327#endif /* not IN_PLACE_B */
328#ifdef HAS_BIAS
329 ,
330 VECTOR_DECLARATION(conv_b)
331#endif /* HAS_BIAS */
332#ifndef USE_DEFAULT_BETA
333 ,
334 VECTOR_DECLARATION(bn_beta)
335#endif /* USE_DEFAULT_BETA */
336#ifndef USE_DEFAULT_GAMMA
337 ,
338 VECTOR_DECLARATION(bn_gamma)
339#endif /* USE_DEFAULT_GAMMA */
340 )
341{
342 Tensor4D conv_w = CONVERT_TO_TENSOR4D_STRUCT(conv_w, NUM_CHANNELS);
343 Vector bn_mean = CONVERT_TO_VECTOR_STRUCT_NO_STEP(bn_mean);
344 Vector bn_var = CONVERT_TO_VECTOR_STRUCT_NO_STEP(bn_var);
345
Georgios Pinitasc9369172018-09-26 11:25:40 +0100346 // Conditional ops
347#ifdef HAS_BIAS
348 Vector conv_b = CONVERT_TO_VECTOR_STRUCT_NO_STEP(conv_b);
giuros01acce5042019-02-21 17:32:34 +0000349#endif /* HAS_BIAS */
Georgios Pinitasc9369172018-09-26 11:25:40 +0100350#ifndef USE_DEFAULT_BETA
351 Vector bn_beta = CONVERT_TO_VECTOR_STRUCT_NO_STEP(bn_beta);
352#endif /* USE_DEFAULT_BETA */
353#ifndef USE_DEFAULT_GAMMA
354 Vector bn_gamma = CONVERT_TO_VECTOR_STRUCT_NO_STEP(bn_gamma);
355#endif /* USE_DEFAULT_GAMMA */
356
giuros01acce5042019-02-21 17:32:34 +0000357 // In-place ops
358#ifdef IN_PLACE_W
359 Tensor4D fused_w = conv_w;
360 uint fused_w_stride_x = conv_w_stride_x;
361#else /* IN_PLACE_W */
362 Tensor4D fused_w = CONVERT_TO_TENSOR4D_STRUCT(fused_w, NUM_CHANNELS);
363#endif /* IN_PLACE_W */
364#ifdef IN_PLACE_B
365 Vector fused_b = conv_b;
366#else /* IN_PLACE_B */
367 Vector fused_b = CONVERT_TO_VECTOR_STRUCT_NO_STEP(fused_b);
368#endif /* IN_PLACE_B */
369
Georgios Pinitasc9369172018-09-26 11:25:40 +0100370 const int current_slice = get_global_id(2) / NUM_CHANNELS;
371
372#if defined(VEC_SIZE) && defined(LAST_ACCESSED_X)
373 // Check if access on width gets out of bounds
374 // If it does shift access vector to access elements within bounds
375 const int xi = (int)(get_global_id(0) * VEC_SIZE);
376 conv_w.ptr -= max(xi - (int)LAST_ACCESSED_X, 0) * conv_w_stride_x;
377 fused_w.ptr -= max(xi - (int)LAST_ACCESSED_X, 0) * fused_w_stride_x;
378
379 // Load W
380 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
381 wn = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)conv_w.ptr);
382#else // !defined(VEC_SIZE) || !defined(LAST_ACCESSED_X)
383 DATA_TYPE wn = *((__global DATA_TYPE *)(conv_w.ptr));
384#endif // defined(VEC_SIZE) && defined(LAST_ACCESSED_X)
385
386 // rvar = 1 / sqrt(var + epsilon)
387 const DATA_TYPE var = *((__global DATA_TYPE *)(bn_var.ptr + current_slice * bn_var.stride_x));
388 const DATA_TYPE rvar = INVSQRT_OP(ADD_OP(var, SQCVT_SAT((float)EPSILON)));
389 wn *= rvar;
390
391 // Load b
392 const DATA_TYPE mean = *((__global DATA_TYPE *)(bn_mean.ptr + current_slice * bn_mean.stride_x));
393 DATA_TYPE bn = 0;
394#ifdef HAS_BIAS
395 bn = *((__global DATA_TYPE *)(conv_b.ptr + current_slice * conv_b.stride_x));
396#endif /* HAS_BIAS */
397 bn = (bn - mean) * rvar;
398
399#ifndef USE_DEFAULT_GAMMA
400 const DATA_TYPE gamma_scalar = *((__global DATA_TYPE *)(bn_gamma.ptr + current_slice * bn_gamma.stride_x));
401 wn *= gamma_scalar;
402 bn *= gamma_scalar;
403#endif /* USE_DEFAULT_GAMMA */
404
405#ifndef USE_DEFAULT_BETA
406 const DATA_TYPE beta_scalar = *((__global DATA_TYPE *)(bn_beta.ptr + current_slice * bn_beta.stride_x));
407 bn += beta_scalar;
408#endif /* USE_DEFAULT_BETA */
409
410#if defined(VEC_SIZE) && defined(LAST_ACCESSED_X)
411 // Store updated weights
412 VSTORE(VEC_SIZE)
413 (wn, 0, (__global DATA_TYPE *)fused_w.ptr);
414#else // !defined(VEC_SIZE) || !defined(LAST_ACCESSED_X)
415 *((__global DATA_TYPE *)(fused_w.ptr)) = wn;
416#endif // defined(VEC_SIZE) && defined(LAST_ACCESSED_X)
417
418 // Store updated bias
419 *((__global DATA_TYPE *)(fused_b.ptr + current_slice * fused_b.stride_x)) = bn;
420}
421#endif /* defined(NUM_CHANNELS) && defined(DATA_TYPE) && defined(EPSILON) */