Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
giuros01 | acce504 | 2019-02-21 17:32:34 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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 | |
Michalis Spyrou | 172e570 | 2017-06-26 14:18:47 +0100 | [diff] [blame] | 26 | #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 Arif | 6a98a6e | 2019-05-10 17:07:27 +0100 | [diff] [blame] | 32 | #if defined(VEC_SIZE) && defined(DATA_TYPE) && defined(ACTIVATION_TYPE) |
| 33 | #include "activation_float_helpers.h" |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 34 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 35 | /** Apply batch normalization. |
| 36 | * |
Usama Arif | 6a98a6e | 2019-05-10 17:07:27 +0100 | [diff] [blame] | 37 | * @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 Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 40 | * @param[in] input_ptr Pointer to the first source tensor. Supported data types: F16/F32 |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 41 | * @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 Spyrou | 172e570 | 2017-06-26 14:18:47 +0100 | [diff] [blame] | 48 | * @param[out] output_ptr Pointer to the destination tensor. Supported data types: same as @p input_ptr |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 49 | * @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 Spyrou | 172e570 | 2017-06-26 14:18:47 +0100 | [diff] [blame] | 56 | * @param[in] mean_ptr Pointer to the mean source tensor. Supported data types: same as @p input_ptr |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 57 | * @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 Spyrou | 172e570 | 2017-06-26 14:18:47 +0100 | [diff] [blame] | 60 | * @param[in] var_ptr Pointer to the var tensor. Supported data types: same as @p input_ptr |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 61 | * @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 Spyrou | 172e570 | 2017-06-26 14:18:47 +0100 | [diff] [blame] | 64 | * @param[in] beta_ptr Pointer to the beta source tensor. Supported data types: same as @p input_ptr |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 65 | * @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 Spyrou | 172e570 | 2017-06-26 14:18:47 +0100 | [diff] [blame] | 68 | * @param[in] gamma_ptr Pointer to the gamma source tensor. Supported data types: same as @p input_ptr |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 69 | * @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 Giorgio | bf3c662 | 2018-03-08 11:52:27 +0000 | [diff] [blame] | 74 | __kernel void batchnormalization_layer_nchw(TENSOR3D_DECLARATION(input), |
Georgios Pinitas | 409ee0a | 2017-08-18 10:16:09 +0100 | [diff] [blame] | 75 | #ifndef IN_PLACE |
Michele Di Giorgio | bf3c662 | 2018-03-08 11:52:27 +0000 | [diff] [blame] | 76 | TENSOR3D_DECLARATION(output), |
Georgios Pinitas | 409ee0a | 2017-08-18 10:16:09 +0100 | [diff] [blame] | 77 | #endif /* not IN_PLACE */ |
Michele Di Giorgio | bf3c662 | 2018-03-08 11:52:27 +0000 | [diff] [blame] | 78 | VECTOR_DECLARATION(mean), |
| 79 | VECTOR_DECLARATION(var), |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 80 | #ifndef USE_DEFAULT_BETA |
Michele Di Giorgio | bf3c662 | 2018-03-08 11:52:27 +0000 | [diff] [blame] | 81 | VECTOR_DECLARATION(beta), |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 82 | #endif /* USE_DEFAULT_BETA */ |
| 83 | #ifndef USE_DEFAULT_GAMMA |
Michele Di Giorgio | bf3c662 | 2018-03-08 11:52:27 +0000 | [diff] [blame] | 84 | VECTOR_DECLARATION(gamma), |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 85 | #endif /* USE_DEFAULT_GAMMA */ |
Michele Di Giorgio | bf3c662 | 2018-03-08 11:52:27 +0000 | [diff] [blame] | 86 | float epsilon) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 87 | { |
Georgios Pinitas | 409ee0a | 2017-08-18 10:16:09 +0100 | [diff] [blame] | 88 | 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 Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 94 | 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 Pinitas | 409ee0a | 2017-08-18 10:16:09 +0100 | [diff] [blame] | 100 | Vector gamma = CONVERT_TO_VECTOR_STRUCT(gamma); |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 101 | #endif /* USE_DEFAULT_GAMMA */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 102 | |
Michalis Spyrou | 172e570 | 2017-06-26 14:18:47 +0100 | [diff] [blame] | 103 | VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) |
Gian Marco Iodice | 349feef | 2017-09-28 11:21:29 +0100 | [diff] [blame] | 104 | data = 0; |
Michalis Spyrou | 172e570 | 2017-06-26 14:18:47 +0100 | [diff] [blame] | 105 | 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 Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 112 | res = 0; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 113 | |
| 114 | const int current_slice = get_global_id(2); |
| 115 | |
Gian Marco Iodice | 349feef | 2017-09-28 11:21:29 +0100 | [diff] [blame] | 116 | data = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)in.ptr); |
Michalis Spyrou | 172e570 | 2017-06-26 14:18:47 +0100 | [diff] [blame] | 117 | denominator = *((__global DATA_TYPE *)(var.ptr + current_slice * var.stride_x)); |
Gian Marco Iodice | 349feef | 2017-09-28 11:21:29 +0100 | [diff] [blame] | 118 | denominator = INVSQRT_OP(ADD_OP(denominator, ((VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE))SQCVT_SAT(epsilon)))); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 119 | |
| 120 | // Calculate x bar and store results |
Michalis Spyrou | 172e570 | 2017-06-26 14:18:47 +0100 | [diff] [blame] | 121 | numerator = *((__global DATA_TYPE *)(mean.ptr + current_slice * mean.stride_x)); |
Gian Marco Iodice | 349feef | 2017-09-28 11:21:29 +0100 | [diff] [blame] | 122 | numerator = SUB_OP(data, numerator); |
Michalis Spyrou | 172e570 | 2017-06-26 14:18:47 +0100 | [diff] [blame] | 123 | x_bar = MUL_OP(numerator, denominator); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 124 | |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 125 | #ifndef USE_DEFAULT_GAMMA |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 126 | VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 127 | 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 |
Michele Di Giorgio | bf3c662 | 2018-03-08 11:52:27 +0000 | [diff] [blame] | 132 | res = x_bar; |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 133 | #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 Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 141 | |
Usama Arif | 6a98a6e | 2019-05-10 17:07:27 +0100 | [diff] [blame] | 142 | res = ACTIVATION(ACTIVATION_TYPE, DATA_TYPE, res, A_VAL, B_VAL); |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 143 | |
Michalis Spyrou | 172e570 | 2017-06-26 14:18:47 +0100 | [diff] [blame] | 144 | VSTORE(VEC_SIZE) |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 145 | (res, 0, (__global DATA_TYPE *)out.ptr); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 146 | } |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 147 | |
Michele Di Giorgio | bf3c662 | 2018-03-08 11:52:27 +0000 | [diff] [blame] | 148 | /** Apply batch normalization on tensors with NHWC format. |
| 149 | * |
Usama Arif | 6a98a6e | 2019-05-10 17:07:27 +0100 | [diff] [blame] | 150 | * @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 Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 153 | * @param[in] input_ptr Pointer to the first source tensor. Supported data types: F16/F32 |
Michele Di Giorgio | bf3c662 | 2018-03-08 11:52:27 +0000 | [diff] [blame] | 154 | * @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 | { |
| 201 | Tensor3D in = CONVERT_TO_TENSOR3D_STRUCT(input); |
| 202 | #ifdef IN_PLACE |
| 203 | Tensor3D out = in; |
| 204 | #else /* IN_PLACE */ |
| 205 | Tensor3D out = CONVERT_TO_TENSOR3D_STRUCT(output); |
| 206 | #endif /* IN_PLACE */ |
| 207 | Vector mean = CONVERT_TO_VECTOR_STRUCT(mean); |
| 208 | Vector var = CONVERT_TO_VECTOR_STRUCT(var); |
| 209 | #ifndef USE_DEFAULT_BETA |
| 210 | Vector beta = CONVERT_TO_VECTOR_STRUCT(beta); |
| 211 | #endif /* USE_DEFAULT_BETA */ |
| 212 | #ifndef USE_DEFAULT_GAMMA |
| 213 | Vector gamma = CONVERT_TO_VECTOR_STRUCT(gamma); |
| 214 | #endif /* USE_DEFAULT_GAMMA */ |
| 215 | |
| 216 | VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) |
| 217 | data = 0; |
| 218 | VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) |
| 219 | denominator = 0; |
| 220 | VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) |
| 221 | numerator = 0; |
| 222 | VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) |
| 223 | x_bar = 0; |
| 224 | VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) |
| 225 | res = 0; |
| 226 | |
| 227 | const int current_slice = get_global_id(0); |
| 228 | |
| 229 | data = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)in.ptr); |
| 230 | denominator = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(var.ptr + current_slice * VEC_SIZE * var.stride_x)); |
| 231 | denominator = INVSQRT_OP(ADD_OP(denominator, ((VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE))SQCVT_SAT(epsilon)))); |
| 232 | |
| 233 | // Calculate x bar and store results |
| 234 | numerator = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(mean.ptr + current_slice * VEC_SIZE * mean.stride_x)); |
| 235 | 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) |
| 240 | gamma_vec = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(gamma.ptr + current_slice * VEC_SIZE * gamma.stride_x)); |
| 241 | |
| 242 | res = MUL_OP(gamma_vec, x_bar); |
| 243 | #else /* USE_DEFAULT_GAMMA */ |
| 244 | // gamma is equal to 1, no need to perform multiplications |
| 245 | res = x_bar; |
| 246 | #endif /* USE_DEFAULT_GAMMA */ |
| 247 | |
| 248 | #ifndef USE_DEFAULT_BETA |
| 249 | VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) |
| 250 | beta_vec = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(beta.ptr + current_slice * VEC_SIZE * beta.stride_x)); |
| 251 | // beta is not zero, hence we need to perform the addition |
| 252 | res = ADD_OP(res, beta_vec); |
| 253 | #endif /* USE_DEFAULT_BETA */ |
| 254 | |
Usama Arif | 6a98a6e | 2019-05-10 17:07:27 +0100 | [diff] [blame] | 255 | res = ACTIVATION(ACTIVATION_TYPE, DATA_TYPE, res, A_VAL, B_VAL); |
Michele Di Giorgio | bf3c662 | 2018-03-08 11:52:27 +0000 | [diff] [blame] | 256 | |
| 257 | VSTORE(VEC_SIZE) |
| 258 | (res, 0, (__global DATA_TYPE *)out.ptr); |
| 259 | } |
Usama Arif | 6a98a6e | 2019-05-10 17:07:27 +0100 | [diff] [blame] | 260 | #endif /* defined(VEC_SIZE) && defined(DATA_TYPE) && defined(DATA_TYPE)*/ |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 261 | |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 262 | #if defined(DATA_TYPE) && defined(EPSILON) |
| 263 | /** 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 Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 264 | * |
Gian Marco Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 265 | * @note The input weights tensor is assumed 4D with the OFMs in the fourth dimension |
| 266 | * @note Data type should be passed at compile time using the -DDATA_TYPE, e.g. -DDATA_TYPE=float |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 267 | * @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. |
| 268 | * For depthwise convolution weight do not pass DIM2 |
| 269 | * @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 Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 270 | * @note Batch normalization epsilon parameter should be passed at compile time using -DEPSILON=value. e.g. -DEPSILON=0.001f |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 271 | * |
Gian Marco Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 272 | * @param[in] w_ptr Pointer to the weights tensor. Supported data types: F16/F32 |
| 273 | * @param[in] w_stride_x Stride of the weights tensor in X dimension (in bytes) |
| 274 | * @param[in] w_step_x w_stride_x * number of elements along X processed per workitem(in bytes) |
| 275 | * @param[in] w_stride_y Stride of the weights tensor in Y dimension (in bytes) |
| 276 | * @param[in] w_step_y w_stride_y * number of elements along Y processed per workitem(in bytes) |
| 277 | * @param[in] w_stride_z Stride of the weights tensor in Z dimension (in bytes) |
| 278 | * @param[in] w_step_z w_stride_z * number of elements along Z processed per workitem(in bytes) |
| 279 | * @param[in] w_offset_first_element_in_bytes The offset of the first element in the weights tensor |
| 280 | * @param[in] b_ptr (Optional) Pointer to the bias tensor. Supported data types: same as @p w_ptr |
| 281 | * @param[in] b_stride_x (Optional) Stride of the bias tensor in X dimension (in bytes) |
| 282 | * @param[in] b_step_x (Optional) b_stride_x * number of elements along X processed per workitem(in bytes) |
| 283 | * @param[in] b_stride_y (Optional) Stride of the bias tensor in Y dimension (in bytes) |
| 284 | * @param[in] b_step_y (Optional) b_stride_y * number of elements along Y processed per workitem(in bytes) |
| 285 | * @param[in] b_stride_z (Optional) Stride of the bias tensor in Z dimension (in bytes) |
| 286 | * @param[in] b_step_z (Optional) b_stride_z * number of elements along Z processed per workitem(in bytes) |
| 287 | * @param[in] b_offset_first_element_in_bytes (Optional) The offset of the first element in the bias tensor |
| 288 | * @param[in] mean_ptr Pointer to the mean source tensor. Supported data types: same as @p w_ptr |
| 289 | * @param[in] mean_stride_x Stride of the mean source tensor in X dimension (in bytes) |
| 290 | * @param[in] mean_step_x mean_stride_x * number of elements along X processed per workitem(in bytes) |
| 291 | * @param[in] mean_offset_first_element_in_bytes The offset of the first element in the mean source tensor |
| 292 | * @param[in] var_ptr Pointer to the var tensor. Supported data types: same as @p w_ptr |
| 293 | * @param[in] var_stride_x Stride of the var tensor in X dimension (in bytes) |
| 294 | * @param[in] var_step_x var_stride_x * number of elements along X processed per workitem(in bytes) |
| 295 | * @param[in] var_offset_first_element_in_bytes The offset of the first element in the var source tensor |
| 296 | * @param[out] w_fused_ptr (Optional) Pointer to the destination weights tensors. Supported data types: same as @p w_ptr |
| 297 | * @param[in] w_fused_stride_x (Optional) Stride of the destination weights tensor in X dimension (in bytes) |
| 298 | * @param[in] w_fused_step_x (Optional) w_fused_stride_x * number of elements along X processed per workitem(in bytes) |
| 299 | * @param[in] w_fused_stride_y (Optional) Stride of the destination weights tensor in Y dimension (in bytes) |
| 300 | * @param[in] w_fused_step_y (Optional) w_fused_stride_y * number of elements along Y processed per workitem(in bytes) |
| 301 | * @param[in] w_fused_stride_z (Optional) Stride of the destination weights tensor in Z dimension (in bytes) |
| 302 | * @param[in] w_fused_step_z (Optional) w_fused_stride_z * number of elements along Z processed per workitem(in bytes) |
| 303 | * @param[in] w_fused_offset_first_element_in_bytes (Optional) The offset of the first element in the destination weights tensor |
| 304 | * @param[in] b_fused_ptr (Optional) Pointer to the destination bias tensor. Supported data types: same as @p w_ptr |
| 305 | * @param[in] b_fused_stride_x (Optional) Stride of the destination bias tensor in X dimension (in bytes) |
| 306 | * @param[in] b_fused_step_x (Optional) b_fused_stride_x * number of elements along X processed per workitem(in bytes) |
| 307 | * @param[in] b_fused_offset_first_element_in_bytes (Optional) The offset of the first element in the destination bias tensor |
| 308 | * @param[in] beta_ptr (Optional) Pointer to the beta source tensor. Supported data types: same as @p w_ptr |
| 309 | * @param[in] beta_stride_x (Optional) Stride of the beta source tensor in X dimension (in bytes) |
| 310 | * @param[in] beta_step_x (Optional) beta_stride_x * number of elements along X processed per workitem(in bytes) |
| 311 | * @param[in] beta_offset_first_element_in_bytes (Optional) The offset of the first element in the beta source tensor |
| 312 | * @param[in] gamma_ptr (Optional) Pointer to the gamma source tensor. Supported data types: same as @p w_ptr |
| 313 | * @param[in] gamma_stride_x (Optional) Stride of the gamma source tensor in X dimension (in bytes) |
| 314 | * @param[in] gamma_step_x (Optional) gamma_stride_x * number of elements along X processed per workitem(in bytes) |
| 315 | * @param[in] gamma_offset_first_element_in_bytes (Optional) The offset of the first element in the gamma source tensor |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 316 | */ |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 317 | __kernel void fuse_batchnormalization_layer(TENSOR3D_DECLARATION(w), |
Gian Marco Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 318 | #if defined(BIAS) |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 319 | VECTOR_DECLARATION(b), |
Gian Marco Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 320 | #endif // defined(BIAS) |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 321 | VECTOR_DECLARATION(mean), |
| 322 | VECTOR_DECLARATION(var) |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 323 | #ifndef IN_PLACE_W |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 324 | , |
| 325 | TENSOR3D_DECLARATION(w_fused) |
Gian Marco Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 326 | #endif // ifndef IN_PLACE_W |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 327 | #ifndef IN_PLACE_B |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 328 | , |
| 329 | VECTOR_DECLARATION(b_fused) |
Gian Marco Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 330 | #endif // ifndef IN_PLACE_B |
| 331 | #if defined(BETA) |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 332 | , |
| 333 | VECTOR_DECLARATION(beta) |
Gian Marco Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 334 | #endif // defined(BETA) |
| 335 | #if defined(GAMMA) |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 336 | , |
| 337 | VECTOR_DECLARATION(gamma) |
Gian Marco Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 338 | #endif // defined(GAMMA) |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 339 | ) |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 340 | { |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 341 | int x = get_global_id(0); |
| 342 | int y = get_global_id(1); |
| 343 | int z = get_global_id(2); |
| 344 | |
| 345 | #if defined(DIM2) |
Gian Marco Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 346 | int c0 = z % DIM2; |
| 347 | int c1 = z / DIM2; |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 348 | #else // ! defined(DIM2) |
| 349 | int c0 = 0; |
| 350 | #if defined(NHWC) |
| 351 | int c1 = x; |
| 352 | #else // defined(NHWC) |
| 353 | int c1 = z; |
| 354 | #endif // defined(NHWC) |
| 355 | #endif // defined(DIM2) |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 356 | |
Gian Marco Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 357 | int w_offset = x * sizeof(DATA_TYPE) + y * w_stride_y + z * w_stride_z; |
| 358 | int v_offset = c1 * sizeof(DATA_TYPE); |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 359 | |
Gian Marco Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 360 | DATA_TYPE w_old = 0.0f; |
| 361 | DATA_TYPE b_old = 0.0f; |
| 362 | DATA_TYPE w_new = 0.0f; |
| 363 | DATA_TYPE b_new = 0.0f; |
| 364 | DATA_TYPE gamma = 1.0f; |
| 365 | DATA_TYPE mean = 0.0f; |
| 366 | DATA_TYPE var = 1.0f; |
| 367 | DATA_TYPE beta = 0.0f; |
giuros01 | acce504 | 2019-02-21 17:32:34 +0000 | [diff] [blame] | 368 | |
Gian Marco Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 369 | w_old = *((__global DATA_TYPE *)(w_ptr + w_offset + w_offset_first_element_in_bytes)); |
| 370 | var = *((__global DATA_TYPE *)(var_ptr + v_offset + var_offset_first_element_in_bytes)); |
| 371 | mean = *((__global DATA_TYPE *)(mean_ptr + v_offset + mean_offset_first_element_in_bytes)); |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 372 | |
Gian Marco Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 373 | #if defined(GAMMA) |
| 374 | gamma = *((__global DATA_TYPE *)(gamma_ptr + v_offset + gamma_offset_first_element_in_bytes)); |
| 375 | #endif // defined(GAMMA) |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 376 | |
Gian Marco Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 377 | // Compute new weight |
| 378 | w_new = (gamma * w_old) / (sqrt(var + EPSILON)); |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 379 | |
Gian Marco Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 380 | #if defined(IN_PLACE_W) |
| 381 | *((__global DATA_TYPE *)(w_ptr + w_offset + w_offset_first_element_in_bytes)) = w_new; |
| 382 | #else // defined(IN_PLACE_W) |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 383 | *((__global DATA_TYPE *)(w_fused_ptr + w_offset + w_fused_offset_first_element_in_bytes)) = w_new; |
Gian Marco Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 384 | #endif // defined(IN_PLACE_W) |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 385 | |
Gian Marco Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 386 | // Compute bias |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 387 | #if !defined(DIM2) && defined(NHWC) |
| 388 | if(z == 0 && y == 0) |
| 389 | #else !defined(DIM2) && defined(NHWC) |
Gian Marco Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 390 | if(x == 0 && y == 0 && c0 == 0) |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 391 | #endif // !defined(DIM2) && defined(NHWC) |
Gian Marco Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 392 | { |
| 393 | #if defined(BIAS) |
| 394 | b_old = *((__global DATA_TYPE *)(b_ptr + v_offset + b_offset_first_element_in_bytes)); |
| 395 | #endif // defined(BIAS) |
| 396 | #if defined(BETA) |
| 397 | beta = *((__global DATA_TYPE *)(beta_ptr + v_offset + beta_offset_first_element_in_bytes)); |
| 398 | #endif // defined(BETA) |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 399 | |
Gian Marco Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 400 | b_new = ((gamma * (b_old - mean)) / (sqrt(var + EPSILON))) + beta; |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 401 | |
Gian Marco Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 402 | #if defined(BIAS) |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 403 | |
Gian Marco Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 404 | #if defined(IN_PLACE_B) |
| 405 | *((__global DATA_TYPE *)(b_ptr + v_offset + b_offset_first_element_in_bytes)) = b_new; |
| 406 | #else // defined(IN_PLACE_B) |
| 407 | *((__global DATA_TYPE *)(b_fused_ptr + v_offset + b_fused_offset_first_element_in_bytes)) = b_new; |
| 408 | #endif // defined(IN_PLACE_B) |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 409 | |
Gian Marco Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 410 | #else // defined(BIAS) |
| 411 | |
| 412 | #ifndef IN_PLACE_B |
| 413 | *((__global DATA_TYPE *)(b_fused_ptr + v_offset + b_fused_offset_first_element_in_bytes)) = b_new; |
| 414 | #endif // ifndef IN_PLACE_B |
| 415 | |
| 416 | #endif // defined(BIAS) |
| 417 | } |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 418 | } |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 419 | #endif // defined(DATA_TYPE) && defined(EPSILON) |