2 * Copyright (c) 2017-2018 ARM Limited.
4 * SPDX-License-Identifier: MIT
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:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
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
26 #if defined(VEC_SIZE) && defined(DATA_TYPE)
28 #if defined(FIXED_POINT_POSITION)
29 #include "fixed_point.h"
31 #define ADD_OP(a, b) ADD_SAT_OP_EXPAND((a), (b), DATA_TYPE, VEC_SIZE)
32 #define SUB_OP(a, b) SUB_SAT_OP_EXPAND((a), (b), DATA_TYPE, VEC_SIZE)
33 #define MUL_OP(a, b) MUL_SAT_OP_EXPAND((a), (b), DATA_TYPE, VEC_SIZE, FIXED_POINT_POSITION)
34 #define INVSQRT_OP(a) INVSQRT_OP_EXPAND((a), DATA_TYPE, VEC_SIZE, FIXED_POINT_POSITION)
35 #define SQCVT_SAT(a) SQCVT_SAT_OP_EXPAND((a), DATA_TYPE, FIXED_POINT_POSITION)
37 #else /* FIXED_POINT_POSITION */
39 #define ADD_OP(a, b) ((a) + (b))
40 #define SUB_OP(a, b) ((a) - (b))
41 #define MUL_OP(a, b) ((a) * (b))
42 #define INVSQRT_OP(a) rsqrt((a))
43 #define SQCVT_SAT(a) (a)
45 #endif /* FIXED_POINT_POSITION */
47 #if defined(FUSED_ACTIVATION)
48 #include "activation_layer.cl"
49 #define ACTIVATION_FUNC(x) ACTIVATION_OP(FUSED_ACTIVATION, x)
50 #else /* defined(FUSED_ACTIVATION) */
51 #define ACTIVATION_FUNC(x) (x)
52 #endif /* defined(FUSED_ACTIVATION) */
54 /** Apply batch normalization.
56 * @param[in] input_ptr Pointer to the first source tensor. Supported data types: QS8/QS16/F16/F32
57 * @param[in] input_stride_x Stride of the first source tensor in X dimension (in bytes)
58 * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes)
59 * @param[in] input_stride_y Stride of the first source tensor in Y dimension (in bytes)
60 * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes)
61 * @param[in] input_stride_z Stride of the first source tensor in Z dimension (in bytes)
62 * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes)
63 * @param[in] input_offset_first_element_in_bytes The offset of the first element in the first source tensor
64 * @param[out] output_ptr Pointer to the destination tensor. Supported data types: same as @p input_ptr
65 * @param[in] output_stride_x Stride of the destination tensor in X dimension (in bytes)
66 * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes)
67 * @param[in] output_stride_y Stride of the destination tensor in Y dimension (in bytes)
68 * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes)
69 * @param[in] output_stride_z Stride of the destination tensor in Z dimension (in bytes)
70 * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes)
71 * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination tensor
72 * @param[in] mean_ptr Pointer to the mean source tensor. Supported data types: same as @p input_ptr
73 * @param[in] mean_stride_x Stride of the mean source tensor in X dimension (in bytes)
74 * @param[in] mean_step_x mean_stride_x * number of elements along X processed per workitem(in bytes)
75 * @param[in] mean_offset_first_element_in_bytes The offset of the first element in the mean source tensor
76 * @param[in] var_ptr Pointer to the var tensor. Supported data types: same as @p input_ptr
77 * @param[in] var_stride_x Stride of the var tensor in X dimension (in bytes)
78 * @param[in] var_step_x var_stride_x * number of elements along X processed per workitem(in bytes)
79 * @param[in] var_offset_first_element_in_bytes The offset of the first element in the var source tensor
80 * @param[in] beta_ptr Pointer to the beta source tensor. Supported data types: same as @p input_ptr
81 * @param[in] beta_stride_x Stride of the beta source tensor in X dimension (in bytes)
82 * @param[in] beta_step_x beta_stride_x * number of elements along X processed per workitem(in bytes)
83 * @param[in] beta_offset_first_element_in_bytes The offset of the first element in the beta source tensor
84 * @param[in] gamma_ptr Pointer to the gamma source tensor. Supported data types: same as @p input_ptr
85 * @param[in] gamma_stride_x Stride of the gamma source tensor in X dimension (in bytes)
86 * @param[in] gamma_step_x gamma_stride_x * number of elements along X processed per workitem(in bytes)
87 * @param[in] gamma_offset_first_element_in_bytes The offset of the first element in the gamma source tensor
88 * @param[in] epsilon Epsilon parameter in the batch normalization equation
90 __kernel void batchnormalization_layer_nchw(TENSOR3D_DECLARATION(input),
92 TENSOR3D_DECLARATION(output),
93 #endif /* not IN_PLACE */
94 VECTOR_DECLARATION(mean),
95 VECTOR_DECLARATION(var),
96 #ifndef USE_DEFAULT_BETA
97 VECTOR_DECLARATION(beta),
98 #endif /* USE_DEFAULT_BETA */
99 #ifndef USE_DEFAULT_GAMMA
100 VECTOR_DECLARATION(gamma),
101 #endif /* USE_DEFAULT_GAMMA */
104 Tensor3D in = CONVERT_TO_TENSOR3D_STRUCT(input);
108 Tensor3D out = CONVERT_TO_TENSOR3D_STRUCT(output);
109 #endif /* IN_PLACE */
110 Vector mean = CONVERT_TO_VECTOR_STRUCT(mean);
111 Vector var = CONVERT_TO_VECTOR_STRUCT(var);
112 #ifndef USE_DEFAULT_BETA
113 Vector beta = CONVERT_TO_VECTOR_STRUCT(beta);
114 #endif /* USE_DEFAULT_BETA */
115 #ifndef USE_DEFAULT_GAMMA
116 Vector gamma = CONVERT_TO_VECTOR_STRUCT(gamma);
117 #endif /* USE_DEFAULT_GAMMA */
119 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
121 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
123 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
125 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
127 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
130 const int current_slice = get_global_id(2);
132 data = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)in.ptr);
133 denominator = *((__global DATA_TYPE *)(var.ptr + current_slice * var.stride_x));
134 denominator = INVSQRT_OP(ADD_OP(denominator, ((VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE))SQCVT_SAT(epsilon))));
136 // Calculate x bar and store results
137 numerator = *((__global DATA_TYPE *)(mean.ptr + current_slice * mean.stride_x));
138 numerator = SUB_OP(data, numerator);
139 x_bar = MUL_OP(numerator, denominator);
141 #ifndef USE_DEFAULT_GAMMA
142 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
143 gamma_vec = *((__global DATA_TYPE *)(gamma.ptr + current_slice * gamma.stride_x));
145 res = MUL_OP(gamma_vec, x_bar);
146 #else /* USE_DEFAULT_GAMMA */
147 // gamma is equal to 1, no need to perform multiplications
149 #endif /* USE_DEFAULT_GAMMA */
151 #ifndef USE_DEFAULT_BETA
152 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
153 beta_vec = *((__global DATA_TYPE *)(beta.ptr + current_slice * beta.stride_x));
154 // beta is not zero, hence we need to perform the addition
155 res = ADD_OP(res, beta_vec);
156 #endif /* USE_DEFAULT_BETA */
158 res = ACTIVATION_FUNC(res);
161 (res, 0, (__global DATA_TYPE *)out.ptr);
164 /** Apply batch normalization on tensors with NHWC format.
166 * @param[in] input_ptr Pointer to the first source tensor. Supported data types: QS8/QS16/F16/F32
167 * @param[in] input_stride_x Stride of the first source tensor in X dimension (in bytes)
168 * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes)
169 * @param[in] input_stride_y Stride of the first source tensor in Y dimension (in bytes)
170 * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes)
171 * @param[in] input_stride_z Stride of the first source tensor in Z dimension (in bytes)
172 * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes)
173 * @param[in] input_offset_first_element_in_bytes The offset of the first element in the first source tensor
174 * @param[out] output_ptr Pointer to the destination tensor. Supported data types: same as @p input_ptr
175 * @param[in] output_stride_x Stride of the destination tensor in X dimension (in bytes)
176 * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes)
177 * @param[in] output_stride_y Stride of the destination tensor in Y dimension (in bytes)
178 * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes)
179 * @param[in] output_stride_z Stride of the destination tensor in Z dimension (in bytes)
180 * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes)
181 * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination tensor
182 * @param[in] mean_ptr Pointer to the mean source tensor. Supported data types: same as @p input_ptr
183 * @param[in] mean_stride_x Stride of the mean source tensor in X dimension (in bytes)
184 * @param[in] mean_step_x mean_stride_x * number of elements along X processed per workitem(in bytes)
185 * @param[in] mean_offset_first_element_in_bytes The offset of the first element in the mean source tensor
186 * @param[in] var_ptr Pointer to the var tensor. Supported data types: same as @p input_ptr
187 * @param[in] var_stride_x Stride of the var tensor in X dimension (in bytes)
188 * @param[in] var_step_x var_stride_x * number of elements along X processed per workitem(in bytes)
189 * @param[in] var_offset_first_element_in_bytes The offset of the first element in the var source tensor
190 * @param[in] beta_ptr Pointer to the beta source tensor. Supported data types: same as @p input_ptr
191 * @param[in] beta_stride_x Stride of the beta source tensor in X dimension (in bytes)
192 * @param[in] beta_step_x beta_stride_x * number of elements along X processed per workitem(in bytes)
193 * @param[in] beta_offset_first_element_in_bytes The offset of the first element in the beta source tensor
194 * @param[in] gamma_ptr Pointer to the gamma source tensor. Supported data types: same as @p input_ptr
195 * @param[in] gamma_stride_x Stride of the gamma source tensor in X dimension (in bytes)
196 * @param[in] gamma_step_x gamma_stride_x * number of elements along X processed per workitem(in bytes)
197 * @param[in] gamma_offset_first_element_in_bytes The offset of the first element in the gamma source tensor
198 * @param[in] epsilon Epsilon parameter in the batch normalization equation
200 __kernel void batchnormalization_layer_nhwc(TENSOR3D_DECLARATION(input),
202 TENSOR3D_DECLARATION(output),
203 #endif /* not IN_PLACE */
204 VECTOR_DECLARATION(mean),
205 VECTOR_DECLARATION(var),
206 #ifndef USE_DEFAULT_BETA
207 VECTOR_DECLARATION(beta),
208 #endif /* USE_DEFAULT_BETA */
209 #ifndef USE_DEFAULT_GAMMA
210 VECTOR_DECLARATION(gamma),
211 #endif /* USE_DEFAULT_GAMMA */
214 Tensor3D in = CONVERT_TO_TENSOR3D_STRUCT(input);
218 Tensor3D out = CONVERT_TO_TENSOR3D_STRUCT(output);
219 #endif /* IN_PLACE */
220 Vector mean = CONVERT_TO_VECTOR_STRUCT(mean);
221 Vector var = CONVERT_TO_VECTOR_STRUCT(var);
222 #ifndef USE_DEFAULT_BETA
223 Vector beta = CONVERT_TO_VECTOR_STRUCT(beta);
224 #endif /* USE_DEFAULT_BETA */
225 #ifndef USE_DEFAULT_GAMMA
226 Vector gamma = CONVERT_TO_VECTOR_STRUCT(gamma);
227 #endif /* USE_DEFAULT_GAMMA */
229 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
231 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
233 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
235 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
237 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
240 const int current_slice = get_global_id(0);
242 data = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)in.ptr);
243 denominator = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(var.ptr + current_slice * VEC_SIZE * var.stride_x));
244 denominator = INVSQRT_OP(ADD_OP(denominator, ((VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE))SQCVT_SAT(epsilon))));
246 // Calculate x bar and store results
247 numerator = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(mean.ptr + current_slice * VEC_SIZE * mean.stride_x));
248 numerator = SUB_OP(data, numerator);
249 x_bar = MUL_OP(numerator, denominator);
251 #ifndef USE_DEFAULT_GAMMA
252 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
253 gamma_vec = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(gamma.ptr + current_slice * VEC_SIZE * gamma.stride_x));
255 res = MUL_OP(gamma_vec, x_bar);
256 #else /* USE_DEFAULT_GAMMA */
257 // gamma is equal to 1, no need to perform multiplications
259 #endif /* USE_DEFAULT_GAMMA */
261 #ifndef USE_DEFAULT_BETA
262 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
263 beta_vec = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(beta.ptr + current_slice * VEC_SIZE * beta.stride_x));
264 // beta is not zero, hence we need to perform the addition
265 res = ADD_OP(res, beta_vec);
266 #endif /* USE_DEFAULT_BETA */
268 res = ACTIVATION_FUNC(res);
271 (res, 0, (__global DATA_TYPE *)out.ptr);
273 #endif /* defined(VEC_SIZE) && defined(DATA_TYPE) */