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#if defined(DATA_TYPE) && defined(EPSILON) 27/** OpenCL kernel to fuse the weights of convolution or depthwise convolution layer with batch normalization when the data layout is either NCHW or NHWC 28 * 29 * @note The input weights tensor is assumed 4D with the OFMs in the fourth dimension 30 * @note Data type should be passed at compile time using the -DDATA_TYPE, e.g. -DDATA_TYPE=float 31 * @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. 32 * For depthwise convolution weight do not pass DIM2 33 * @note Data layout NHWC should be passed at compile time with -DNHWC. For data layout NCHW it is not required to pass any parameter 34 * @note Batch normalization epsilon parameter should be passed at compile time using -DEPSILON=value. e.g. -DEPSILON=0.001f 35 * 36 * @param[in] w_ptr Pointer to the weights tensor. Supported data types: F16/F32 37 * @param[in] w_stride_x Stride of the weights tensor in X dimension (in bytes) 38 * @param[in] w_step_x w_stride_x * number of elements along X processed per workitem(in bytes) 39 * @param[in] w_stride_y Stride of the weights tensor in Y dimension (in bytes) 40 * @param[in] w_step_y w_stride_y * number of elements along Y processed per workitem(in bytes) 41 * @param[in] w_stride_z Stride of the weights tensor in Z dimension (in bytes) 42 * @param[in] w_step_z w_stride_z * number of elements along Z processed per workitem(in bytes) 43 * @param[in] w_offset_first_element_in_bytes The offset of the first element in the weights tensor 44 * @param[in] b_ptr (Optional) Pointer to the bias tensor. Supported data types: same as @p w_ptr 45 * @param[in] b_stride_x (Optional) Stride of the bias tensor in X dimension (in bytes) 46 * @param[in] b_step_x (Optional) b_stride_x * number of elements along X processed per workitem(in bytes) 47 * @param[in] b_stride_y (Optional) Stride of the bias tensor in Y dimension (in bytes) 48 * @param[in] b_step_y (Optional) b_stride_y * number of elements along Y processed per workitem(in bytes) 49 * @param[in] b_stride_z (Optional) Stride of the bias tensor in Z dimension (in bytes) 50 * @param[in] b_step_z (Optional) b_stride_z * number of elements along Z processed per workitem(in bytes) 51 * @param[in] b_offset_first_element_in_bytes (Optional) The offset of the first element in the bias tensor 52 * @param[in] mean_ptr Pointer to the mean source tensor. Supported data types: same as @p w_ptr 53 * @param[in] mean_stride_x Stride of the mean source tensor in X dimension (in bytes) 54 * @param[in] mean_step_x mean_stride_x * number of elements along X processed per workitem(in bytes) 55 * @param[in] mean_offset_first_element_in_bytes The offset of the first element in the mean source tensor 56 * @param[in] var_ptr Pointer to the var tensor. Supported data types: same as @p w_ptr 57 * @param[in] var_stride_x Stride of the var tensor in X dimension (in bytes) 58 * @param[in] var_step_x var_stride_x * number of elements along X processed per workitem(in bytes) 59 * @param[in] var_offset_first_element_in_bytes The offset of the first element in the var source tensor 60 * @param[out] w_fused_ptr (Optional) Pointer to the destination weights tensors. Supported data types: same as @p w_ptr 61 * @param[in] w_fused_stride_x (Optional) Stride of the destination weights tensor in X dimension (in bytes) 62 * @param[in] w_fused_step_x (Optional) w_fused_stride_x * number of elements along X processed per workitem(in bytes) 63 * @param[in] w_fused_stride_y (Optional) Stride of the destination weights tensor in Y dimension (in bytes) 64 * @param[in] w_fused_step_y (Optional) w_fused_stride_y * number of elements along Y processed per workitem(in bytes) 65 * @param[in] w_fused_stride_z (Optional) Stride of the destination weights tensor in Z dimension (in bytes) 66 * @param[in] w_fused_step_z (Optional) w_fused_stride_z * number of elements along Z processed per workitem(in bytes) 67 * @param[in] w_fused_offset_first_element_in_bytes (Optional) The offset of the first element in the destination weights tensor 68 * @param[in] b_fused_ptr (Optional) Pointer to the destination bias tensor. Supported data types: same as @p w_ptr 69 * @param[in] b_fused_stride_x (Optional) Stride of the destination bias tensor in X dimension (in bytes) 70 * @param[in] b_fused_step_x (Optional) b_fused_stride_x * number of elements along X processed per workitem(in bytes) 71 * @param[in] b_fused_offset_first_element_in_bytes (Optional) The offset of the first element in the destination bias tensor 72 * @param[in] beta_ptr (Optional) Pointer to the beta source tensor. Supported data types: same as @p w_ptr 73 * @param[in] beta_stride_x (Optional) Stride of the beta source tensor in X dimension (in bytes) 74 * @param[in] beta_step_x (Optional) beta_stride_x * number of elements along X processed per workitem(in bytes) 75 * @param[in] beta_offset_first_element_in_bytes (Optional) The offset of the first element in the beta source tensor 76 * @param[in] gamma_ptr (Optional) Pointer to the gamma source tensor. Supported data types: same as @p w_ptr 77 * @param[in] gamma_stride_x (Optional) Stride of the gamma source tensor in X dimension (in bytes) 78 * @param[in] gamma_step_x (Optional) gamma_stride_x * number of elements along X processed per workitem(in bytes) 79 * @param[in] gamma_offset_first_element_in_bytes (Optional) The offset of the first element in the gamma source tensor 80 */ 81__kernel void fuse_batchnormalization_layer(TENSOR3D_DECLARATION(w), 82#if defined(BIAS) 83 VECTOR_DECLARATION(b), 84#endif // defined(BIAS) 85 VECTOR_DECLARATION(mean), 86 VECTOR_DECLARATION(var) 87#ifndef IN_PLACE_W 88 , 89 TENSOR3D_DECLARATION(w_fused) 90#endif // ifndef IN_PLACE_W 91#ifndef IN_PLACE_B 92 , 93 VECTOR_DECLARATION(b_fused) 94#endif // ifndef IN_PLACE_B 95#if defined(BETA) 96 , 97 VECTOR_DECLARATION(beta) 98#endif // defined(BETA) 99#if defined(GAMMA) 100 , 101 VECTOR_DECLARATION(gamma) 102#endif // defined(GAMMA) 103 ) 104{ 105 int x = get_global_id(0); 106 int y = get_global_id(1); 107 int z = get_global_id(2); 108 109#if defined(DIM2) 110 int c0 = z % DIM2; 111 int c1 = z / DIM2; 112#else // ! defined(DIM2) 113 int c0 = 0; 114#if defined(NHWC) 115 int c1 = x; 116#else // defined(NHWC) 117 int c1 = z; 118#endif // defined(NHWC) 119#endif // defined(DIM2) 120 121 int w_offset = x * sizeof(DATA_TYPE) + y * w_stride_y + z * w_stride_z; 122 int v_offset = c1 * sizeof(DATA_TYPE); 123 124 DATA_TYPE w_old = 0.0f; 125 DATA_TYPE b_old = 0.0f; 126 DATA_TYPE w_new = 0.0f; 127 DATA_TYPE b_new = 0.0f; 128 DATA_TYPE gamma = 1.0f; 129 DATA_TYPE mean = 0.0f; 130 DATA_TYPE var = 1.0f; 131 DATA_TYPE beta = 0.0f; 132 133 w_old = *((__global DATA_TYPE *)(w_ptr + w_offset + w_offset_first_element_in_bytes)); 134 var = *((__global DATA_TYPE *)(var_ptr + v_offset + var_offset_first_element_in_bytes)); 135 mean = *((__global DATA_TYPE *)(mean_ptr + v_offset + mean_offset_first_element_in_bytes)); 136 137#if defined(GAMMA) 138 gamma = *((__global DATA_TYPE *)(gamma_ptr + v_offset + gamma_offset_first_element_in_bytes)); 139#endif // defined(GAMMA) 140 141 // Compute new weight 142 w_new = (gamma * w_old) / (sqrt(var + EPSILON)); 143 144#if defined(IN_PLACE_W) 145 *((__global DATA_TYPE *)(w_ptr + w_offset + w_offset_first_element_in_bytes)) = w_new; 146#else // defined(IN_PLACE_W) 147 *((__global DATA_TYPE *)(w_fused_ptr + w_offset + w_fused_offset_first_element_in_bytes)) = w_new; 148#endif // defined(IN_PLACE_W) 149 150 // Compute bias 151#if !defined(DIM2) && defined(NHWC) 152 if(z == 0 && y == 0) 153#else // !defined(DIM2) && defined(NHWC) 154 if(x == 0 && y == 0 && c0 == 0) 155#endif // !defined(DIM2) && defined(NHWC) 156 { 157#if defined(BIAS) 158 b_old = *((__global DATA_TYPE *)(b_ptr + v_offset + b_offset_first_element_in_bytes)); 159#endif // defined(BIAS) 160#if defined(BETA) 161 beta = *((__global DATA_TYPE *)(beta_ptr + v_offset + beta_offset_first_element_in_bytes)); 162#endif // defined(BETA) 163 164 b_new = ((gamma * (b_old - mean)) / (sqrt(var + EPSILON))) + beta; 165 166#if defined(BIAS) 167 168#if defined(IN_PLACE_B) 169 *((__global DATA_TYPE *)(b_ptr + v_offset + b_offset_first_element_in_bytes)) = b_new; 170#else // defined(IN_PLACE_B) 171 *((__global DATA_TYPE *)(b_fused_ptr + v_offset + b_fused_offset_first_element_in_bytes)) = b_new; 172#endif // defined(IN_PLACE_B) 173 174#else // defined(BIAS) 175 176#ifndef IN_PLACE_B 177 *((__global DATA_TYPE *)(b_fused_ptr + v_offset + b_fused_offset_first_element_in_bytes)) = b_new; 178#endif // ifndef IN_PLACE_B 179 180#endif // defined(BIAS) 181 } 182} 183#endif // defined(DATA_TYPE) && defined(EPSILON)