Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 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 | |
| 25 | layout(local_size_x = LOCAL_SIZE_X, local_size_y = LOCAL_SIZE_Y, local_size_z = LOCAL_SIZE_Z) in; |
| 26 | |
| 27 | #include "helpers.h" |
| 28 | |
| 29 | #ifdef DATA_TYPE_FP32 |
| 30 | precision highp float; |
| 31 | #elif defined(DATA_TYPE_FP16) |
| 32 | precision mediump float; |
| 33 | #endif /*DATA_TYPE_FP32*/ |
| 34 | |
| 35 | #define ADD_OP(a, b) ((a) + (b)) |
| 36 | #define SUB_OP(a, b) ((a) - (b)) |
| 37 | #define MUL_OP(a, b) ((a) * (b)) |
| 38 | #define INVSQRT_OP(a) inversesqrt((a)) |
| 39 | #define SQCVT_SAT(a) (a) |
| 40 | |
| 41 | layout(std140) uniform shader_params |
| 42 | { |
| 43 | TENSOR3D_PARAM_DECLARATION(src); |
| 44 | TENSOR3D_PARAM_DECLARATION(dst); |
| 45 | VECTOR_PARAM_DECLARATION(mean); |
| 46 | VECTOR_PARAM_DECLARATION(var); |
| 47 | VECTOR_PARAM_DECLARATION(beta); |
| 48 | VECTOR_PARAM_DECLARATION(gamma); |
| 49 | }; |
| 50 | |
| 51 | #ifdef DATA_TYPE_FP32 |
| 52 | BUFFER_DECLARATION(src, 1, float, readonly); |
| 53 | BUFFER_DECLARATION(dst, 2, float, writeonly); |
| 54 | BUFFER_DECLARATION(mean, 3, float, readonly); |
| 55 | BUFFER_DECLARATION(var, 4, float, readonly); |
| 56 | BUFFER_DECLARATION(beta, 5, float, readonly); |
| 57 | BUFFER_DECLARATION(gamma, 6, float, readonly); |
| 58 | |
| 59 | /** Apply batch normalization. |
| 60 | * |
| 61 | * @note Epsilon parameter in the batch normalization equation should be given as a preprocessor argument using "#define EPSILON". e.g. "#define EPSILON 0.1" |
| 62 | * |
Joel Liang | f1f3ebd | 2017-11-10 09:59:19 +0800 | [diff] [blame] | 63 | * @param[in] src_ptr Pointer to the first source tensor. Supported data types: F32 |
| 64 | * @param[in] src_stride_x Stride of the first source tensor in X dimension (in bytes) |
| 65 | * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 66 | * @param[in] src_stride_y Stride of the first source tensor in Y dimension (in bytes) |
| 67 | * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 68 | * @param[in] src_stride_z Stride of the first source tensor in Z dimension (in bytes) |
| 69 | * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes) |
| 70 | * @param[in] src_offset_first_element_in_bytes The offset of the first element in the first source tensor |
| 71 | * @param[out] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr |
| 72 | * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 73 | * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes) |
| 74 | * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes) |
| 75 | * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes) |
| 76 | * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes) |
| 77 | * @param[in] dst_step_z dst_stride_z * number of elements along Z processed per workitem(in bytes) |
| 78 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor |
| 79 | * @param[in] mean_ptr Pointer to the mean source tensor. Supported data types: same as @p src_ptr |
| 80 | * @param[in] mean_stride_x Stride of the mean source tensor in X dimension (in bytes) |
| 81 | * @param[in] mean_step_x mean_stride_x * number of elements along X processed per workitem(in bytes) |
| 82 | * @param[in] mean_offset_first_element_in_bytes The offset of the first element in the mean source tensor |
| 83 | * @param[in] var_ptr Pointer to the var tensor. Supported data types: same as @p src_ptr |
| 84 | * @param[in] var_stride_x Stride of the var tensor in X dimension (in bytes) |
| 85 | * @param[in] var_step_x var_stride_x * number of elements along X processed per workitem(in bytes) |
| 86 | * @param[in] var_offset_first_element_in_bytes The offset of the first element in the var source tensor |
| 87 | * @param[in] beta_ptr Pointer to the beta source tensor. Supported data types: same as @p src_ptr |
| 88 | * @param[in] beta_stride_x Stride of the beta source tensor in X dimension (in bytes) |
| 89 | * @param[in] beta_step_x beta_stride_x * number of elements along X processed per workitem(in bytes) |
| 90 | * @param[in] beta_offset_first_element_in_bytes The offset of the first element in the beta source tensor |
| 91 | * @param[in] gamma_ptr Pointer to the gamma source tensor. Supported data types: same as @p src_ptr |
| 92 | * @param[in] gamma_stride_x Stride of the gamma source tensor in X dimension (in bytes) |
| 93 | * @param[in] gamma_step_x gamma_stride_x * number of elements along X processed per workitem(in bytes) |
| 94 | * @param[in] gamma_offset_first_element_in_bytes The offset of the first element in the gamma source tensor |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 95 | */ |
| 96 | void main(void) |
| 97 | { |
| 98 | Tensor3D src = CONVERT_TO_TENSOR3D_STRUCT(src); |
| 99 | Tensor3D dst = CONVERT_TO_TENSOR3D_STRUCT(dst); |
| 100 | Vector mean = CONVERT_TO_VECTOR_STRUCT(mean); |
| 101 | Vector var = CONVERT_TO_VECTOR_STRUCT(var); |
| 102 | Vector beta = CONVERT_TO_VECTOR_STRUCT(beta); |
| 103 | Vector gamma = CONVERT_TO_VECTOR_STRUCT(gamma); |
| 104 | |
| 105 | float input_value = 0.f; |
| 106 | float denominator = 0.f; |
| 107 | float numerator = 0.f; |
| 108 | float x_bar = 0.f; |
| 109 | float gamma_param = 0.f; |
| 110 | float beta_param = 0.f; |
| 111 | |
| 112 | uint current_slice = gl_GlobalInvocationID.z; |
| 113 | |
| 114 | input_value = src_ptr[src.current_offset]; |
| 115 | denominator = var_ptr[var.current_offset + (current_slice * var.stride_x) >> 2]; |
| 116 | denominator = INVSQRT_OP(ADD_OP(denominator, SQCVT_SAT(float(ESPILON)))); |
| 117 | |
| 118 | // Calculate x bar and store results |
| 119 | numerator = mean_ptr[mean.current_offset + (current_slice * mean.stride_x) >> 2]; |
| 120 | numerator = SUB_OP(input_value, numerator); |
| 121 | x_bar = MUL_OP(numerator, denominator); |
| 122 | |
| 123 | gamma_param = gamma_ptr[gamma.current_offset + (current_slice * beta.stride_x) >> 2]; |
| 124 | beta_param = beta_ptr[beta.current_offset + (current_slice * beta.stride_x) >> 2]; |
| 125 | |
| 126 | dst_ptr[dst.current_offset] = ADD_OP(MUL_OP(gamma_param, x_bar), beta_param); |
| 127 | } |
| 128 | |
| 129 | #elif defined(DATA_TYPE_FP16) |
zhenglin | 923241e | 2017-12-05 11:30:51 +0800 | [diff] [blame^] | 130 | BUFFER_DECLARATION(src, 1, uvec2, readonly); |
| 131 | BUFFER_DECLARATION(dst, 2, uvec2, writeonly); |
| 132 | BUFFER_DECLARATION(mean, 3, uvec2, readonly); |
| 133 | BUFFER_DECLARATION(var, 4, uvec2, readonly); |
| 134 | BUFFER_DECLARATION(beta, 5, uvec2, readonly); |
| 135 | BUFFER_DECLARATION(gamma, 6, uvec2, readonly); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 136 | |
| 137 | /** Apply batch normalization. |
| 138 | * |
| 139 | * @note Epsilon parameter in the batch normalization equation should be given as a preprocessor argument using "#define EPSILON". e.g. "#define EPSILON 0.1" |
| 140 | * |
Joel Liang | f1f3ebd | 2017-11-10 09:59:19 +0800 | [diff] [blame] | 141 | * @param[in] src_ptr Pointer to the first source tensor. Supported data types: F16 |
| 142 | * @param[in] src_stride_x Stride of the first source tensor in X dimension (in bytes) |
| 143 | * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 144 | * @param[in] src_stride_y Stride of the first source tensor in Y dimension (in bytes) |
| 145 | * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 146 | * @param[in] src_stride_z Stride of the first source tensor in Z dimension (in bytes) |
| 147 | * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes) |
| 148 | * @param[in] src_offset_first_element_in_bytes The offset of the first element in the first source tensor |
| 149 | * @param[out] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr |
| 150 | * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 151 | * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes) |
| 152 | * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes) |
| 153 | * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes) |
| 154 | * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes) |
| 155 | * @param[in] dst_step_z dst_stride_z * number of elements along Z processed per workitem(in bytes) |
| 156 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor |
| 157 | * @param[in] mean_ptr Pointer to the mean source tensor. Supported data types: same as @p src_ptr |
| 158 | * @param[in] mean_stride_x Stride of the mean source tensor in X dimension (in bytes) |
| 159 | * @param[in] mean_step_x mean_stride_x * number of elements along X processed per workitem(in bytes) |
| 160 | * @param[in] mean_offset_first_element_in_bytes The offset of the first element in the mean source tensor |
| 161 | * @param[in] var_ptr Pointer to the var tensor. Supported data types: same as @p src_ptr |
| 162 | * @param[in] var_stride_x Stride of the var tensor in X dimension (in bytes) |
| 163 | * @param[in] var_step_x var_stride_x * number of elements along X processed per workitem(in bytes) |
| 164 | * @param[in] var_offset_first_element_in_bytes The offset of the first element in the var source tensor |
| 165 | * @param[in] beta_ptr Pointer to the beta source tensor. Supported data types: same as @p src_ptr |
| 166 | * @param[in] beta_stride_x Stride of the beta source tensor in X dimension (in bytes) |
| 167 | * @param[in] beta_step_x beta_stride_x * number of elements along X processed per workitem(in bytes) |
| 168 | * @param[in] beta_offset_first_element_in_bytes The offset of the first element in the beta source tensor |
| 169 | * @param[in] gamma_ptr Pointer to the gamma source tensor. Supported data types: same as @p src_ptr |
| 170 | * @param[in] gamma_stride_x Stride of the gamma source tensor in X dimension (in bytes) |
| 171 | * @param[in] gamma_step_x gamma_stride_x * number of elements along X processed per workitem(in bytes) |
| 172 | * @param[in] gamma_offset_first_element_in_bytes The offset of the first element in the gamma source tensor |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 173 | */ |
| 174 | void main(void) |
| 175 | { |
| 176 | Tensor3D src = CONVERT_TO_TENSOR3D_STRUCT_FP16(src); |
| 177 | Tensor3D dst = CONVERT_TO_TENSOR3D_STRUCT_FP16(dst); |
| 178 | Vector mean = CONVERT_TO_VECTOR_STRUCT_FP16(mean); |
| 179 | Vector var = CONVERT_TO_VECTOR_STRUCT_FP16(var); |
| 180 | Vector beta = CONVERT_TO_VECTOR_STRUCT_FP16(beta); |
| 181 | Vector gamma = CONVERT_TO_VECTOR_STRUCT_FP16(gamma); |
| 182 | |
zhenglin | 923241e | 2017-12-05 11:30:51 +0800 | [diff] [blame^] | 183 | uvec2 packed_s[5]; |
| 184 | vec4 unpacked_s[5]; |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 185 | float denominator; |
| 186 | float numerator; |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 187 | float gamma_param; |
| 188 | float beta_param; |
zhenglin | 923241e | 2017-12-05 11:30:51 +0800 | [diff] [blame^] | 189 | vec4 x_bar; |
| 190 | vec4 result; |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 191 | |
| 192 | uint current_slice = gl_GlobalInvocationID.z; |
zhenglin | 923241e | 2017-12-05 11:30:51 +0800 | [diff] [blame^] | 193 | packed_s[0] = src_ptr[src.current_offset >> 3]; |
| 194 | packed_s[1] = var_ptr[(var.current_offset + current_slice * var.stride_x) >> 3]; |
| 195 | packed_s[2] = mean_ptr[(mean.current_offset + current_slice * mean.stride_x) >> 3]; |
| 196 | packed_s[3] = gamma_ptr[(gamma.current_offset + current_slice * beta.stride_x) >> 3]; |
| 197 | packed_s[4] = beta_ptr[(beta.current_offset + current_slice * beta.stride_x) >> 3]; |
| 198 | unpacked_s[0] = vec4(unpackHalf2x16(packed_s[0].x), unpackHalf2x16(packed_s[0].y)); |
| 199 | unpacked_s[1] = vec4(unpackHalf2x16(packed_s[1].x), unpackHalf2x16(packed_s[1].y)); |
| 200 | unpacked_s[2] = vec4(unpackHalf2x16(packed_s[2].x), unpackHalf2x16(packed_s[2].y)); |
| 201 | unpacked_s[3] = vec4(unpackHalf2x16(packed_s[3].x), unpackHalf2x16(packed_s[3].y)); |
| 202 | unpacked_s[4] = vec4(unpackHalf2x16(packed_s[4].x), unpackHalf2x16(packed_s[4].y)); |
| 203 | |
| 204 | if((current_slice % uint(4)) == uint(0)) |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 205 | { |
zhenglin | 923241e | 2017-12-05 11:30:51 +0800 | [diff] [blame^] | 206 | denominator = unpacked_s[1].x; |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 207 | denominator = INVSQRT_OP(ADD_OP(denominator, SQCVT_SAT(float(ESPILON)))); |
| 208 | |
| 209 | //Calculate x bar and store results |
zhenglin | 923241e | 2017-12-05 11:30:51 +0800 | [diff] [blame^] | 210 | numerator = unpacked_s[2].x; |
| 211 | x_bar = MUL_OP(SUB_OP(unpacked_s[0], numerator), denominator); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 212 | |
zhenglin | 923241e | 2017-12-05 11:30:51 +0800 | [diff] [blame^] | 213 | gamma_param = unpacked_s[3].x; |
| 214 | beta_param = unpacked_s[4].x; |
| 215 | result = ADD_OP(MUL_OP(gamma_param, x_bar), beta_param); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 216 | |
zhenglin | 923241e | 2017-12-05 11:30:51 +0800 | [diff] [blame^] | 217 | dst_ptr[dst.current_offset >> 3] = uvec2(packHalf2x16(result.xy), packHalf2x16(result.zw)); |
| 218 | } |
| 219 | else if((current_slice % uint(4)) == uint(1)) |
| 220 | { |
| 221 | denominator = unpacked_s[1].y; |
| 222 | denominator = INVSQRT_OP(ADD_OP(denominator, SQCVT_SAT(float(ESPILON)))); |
| 223 | |
| 224 | //Calculate x bar and store results |
| 225 | numerator = unpacked_s[2].y; |
| 226 | x_bar = MUL_OP(SUB_OP(unpacked_s[0], numerator), denominator); |
| 227 | |
| 228 | gamma_param = unpacked_s[3].y; |
| 229 | beta_param = unpacked_s[4].y; |
| 230 | result = ADD_OP(MUL_OP(gamma_param, x_bar), beta_param); |
| 231 | |
| 232 | dst_ptr[dst.current_offset >> 3] = uvec2(packHalf2x16(result.xy), packHalf2x16(result.zw)); |
| 233 | } |
| 234 | else if((current_slice % uint(4)) == uint(2)) |
| 235 | { |
| 236 | denominator = unpacked_s[1].z; |
| 237 | denominator = INVSQRT_OP(ADD_OP(denominator, SQCVT_SAT(float(ESPILON)))); |
| 238 | |
| 239 | //Calculate x bar and store results |
| 240 | numerator = unpacked_s[2].z; |
| 241 | x_bar = MUL_OP(SUB_OP(unpacked_s[0], numerator), denominator); |
| 242 | |
| 243 | gamma_param = unpacked_s[3].z; |
| 244 | beta_param = unpacked_s[4].z; |
| 245 | result = ADD_OP(MUL_OP(gamma_param, x_bar), beta_param); |
| 246 | |
| 247 | dst_ptr[dst.current_offset >> 3] = uvec2(packHalf2x16(result.xy), packHalf2x16(result.zw)); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 248 | } |
| 249 | else |
| 250 | { |
zhenglin | 923241e | 2017-12-05 11:30:51 +0800 | [diff] [blame^] | 251 | denominator = unpacked_s[1].w; |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 252 | denominator = INVSQRT_OP(ADD_OP(denominator, SQCVT_SAT(float(ESPILON)))); |
| 253 | |
| 254 | //Calculate x bar and store results |
zhenglin | 923241e | 2017-12-05 11:30:51 +0800 | [diff] [blame^] | 255 | numerator = unpacked_s[2].w; |
| 256 | x_bar = MUL_OP(SUB_OP(unpacked_s[0], numerator), denominator); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 257 | |
zhenglin | 923241e | 2017-12-05 11:30:51 +0800 | [diff] [blame^] | 258 | gamma_param = unpacked_s[3].w; |
| 259 | beta_param = unpacked_s[4].w; |
| 260 | result = ADD_OP(MUL_OP(gamma_param, x_bar), beta_param); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 261 | |
zhenglin | 923241e | 2017-12-05 11:30:51 +0800 | [diff] [blame^] | 262 | dst_ptr[dst.current_offset >> 3] = uvec2(packHalf2x16(result.xy), packHalf2x16(result.zw)); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 263 | } |
| 264 | } |
zhenglin | 923241e | 2017-12-05 11:30:51 +0800 | [diff] [blame^] | 265 | #endif /*DATA_TYPE_FP16*/ |