1/* 2 * Copyright (c) 2017-2021 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#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 32#if defined(VEC_SIZE) && defined(DATA_TYPE) && defined(ACTIVATION_TYPE) 33#include "activation_float_helpers.h" 34 35/** Apply batch normalization on tensors with NHWC format. 36 * 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 * 40 * @param[in] input_ptr Pointer to the first source tensor. Supported data types: F16/F32 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 48 * @param[out] output_ptr Pointer to the destination tensor. Supported data types: same as @p input_ptr 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 56 * @param[in] mean_ptr Pointer to the mean source tensor. Supported data types: same as @p input_ptr 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 60 * @param[in] var_ptr Pointer to the var tensor. Supported data types: same as @p input_ptr 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 64 * @param[in] beta_ptr Pointer to the beta source tensor. Supported data types: same as @p input_ptr 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 68 * @param[in] gamma_ptr Pointer to the gamma source tensor. Supported data types: same as @p input_ptr 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 */ 74__kernel void batchnormalization_layer_nhwc(TENSOR3D_DECLARATION(input), 75#ifndef IN_PLACE 76 TENSOR3D_DECLARATION(output), 77#endif /* not IN_PLACE */ 78 VECTOR_DECLARATION(mean), 79 VECTOR_DECLARATION(var), 80#ifndef USE_DEFAULT_BETA 81 VECTOR_DECLARATION(beta), 82#endif /* USE_DEFAULT_BETA */ 83#ifndef USE_DEFAULT_GAMMA 84 VECTOR_DECLARATION(gamma), 85#endif /* USE_DEFAULT_GAMMA */ 86 float epsilon) 87{ 88 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); 89 90 __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; 91#ifdef IN_PLACE 92 __global uchar *output_addr = input_ptr; 93#else /* IN_PLACE */ 94 __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; 95#endif /* IN_PLACE */ 96 __global uchar *mean_addr = mean_ptr + mean_offset_first_element_in_bytes + x_offs; 97 __global uchar *var_addr = var_ptr + var_offset_first_element_in_bytes + x_offs; 98#ifndef USE_DEFAULT_BETA 99 __global uchar *beta_addr = beta_ptr + beta_offset_first_element_in_bytes + x_offs; 100#endif /* USE_DEFAULT_BETA */ 101#ifndef USE_DEFAULT_GAMMA 102 __global uchar *gamma_addr = gamma_ptr + gamma_offset_first_element_in_bytes + x_offs; 103#endif /* USE_DEFAULT_GAMMA */ 104 105 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) 106 data = 0; 107 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) 108 denominator = 0; 109 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) 110 numerator = 0; 111 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) 112 x_bar = 0; 113 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) 114 res0 = 0; 115 116 data = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)input_addr); 117 denominator = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)var_addr); 118 denominator = INVSQRT_OP(ADD_OP(denominator, ((VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE))SQCVT_SAT(epsilon)))); 119 120 // Calculate x bar and store results 121 numerator = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)mean_addr); 122 numerator = SUB_OP(data, numerator); 123 x_bar = MUL_OP(numerator, denominator); 124 125#ifndef USE_DEFAULT_GAMMA 126 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) 127 gamma_vec = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)gamma_addr); 128 129 res0 = MUL_OP(gamma_vec, x_bar); 130#else /* USE_DEFAULT_GAMMA */ 131 // gamma is equal to 1, no need to perform multiplications 132 res0 = x_bar; 133#endif /* USE_DEFAULT_GAMMA */ 134 135#ifndef USE_DEFAULT_BETA 136 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) 137 beta_vec = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)beta_addr); 138 // beta is not zero, hence we need to perform the addition 139 res0 = ADD_OP(res0, beta_vec); 140#endif /* USE_DEFAULT_BETA */ 141 142 res0 = ACTIVATION(ACTIVATION_TYPE, DATA_TYPE, VEC_SIZE, res0, A_VAL, B_VAL); 143 144 STORE_VECTOR_SELECT(res, DATA_TYPE, output_addr, VEC_SIZE, VEC_SIZE_LEFTOVER, VEC_SIZE_LEFTOVER != 0 && get_global_id(0) == 0) 145} 146#endif /* defined(VEC_SIZE) && defined(DATA_TYPE) && defined(DATA_TYPE)*/