1/* 2 * Copyright (c) 2016-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(VEC_SIZE_X) && defined(VEC_SIZE_LEFTOVER_X) 27/** This kernel performs l2 normalization on x-axis 28 * 29 * @note The data type must be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=float 30 * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE_X=size. e.g. -DVEC_SIZE_X=16 31 * @note The leftover size in the X dimension shoud be given as preprocessor argument using -DVEC_SIZE_LEFTOVER_X is; x_dimension % VEC_SIZE_X. e.g. -DVEC_SIZE_LEFTOVER_X=1 32 * 33 * @param[in] input_ptr Pointer to the source tensor. Supported data types: F16/F32 34 * @param[in] input_stride_x Stride of the source tensor in X dimension (in bytes) 35 * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes) 36 * @param[in] input_stride_y Stride of the source tensor in Y dimension (in bytes) 37 * @param[in] input_step_y input_stride_y * number of elements along X processed per workitem(in bytes) 38 * @param[in] input_offset_first_element_in_bytes The offset of the first element in the source tensor 39 * @param[in] sum_ptr Pointer to the source tensor. Supported data types: F16/F32 40 * @param[in] sum_stride_x Stride of the source tensor in X dimension (in bytes) 41 * @param[in] sum_step_x sum_stride_x * number of elements along X processed per workitem(in bytes) 42 * @param[in] sum_stride_y Stride of the source tensor in Y dimension (in bytes) 43 * @param[in] sum_step_y sum_stride_y * number of elements along Y processed per workitem(in bytes) 44 * @param[in] sum_offset_first_element_in_bytes The offset of the first element in the source tensor 45 * @param[out] output_ptr Pointer to the destination tensor. Supported data types: same as @p input_ptr 46 * @param[in] output_stride_x Stride of the destination tensor in X dimension (in bytes) 47 * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes) 48 * @param[in] output_stride_y Stride of the destination tensor in Y dimension (in bytes) 49 * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes) 50 * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination tensor 51 * @param[in] epsilon Epsilon value 52 */ 53__kernel void l2_normalize_x( 54 IMAGE_DECLARATION(input), 55 IMAGE_DECLARATION(sum), 56 IMAGE_DECLARATION(output), 57 DATA_TYPE epsilon) 58{ 59 // Offset computation 60 const uint x_offs = max((int)(get_global_id(0) * VEC_SIZE_X - (VEC_SIZE_X - VEC_SIZE_LEFTOVER_X) % VEC_SIZE_X), 0); 61 62 // Address computation 63 __global uchar *input_addr = input_ptr + input_offset_first_element_in_bytes + x_offs * sizeof(DATA_TYPE) + get_global_id(1) * input_stride_y; 64 __global uchar *sum_addr = sum_ptr + sum_offset_first_element_in_bytes + get_global_id(1) * sum_stride_y; 65 __global uchar *output_addr = output_ptr + output_offset_first_element_in_bytes + x_offs * sizeof(DATA_TYPE) + get_global_id(1) * output_stride_y; 66 67 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE_X) 68 in = VLOAD(VEC_SIZE_X)(0, (__global DATA_TYPE *)input_addr); 69 70 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE_X) 71 normalize_value = (VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE_X))rsqrt(fmax(*((__global DATA_TYPE *)sum_addr), epsilon)); 72 73 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE_X) 74 data0 = in * normalize_value; 75 76 STORE_VECTOR_SELECT(data, DATA_TYPE, output_addr, VEC_SIZE_X, VEC_SIZE_LEFTOVER_X, VEC_SIZE_LEFTOVER_X != 0 && get_global_id(0) == 0); 77} 78 79/** This kernel performs l2 normalization on y-axis. 80 * 81 * @note The data type must be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=float 82 * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE_X=size. e.g. -DVEC_SIZE_X=16 83 * @note The leftover size in the X dimension shoud be given as preprocessor argument using -DVEC_SIZE_LEFTOVER_X is; x_dimension % VEC_SIZE_X. e.g. -DVEC_SIZE_LEFTOVER_X=1 84 * 85 * @param[in] input_ptr Pointer to the source tensor. Supported data types: F16/F32 86 * @param[in] input_stride_x Stride of the source tensor in X dimension (in bytes) 87 * @param[in] input_step_x input_stride_x * number of elements along Y processed per workitem(in bytes) 88 * @param[in] input_stride_y Stride of the source tensor in Y dimension (in bytes) 89 * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes) 90 * @param[in] input_offset_first_element_in_bytes The offset of the first element in the source tensor 91 * @param[in] sum_ptr Pointer to the source tensor. Supported data types: F16/F32 92 * @param[in] sum_stride_x Stride of the source tensor in X dimension (in bytes) 93 * @param[in] sum_step_x sum_stride_x * number of elements along X processed per workitem(in bytes) 94 * @param[in] sum_stride_y Stride of the source tensor in Y dimension (in bytes) 95 * @param[in] sum_step_y sum_stride_y * number of elements along Y processed per workitem(in bytes) 96 * @param[in] sum_offset_first_element_in_bytes The offset of the first element in the source tensor 97 * @param[out] output_ptr Pointer to the destination tensor. Supported data types: same as @p input_ptr 98 * @param[in] output_stride_x Stride of the destination tensor in X dimension (in bytes) 99 * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes) 100 * @param[in] output_stride_y Stride of the destination tensor in Y dimension (in bytes) 101 * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes) 102 * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination tensor 103 * @param[in] epsilon Epsilon value 104 */ 105__kernel void l2_normalize_y( 106 IMAGE_DECLARATION(input), 107 IMAGE_DECLARATION(sum), 108 IMAGE_DECLARATION(output), 109 DATA_TYPE epsilon) 110{ 111 // Offset computation 112 const uint x_offs = max((int)(get_global_id(0) * VEC_SIZE_X - (VEC_SIZE_X - VEC_SIZE_LEFTOVER_X) % VEC_SIZE_X), 0); 113 114 // Address computation 115 __global uchar *input_addr = input_ptr + input_offset_first_element_in_bytes + x_offs * sizeof(DATA_TYPE) + get_global_id(1) * input_stride_y; 116 __global uchar *sum_addr = sum_ptr + sum_offset_first_element_in_bytes + x_offs * sizeof(DATA_TYPE); 117 __global uchar *output_addr = output_ptr + output_offset_first_element_in_bytes + x_offs * sizeof(DATA_TYPE) + get_global_id(1) * output_stride_y; 118 119 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE_X) 120 in = VLOAD(VEC_SIZE_X)(0, (__global DATA_TYPE *)input_addr); 121 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE_X) 122 sums = VLOAD(VEC_SIZE_X)(0, (__global DATA_TYPE *)sum_addr); 123 124 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE_X) 125 normalize_value = (VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE_X))rsqrt(fmax(sums, epsilon)); 126 127 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE_X) 128 data0 = in * normalize_value; 129 130 STORE_VECTOR_SELECT(data, DATA_TYPE, output_addr, VEC_SIZE_X, VEC_SIZE_LEFTOVER_X, VEC_SIZE_LEFTOVER_X != 0 && get_global_id(0) == 0); 131} 132 133/** This kernel performs l2 normalization on z-axis. 134 * 135 * @note The data type must be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=float 136 * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE_X=size. e.g. -DVEC_SIZE_X=16 137 * @note The leftover size in the X dimension shoud be given as preprocessor argument using -DVEC_SIZE_LEFTOVER_X is; x_dimension % VEC_SIZE_X. e.g. -DVEC_SIZE_LEFTOVER_X=1 138 * 139 * @param[in] input_ptr Pointer to the source tensor. Supported data types: F16/F32 140 * @param[in] input_stride_x Stride of the source tensor in X dimension (in bytes) 141 * @param[in] input_step_x input_stride_x * number of elements along Y processed per workitem(in bytes) 142 * @param[in] input_stride_y Stride of the source tensor in Y dimension (in bytes) 143 * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes) 144 * @param[in] input_stride_z Stride of the source tensor in Z dimension (in bytes) 145 * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes) 146 * @param[in] input_offset_first_element_in_bytes The offset of the first element in the source tensor 147 * @param[in] sum_ptr Pointer to the source tensor. Supported data types: F16/F32 148 * @param[in] sum_stride_x Stride of the source tensor in X dimension (in bytes) 149 * @param[in] sum_step_x sum_stride_x * number of elements along X processed per workitem(in bytes) 150 * @param[in] sum_stride_y Stride of the source tensor in Y dimension (in bytes) 151 * @param[in] sum_step_y sum_stride_y * number of elements along Y processed per workitem(in bytes) 152 * @param[in] sum_stride_z Stride of the source tensor in Z dimension (in bytes) 153 * @param[in] sum_step_z sum_stride_z * number of elements along Z processed per workitem(in bytes) 154 * @param[in] sum_offset_first_element_in_bytes The offset of the first element in the source tensor 155 * @param[out] output_ptr Pointer to the destination tensor. Supported data types: same as @p input_ptr 156 * @param[in] output_stride_x Stride of the destination tensor in X dimension (in bytes) 157 * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes) 158 * @param[in] output_stride_y Stride of the destination tensor in Y dimension (in bytes) 159 * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes) 160 * @param[in] output_stride_z Stride of the destination tensor in Z dimension (in bytes) 161 * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes) 162 * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination tensor 163 * @param[in] epsilon Epsilon value 164 */ 165__kernel void l2_normalize_z( 166 TENSOR3D_DECLARATION(input), 167 TENSOR3D_DECLARATION(sum), 168 TENSOR3D_DECLARATION(output), 169 DATA_TYPE epsilon) 170{ 171 // Offset computation 172 const uint x_offs = max((int)(get_global_id(0) * VEC_SIZE_X - (VEC_SIZE_X - VEC_SIZE_LEFTOVER_X) % VEC_SIZE_X), 0); 173 174 // Address computation 175 __global uchar *input_addr = input_ptr + input_offset_first_element_in_bytes + x_offs * sizeof(DATA_TYPE) + get_global_id(1) * input_stride_y + get_global_id(2) * input_stride_z; 176 __global uchar *sum_addr = sum_ptr + sum_offset_first_element_in_bytes + x_offs * sizeof(DATA_TYPE) + get_global_id(1) * sum_stride_y; 177 __global uchar *output_addr = output_ptr + output_offset_first_element_in_bytes + x_offs * sizeof(DATA_TYPE) + get_global_id(1) * output_stride_y + get_global_id(2) * output_stride_z; 178 179 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE_X) 180 in = VLOAD(VEC_SIZE_X)(0, (__global DATA_TYPE *)input_addr); 181 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE_X) 182 sums = VLOAD(VEC_SIZE_X)(0, (__global DATA_TYPE *)sum_addr); 183 184 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE_X) 185 data0 = in * ((VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE_X))(rsqrt(fmax(sums, epsilon)))); 186 187 STORE_VECTOR_SELECT(data, DATA_TYPE, output_addr, VEC_SIZE_X, VEC_SIZE_LEFTOVER_X, VEC_SIZE_LEFTOVER_X != 0 && get_global_id(0) == 0); 188} 189#endif // defined(VEC_SIZE_X) && defined(VEC_SIZE_LEFTOVER_X)