arm_compute v18.02
[platform/upstream/armcl.git] / src / core / CL / cl_kernels / batchnormalization_layer.cl
1 /*
2  * Copyright (c) 2017-2018 ARM Limited.
3  *
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
26 #if defined(VEC_SIZE) && defined(DATA_TYPE)
27
28 #if defined(FIXED_POINT_POSITION)
29 #include "fixed_point.h"
30
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)
36
37 #else /* FIXED_POINT_POSITION */
38
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)
44
45 #endif /* FIXED_POINT_POSITION */
46
47 #if defined(LU_BRELU)
48 #define ACTIVATION_FUNC(x) CLAMP(x, (DATA_TYPE)B_VAL, (DATA_TYPE)A_VAL)
49 #elif defined(BRELU)
50 #define ACTIVATION_FUNC(x) CLAMP(x, (DATA_TYPE)0, (DATA_TYPE)A_VAL)
51 #elif defined(RELU)
52 #define ACTIVATION_FUNC(x) max(x, (DATA_TYPE)0)
53 #else /* FUSED_ACT */
54 #define ACTIVATION_FUNC(x) (x)
55 #endif /* FUSED_ACT */
56
57 /** Apply batch normalization.
58  *
59  * @param[in]  input_ptr                            Pointer to the first source tensor. Supported data types: QS8/QS16/F16/F32
60  * @param[in]  input_stride_x                       Stride of the first source tensor in X dimension (in bytes)
61  * @param[in]  input_step_x                         input_stride_x * number of elements along X processed per workitem(in bytes)
62  * @param[in]  input_stride_y                       Stride of the first source tensor in Y dimension (in bytes)
63  * @param[in]  input_step_y                         input_stride_y * number of elements along Y processed per workitem(in bytes)
64  * @param[in]  input_stride_z                       Stride of the first source tensor in Z dimension (in bytes)
65  * @param[in]  input_step_z                         input_stride_z * number of elements along Z processed per workitem(in bytes)
66  * @param[in]  input_offset_first_element_in_bytes  The offset of the first element in the first source tensor
67  * @param[out] output_ptr                           Pointer to the destination tensor. Supported data types: same as @p input_ptr
68  * @param[in]  output_stride_x                      Stride of the destination tensor in X dimension (in bytes)
69  * @param[in]  output_step_x                        output_stride_x * number of elements along X processed per workitem(in bytes)
70  * @param[in]  output_stride_y                      Stride of the destination tensor in Y dimension (in bytes)
71  * @param[in]  output_step_y                        output_stride_y * number of elements along Y processed per workitem(in bytes)
72  * @param[in]  output_stride_z                      Stride of the destination tensor in Z dimension (in bytes)
73  * @param[in]  output_step_z                        output_stride_z * number of elements along Z processed per workitem(in bytes)
74  * @param[in]  output_offset_first_element_in_bytes The offset of the first element in the destination tensor
75  * @param[in]  mean_ptr                             Pointer to the mean source tensor. Supported data types: same as @p input_ptr
76  * @param[in]  mean_stride_x                        Stride of the mean source tensor in X dimension (in bytes)
77  * @param[in]  mean_step_x                          mean_stride_x * number of elements along X processed per workitem(in bytes)
78  * @param[in]  mean_offset_first_element_in_bytes   The offset of the first element in the mean source tensor
79  * @param[in]  var_ptr                              Pointer to the var tensor. Supported data types: same as @p input_ptr
80  * @param[in]  var_stride_x                         Stride of the var tensor in X dimension (in bytes)
81  * @param[in]  var_step_x                           var_stride_x * number of elements along X processed per workitem(in bytes)
82  * @param[in]  var_offset_first_element_in_bytes    The offset of the first element in the var source tensor
83  * @param[in]  beta_ptr                             Pointer to the beta source tensor. Supported data types: same as @p input_ptr
84  * @param[in]  beta_stride_x                        Stride of the beta source tensor in X dimension (in bytes)
85  * @param[in]  beta_step_x                          beta_stride_x * number of elements along X processed per workitem(in bytes)
86  * @param[in]  beta_offset_first_element_in_bytes   The offset of the first element in the beta source tensor
87  * @param[in]  gamma_ptr                            Pointer to the gamma source tensor. Supported data types: same as @p input_ptr
88  * @param[in]  gamma_stride_x                       Stride of the gamma source tensor in X dimension (in bytes)
89  * @param[in]  gamma_step_x                         gamma_stride_x * number of elements along X processed per workitem(in bytes)
90  * @param[in]  gamma_offset_first_element_in_bytes  The offset of the first element in the gamma source tensor
91  * @param[in]  epsilon                              Epsilon parameter in the batch normalization equation
92  */
93 __kernel void batchnormalization_layer(TENSOR3D_DECLARATION(input),
94 #ifndef IN_PLACE
95                                        TENSOR3D_DECLARATION(output),
96 #endif /* not IN_PLACE */
97                                        VECTOR_DECLARATION(mean),
98                                        VECTOR_DECLARATION(var),
99                                        VECTOR_DECLARATION(beta),
100                                        VECTOR_DECLARATION(gamma),
101                                        float epsilon)
102 {
103     Tensor3D in = CONVERT_TO_TENSOR3D_STRUCT(input);
104 #ifdef IN_PLACE
105     Tensor3D out = in;
106 #else  /* IN_PLACE */
107     Tensor3D out = CONVERT_TO_TENSOR3D_STRUCT(output);
108 #endif /* IN_PLACE */
109     Vector mean  = CONVERT_TO_VECTOR_STRUCT(mean);
110     Vector var   = CONVERT_TO_VECTOR_STRUCT(var);
111     Vector beta  = CONVERT_TO_VECTOR_STRUCT(beta);
112     Vector gamma = CONVERT_TO_VECTOR_STRUCT(gamma);
113
114     VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
115     data = 0;
116     VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
117     denominator = 0;
118     VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
119     numerator = 0;
120     VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
121     x_bar = 0;
122     VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
123     gamma_vec = 0;
124     VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
125     beta_vec = 0;
126
127     const int current_slice = get_global_id(2);
128
129     data        = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)in.ptr);
130     denominator = *((__global DATA_TYPE *)(var.ptr + current_slice * var.stride_x));
131     denominator = INVSQRT_OP(ADD_OP(denominator, ((VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE))SQCVT_SAT(epsilon))));
132
133     // Calculate x bar and store results
134     numerator = *((__global DATA_TYPE *)(mean.ptr + current_slice * mean.stride_x));
135     numerator = SUB_OP(data, numerator);
136     x_bar     = MUL_OP(numerator, denominator);
137
138     gamma_vec = *((__global DATA_TYPE *)(gamma.ptr + current_slice * gamma.stride_x));
139     beta_vec  = *((__global DATA_TYPE *)(beta.ptr + current_slice * beta.stride_x));
140
141     VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
142     res = ADD_OP(MUL_OP(gamma_vec, x_bar), beta_vec);
143
144     res = ACTIVATION_FUNC(res);
145
146     VSTORE(VEC_SIZE)
147     (res, 0, (__global DATA_TYPE *)out.ptr);
148 }
149
150 #endif /* defined(VEC_SIZE) && defined(DATA_TYPE) */