Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Michalis Spyrou | ef6ec50 | 2020-07-31 11:38:36 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2020 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 |
Sheri Zhang | 141c31a | 2020-10-08 12:35:28 +0100 | [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 | |
Giorgio Arena | d056e57 | 2020-10-12 11:53:51 +0100 | [diff] [blame] | 142 | res = ACTIVATION(ACTIVATION_TYPE, DATA_TYPE, VEC_SIZE, 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 | { |
Sheri Zhang | 141c31a | 2020-10-08 12:35:28 +0100 | [diff] [blame] | 201 | 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 Giorgio | bf3c662 | 2018-03-08 11:52:27 +0000 | [diff] [blame] | 204 | #ifdef IN_PLACE |
Sheri Zhang | 141c31a | 2020-10-08 12:35:28 +0100 | [diff] [blame] | 205 | __global uchar *output_addr = input_ptr; |
Michele Di Giorgio | bf3c662 | 2018-03-08 11:52:27 +0000 | [diff] [blame] | 206 | #else /* IN_PLACE */ |
Sheri Zhang | 141c31a | 2020-10-08 12:35:28 +0100 | [diff] [blame] | 207 | __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 Giorgio | bf3c662 | 2018-03-08 11:52:27 +0000 | [diff] [blame] | 208 | #endif /* IN_PLACE */ |
Sheri Zhang | 141c31a | 2020-10-08 12:35:28 +0100 | [diff] [blame] | 209 | __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 Giorgio | bf3c662 | 2018-03-08 11:52:27 +0000 | [diff] [blame] | 211 | #ifndef USE_DEFAULT_BETA |
Sheri Zhang | 141c31a | 2020-10-08 12:35:28 +0100 | [diff] [blame] | 212 | __global uchar *beta_addr = beta_ptr + beta_offset_first_element_in_bytes + x_offs; |
Michele Di Giorgio | bf3c662 | 2018-03-08 11:52:27 +0000 | [diff] [blame] | 213 | #endif /* USE_DEFAULT_BETA */ |
| 214 | #ifndef USE_DEFAULT_GAMMA |
Sheri Zhang | 141c31a | 2020-10-08 12:35:28 +0100 | [diff] [blame] | 215 | __global uchar *gamma_addr = gamma_ptr + gamma_offset_first_element_in_bytes + x_offs; |
Michele Di Giorgio | bf3c662 | 2018-03-08 11:52:27 +0000 | [diff] [blame] | 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) |
Sheri Zhang | 141c31a | 2020-10-08 12:35:28 +0100 | [diff] [blame] | 227 | res0 = 0; |
Michele Di Giorgio | bf3c662 | 2018-03-08 11:52:27 +0000 | [diff] [blame] | 228 | |
Sheri Zhang | 141c31a | 2020-10-08 12:35:28 +0100 | [diff] [blame] | 229 | data = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)input_addr); |
| 230 | denominator = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)var_addr); |
Michele Di Giorgio | bf3c662 | 2018-03-08 11:52:27 +0000 | [diff] [blame] | 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 |
Sheri Zhang | 141c31a | 2020-10-08 12:35:28 +0100 | [diff] [blame] | 234 | numerator = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)mean_addr); |
Michele Di Giorgio | bf3c662 | 2018-03-08 11:52:27 +0000 | [diff] [blame] | 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) |
Sheri Zhang | 141c31a | 2020-10-08 12:35:28 +0100 | [diff] [blame] | 240 | gamma_vec = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)gamma_addr); |
Michele Di Giorgio | bf3c662 | 2018-03-08 11:52:27 +0000 | [diff] [blame] | 241 | |
Sheri Zhang | 141c31a | 2020-10-08 12:35:28 +0100 | [diff] [blame] | 242 | res0 = MUL_OP(gamma_vec, x_bar); |
Michele Di Giorgio | bf3c662 | 2018-03-08 11:52:27 +0000 | [diff] [blame] | 243 | #else /* USE_DEFAULT_GAMMA */ |
| 244 | // gamma is equal to 1, no need to perform multiplications |
Sheri Zhang | 141c31a | 2020-10-08 12:35:28 +0100 | [diff] [blame] | 245 | res0 = x_bar; |
Michele Di Giorgio | bf3c662 | 2018-03-08 11:52:27 +0000 | [diff] [blame] | 246 | #endif /* USE_DEFAULT_GAMMA */ |
| 247 | |
| 248 | #ifndef USE_DEFAULT_BETA |
| 249 | VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) |
Sheri Zhang | 141c31a | 2020-10-08 12:35:28 +0100 | [diff] [blame] | 250 | beta_vec = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)beta_addr); |
Michele Di Giorgio | bf3c662 | 2018-03-08 11:52:27 +0000 | [diff] [blame] | 251 | // beta is not zero, hence we need to perform the addition |
Sheri Zhang | 141c31a | 2020-10-08 12:35:28 +0100 | [diff] [blame] | 252 | res0 = ADD_OP(res0, beta_vec); |
Michele Di Giorgio | bf3c662 | 2018-03-08 11:52:27 +0000 | [diff] [blame] | 253 | #endif /* USE_DEFAULT_BETA */ |
| 254 | |
Sheri Zhang | 141c31a | 2020-10-08 12:35:28 +0100 | [diff] [blame] | 255 | res0 = ACTIVATION(ACTIVATION_TYPE, DATA_TYPE, VEC_SIZE, res0, A_VAL, B_VAL); |
Michele Di Giorgio | bf3c662 | 2018-03-08 11:52:27 +0000 | [diff] [blame] | 256 | |
Sheri Zhang | 141c31a | 2020-10-08 12:35:28 +0100 | [diff] [blame] | 257 | STORE_VECTOR_SELECT(res, DATA_TYPE, output_addr, VEC_SIZE, VEC_SIZE_LEFTOVER, VEC_SIZE_LEFTOVER != 0 && get_global_id(0) == 0) |
Michele Di Giorgio | bf3c662 | 2018-03-08 11:52:27 +0000 | [diff] [blame] | 258 | } |
Usama Arif | 6a98a6e | 2019-05-10 17:07:27 +0100 | [diff] [blame] | 259 | #endif /* defined(VEC_SIZE) && defined(DATA_TYPE) && defined(DATA_TYPE)*/ |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 260 | |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 261 | #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 Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 263 | * |
Gian Marco Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 264 | * @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 Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 266 | * @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 Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 269 | * @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] | 270 | * |
Gian Marco Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 271 | * @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 Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 315 | */ |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 316 | __kernel void fuse_batchnormalization_layer(TENSOR3D_DECLARATION(w), |
Gian Marco Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 317 | #if defined(BIAS) |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 318 | VECTOR_DECLARATION(b), |
Gian Marco Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 319 | #endif // defined(BIAS) |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 320 | VECTOR_DECLARATION(mean), |
| 321 | VECTOR_DECLARATION(var) |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 322 | #ifndef IN_PLACE_W |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 323 | , |
| 324 | TENSOR3D_DECLARATION(w_fused) |
Gian Marco Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 325 | #endif // ifndef IN_PLACE_W |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 326 | #ifndef IN_PLACE_B |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 327 | , |
| 328 | VECTOR_DECLARATION(b_fused) |
Gian Marco Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 329 | #endif // ifndef IN_PLACE_B |
| 330 | #if defined(BETA) |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 331 | , |
| 332 | VECTOR_DECLARATION(beta) |
Gian Marco Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 333 | #endif // defined(BETA) |
| 334 | #if defined(GAMMA) |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 335 | , |
| 336 | VECTOR_DECLARATION(gamma) |
Gian Marco Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 337 | #endif // defined(GAMMA) |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 338 | ) |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 339 | { |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 340 | 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 Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 345 | int c0 = z % DIM2; |
| 346 | int c1 = z / DIM2; |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 347 | #else // ! defined(DIM2) |
Michalis Spyrou | ef6ec50 | 2020-07-31 11:38:36 +0100 | [diff] [blame] | 348 | int c0 = 0; |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 349 | #if defined(NHWC) |
Michalis Spyrou | ef6ec50 | 2020-07-31 11:38:36 +0100 | [diff] [blame] | 350 | int c1 = x; |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 351 | #else // defined(NHWC) |
| 352 | int c1 = z; |
| 353 | #endif // defined(NHWC) |
| 354 | #endif // defined(DIM2) |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 355 | |
Gian Marco Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 356 | int w_offset = x * sizeof(DATA_TYPE) + y * w_stride_y + z * w_stride_z; |
| 357 | int v_offset = c1 * sizeof(DATA_TYPE); |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 358 | |
Gian Marco Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 359 | 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; |
giuros01 | acce504 | 2019-02-21 17:32:34 +0000 | [diff] [blame] | 367 | |
Gian Marco Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 368 | 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 Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 371 | |
Gian Marco Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 372 | #if defined(GAMMA) |
| 373 | gamma = *((__global DATA_TYPE *)(gamma_ptr + v_offset + gamma_offset_first_element_in_bytes)); |
| 374 | #endif // defined(GAMMA) |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 375 | |
Gian Marco Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 376 | // Compute new weight |
| 377 | w_new = (gamma * w_old) / (sqrt(var + EPSILON)); |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 378 | |
Gian Marco Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 379 | #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 Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 382 | *((__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] | 383 | #endif // defined(IN_PLACE_W) |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 384 | |
Gian Marco Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 385 | // Compute bias |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 386 | #if !defined(DIM2) && defined(NHWC) |
| 387 | if(z == 0 && y == 0) |
Michalis Spyrou | ef6ec50 | 2020-07-31 11:38:36 +0100 | [diff] [blame] | 388 | #else // !defined(DIM2) && defined(NHWC) |
Gian Marco Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 389 | if(x == 0 && y == 0 && c0 == 0) |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 390 | #endif // !defined(DIM2) && defined(NHWC) |
Gian Marco Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 391 | { |
| 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 Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 398 | |
Gian Marco Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 399 | b_new = ((gamma * (b_old - mean)) / (sqrt(var + EPSILON))) + beta; |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 400 | |
Gian Marco Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 401 | #if defined(BIAS) |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 402 | |
Gian Marco Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 403 | #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 Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 408 | |
Gian Marco Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 409 | #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 Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 417 | } |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 418 | #endif // defined(DATA_TYPE) && defined(EPSILON) |