Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Manuel Bottini | 21c2895 | 2021-04-08 12:50:12 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2021 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" |
Manuel Bottini | 21c2895 | 2021-04-08 12:50:12 +0100 | [diff] [blame] | 25 | #include "tile_helpers.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 26 | |
Michele Di Giorgio | 6c92834 | 2017-06-22 16:55:57 +0100 | [diff] [blame] | 27 | #define MUL_OP(x, y) ((x) * (y)) |
| 28 | #define ADD_OP(x, y) ((x) + (y)) |
| 29 | #define DIV_OP(x, y) ((x) / (y)) |
| 30 | #define POW_OP(x, y) pow((x), (y)) |
| 31 | #define SQCVT_SAT(a) (a) |
| 32 | |
Michele Di Giorgio | 9d3a831 | 2018-11-20 12:31:24 +0000 | [diff] [blame] | 33 | #if defined(NUM_SLICES) |
Georgios Pinitas | 0162436 | 2017-11-30 10:53:31 +0000 | [diff] [blame] | 34 | /** Apply cross-map normalization. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 35 | * |
| 36 | * @note Datatype should be given as a preprocessor argument using -DDATA_TYPE=type. e.g. -DDATA_TYPE=short |
Michele Di Giorgio | 6c92834 | 2017-06-22 16:55:57 +0100 | [diff] [blame] | 37 | * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size, e.g. -DVEC_SIZE=16 |
Gian Marco Iodice | d60a6b9 | 2017-08-10 10:43:40 +0100 | [diff] [blame] | 38 | * @note The radius should be given as a preprocessor argument using -DRADIUS=size. e.g. -DRADIUS=5 |
| 39 | * @note The number of slices should be given as a preprocessor argument using -DNUM_SLICES=size. e.g. -DNUM_SLICES=192 |
Michele Di Giorgio | 6c92834 | 2017-06-22 16:55:57 +0100 | [diff] [blame] | 40 | * @note Scaling coefficient (= alpha/norm_size), beta and kappa need to be passed at compile time using -DCOEFF, -DALPHA and -DKAPPA |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 41 | * |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 42 | * @param[in] input_ptr Pointer to the first source tensor. Supported data types: F16/F32 |
Gian Marco Iodice | d60a6b9 | 2017-08-10 10:43:40 +0100 | [diff] [blame] | 43 | * @param[in] input_stride_x Stride of the first source tensor in X dimension (in bytes) |
| 44 | * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes) |
| 45 | * @param[in] input_stride_y Stride of the first source tensor in Y dimension (in bytes) |
| 46 | * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes) |
| 47 | * @param[in] input_stride_z Stride of the first source tensor in Z dimension (in bytes) |
| 48 | * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes) |
| 49 | * @param[in] input_offset_first_element_in_bytes The offset of the first element in the first source tensor |
| 50 | * @param[out] output_ptr Pointer to the destination tensor. Supported data types: same as @p input_ptr |
| 51 | * @param[in] output_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 52 | * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes) |
| 53 | * @param[in] output_stride_y Stride of the destination tensor in Y dimension (in bytes) |
| 54 | * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes) |
| 55 | * @param[in] output_stride_z Stride of the destination tensor in Z dimension (in bytes) |
| 56 | * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes) |
| 57 | * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination tensor |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 58 | */ |
Manuel Bottini | 21c2895 | 2021-04-08 12:50:12 +0100 | [diff] [blame] | 59 | __kernel void normalization_layer_cross_map_nchw(TENSOR3D_DECLARATION(input), |
| 60 | TENSOR3D_DECLARATION(output)) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 61 | { |
Gian Marco Iodice | d60a6b9 | 2017-08-10 10:43:40 +0100 | [diff] [blame] | 62 | Tensor3D in = CONVERT_TO_TENSOR3D_STRUCT(input); |
| 63 | Tensor3D out = CONVERT_TO_TENSOR3D_STRUCT(output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 64 | |
Michele Di Giorgio | 6c92834 | 2017-06-22 16:55:57 +0100 | [diff] [blame] | 65 | VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) |
| 66 | acc = (VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE))0; |
| 67 | const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) |
| 68 | coeff_v = (VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE))SQCVT_SAT(COEFF); |
| 69 | const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) |
| 70 | beta_v = (VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE))SQCVT_SAT(BETA); |
| 71 | const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) |
| 72 | kappa_v = (VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE))SQCVT_SAT(KAPPA); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 73 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 74 | const int current_slice = get_global_id(2); |
Georgios Pinitas | 0162436 | 2017-11-30 10:53:31 +0000 | [diff] [blame] | 75 | const int left_slice = max(-(int)RADIUS, -current_slice); |
| 76 | const int right_slice = min((int)RADIUS, (int)NUM_SLICES - 1 - current_slice); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 77 | |
| 78 | for(int i = left_slice; i <= right_slice; i++) |
| 79 | { |
Gian Marco Iodice | d60a6b9 | 2017-08-10 10:43:40 +0100 | [diff] [blame] | 80 | VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) |
Manuel Bottini | 21c2895 | 2021-04-08 12:50:12 +0100 | [diff] [blame] | 81 | values = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)tensor3D_offset(&in, 0, 0, i)); |
Gian Marco Iodice | d60a6b9 | 2017-08-10 10:43:40 +0100 | [diff] [blame] | 82 | acc = ADD_OP(acc, MUL_OP(values, values)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 83 | } |
| 84 | |
Michele Di Giorgio | 6c92834 | 2017-06-22 16:55:57 +0100 | [diff] [blame] | 85 | acc = ADD_OP(MUL_OP(acc, coeff_v), kappa_v); |
| 86 | const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) |
| 87 | normalized = POW_OP(acc, beta_v); |
| 88 | const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) |
Manuel Bottini | 21c2895 | 2021-04-08 12:50:12 +0100 | [diff] [blame] | 89 | normalized_pixel = DIV_OP(VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)in.ptr), normalized); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 90 | |
Manuel Bottini | 21c2895 | 2021-04-08 12:50:12 +0100 | [diff] [blame] | 91 | VSTORE(VEC_SIZE) |
| 92 | (normalized_pixel, 0, (__global DATA_TYPE *)out.ptr); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 93 | } |
Michele Di Giorgio | 9d3a831 | 2018-11-20 12:31:24 +0000 | [diff] [blame] | 94 | #endif /* defined(NUM_SLICES) */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 95 | |
Georgios Pinitas | 7f32d01 | 2018-10-11 18:41:19 +0100 | [diff] [blame] | 96 | #if defined(WIDTH_SIZE) |
Manuel Bottini | 21c2895 | 2021-04-08 12:50:12 +0100 | [diff] [blame] | 97 | /** Apply cross-map normalization. |
| 98 | * |
| 99 | * @note Datatype should be given as a preprocessor argument using -DDATA_TYPE=type. e.g. -DDATA_TYPE=short |
| 100 | * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size, e.g. -DVEC_SIZE=16 |
| 101 | * @note The radius should be given as a preprocessor argument using -DRADIUS=size. e.g. -DRADIUS=5 |
| 102 | * @note The number of slices should be given as a preprocessor argument using -DNUM_SLICES=size. e.g. -DNUM_SLICES=192 |
| 103 | * @note Scaling coefficient (= alpha/norm_size), beta and kappa need to be passed at compile time using -DCOEFF, -DALPHA and -DKAPPA |
| 104 | * |
| 105 | * @param[in] input_ptr Pointer to the first source tensor. Supported data types: F16/F32 |
| 106 | * @param[in] input_stride_x Stride of the first source tensor in X dimension (in bytes) |
| 107 | * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes) |
| 108 | * @param[in] input_stride_y Stride of the first source tensor in Y dimension (in bytes) |
| 109 | * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes) |
| 110 | * @param[in] input_stride_z Stride of the first source tensor in Z dimension (in bytes) |
| 111 | * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes) |
| 112 | * @param[in] input_offset_first_element_in_bytes The offset of the first element in the first source tensor |
| 113 | * @param[out] output_ptr Pointer to the destination tensor. Supported data types: same as @p input_ptr |
| 114 | * @param[in] output_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 115 | * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes) |
| 116 | * @param[in] output_stride_y Stride of the destination tensor in Y dimension (in bytes) |
| 117 | * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes) |
| 118 | * @param[in] output_stride_z Stride of the destination tensor in Z dimension (in bytes) |
| 119 | * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes) |
| 120 | * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination tensor |
| 121 | */ |
| 122 | __kernel void normalization_layer_cross_map_nhwc(TENSOR3D_DECLARATION(input), |
| 123 | TENSOR3D_DECLARATION(output)) |
| 124 | { |
| 125 | // Offset computation |
| 126 | const uint x_offs = GET_SPATIAL_IDX(0, VEC_SIZE, VEC_SIZE_LEFTOVER); |
| 127 | |
| 128 | // Address computation |
| 129 | __global uchar *input_addr = input_ptr + input_offset_first_element_in_bytes + get_global_id(1) * input_stride_y + get_global_id(2) * input_stride_z; |
| 130 | __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; |
| 131 | |
| 132 | VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) |
| 133 | acc = 0; |
| 134 | const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) |
| 135 | coeff_v = SQCVT_SAT(COEFF); |
| 136 | const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) |
| 137 | beta_v = SQCVT_SAT(BETA); |
| 138 | const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) |
| 139 | kappa_v = SQCVT_SAT(KAPPA); |
| 140 | |
| 141 | const int left_slice = max((int)0, (int)x_offs - (int)RADIUS); |
| 142 | const int right_slice = min((int)WIDTH_SIZE - 1, (int)x_offs + (int)RADIUS); |
| 143 | |
| 144 | for(int i = left_slice; i <= right_slice; ++i) |
| 145 | { |
| 146 | VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) |
| 147 | values = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(input_addr + i * sizeof(DATA_TYPE))); |
| 148 | acc = ADD_OP(acc, MUL_OP(values, values)); |
| 149 | } |
| 150 | |
| 151 | acc = ADD_OP(MUL_OP(acc, coeff_v), kappa_v); |
| 152 | const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) |
| 153 | normalized = POW_OP(acc, beta_v); |
| 154 | const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) |
| 155 | normalized_pixel0 = DIV_OP(VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(input_addr + x_offs * sizeof(DATA_TYPE))), normalized); |
| 156 | |
| 157 | STORE_VECTOR_SELECT(normalized_pixel, DATA_TYPE, output_addr, VEC_SIZE, VEC_SIZE_LEFTOVER, VEC_SIZE_LEFTOVER != 0 && get_global_id(0) == 0); |
| 158 | } |
| 159 | |
Michele Di Giorgio | 9d3a831 | 2018-11-20 12:31:24 +0000 | [diff] [blame] | 160 | /** Apply in-map normalization when tensors are in the NCHW data layout format. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 161 | * |
| 162 | * @note Datatype should be given as a preprocessor argument using -DDATA_TYPE=type. e.g. -DDATA_TYPE=short |
Michele Di Giorgio | 6c92834 | 2017-06-22 16:55:57 +0100 | [diff] [blame] | 163 | * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size, e.g. -DVEC_SIZE=16 |
Gian Marco Iodice | d60a6b9 | 2017-08-10 10:43:40 +0100 | [diff] [blame] | 164 | * @note The radius should be given as a preprocessor argument using -DRADIUS=size. e.g. -DRADIUS=5 |
Michele Di Giorgio | 6c92834 | 2017-06-22 16:55:57 +0100 | [diff] [blame] | 165 | * @note Scaling coefficient (= alpha/norm_size), beta and kappa need to be passed at compile time using -DCOEFF, -DALPHA and -DKAPPA |
Manuel Bottini | 21c2895 | 2021-04-08 12:50:12 +0100 | [diff] [blame] | 166 | * @note The leftover size in the X dimension shoud be given as preprocessor argument using -DVEC_SIZE_LEFTOVER is; x_dimension % VEC_SIZE. e.g. -DVEC_SIZE_LEFTOVER=1 |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 167 | * |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 168 | * @param[in] input_ptr Pointer to the first source tensor. Supported data types: F16/F32 |
Gian Marco Iodice | d60a6b9 | 2017-08-10 10:43:40 +0100 | [diff] [blame] | 169 | * @param[in] input_stride_x Stride of the first source tensor in X dimension (in bytes) |
| 170 | * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes) |
| 171 | * @param[in] input_stride_y Stride of the first source tensor in Y dimension (in bytes) |
| 172 | * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes) |
| 173 | * @param[in] input_stride_z Stride of the first source tensor in Z dimension (in bytes) |
| 174 | * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes) |
| 175 | * @param[in] input_offset_first_element_in_bytes The offset of the first element in the first source tensor |
| 176 | * @param[out] output_ptr Pointer to the destination tensor. Supported data types: same as @p input_ptr |
| 177 | * @param[in] output_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 178 | * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes) |
| 179 | * @param[in] output_stride_y Stride of the first destination tensor in Y dimension (in bytes) |
| 180 | * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes) |
| 181 | * @param[in] output_stride_z Stride of the first source tensor in Z dimension (in bytes) |
| 182 | * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes) |
| 183 | * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination tensor |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 184 | */ |
Michele Di Giorgio | 9d3a831 | 2018-11-20 12:31:24 +0000 | [diff] [blame] | 185 | __kernel void normalization_layer_in_map_nchw(TENSOR3D_DECLARATION(input), |
| 186 | TENSOR3D_DECLARATION(output)) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 187 | { |
Gian Marco Iodice | d60a6b9 | 2017-08-10 10:43:40 +0100 | [diff] [blame] | 188 | Tensor3D in = CONVERT_TO_TENSOR3D_STRUCT(input); |
| 189 | Tensor3D out = CONVERT_TO_TENSOR3D_STRUCT(output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 190 | |
Michele Di Giorgio | 6c92834 | 2017-06-22 16:55:57 +0100 | [diff] [blame] | 191 | VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) |
Manuel Bottini | 21c2895 | 2021-04-08 12:50:12 +0100 | [diff] [blame] | 192 | acc = 0; |
Michele Di Giorgio | 6c92834 | 2017-06-22 16:55:57 +0100 | [diff] [blame] | 193 | const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) |
Manuel Bottini | 21c2895 | 2021-04-08 12:50:12 +0100 | [diff] [blame] | 194 | coeff_v = SQCVT_SAT(COEFF); |
Michele Di Giorgio | 6c92834 | 2017-06-22 16:55:57 +0100 | [diff] [blame] | 195 | const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) |
Manuel Bottini | 21c2895 | 2021-04-08 12:50:12 +0100 | [diff] [blame] | 196 | beta_v = SQCVT_SAT(BETA); |
Michele Di Giorgio | 6c92834 | 2017-06-22 16:55:57 +0100 | [diff] [blame] | 197 | const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) |
Manuel Bottini | 21c2895 | 2021-04-08 12:50:12 +0100 | [diff] [blame] | 198 | kappa_v = SQCVT_SAT(KAPPA); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 199 | |
Georgios Pinitas | 0162436 | 2017-11-30 10:53:31 +0000 | [diff] [blame] | 200 | const int current_col = get_global_id(0) << 2; |
| 201 | const int left_pos = max(-(int)RADIUS, -3 - current_col); |
Georgios Pinitas | 7f32d01 | 2018-10-11 18:41:19 +0100 | [diff] [blame] | 202 | const int right_pos = min((int)RADIUS, (int)WIDTH_SIZE - 1 - current_col); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 203 | |
Georgios Pinitas | 0162436 | 2017-11-30 10:53:31 +0000 | [diff] [blame] | 204 | #if defined(IN_MAP_2D) |
| 205 | const int current_row = get_global_id(1); |
| 206 | const int first_row = max(-(int)RADIUS, -current_row); |
| 207 | const int last_row = min((int)RADIUS, (int)get_global_size(1) - 1 - current_row); |
| 208 | #endif /* defined(IN_MAP_2D) */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 209 | |
Georgios Pinitas | 0162436 | 2017-11-30 10:53:31 +0000 | [diff] [blame] | 210 | #if defined(IN_MAP_2D) |
| 211 | for(int j = first_row; j <= last_row; ++j) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 212 | { |
Georgios Pinitas | 0162436 | 2017-11-30 10:53:31 +0000 | [diff] [blame] | 213 | #endif /* defined(IN_MAP_2D) */ |
| 214 | for(int i = left_pos; i <= right_pos; ++i) |
| 215 | { |
| 216 | #if defined(IN_MAP_2D) |
| 217 | VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) |
Manuel Bottini | 21c2895 | 2021-04-08 12:50:12 +0100 | [diff] [blame] | 218 | values = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)tensor3D_offset(&in, i, j, 0)); |
Georgios Pinitas | 0162436 | 2017-11-30 10:53:31 +0000 | [diff] [blame] | 219 | #else /* defined(IN_MAP_2D) */ |
| 220 | VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) |
Manuel Bottini | 21c2895 | 2021-04-08 12:50:12 +0100 | [diff] [blame] | 221 | values = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)tensor3D_offset(&in, i, 0, 0)); |
Georgios Pinitas | 0162436 | 2017-11-30 10:53:31 +0000 | [diff] [blame] | 222 | #endif /* defined(IN_MAP_2D) */ |
| 223 | acc = ADD_OP(acc, MUL_OP(values, values)); |
| 224 | } |
| 225 | #if defined(IN_MAP_2D) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 226 | } |
Georgios Pinitas | 0162436 | 2017-11-30 10:53:31 +0000 | [diff] [blame] | 227 | #endif /* defined(IN_MAP_2D) */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 228 | |
Michele Di Giorgio | 6c92834 | 2017-06-22 16:55:57 +0100 | [diff] [blame] | 229 | acc = ADD_OP(MUL_OP(acc, coeff_v), kappa_v); |
| 230 | const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) |
| 231 | normalized = POW_OP(acc, beta_v); |
| 232 | const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) |
Manuel Bottini | 21c2895 | 2021-04-08 12:50:12 +0100 | [diff] [blame] | 233 | normalized_pixel = DIV_OP(VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)in.ptr), normalized); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 234 | |
Manuel Bottini | 21c2895 | 2021-04-08 12:50:12 +0100 | [diff] [blame] | 235 | VSTORE(VEC_SIZE) |
| 236 | (normalized_pixel, 0, (__global DATA_TYPE *)out.ptr); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 237 | } |
Georgios Pinitas | 7f32d01 | 2018-10-11 18:41:19 +0100 | [diff] [blame] | 238 | #endif // defined(WIDTH_SIZE) |
Michele Di Giorgio | 9d3a831 | 2018-11-20 12:31:24 +0000 | [diff] [blame] | 239 | |
Manuel Bottini | 21c2895 | 2021-04-08 12:50:12 +0100 | [diff] [blame] | 240 | #if defined(NUM_SLICES) && defined(DIM1_SIZE) |
Michele Di Giorgio | 9d3a831 | 2018-11-20 12:31:24 +0000 | [diff] [blame] | 241 | /** Apply in-map normalization when tensors are in the NHWC data layout format. |
| 242 | * |
| 243 | * @note Datatype should be given as a preprocessor argument using -DDATA_TYPE=type. e.g. -DDATA_TYPE=short |
| 244 | * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size, e.g. -DVEC_SIZE=16 |
| 245 | * @note The radius should be given as a preprocessor argument using -DRADIUS=size. e.g. -DRADIUS=5 |
| 246 | * @note The number of slices should be given as a preprocessor argument using -DNUM_SLICES=size. e.g. -DNUM_SLICES=192 |
| 247 | * @note Scaling coefficient (= alpha/norm_size), beta and kappa need to be passed at compile time using -DCOEFF, -DALPHA and -DKAPPA |
| 248 | * |
| 249 | * @param[in] input_ptr Pointer to the first source tensor. Supported data types: F16/F32 |
| 250 | * @param[in] input_stride_x Stride of the first source tensor in X dimension (in bytes) |
| 251 | * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes) |
| 252 | * @param[in] input_stride_y Stride of the first source tensor in Y dimension (in bytes) |
| 253 | * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes) |
| 254 | * @param[in] input_stride_z Stride of the first source tensor in Z dimension (in bytes) |
| 255 | * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes) |
| 256 | * @param[in] input_offset_first_element_in_bytes The offset of the first element in the first source tensor |
| 257 | * @param[out] output_ptr Pointer to the destination tensor. Supported data types: same as @p input_ptr |
| 258 | * @param[in] output_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 259 | * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes) |
| 260 | * @param[in] output_stride_y Stride of the first destination tensor in Y dimension (in bytes) |
| 261 | * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes) |
| 262 | * @param[in] output_stride_z Stride of the first source tensor in Z dimension (in bytes) |
| 263 | * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes) |
| 264 | * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination tensor |
| 265 | */ |
| 266 | __kernel void normalization_layer_in_map_nhwc(TENSOR3D_DECLARATION(input), |
| 267 | TENSOR3D_DECLARATION(output)) |
| 268 | { |
Manuel Bottini | 21c2895 | 2021-04-08 12:50:12 +0100 | [diff] [blame] | 269 | // Offset computation |
| 270 | const uint x_offs = GET_SPATIAL_IDX(0, VEC_SIZE, VEC_SIZE_LEFTOVER); |
| 271 | const int current_cols = get_global_id(1); |
| 272 | const int current_rows = get_global_id(2); |
| 273 | |
| 274 | // Address computation |
| 275 | __global uchar *input_addr = input_ptr + input_offset_first_element_in_bytes + x_offs * sizeof(DATA_TYPE); |
| 276 | __global uchar *output_addr = output_ptr + output_offset_first_element_in_bytes + x_offs * sizeof(DATA_TYPE) + current_cols * output_stride_y + current_rows * output_stride_z; |
Michele Di Giorgio | 9d3a831 | 2018-11-20 12:31:24 +0000 | [diff] [blame] | 277 | |
| 278 | VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) |
Manuel Bottini | 21c2895 | 2021-04-08 12:50:12 +0100 | [diff] [blame] | 279 | acc = 0; |
Michele Di Giorgio | 9d3a831 | 2018-11-20 12:31:24 +0000 | [diff] [blame] | 280 | const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) |
Manuel Bottini | 21c2895 | 2021-04-08 12:50:12 +0100 | [diff] [blame] | 281 | coeff_v = SQCVT_SAT(COEFF); |
Michele Di Giorgio | 9d3a831 | 2018-11-20 12:31:24 +0000 | [diff] [blame] | 282 | const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) |
Manuel Bottini | 21c2895 | 2021-04-08 12:50:12 +0100 | [diff] [blame] | 283 | beta_v = SQCVT_SAT(BETA); |
Michele Di Giorgio | 9d3a831 | 2018-11-20 12:31:24 +0000 | [diff] [blame] | 284 | const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) |
Manuel Bottini | 21c2895 | 2021-04-08 12:50:12 +0100 | [diff] [blame] | 285 | kappa_v = SQCVT_SAT(KAPPA); |
Michele Di Giorgio | 9d3a831 | 2018-11-20 12:31:24 +0000 | [diff] [blame] | 286 | |
Manuel Bottini | 21c2895 | 2021-04-08 12:50:12 +0100 | [diff] [blame] | 287 | const int first_col = max(0, current_cols - (int)RADIUS); |
| 288 | const int last_col = min((int)DIM1_SIZE - 1, current_cols + (int)RADIUS); |
Michele Di Giorgio | 9d3a831 | 2018-11-20 12:31:24 +0000 | [diff] [blame] | 289 | |
| 290 | #if defined(IN_MAP_2D) |
Manuel Bottini | 21c2895 | 2021-04-08 12:50:12 +0100 | [diff] [blame] | 291 | const int first_row = max(0, current_rows - (int)RADIUS); |
| 292 | const int last_row = min((int)NUM_SLICES - 1, current_rows + (int)RADIUS); |
Michele Di Giorgio | 9d3a831 | 2018-11-20 12:31:24 +0000 | [diff] [blame] | 293 | #endif /* defined(IN_MAP_2D) */ |
| 294 | |
| 295 | #if defined(IN_MAP_2D) |
| 296 | for(int j = first_row; j <= last_row; ++j) |
| 297 | { |
Manuel Bottini | 21c2895 | 2021-04-08 12:50:12 +0100 | [diff] [blame] | 298 | #else // defined(IN_MAP_2D) |
| 299 | const int j = current_rows; |
Michele Di Giorgio | 9d3a831 | 2018-11-20 12:31:24 +0000 | [diff] [blame] | 300 | #endif /* defined(IN_MAP_2D) */ |
| 301 | for(int i = first_col; i <= last_col; ++i) |
| 302 | { |
Michele Di Giorgio | 9d3a831 | 2018-11-20 12:31:24 +0000 | [diff] [blame] | 303 | VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) |
Manuel Bottini | 21c2895 | 2021-04-08 12:50:12 +0100 | [diff] [blame] | 304 | values = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(input_addr + i * input_stride_y + j * input_stride_z)); |
| 305 | acc = ADD_OP(acc, MUL_OP(values, values)); |
Michele Di Giorgio | 9d3a831 | 2018-11-20 12:31:24 +0000 | [diff] [blame] | 306 | } |
| 307 | #if defined(IN_MAP_2D) |
| 308 | } |
| 309 | #endif /* defined(IN_MAP_2D) */ |
| 310 | |
| 311 | acc = ADD_OP(MUL_OP(acc, coeff_v), kappa_v); |
| 312 | const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) |
| 313 | normalized = POW_OP(acc, beta_v); |
| 314 | const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) |
Manuel Bottini | 21c2895 | 2021-04-08 12:50:12 +0100 | [diff] [blame] | 315 | normalized_pixel0 = DIV_OP(VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(input_addr + current_cols * output_stride_y + current_rows * output_stride_z)), normalized); |
Michele Di Giorgio | 9d3a831 | 2018-11-20 12:31:24 +0000 | [diff] [blame] | 316 | |
Manuel Bottini | 21c2895 | 2021-04-08 12:50:12 +0100 | [diff] [blame] | 317 | STORE_VECTOR_SELECT(normalized_pixel, DATA_TYPE, output_addr, VEC_SIZE, VEC_SIZE_LEFTOVER, VEC_SIZE_LEFTOVER != 0 && get_global_id(0) == 0); |
Michele Di Giorgio | 9d3a831 | 2018-11-20 12:31:24 +0000 | [diff] [blame] | 318 | } |
Manuel Bottini | 21c2895 | 2021-04-08 12:50:12 +0100 | [diff] [blame] | 319 | #endif // defined(NUM_SLICES) && defined(DIM1_SIZE) |