Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Gian Marco Iodice | 40471d1 | 2021-04-26 08:39:28 +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" |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 25 | #include "repeat.h" |
Gian Marco Iodice | 40471d1 | 2021-04-26 08:39:28 +0100 | [diff] [blame^] | 26 | #include "tile_helpers.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 27 | |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 28 | #if defined(POOL_AVG) || defined(POOL_L2) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 29 | #define POOL_OP(x, y) ((x) + (y)) |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 30 | #else /* defined(POOL_AVG) || defined(POOL_L2) */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 31 | #define POOL_OP(x, y) (fmax((x), (y))) |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 32 | #endif /* defined(POOL_AVG) || defined(POOL_L2) */ |
| 33 | |
| 34 | #if defined(POOL_L2) |
| 35 | #define POW2_OP(x, vec_size) ((x) * (x)) |
| 36 | #else /* defined(POOL_L2) */ |
| 37 | #define POW2_OP(x, vec_size) (x) |
| 38 | #endif /* defined(POOL_L2) */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 39 | |
steniu01 | 0c7614f | 2017-06-23 17:00:26 +0100 | [diff] [blame] | 40 | #define DIV_OP(x, y) (x * (1.f / y)) |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 41 | #define SQRT_OP(x) sqrt((x)) |
steniu01 | 0c7614f | 2017-06-23 17:00:26 +0100 | [diff] [blame] | 42 | |
Gian Marco Iodice | cb29283 | 2017-08-02 13:19:48 +0100 | [diff] [blame] | 43 | #if STRIDE_X == 1 |
| 44 | #define POOLING3x3(res, input, output) POOLING3x3_STRIDE1(res, input, output) |
| 45 | #elif STRIDE_X == 2 /* STRIDE_X == 1 */ |
| 46 | #define POOLING3x3(res, input, output) POOLING3x3_STRIDE2(res, input, output) |
| 47 | #elif STRIDE_X == 3 /* STRIDE_X not equals 1 or 2 */ |
| 48 | #define POOLING3x3(res, input, output) POOLING3x3_STRIDE3(res, input, output) |
| 49 | #endif /* STRIDE_X == 3 */ |
| 50 | |
Sang-Hoon Park | 2aa7fd0 | 2019-09-18 13:39:00 +0100 | [diff] [blame] | 51 | #if defined(FP_MIXED_PRECISION) |
| 52 | #define CONVERT_TO_ACC_DATA_TYPE(x, n) CONVERT(x, VEC_DATA_TYPE(ACC_DATA_TYPE, n)) |
| 53 | #define VLOAD_AND_CONVERT_TO_ACC_DATA_TYPE(n, offset, ptr) \ |
| 54 | CONVERT_TO_ACC_DATA_TYPE(vload##n(offset, ptr), n) |
| 55 | #else /* defined(FP_MIXED_PRECISION) */ |
| 56 | #define VLOAD_AND_CONVERT_TO_ACC_DATA_TYPE(n, offset, ptr) vload##n(offset, ptr) |
| 57 | #endif /* defined(FP_MIXED_PRECISION) */ |
| 58 | |
| 59 | #define POOLING3x3_STRIDE1(res, input, output) \ |
| 60 | ({ \ |
| 61 | VEC_DATA_TYPE(ACC_DATA_TYPE, 4) \ |
| 62 | data00 = VLOAD_AND_CONVERT_TO_ACC_DATA_TYPE(4, 0, (__global DATA_TYPE *)tensor3D_offset(&input, 0, 0, 0)); \ |
| 63 | VEC_DATA_TYPE(ACC_DATA_TYPE, 2) \ |
| 64 | data01 = VLOAD_AND_CONVERT_TO_ACC_DATA_TYPE(2, 0, (__global DATA_TYPE *)tensor3D_offset(&input, 0, 0, 0) + 4); \ |
| 65 | VEC_DATA_TYPE(ACC_DATA_TYPE, 4) \ |
| 66 | data10 = VLOAD_AND_CONVERT_TO_ACC_DATA_TYPE(4, 0, (__global DATA_TYPE *)tensor3D_offset(&input, 0, 1, 0)); \ |
| 67 | VEC_DATA_TYPE(ACC_DATA_TYPE, 2) \ |
| 68 | data11 = VLOAD_AND_CONVERT_TO_ACC_DATA_TYPE(2, 0, (__global DATA_TYPE *)tensor3D_offset(&input, 0, 1, 0) + 4); \ |
| 69 | VEC_DATA_TYPE(ACC_DATA_TYPE, 4) \ |
| 70 | data20 = VLOAD_AND_CONVERT_TO_ACC_DATA_TYPE(4, 0, (__global DATA_TYPE *)tensor3D_offset(&input, 0, 2, 0)); \ |
| 71 | VEC_DATA_TYPE(ACC_DATA_TYPE, 2) \ |
| 72 | data21 = VLOAD_AND_CONVERT_TO_ACC_DATA_TYPE(2, 0, (__global DATA_TYPE *)tensor3D_offset(&input, 0, 2, 0) + 4); \ |
| 73 | data00 = POW2_OP(data00, 4); \ |
| 74 | data01 = POW2_OP(data01, 2); \ |
| 75 | data10 = POW2_OP(data10, 4); \ |
| 76 | data11 = POW2_OP(data11, 2); \ |
| 77 | data20 = POW2_OP(data20, 4); \ |
| 78 | data21 = POW2_OP(data21, 2); \ |
| 79 | \ |
| 80 | VEC_DATA_TYPE(ACC_DATA_TYPE, 8) \ |
| 81 | values00 = (VEC_DATA_TYPE(ACC_DATA_TYPE, 8))(data00.s01212323); \ |
| 82 | VEC_DATA_TYPE(ACC_DATA_TYPE, 4) \ |
| 83 | values01 = (VEC_DATA_TYPE(ACC_DATA_TYPE, 4))(data01.s0, data00.s3, data01.s01); \ |
| 84 | VEC_DATA_TYPE(ACC_DATA_TYPE, 8) \ |
| 85 | values10 = (VEC_DATA_TYPE(ACC_DATA_TYPE, 8))(data10.s01212323); \ |
| 86 | VEC_DATA_TYPE(ACC_DATA_TYPE, 4) \ |
| 87 | values11 = (VEC_DATA_TYPE(ACC_DATA_TYPE, 4))(data11.s0, data10.s3, data11.s01); \ |
| 88 | VEC_DATA_TYPE(ACC_DATA_TYPE, 8) \ |
| 89 | values20 = (VEC_DATA_TYPE(ACC_DATA_TYPE, 8))(data20.s01212323); \ |
| 90 | VEC_DATA_TYPE(ACC_DATA_TYPE, 4) \ |
| 91 | values21 = (VEC_DATA_TYPE(ACC_DATA_TYPE, 4))(data21.s0, data20.s3, data21.s01); \ |
| 92 | \ |
| 93 | values00 = POOL_OP(values00, values10); \ |
| 94 | values01 = POOL_OP(values01, values11); \ |
| 95 | values00 = POOL_OP(values00, values20); \ |
| 96 | values01 = POOL_OP(values01, values21); \ |
| 97 | \ |
| 98 | res = POOL_OP((VEC_DATA_TYPE(ACC_DATA_TYPE, 4))(values00.s036, values01.s1), (VEC_DATA_TYPE(ACC_DATA_TYPE, 4))(values00.s147, values01.s2)); \ |
| 99 | res = POOL_OP(res, (VEC_DATA_TYPE(ACC_DATA_TYPE, 4))(values00.s25, values01.s03)); \ |
| 100 | }) |
| 101 | |
| 102 | #define POOLING3x3_STRIDE2(res, input, output) \ |
| 103 | ({ \ |
| 104 | VEC_DATA_TYPE(ACC_DATA_TYPE, 8) \ |
| 105 | data00 = VLOAD_AND_CONVERT_TO_ACC_DATA_TYPE(8, 0, (__global DATA_TYPE *)tensor3D_offset(&input, 0, 0, 0)); \ |
| 106 | ACC_DATA_TYPE data01 = (ACC_DATA_TYPE)(*((__global DATA_TYPE *)tensor3D_offset(&input, 0, 0, 0) + 8)); \ |
| 107 | VEC_DATA_TYPE(ACC_DATA_TYPE, 8) \ |
| 108 | data10 = VLOAD_AND_CONVERT_TO_ACC_DATA_TYPE(8, 0, (__global DATA_TYPE *)tensor3D_offset(&input, 0, 1, 0)); \ |
| 109 | ACC_DATA_TYPE data11 = (ACC_DATA_TYPE)(*((__global DATA_TYPE *)tensor3D_offset(&input, 0, 1, 0) + 8)); \ |
| 110 | VEC_DATA_TYPE(ACC_DATA_TYPE, 8) \ |
| 111 | data20 = VLOAD_AND_CONVERT_TO_ACC_DATA_TYPE(8, 0, (__global DATA_TYPE *)tensor3D_offset(&input, 0, 2, 0)); \ |
| 112 | ACC_DATA_TYPE data21 = (ACC_DATA_TYPE)(*((__global DATA_TYPE *)tensor3D_offset(&input, 0, 2, 0) + 8)); \ |
| 113 | data00 = POW2_OP(data00, 8); \ |
| 114 | data01 = POW2_OP(data01, 1); \ |
| 115 | data10 = POW2_OP(data10, 8); \ |
| 116 | data11 = POW2_OP(data11, 1); \ |
| 117 | data20 = POW2_OP(data20, 8); \ |
| 118 | data21 = POW2_OP(data21, 1); \ |
| 119 | \ |
| 120 | VEC_DATA_TYPE(ACC_DATA_TYPE, 8) \ |
| 121 | values00 = (VEC_DATA_TYPE(ACC_DATA_TYPE, 8))(data00.s01223445); \ |
| 122 | VEC_DATA_TYPE(ACC_DATA_TYPE, 4) \ |
| 123 | values01 = (VEC_DATA_TYPE(ACC_DATA_TYPE, 4))(data00.s667, data01); \ |
| 124 | VEC_DATA_TYPE(ACC_DATA_TYPE, 8) \ |
| 125 | values10 = (VEC_DATA_TYPE(ACC_DATA_TYPE, 8))(data10.s01223445); \ |
| 126 | VEC_DATA_TYPE(ACC_DATA_TYPE, 4) \ |
| 127 | values11 = (VEC_DATA_TYPE(ACC_DATA_TYPE, 4))(data10.s667, data11); \ |
| 128 | VEC_DATA_TYPE(ACC_DATA_TYPE, 8) \ |
| 129 | values20 = (VEC_DATA_TYPE(ACC_DATA_TYPE, 8))(data20.s01223445); \ |
| 130 | VEC_DATA_TYPE(ACC_DATA_TYPE, 4) \ |
| 131 | values21 = (VEC_DATA_TYPE(ACC_DATA_TYPE, 4))(data20.s667, data21); \ |
| 132 | \ |
| 133 | values00 = POOL_OP(values00, values10); \ |
| 134 | values01 = POOL_OP(values01, values11); \ |
| 135 | values00 = POOL_OP(values00, values20); \ |
| 136 | values01 = POOL_OP(values01, values21); \ |
| 137 | \ |
| 138 | res = POOL_OP((VEC_DATA_TYPE(ACC_DATA_TYPE, 4))(values00.s036, values01.s1), (VEC_DATA_TYPE(ACC_DATA_TYPE, 4))(values00.s147, values01.s2)); \ |
| 139 | res = POOL_OP(res, (VEC_DATA_TYPE(ACC_DATA_TYPE, 4))(values00.s25, values01.s03)); \ |
| 140 | }) |
| 141 | |
| 142 | #define POOLING3x3_STRIDE3(res, input, output) \ |
Gian Marco Iodice | cb29283 | 2017-08-02 13:19:48 +0100 | [diff] [blame] | 143 | ({ \ |
Sang-Hoon Park | 2aa7fd0 | 2019-09-18 13:39:00 +0100 | [diff] [blame] | 144 | VEC_DATA_TYPE(ACC_DATA_TYPE, 8) \ |
| 145 | data00 = VLOAD_AND_CONVERT_TO_ACC_DATA_TYPE(8, 0, (__global DATA_TYPE *)tensor3D_offset(&input, 0, 0, 0)); \ |
| 146 | VEC_DATA_TYPE(ACC_DATA_TYPE, 4) \ |
| 147 | data01 = VLOAD_AND_CONVERT_TO_ACC_DATA_TYPE(4, 0, (__global DATA_TYPE *)tensor3D_offset(&input, 0, 0, 0) + 8); \ |
| 148 | VEC_DATA_TYPE(ACC_DATA_TYPE, 8) \ |
| 149 | data10 = VLOAD_AND_CONVERT_TO_ACC_DATA_TYPE(8, 0, (__global DATA_TYPE *)tensor3D_offset(&input, 0, 1, 0)); \ |
| 150 | VEC_DATA_TYPE(ACC_DATA_TYPE, 4) \ |
| 151 | data11 = VLOAD_AND_CONVERT_TO_ACC_DATA_TYPE(4, 0, (__global DATA_TYPE *)tensor3D_offset(&input, 0, 1, 0) + 8); \ |
| 152 | VEC_DATA_TYPE(ACC_DATA_TYPE, 8) \ |
| 153 | data20 = VLOAD_AND_CONVERT_TO_ACC_DATA_TYPE(8, 0, (__global DATA_TYPE *)tensor3D_offset(&input, 0, 2, 0)); \ |
| 154 | VEC_DATA_TYPE(ACC_DATA_TYPE, 4) \ |
| 155 | data21 = VLOAD_AND_CONVERT_TO_ACC_DATA_TYPE(4, 0, (__global DATA_TYPE *)tensor3D_offset(&input, 0, 2, 0) + 8); \ |
| 156 | data00 = POW2_OP(data00, 8); \ |
| 157 | data01 = POW2_OP(data01, 4); \ |
| 158 | data10 = POW2_OP(data10, 8); \ |
| 159 | data11 = POW2_OP(data11, 4); \ |
| 160 | data20 = POW2_OP(data20, 8); \ |
| 161 | data21 = POW2_OP(data21, 4); \ |
Gian Marco Iodice | cb29283 | 2017-08-02 13:19:48 +0100 | [diff] [blame] | 162 | \ |
Sang-Hoon Park | 2aa7fd0 | 2019-09-18 13:39:00 +0100 | [diff] [blame] | 163 | data00 = POOL_OP(data00, data10); \ |
| 164 | data01 = POOL_OP(data01, data11); \ |
| 165 | data00 = POOL_OP(data00, data20); \ |
| 166 | data01 = POOL_OP(data01, data21); \ |
Gian Marco Iodice | cb29283 | 2017-08-02 13:19:48 +0100 | [diff] [blame] | 167 | \ |
Sang-Hoon Park | 2aa7fd0 | 2019-09-18 13:39:00 +0100 | [diff] [blame] | 168 | res = POOL_OP((VEC_DATA_TYPE(ACC_DATA_TYPE, 4))(data00.s036, data01.s1), (VEC_DATA_TYPE(ACC_DATA_TYPE, 4))(data00.s147, data01.s2)); \ |
| 169 | res = POOL_OP(res, (VEC_DATA_TYPE(ACC_DATA_TYPE, 4))(data00.s25, data01.s03)); \ |
Gian Marco Iodice | cb29283 | 2017-08-02 13:19:48 +0100 | [diff] [blame] | 170 | }) |
| 171 | |
Sang-Hoon Park | 2aa7fd0 | 2019-09-18 13:39:00 +0100 | [diff] [blame] | 172 | ACC_DATA_TYPE calculate_avg_scale(const int pool_size_x, const int pool_size_y, const int upper_bound_w, const int upper_bound_h, |
| 173 | const int pad_x, const int pad_y, const int stride_x, const int stride_y) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 174 | { |
Georgios Pinitas | adaae7e | 2017-10-30 15:56:32 +0000 | [diff] [blame] | 175 | int start_x = get_global_id(0) * stride_x - pad_x; |
| 176 | int start_y = get_global_id(1) * stride_y - pad_y; |
Isabella Gottardi | a527e8c | 2018-01-31 17:49:25 +0000 | [diff] [blame] | 177 | const int end_x = min(start_x + pool_size_x, upper_bound_w); |
| 178 | const int end_y = min(start_y + pool_size_y, upper_bound_h); |
Georgios Pinitas | adaae7e | 2017-10-30 15:56:32 +0000 | [diff] [blame] | 179 | #if defined(EXCLUDE_PADDING) |
| 180 | start_x = max(0, start_x); |
| 181 | start_y = max(0, start_y); |
| 182 | #endif /* defined(EXCLUDE_PADDING) */ |
steniu01 | 0c7614f | 2017-06-23 17:00:26 +0100 | [diff] [blame] | 183 | return ((end_y - start_y) * (end_x - start_x)); |
Gian Marco Iodice | cb29283 | 2017-08-02 13:19:48 +0100 | [diff] [blame] | 184 | } |
| 185 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 186 | /** Performs a pooling function of pool size equal to 2. |
| 187 | * |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 188 | * @note Datatype must be passed using -DDATA_TYPE e.g. -DDATA_TYPE=float. Supported data types are F16/F32; |
Gian Marco Iodice | cb29283 | 2017-08-02 13:19:48 +0100 | [diff] [blame] | 189 | * @note In case of average pooling the following information must be passed at compile time: |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 190 | * -DPOOL_AVG or -DPOOL_L2 must be provided otherwise max pooling will be performed. |
Gian Marco Iodice | cb29283 | 2017-08-02 13:19:48 +0100 | [diff] [blame] | 191 | * -DMAX_WIDTH and -DMAX_HEIGHT which are the maximum accessible indeces in x and y dimensions (width + pad) |
| 192 | * -DSTRIDE_X and -DSTRIDE_Y which are the steps of the window along the x and y directions |
| 193 | * -DPAD_X and -DPAD_Y which are the pooling paddings in x and y dimension |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 194 | * |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 195 | * @param[in] input_ptr Pointer to the source tensor. Supported data types: F16/F32 |
| 196 | * @param[in] input_stride_x Stride of the source tensor in X dimension (in bytes) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 197 | * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes) |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 198 | * @param[in] input_stride_y Stride of the source tensor in Y dimension (in bytes) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 199 | * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes) |
| 200 | * @param[in] input_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 201 | * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes) |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 202 | * @param[in] input_offset_first_element_in_bytes The offset of the first element in the source tensor |
| 203 | * @param[out] output_ptr Pointer to the destination tensor. Supported data types: same as @p input_ptr |
| 204 | * @param[in] output_stride_x Stride of the destination tensor in X dimension (in bytes) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 205 | * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes) |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 206 | * @param[in] output_stride_y Stride of the destination tensor in Y dimension (in bytes) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 207 | * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes) |
| 208 | * @param[in] output_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 209 | * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes) |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 210 | * @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] | 211 | */ |
| 212 | __kernel void pooling_layer_2( |
| 213 | TENSOR3D_DECLARATION(input), |
Gian Marco Iodice | cb29283 | 2017-08-02 13:19:48 +0100 | [diff] [blame] | 214 | TENSOR3D_DECLARATION(output)) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 215 | { |
| 216 | // Get pixels pointer |
| 217 | Tensor3D input = CONVERT_TO_TENSOR3D_STRUCT(input); |
| 218 | Tensor3D output = CONVERT_TO_TENSOR3D_STRUCT(output); |
| 219 | |
| 220 | // Load data |
Sang-Hoon Park | 2aa7fd0 | 2019-09-18 13:39:00 +0100 | [diff] [blame] | 221 | VEC_DATA_TYPE(ACC_DATA_TYPE, 2) |
| 222 | data0 = VLOAD_AND_CONVERT_TO_ACC_DATA_TYPE(2, 0, (__global DATA_TYPE *)tensor3D_offset(&input, 0, 0, 0)); |
| 223 | VEC_DATA_TYPE(ACC_DATA_TYPE, 2) |
| 224 | data1 = VLOAD_AND_CONVERT_TO_ACC_DATA_TYPE(2, 0, (__global DATA_TYPE *)tensor3D_offset(&input, 0, 1, 0)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 225 | |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 226 | #if defined(POOL_L2) |
| 227 | // Raise to power of 2 for L2 Pooling |
| 228 | data0 = POW2_OP(data0, 2); |
| 229 | data1 = POW2_OP(data1, 2); |
| 230 | #endif /* defined(POOL_L2) */ |
| 231 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 232 | // Perform calculations |
Sang-Hoon Park | 2aa7fd0 | 2019-09-18 13:39:00 +0100 | [diff] [blame] | 233 | data0 = POOL_OP(data0, data1); |
| 234 | ACC_DATA_TYPE res = POOL_OP(data0.s0, data0.s1); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 235 | |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 236 | #if defined(POOL_AVG) || defined(POOL_L2) |
| 237 | // Divide by pool region in case of average or l2 pooling |
Isabella Gottardi | a527e8c | 2018-01-31 17:49:25 +0000 | [diff] [blame] | 238 | res = DIV_OP(res, calculate_avg_scale(2, 2, MAX_WIDTH, MAX_HEIGHT, PAD_X, PAD_Y, STRIDE_X, STRIDE_Y)); |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 239 | #endif /* defined(POOL_AVG) || defined(POOL_L2) */ |
| 240 | |
| 241 | #if defined(POOL_L2) |
| 242 | // Take square root of the result in L2 pooling |
| 243 | res = SQRT_OP(res); |
| 244 | #endif /* defined(POOL_L2) */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 245 | |
| 246 | // Store result |
Sang-Hoon Park | 2aa7fd0 | 2019-09-18 13:39:00 +0100 | [diff] [blame] | 247 | *(__global DATA_TYPE *)output.ptr = (DATA_TYPE)res; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 248 | } |
| 249 | |
Gian Marco Iodice | cb29283 | 2017-08-02 13:19:48 +0100 | [diff] [blame] | 250 | /** Performs a pooling function of pool size equal to 3 |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 251 | * |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 252 | * @note Datatype must be passed using -DDATA_TYPE e.g. -DDATA_TYPE=float. Supported data types are F16/F32; |
Gian Marco Iodice | cb29283 | 2017-08-02 13:19:48 +0100 | [diff] [blame] | 253 | * @note In case of average pooling the following information must be passed at compile time: |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 254 | * -DPOOL_AVG or -DPOOL_L2 must be provided otherwise max pooling will be performed. |
Gian Marco Iodice | cb29283 | 2017-08-02 13:19:48 +0100 | [diff] [blame] | 255 | * -DMAX_WIDTH and -DMAX_HEIGHT which are the maximum accessible indeces in x and y dimensions (width + pad) |
| 256 | * -DSTRIDE_X and -DSTRIDE_Y which are the steps of the window along the x and y directions |
| 257 | * -DPAD_X and -DPAD_Y which are the pooling paddings in x and y dimension |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 258 | * |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 259 | * @param[in] input_ptr Pointer to the source tensor. Supported data types: F16/F32 |
| 260 | * @param[in] input_stride_x Stride of the source tensor in X dimension (in bytes) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 261 | * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes) |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 262 | * @param[in] input_stride_y Stride of the source tensor in Y dimension (in bytes) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 263 | * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes) |
| 264 | * @param[in] input_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 265 | * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes) |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 266 | * @param[in] input_offset_first_element_in_bytes The offset of the first element in the source tensor |
| 267 | * @param[out] output_ptr Pointer to the destination tensor. Supported data types: same as @p input_ptr |
| 268 | * @param[in] output_stride_x Stride of the destination tensor in X dimension (in bytes) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 269 | * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes) |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 270 | * @param[in] output_stride_y Stride of the destination tensor in Y dimension (in bytes) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 271 | * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes) |
| 272 | * @param[in] output_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 273 | * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes) |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 274 | * @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] | 275 | */ |
| 276 | __kernel void pooling_layer_3( |
| 277 | TENSOR3D_DECLARATION(input), |
Gian Marco Iodice | cb29283 | 2017-08-02 13:19:48 +0100 | [diff] [blame] | 278 | TENSOR3D_DECLARATION(output)) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 279 | { |
| 280 | // Get pixels pointer |
| 281 | Tensor3D input = CONVERT_TO_TENSOR3D_STRUCT(input); |
| 282 | Tensor3D output = CONVERT_TO_TENSOR3D_STRUCT(output); |
| 283 | |
| 284 | // Load data |
Sang-Hoon Park | 2aa7fd0 | 2019-09-18 13:39:00 +0100 | [diff] [blame] | 285 | VEC_DATA_TYPE(ACC_DATA_TYPE, 3) |
| 286 | data0 = VLOAD_AND_CONVERT_TO_ACC_DATA_TYPE(3, 0, (__global DATA_TYPE *)tensor3D_offset(&input, 0, 0, 0)); |
| 287 | VEC_DATA_TYPE(ACC_DATA_TYPE, 3) |
| 288 | data1 = VLOAD_AND_CONVERT_TO_ACC_DATA_TYPE(3, 0, (__global DATA_TYPE *)tensor3D_offset(&input, 0, 1, 0)); |
| 289 | VEC_DATA_TYPE(ACC_DATA_TYPE, 3) |
| 290 | data2 = VLOAD_AND_CONVERT_TO_ACC_DATA_TYPE(3, 0, (__global DATA_TYPE *)tensor3D_offset(&input, 0, 2, 0)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 291 | |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 292 | #if defined(POOL_L2) |
| 293 | // Raise to power of 2 for L2 Pooling |
| 294 | data0 = POW2_OP(data0, 3); |
| 295 | data1 = POW2_OP(data1, 3); |
| 296 | data2 = POW2_OP(data2, 3); |
| 297 | #endif /* defined(POOL_L2) */ |
| 298 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 299 | // Perform calculations |
Sang-Hoon Park | 2aa7fd0 | 2019-09-18 13:39:00 +0100 | [diff] [blame] | 300 | data0 = POOL_OP(data0, data1); |
| 301 | data0 = POOL_OP(data0, data2); |
| 302 | ACC_DATA_TYPE res = POOL_OP(POOL_OP(data0.s0, data0.s1), data0.s2); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 303 | |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 304 | #if defined(POOL_AVG) || defined(POOL_L2) |
Georgios Pinitas | ce09314 | 2017-06-19 16:11:53 +0100 | [diff] [blame] | 305 | // Divide by pool region in case of average pooling |
Isabella Gottardi | a527e8c | 2018-01-31 17:49:25 +0000 | [diff] [blame] | 306 | res = DIV_OP(res, calculate_avg_scale(3, 3, MAX_WIDTH, MAX_HEIGHT, PAD_X, PAD_Y, STRIDE_X, STRIDE_Y)); |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 307 | #endif /* defined(POOL_AVG) || defined(POOL_L2) */ |
| 308 | |
| 309 | #if defined(POOL_L2) |
| 310 | // Take square root of the result in L2 pooling |
| 311 | res = SQRT_OP(res); |
| 312 | #endif /* defined(POOL_L2) */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 313 | |
| 314 | // Store result |
Sang-Hoon Park | 2aa7fd0 | 2019-09-18 13:39:00 +0100 | [diff] [blame] | 315 | *(__global DATA_TYPE *)output.ptr = (DATA_TYPE)res; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 316 | } |
Georgios Pinitas | ce09314 | 2017-06-19 16:11:53 +0100 | [diff] [blame] | 317 | |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 318 | #if defined(POOLING3x3) |
steniu01 | 0c7614f | 2017-06-23 17:00:26 +0100 | [diff] [blame] | 319 | |
| 320 | #define CONVERT_OP(data_type) convert_##data_type##4 |
| 321 | #define CONVERT_VECTOR4(data_type) CONVERT_OP(data_type) |
| 322 | |
Sang-Hoon Park | 2aa7fd0 | 2019-09-18 13:39:00 +0100 | [diff] [blame] | 323 | VEC_DATA_TYPE(ACC_DATA_TYPE, 4) |
steniu01 | 0c7614f | 2017-06-23 17:00:26 +0100 | [diff] [blame] | 324 | calculate_avg_scale4(const int pool_size, const int upper_bound_w, const int upper_bound_h, |
| 325 | const int pad_x, const int pad_y, const int stride_x, const int stride_y) |
| 326 | { |
Georgios Pinitas | adaae7e | 2017-10-30 15:56:32 +0000 | [diff] [blame] | 327 | int4 start_x = ((int4)get_global_id(0) * 4 + (int4)(0, 1, 2, 3)) * (int4)stride_x - (int4)pad_x; |
| 328 | int start_y = get_global_id(1) * stride_y - pad_y; |
steniu01 | 0c7614f | 2017-06-23 17:00:26 +0100 | [diff] [blame] | 329 | const int4 end_x = min(start_x + (int4)pool_size, (int4)upper_bound_w); |
| 330 | const int end_y = min(start_y + pool_size, upper_bound_h); |
Georgios Pinitas | adaae7e | 2017-10-30 15:56:32 +0000 | [diff] [blame] | 331 | #if defined(EXCLUDE_PADDING) |
| 332 | start_x = max((int4)0, start_x); |
| 333 | start_y = max(0, start_y); |
| 334 | #endif /* defined(EXCLUDE_PADDING) */ |
Sang-Hoon Park | 2aa7fd0 | 2019-09-18 13:39:00 +0100 | [diff] [blame] | 335 | return (VEC_DATA_TYPE(ACC_DATA_TYPE, 4))(1.f) / CONVERT_VECTOR4(ACC_DATA_TYPE)(((int4)(end_y - start_y)) * (end_x - start_x)); |
steniu01 | 0c7614f | 2017-06-23 17:00:26 +0100 | [diff] [blame] | 336 | } |
| 337 | |
Gian Marco Iodice | cb29283 | 2017-08-02 13:19:48 +0100 | [diff] [blame] | 338 | /** Performs an optimized pooling function of pool size equal to 3 when the stride_x is less equal than 3 |
Georgios Pinitas | ce09314 | 2017-06-19 16:11:53 +0100 | [diff] [blame] | 339 | * |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 340 | * @note Datatype must be passed using -DDATA_TYPE e.g. -DDATA_TYPE=float. Supported data types are F16/F32; |
Gian Marco Iodice | cb29283 | 2017-08-02 13:19:48 +0100 | [diff] [blame] | 341 | * @note In case of average pooling the following information must be passed at compile time: |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 342 | * -DPOOL_AVG or -DPOOL_L2 must be provided otherwise max pooling will be performed. |
Gian Marco Iodice | cb29283 | 2017-08-02 13:19:48 +0100 | [diff] [blame] | 343 | * -DMAX_WIDTH and -DMAX_HEIGHT which are the maximum accessible indeces in x and y dimensions (width + pad) |
| 344 | * -DSTRIDE_X and -DSTRIDE_Y which are the steps of the window along the x and y directions |
| 345 | * -DPAD_X and -DPAD_Y which are the pooling paddings in x and y dimension |
Georgios Pinitas | ce09314 | 2017-06-19 16:11:53 +0100 | [diff] [blame] | 346 | * |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 347 | * @param[in] input_ptr Pointer to the source tensor. Supported data types: F16/F32 |
| 348 | * @param[in] input_stride_x Stride of the source tensor in X dimension (in bytes) |
Georgios Pinitas | ce09314 | 2017-06-19 16:11:53 +0100 | [diff] [blame] | 349 | * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes) |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 350 | * @param[in] input_stride_y Stride of the source tensor in Y dimension (in bytes) |
Georgios Pinitas | ce09314 | 2017-06-19 16:11:53 +0100 | [diff] [blame] | 351 | * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes) |
| 352 | * @param[in] input_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 353 | * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes) |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 354 | * @param[in] input_offset_first_element_in_bytes The offset of the first element in the source tensor |
| 355 | * @param[out] output_ptr Pointer to the destination tensor. Supported data types: same as @p input_ptr |
| 356 | * @param[in] output_stride_x Stride of the destination tensor in X dimension (in bytes) |
Georgios Pinitas | ce09314 | 2017-06-19 16:11:53 +0100 | [diff] [blame] | 357 | * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes) |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 358 | * @param[in] output_stride_y Stride of the destination tensor in Y dimension (in bytes) |
Georgios Pinitas | ce09314 | 2017-06-19 16:11:53 +0100 | [diff] [blame] | 359 | * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes) |
| 360 | * @param[in] output_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 361 | * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes) |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 362 | * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination tensor |
Gian Marco Iodice | cb29283 | 2017-08-02 13:19:48 +0100 | [diff] [blame] | 363 | */ |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 364 | __kernel void pooling_layer_optimized_3( |
Gian Marco Iodice | cb29283 | 2017-08-02 13:19:48 +0100 | [diff] [blame] | 365 | TENSOR3D_DECLARATION(input), |
| 366 | TENSOR3D_DECLARATION(output)) |
| 367 | { |
| 368 | // Get pixels pointer |
| 369 | Tensor3D input = CONVERT_TO_TENSOR3D_STRUCT(input); |
| 370 | Tensor3D output = CONVERT_TO_TENSOR3D_STRUCT(output); |
| 371 | |
Sang-Hoon Park | 2aa7fd0 | 2019-09-18 13:39:00 +0100 | [diff] [blame] | 372 | VEC_DATA_TYPE(ACC_DATA_TYPE, 4) |
Gian Marco Iodice | cb29283 | 2017-08-02 13:19:48 +0100 | [diff] [blame] | 373 | res; |
| 374 | |
| 375 | // Perform pooling 3x3 for 4 output elements |
| 376 | POOLING3x3(res, input, output); |
| 377 | |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 378 | #if defined(POOL_AVG) || defined(POOL_L2) |
Gian Marco Iodice | cb29283 | 2017-08-02 13:19:48 +0100 | [diff] [blame] | 379 | // Divide by pool region in case of average pooling |
Gian Marco Iodice | cb29283 | 2017-08-02 13:19:48 +0100 | [diff] [blame] | 380 | res *= calculate_avg_scale4(3, MAX_WIDTH, MAX_HEIGHT, PAD_X, PAD_Y, STRIDE_X, STRIDE_Y); |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 381 | #endif /* defined(POOL_AVG) || defined(POOL_L2) */ |
| 382 | |
| 383 | #if defined(POOL_L2) |
| 384 | // Take square root of the result in L2 pooling |
| 385 | res = SQRT_OP(res); |
| 386 | #endif /* defined(POOL_L2) */ |
Gian Marco Iodice | cb29283 | 2017-08-02 13:19:48 +0100 | [diff] [blame] | 387 | |
Sang-Hoon Park | 2aa7fd0 | 2019-09-18 13:39:00 +0100 | [diff] [blame] | 388 | vstore4(CONVERT(res, VEC_DATA_TYPE(DATA_TYPE, 4)), 0, (__global DATA_TYPE *)output.ptr); |
Gian Marco Iodice | cb29283 | 2017-08-02 13:19:48 +0100 | [diff] [blame] | 389 | } |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 390 | #endif // defined(POOLING3x3) |
Gian Marco Iodice | cb29283 | 2017-08-02 13:19:48 +0100 | [diff] [blame] | 391 | |
Isabella Gottardi | a527e8c | 2018-01-31 17:49:25 +0000 | [diff] [blame] | 392 | #if defined(POOL_SIZE_X) && defined(POOL_SIZE_Y) |
Gian Marco Iodice | bf17955 | 2017-09-05 13:51:21 +0100 | [diff] [blame] | 393 | |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 394 | /** Performs a pooling function of pool size equal to N (NCHW) |
Gian Marco Iodice | bf17955 | 2017-09-05 13:51:21 +0100 | [diff] [blame] | 395 | * |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 396 | * @note Datatype must be passed using -DDATA_TYPE e.g. -DDATA_TYPE=float. Supported data types are F16/F32; |
Isabella Gottardi | a527e8c | 2018-01-31 17:49:25 +0000 | [diff] [blame] | 397 | * @note Pool sizes must be passed using -DPOOL_SIZE_X and -DPOOL_SIZE_Y e.g. -DPOOL_SIZE_X=13; |
Gian Marco Iodice | bf17955 | 2017-09-05 13:51:21 +0100 | [diff] [blame] | 398 | * @note In case of average pooling the following information must be passed at compile time: |
| 399 | * -DPOOL_AVG must be provided otherwise max pooling will be performed. |
| 400 | * -DMAX_WIDTH and -DMAX_HEIGHT which are the maximum accessible indeces in x and y dimensions (width + pad) |
| 401 | * -DSTRIDE_X and -DSTRIDE_Y which are the steps of the window along the x and y directions |
| 402 | * -DPAD_X and -DPAD_Y which are the pooling paddings in x and y dimension |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame] | 403 | * @note The initial value for the pooling operation must be passed at compile time using -DINITIAL_VALUE e.g. -DINITIAL_VALUE=0 |
Gian Marco Iodice | bf17955 | 2017-09-05 13:51:21 +0100 | [diff] [blame] | 404 | * |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 405 | * @param[in] input_ptr Pointer to the source tensor. Supported data types: F16/F32 |
| 406 | * @param[in] input_stride_x Stride of the source tensor in X dimension (in bytes) |
Gian Marco Iodice | bf17955 | 2017-09-05 13:51:21 +0100 | [diff] [blame] | 407 | * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes) |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 408 | * @param[in] input_stride_y Stride of the source tensor in Y dimension (in bytes) |
Gian Marco Iodice | bf17955 | 2017-09-05 13:51:21 +0100 | [diff] [blame] | 409 | * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes) |
| 410 | * @param[in] input_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 411 | * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes) |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 412 | * @param[in] input_offset_first_element_in_bytes The offset of the first element in the source tensor |
| 413 | * @param[out] output_ptr Pointer to the destination tensor. Supported data types: same as @p input_ptr |
| 414 | * @param[in] output_stride_x Stride of the destination tensor in X dimension (in bytes) |
Gian Marco Iodice | bf17955 | 2017-09-05 13:51:21 +0100 | [diff] [blame] | 415 | * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes) |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 416 | * @param[in] output_stride_y Stride of the destination tensor in Y dimension (in bytes) |
Gian Marco Iodice | bf17955 | 2017-09-05 13:51:21 +0100 | [diff] [blame] | 417 | * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes) |
| 418 | * @param[in] output_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 419 | * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes) |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 420 | * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination tensor |
Gian Marco Iodice | bf17955 | 2017-09-05 13:51:21 +0100 | [diff] [blame] | 421 | */ |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 422 | __kernel void pooling_layer_MxN_nchw( |
Gian Marco Iodice | bf17955 | 2017-09-05 13:51:21 +0100 | [diff] [blame] | 423 | TENSOR3D_DECLARATION(input), |
| 424 | TENSOR3D_DECLARATION(output)) |
| 425 | { |
| 426 | // Get pixels pointer |
| 427 | Tensor3D input = CONVERT_TO_TENSOR3D_STRUCT(input); |
| 428 | Tensor3D output = CONVERT_TO_TENSOR3D_STRUCT(output); |
| 429 | |
Sang-Hoon Park | 2aa7fd0 | 2019-09-18 13:39:00 +0100 | [diff] [blame] | 430 | VEC_DATA_TYPE(ACC_DATA_TYPE, 8) |
| 431 | vdata = INITIAL_VALUE; |
| 432 | ACC_DATA_TYPE sdata = INITIAL_VALUE; |
Gian Marco Iodice | bf17955 | 2017-09-05 13:51:21 +0100 | [diff] [blame] | 433 | |
| 434 | // Load data |
Isabella Gottardi | a527e8c | 2018-01-31 17:49:25 +0000 | [diff] [blame] | 435 | for(int y = 0; y < POOL_SIZE_Y; y++) |
Gian Marco Iodice | bf17955 | 2017-09-05 13:51:21 +0100 | [diff] [blame] | 436 | { |
| 437 | int x = 0; |
Isabella Gottardi | a527e8c | 2018-01-31 17:49:25 +0000 | [diff] [blame] | 438 | for(; x <= ((int)POOL_SIZE_X - 8); x += 8) |
Gian Marco Iodice | bf17955 | 2017-09-05 13:51:21 +0100 | [diff] [blame] | 439 | { |
Sang-Hoon Park | 2aa7fd0 | 2019-09-18 13:39:00 +0100 | [diff] [blame] | 440 | VEC_DATA_TYPE(ACC_DATA_TYPE, 8) |
| 441 | data0 = VLOAD_AND_CONVERT_TO_ACC_DATA_TYPE(8, 0, (__global DATA_TYPE *)tensor3D_offset(&input, x, y, 0)); |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 442 | #if defined(POOL_L2) |
| 443 | // Raise to power of 2 for L2 Pooling |
| 444 | data0 *= data0; |
| 445 | #endif /* defined(POOL_L2) */ |
Gian Marco Iodice | bf17955 | 2017-09-05 13:51:21 +0100 | [diff] [blame] | 446 | vdata = POOL_OP(vdata, data0); |
| 447 | } |
| 448 | |
| 449 | // Leftover |
Isabella Gottardi | a527e8c | 2018-01-31 17:49:25 +0000 | [diff] [blame] | 450 | for(; x < (int)POOL_SIZE_X; ++x) |
Gian Marco Iodice | bf17955 | 2017-09-05 13:51:21 +0100 | [diff] [blame] | 451 | { |
Sang-Hoon Park | 2aa7fd0 | 2019-09-18 13:39:00 +0100 | [diff] [blame] | 452 | ACC_DATA_TYPE data0 = (ACC_DATA_TYPE)(*((__global DATA_TYPE *)tensor3D_offset(&input, x, y, 0))); |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 453 | #if defined(POOL_L2) |
| 454 | // Raise to power of 2 for L2 Pooling |
| 455 | data0 *= data0; |
| 456 | #endif /* defined(POOL_L2) */ |
| 457 | sdata = POOL_OP(sdata, data0); |
Gian Marco Iodice | bf17955 | 2017-09-05 13:51:21 +0100 | [diff] [blame] | 458 | } |
| 459 | } |
| 460 | |
| 461 | // Reduce result |
Sang-Hoon Park | 2aa7fd0 | 2019-09-18 13:39:00 +0100 | [diff] [blame] | 462 | VEC_DATA_TYPE(ACC_DATA_TYPE, 4) |
Gian Marco Iodice | bf17955 | 2017-09-05 13:51:21 +0100 | [diff] [blame] | 463 | reduce4 = POOL_OP(vdata.s0123, vdata.s4567); |
Sang-Hoon Park | 2aa7fd0 | 2019-09-18 13:39:00 +0100 | [diff] [blame] | 464 | VEC_DATA_TYPE(ACC_DATA_TYPE, 2) |
| 465 | reduce2 = POOL_OP(reduce4.s01, reduce4.s23); |
| 466 | ACC_DATA_TYPE res = POOL_OP(reduce2.s0, reduce2.s1); |
| 467 | res = POOL_OP(res, sdata); |
Gian Marco Iodice | bf17955 | 2017-09-05 13:51:21 +0100 | [diff] [blame] | 468 | |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 469 | #if defined(POOL_AVG) || defined(POOL_L2) |
Gian Marco Iodice | bf17955 | 2017-09-05 13:51:21 +0100 | [diff] [blame] | 470 | // Divide by pool region in case of average pooling |
Isabella Gottardi | a527e8c | 2018-01-31 17:49:25 +0000 | [diff] [blame] | 471 | res = DIV_OP(res, calculate_avg_scale(POOL_SIZE_X, POOL_SIZE_Y, MAX_WIDTH, MAX_HEIGHT, PAD_X, PAD_Y, STRIDE_X, STRIDE_Y)); |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 472 | #endif /* defined(POOL_AVG) || defined(POOL_L2) */ |
| 473 | |
| 474 | #if defined(POOL_L2) |
| 475 | // Take square root of the result in L2 pooling |
| 476 | res = SQRT_OP(res); |
| 477 | #endif /* defined(POOL_L2) */ |
Gian Marco Iodice | bf17955 | 2017-09-05 13:51:21 +0100 | [diff] [blame] | 478 | |
| 479 | // Store result |
Sang-Hoon Park | 2aa7fd0 | 2019-09-18 13:39:00 +0100 | [diff] [blame] | 480 | *(__global DATA_TYPE *)output.ptr = (DATA_TYPE)res; |
Gian Marco Iodice | bf17955 | 2017-09-05 13:51:21 +0100 | [diff] [blame] | 481 | } |
Isabella Gottardi | a527e8c | 2018-01-31 17:49:25 +0000 | [diff] [blame] | 482 | #endif // defined(POOL_SIZE_X) && defined(POOL_SIZE_Y) |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 483 | |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 484 | #if defined(PAD_TENSOR_LEFT) && defined(PAD_TENSOR_RIGHT) && defined(PAD_TENSOR_TOP) && defined(PAD_TENSOR_BOTTOM) |
| 485 | |
| 486 | inline void offset_no_padding_nchw(const Tensor3D *input, uint *offset_top, uint *offset_bottom) |
| 487 | { |
| 488 | const int pad_horiz = PAD_TENSOR_LEFT + PAD_TENSOR_RIGHT; |
| 489 | const int pad_vert = PAD_TENSOR_TOP + PAD_TENSOR_BOTTOM; |
| 490 | |
| 491 | const int x = get_global_id(0) * STRIDE_X; |
| 492 | const int y = get_global_id(1) * STRIDE_Y; |
| 493 | const int z = get_global_id(2); |
| 494 | |
| 495 | //x axis: width, y axis: height, z axis: component |
| 496 | const uint padded_offset = input->offset_first_element_in_bytes |
| 497 | + x * input->stride_x |
| 498 | + y * input->stride_y |
| 499 | + z * input->stride_z; |
| 500 | |
| 501 | const uint offset_base = padded_offset |
| 502 | - y * pad_horiz * sizeof(DATA_TYPE) /* Horizontal padding for each row */ |
| 503 | - PAD_TENSOR_TOP * input->stride_y /* top padding */ |
| 504 | - z * MAX_HEIGHT * pad_horiz * sizeof(DATA_TYPE) - z * pad_vert * input->stride_y /* Z plane padding */ |
| 505 | - PAD_TENSOR_LEFT * sizeof(DATA_TYPE); |
| 506 | |
| 507 | #if defined(TENSOR_CHANNEL) && defined(TENSOR_WIDTH) && defined(TENSOR_HEIGHT) |
| 508 | *offset_top = (uint)((offset_base / sizeof(DATA_TYPE)) % (TENSOR_CHANNEL * TENSOR_WIDTH * TENSOR_HEIGHT)); |
| 509 | #else /* defined(TENSOR_CHANNEL) && defined(TENSOR_WIDTH) && defined(TENSOR_HEIGHT) */ |
Gian Marco Iodice | 40471d1 | 2021-04-26 08:39:28 +0100 | [diff] [blame^] | 510 | *offset_top = (uint)(offset_base / sizeof(DATA_TYPE)); |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 511 | #endif /* defined(TENSOR_CHANNEL) && defined(TENSOR_WIDTH) && defined(TENSOR_HEIGHT) */ |
| 512 | |
| 513 | *offset_bottom = *offset_top + input->stride_y / sizeof(DATA_TYPE) - pad_horiz; |
| 514 | |
| 515 | return; |
| 516 | } |
| 517 | |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 518 | #endif //defined(PAD_TENSOR_LEFT) && defined(PAD_TENSOR_RIGHT) && defined(PAD_TENSOR_TOP) && defined(PAD_TENSOR_BOTTOM) |
| 519 | |
| 520 | /** Performs a MAX pooling of pool size equal to 2, and record max value indices for NCHW. |
| 521 | * |
| 522 | * @note Datatype must be passed using -DDATA_TYPE e.g. -DDATA_TYPE=half. Supported data types are F32 |
| 523 | * @note Pool sizes must be passed using -DPOOL_SIZE_X and -DPOOL_SIZE_Y e.g. -DPOOL_SIZE_X=13; |
| 524 | * @note Tensors width and height must be passed at compile time using -DMAX_WIDTH and -DMAX_HEIGHT |
| 525 | * @note Pool strides must be passed at compile time using -DSTRIDE_X and -DSTRIDE_Y which are the steps of the window along the x and y directions |
| 526 | * @note Tensor padding values must be passed at compile time using PAD_TENSOR_LEFT, PAD_TENSOR_RIGHT, PAD_TENSOR_TOP and PAD_TENSOR_BOTTOM |
| 527 | * |
| 528 | * @param[in] input_ptr Pointer to the source tensor. Supported data types: F32 |
| 529 | * @param[in] input_stride_x Stride of the source tensor in X dimension (in bytes) |
| 530 | * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes) |
| 531 | * @param[in] input_stride_y Stride of the source tensor in Y dimension (in bytes) |
| 532 | * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes) |
| 533 | * @param[in] input_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 534 | * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes) |
| 535 | * @param[in] input_offset_first_element_in_bytes The offset of the first element in the source tensor |
| 536 | * @param[out] output_ptr Pointer to the destination tensor. Supported data types: same as @p input_ptr |
| 537 | * @param[in] output_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 538 | * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes) |
| 539 | * @param[in] output_stride_y Stride of the destination tensor in Y dimension (in bytes) |
| 540 | * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes) |
| 541 | * @param[in] output_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 542 | * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes) |
| 543 | * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination tensor |
| 544 | * @param[in] indices_ptr Pointer to the indices tensor. Supported data types: U32 |
| 545 | * @param[in] indices_stride_x Stride of the indices tensor in X dimension (in bytes) |
| 546 | * @param[in] indices_step_x indices_stride_x * number of elements along X processed per workitem(in bytes) |
| 547 | * @param[in] indices_stride_y Stride of the indices tensor in Y dimension (in bytes) |
| 548 | * @param[in] indices_step_y indices_stride_y * number of elements along Y processed per workitem(in bytes) |
| 549 | * @param[in] indices_stride_z Stride of the indices tensor in Z dimension (in bytes) |
| 550 | * @param[in] indices_step_z indices_stride_z * number of elements along Z processed per workitem(in bytes) |
| 551 | * @param[in] indices_offset_first_element_in_bytes The offset of the first element in the indices tensor |
| 552 | */ |
| 553 | __kernel void pooling_layer_2_nchw_indices_fp32( |
| 554 | TENSOR3D_DECLARATION(input), |
| 555 | TENSOR3D_DECLARATION(output), |
| 556 | TENSOR3D_DECLARATION(indices)) |
| 557 | { |
| 558 | // Get pixels pointer |
| 559 | Tensor3D input = CONVERT_TO_TENSOR3D_STRUCT(input); |
| 560 | Tensor3D output = CONVERT_TO_TENSOR3D_STRUCT(output); |
| 561 | Tensor3D indices = CONVERT_TO_TENSOR3D_STRUCT(indices); |
| 562 | |
| 563 | // Load data |
| 564 | float2 data0 = VLOAD(2)(0, (__global float *)tensor3D_offset(&input, 0, 0, 0)); |
| 565 | float2 data1 = VLOAD(2)(0, (__global float *)tensor3D_offset(&input, 0, 1, 0)); |
| 566 | |
| 567 | // Perform calculations |
| 568 | float data0_max = POOL_OP(data0.s0, data0.s1); |
| 569 | float data1_max = POOL_OP(data1.s0, data1.s1); |
| 570 | float res = POOL_OP(data0_max, data1_max); |
| 571 | // Store result |
| 572 | *(__global float *)output.ptr = res; |
| 573 | |
| 574 | #if defined(PAD_TENSOR_LEFT) && defined(PAD_TENSOR_RIGHT) && defined(PAD_TENSOR_TOP) && defined(PAD_TENSOR_BOTTOM) |
| 575 | |
| 576 | uint offset_top = 0; |
| 577 | uint offset_bottom = 0; |
| 578 | |
| 579 | offset_no_padding_nchw(&input, &offset_top, &offset_bottom); |
| 580 | |
| 581 | uint index0 = select(offset_top + 1, offset_top, isgreaterequal(data0.s0, data0.s1)); |
| 582 | uint index1 = select(offset_bottom + 1, offset_bottom, isgreaterequal(data1.s0, data1.s1)); |
| 583 | uint index = select(index1, index0, isgreaterequal(data0_max, data1_max)); |
| 584 | |
| 585 | *(__global uint *)indices.ptr = index; |
| 586 | |
| 587 | #endif //defined(PAD_TENSOR_LEFT) && defined(PAD_TENSOR_RIGHT) && defined(PAD_TENSOR_TOP) && defined(PAD_TENSOR_BOTTOM) |
| 588 | } |
| 589 | |
| 590 | /** Performs a MAX pooling of pool size equal to 2, and record max value indices for NCHW. |
| 591 | * |
| 592 | * @note Datatype must be passed using -DDATA_TYPE e.g. -DDATA_TYPE=half. Supported data types are F16 |
| 593 | * @note Pool sizes must be passed using -DPOOL_SIZE_X and -DPOOL_SIZE_Y e.g. -DPOOL_SIZE_X=13; |
| 594 | * @note Tensors width and height must be passed at compile time using -DMAX_WIDTH and -DMAX_HEIGHT |
| 595 | * @note Pool strides must be passed at compile time using -DSTRIDE_X and -DSTRIDE_Y which are the steps of the window along the x and y directions |
| 596 | * @note Tensor padding values must be passed at compile time using PAD_TENSOR_LEFT, PAD_TENSOR_RIGHT, PAD_TENSOR_TOP and PAD_TENSOR_BOTTOM |
| 597 | * |
| 598 | * @param[in] input_ptr Pointer to the source tensor. Supported data types: F16 |
| 599 | * @param[in] input_stride_x Stride of the source tensor in X dimension (in bytes) |
| 600 | * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes) |
| 601 | * @param[in] input_stride_y Stride of the source tensor in Y dimension (in bytes) |
| 602 | * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes) |
| 603 | * @param[in] input_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 604 | * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes) |
| 605 | * @param[in] input_offset_first_element_in_bytes The offset of the first element in the source tensor |
| 606 | * @param[out] output_ptr Pointer to the destination tensor. Supported data types: same as @p input_ptr |
| 607 | * @param[in] output_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 608 | * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes) |
| 609 | * @param[in] output_stride_y Stride of the destination tensor in Y dimension (in bytes) |
| 610 | * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes) |
| 611 | * @param[in] output_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 612 | * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes) |
| 613 | * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination tensor |
| 614 | * @param[in] indices_ptr Pointer to the indices tensor. Supported data types: U32 |
| 615 | * @param[in] indices_stride_x Stride of the indices tensor in X dimension (in bytes) |
| 616 | * @param[in] indices_step_x indices_stride_x * number of elements along X processed per workitem(in bytes) |
| 617 | * @param[in] indices_stride_y Stride of the indices tensor in Y dimension (in bytes) |
| 618 | * @param[in] indices_step_y indices_stride_y * number of elements along Y processed per workitem(in bytes) |
| 619 | * @param[in] indices_stride_z Stride of the indices tensor in Z dimension (in bytes) |
| 620 | * @param[in] indices_step_z indices_stride_z * number of elements along Z processed per workitem(in bytes) |
| 621 | * @param[in] indices_offset_first_element_in_bytes The offset of the first element in the indices tensor |
| 622 | */ |
| 623 | __kernel void pooling_layer_2_nchw_indices_fp16( |
| 624 | TENSOR3D_DECLARATION(input), |
| 625 | TENSOR3D_DECLARATION(output), |
| 626 | TENSOR3D_DECLARATION(indices)) |
| 627 | { |
| 628 | // Get pixels pointer |
| 629 | Tensor3D input = CONVERT_TO_TENSOR3D_STRUCT(input); |
| 630 | Tensor3D output = CONVERT_TO_TENSOR3D_STRUCT(output); |
| 631 | Tensor3D indices = CONVERT_TO_TENSOR3D_STRUCT(indices); |
| 632 | |
| 633 | // Load data |
| 634 | half2 data0 = VLOAD(2)(0, (__global half *)tensor3D_offset(&input, 0, 0, 0)); |
| 635 | half2 data1 = VLOAD(2)(0, (__global half *)tensor3D_offset(&input, 0, 1, 0)); |
| 636 | |
| 637 | // Perform calculations |
| 638 | half data0_max = POOL_OP(data0.s0, data0.s1); |
| 639 | half data1_max = POOL_OP(data1.s0, data1.s1); |
| 640 | half res = POOL_OP(data0_max, data1_max); |
| 641 | // Store result |
| 642 | *(__global half *)output.ptr = res; |
| 643 | |
| 644 | #if defined(PAD_TENSOR_LEFT) && defined(PAD_TENSOR_RIGHT) && defined(PAD_TENSOR_TOP) && defined(PAD_TENSOR_BOTTOM) |
| 645 | |
| 646 | uint offset_top = 0; |
| 647 | uint offset_bottom = 0; |
| 648 | |
| 649 | offset_no_padding_nchw(&input, &offset_top, &offset_bottom); |
| 650 | |
| 651 | uint index0 = select(offset_top + 1, offset_top, isgreaterequal(data0.s0, data0.s1)); |
| 652 | uint index1 = select(offset_bottom + 1, offset_bottom, isgreaterequal(data1.s0, data1.s1)); |
| 653 | uint index = select(index1, index0, isgreaterequal(data0_max, data1_max)); |
| 654 | |
| 655 | *(__global uint *)indices.ptr = index; |
| 656 | |
| 657 | #endif //defined(PAD_TENSOR_LEFT) && defined(PAD_TENSOR_RIGHT) && defined(PAD_TENSOR_TOP) && defined(PAD_TENSOR_BOTTOM) |
| 658 | } |
| 659 | |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 660 | #if defined(VEC_SIZE) && defined(VEC_SIZE_LEFTOVER) && defined(SRC_WIDTH) && defined(SRC_HEIGHT) && defined(DST_CHANNELS) && defined(DST_HEIGHT) && defined(DST_BATCH_SIZE) && defined(ACC_DATA_TYPE) |
| 661 | |
| 662 | #if defined(POOL_SIZE_X) && defined(POOL_SIZE_Y) |
| 663 | /** Performs pooling layer of size equal to MxN. This OpenCL kernel can perform the following pooling types: |
| 664 | * -# max, -DPOOL_MAX must be passed at compile time |
| 665 | * -# average, -DPOOL_AVG must be passed at compile time. If padding has to be expluded, -DEXCLUDE_PADDING should be passed at compile time |
| 666 | * -# l2 normalisation, -DPOOL_L2 must be passed at compile time |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 667 | * |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 668 | * @note Datatype must be passed at compile type using -DDATA_TYPE e.g. -DDATA_TYPE=half. Supported data types are F32/F16 |
| 669 | * @note Accumulation data type must be passed at compile time using -DACC_DATA_TYPE e.g. -DACC_DATA_TYPE=float |
| 670 | * @note If -DFP_MIXED_PRECISION is passed at compile time, the kernel will use F32 for the partial result |
| 671 | * @note Pool size must be passed at compile time using -DPOOL_SIZE_X and -DPOOL_SIZE_Y. e.g. -DPOOL_SIZE_X=4, -DPOOL_SIZE_Y=4 |
| 672 | * @note Input tensor width and height must be passed at compile time using -DSRC_WIDTH and -DSRC_HEIGHT |
| 673 | * @note Output tensor height, channels and batch size must be passed at compile time using -DDST_HEIGHT, -DDST_CHANNELS and -DDST_BATCH_SIZE |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 674 | * @note Pool strides must be passed at compile time using -DSTRIDE_X and -DSTRIDE_Y which are the steps of the window along the x and y directions |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 675 | * @note Pool pads must be passed at compile time using -DPAD_X and -DPAD_Y |
| 676 | * @note Vector size must be passed at compile time using -DVEC_SIZE=size. e.g. -DVEC_SIZE=16 |
| 677 | * @note Leftover vector size must be passed at compile time using -DVEC_SIZE_LEFTOVER. e.g. -DVEC_SIZE_LEFTOVER=3. It is defined as the remainder between the input's first dimension and VEC_SIZE |
| 678 | * @note The initial value for the pooling operation must be passed at compile time using -DINITIAL_VALUE e.g. -DINITIAL_VALUE=0 |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 679 | * |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 680 | * @param[in] input_ptr Pointer to the source tensor. Supported data types: F32/F16 |
| 681 | * @param[in] input_stride_x Stride of the source tensor in X dimension (in bytes) |
| 682 | * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes) |
| 683 | * @param[in] input_stride_y Stride of the source tensor in Y dimension (in bytes) |
| 684 | * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes) |
| 685 | * @param[in] input_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 686 | * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes) |
| 687 | * @param[in] input_stride_w Stride of the source tensor in W dimension (in bytes) |
| 688 | * @param[in] input_step_w input_stride_w * number of elements along W processed per workitem(in bytes) |
| 689 | * @param[in] input_offset_first_element_in_bytes The offset of the first element in the source tensor |
| 690 | * @param[out] output_ptr Pointer to the destination tensor. Supported data types: same as @p input_ptr |
| 691 | * @param[in] output_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 692 | * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes) |
| 693 | * @param[in] output_stride_y Stride of the destination tensor in Y dimension (in bytes) |
| 694 | * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes) |
| 695 | * @param[in] output_stride_z Stride of the destination tensor in Z dimension (in bytes) |
| 696 | * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes) |
| 697 | * @param[in] output_stride_w Stride of the destination tensor in W dimension (in bytes) |
| 698 | * @param[in] output_step_w output_stride_w * number of elements along W processed per workitem(in bytes) |
| 699 | * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination tensor |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 700 | */ |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 701 | __kernel void pooling_layer_MxN_nhwc( |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 702 | TENSOR4D_DECLARATION(input), |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 703 | TENSOR4D_DECLARATION(output)) |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 704 | { |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 705 | // Note: If C is not multiple of VEC_SIZE, we shift back of VEC_SIZE_LEFTOVER elements to compute the leftover elements for get_global_id(0) == 0 |
| 706 | // Note: If C is less than VEC_SIZE, VEC_SIZE should be SHRINKED to the closest smaller VEC_SIZE. This operation is performed on the host side |
Gian Marco Iodice | 40471d1 | 2021-04-26 08:39:28 +0100 | [diff] [blame^] | 707 | int idx_out_c = GET_SPATIAL_IDX(0, VEC_SIZE, VEC_SIZE_LEFTOVER); |
| 708 | int idx_out_w = GET_SPATIAL_IDX(1, 1, 0); |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 709 | #if DST_BATCH_SIZE != 1 |
| 710 | // If batch size != 1, the batch size dimension is collapsed over the height dimension |
Gian Marco Iodice | 40471d1 | 2021-04-26 08:39:28 +0100 | [diff] [blame^] | 711 | int idx_out_h = GET_SPATIAL_IDX(2, 1, 0) % DST_HEIGHT; |
| 712 | int idx_out_n = GET_SPATIAL_IDX(2, 1, 0) / DST_HEIGHT; |
| 713 | #else //DST_BATCH_SIZE != 1 |
| 714 | int idx_out_h = GET_SPATIAL_IDX(2, 1, 0); |
| 715 | ; |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 716 | int idx_out_n = 0; |
| 717 | #endif // DST_BATCH_SIZE != 1 |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 718 | |
Gian Marco Iodice | 40471d1 | 2021-04-26 08:39:28 +0100 | [diff] [blame^] | 719 | __global unsigned char *in_base_ptr = input_ptr + input_offset_first_element_in_bytes + idx_out_c * sizeof(DATA_TYPE) + idx_out_n * input_stride_w; |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 720 | |
Gian Marco Iodice | 40471d1 | 2021-04-26 08:39:28 +0100 | [diff] [blame^] | 721 | __global unsigned char *out_base_ptr = output_ptr + output_offset_first_element_in_bytes + idx_out_c * sizeof(DATA_TYPE) + idx_out_w * output_stride_y + idx_out_h * output_stride_z + idx_out_n * |
| 722 | output_stride_w; |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 723 | |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 724 | VEC_DATA_TYPE(ACC_DATA_TYPE, VEC_SIZE) |
| 725 | res0 = INITIAL_VALUE; |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 726 | |
Gian Marco Iodice | 40471d1 | 2021-04-26 08:39:28 +0100 | [diff] [blame^] | 727 | #if POOL_SIZE_X == SRC_WIDTH && POOL_SIZE_Y == SRC_HEIGHT |
| 728 | // Global pooling path |
| 729 | |
| 730 | int filter_size = POOL_SIZE_X * POOL_SIZE_Y; |
| 731 | |
| 732 | #pragma unroll 8 |
| 733 | for(int y = 0; y < POOL_SIZE_X * POOL_SIZE_Y; ++y) |
| 734 | { |
| 735 | VEC_DATA_TYPE(ACC_DATA_TYPE, VEC_SIZE) |
| 736 | data0; |
| 737 | #if defined(FP_MIXED_PRECISION) |
| 738 | // In case of FP_MIXED_PRECISION, ACC_DATA_TYPE is != DATA_TYPE |
| 739 | data0 = CONVERT(VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(in_base_ptr), VEC_DATA_TYPE(ACC_DATA_TYPE, VEC_SIZE)); |
| 740 | #else // defined(FP_MIXED_PRECISION) |
| 741 | data0 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(in_base_ptr)); |
| 742 | #endif // defined(FP_MIXED_PRECISION) |
| 743 | |
| 744 | #if defined(POOL_L2) |
| 745 | // Raise to power of 2 for L2 Pooling |
| 746 | data0 *= data0; |
| 747 | #endif // defined(POOL_L2) |
| 748 | |
| 749 | res0 = POOL_OP(res0, data0); |
| 750 | |
| 751 | in_base_ptr += input_stride_y; |
| 752 | } |
| 753 | #else // POOL_SIZE_X == SRC_WIDTH && POOL_SIZE_Y == SRC_HEIGHT |
| 754 | |
| 755 | int idx_in_w = idx_out_w * STRIDE_X - PAD_X; |
| 756 | int idx_in_h = idx_out_h * STRIDE_Y - PAD_Y; |
| 757 | |
| 758 | int pool_x_s = max((int)0, -idx_in_w); |
| 759 | int pool_x_e = min((int)POOL_SIZE_X, (int)SRC_WIDTH - idx_in_w); |
| 760 | int pool_y_s = max((int)0, -idx_in_h); |
| 761 | int pool_y_e = min((int)POOL_SIZE_Y, (int)SRC_HEIGHT - idx_in_h); |
| 762 | |
| 763 | #if defined(EXCLUDE_PADDING) |
| 764 | int filter_size = (pool_y_e - pool_y_s) * (pool_x_e - pool_x_s); |
| 765 | #else // defined(EXCLUDE_PADDING) |
| 766 | int filter_size = POOL_SIZE_X * POOL_SIZE_Y; |
| 767 | #endif // defined(EXCLUDE_PADDING) |
| 768 | |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 769 | for(int y = pool_y_s; y < pool_y_e; ++y) |
| 770 | { |
| 771 | for(int x = pool_x_s; x < pool_x_e; ++x) |
| 772 | { |
Gian Marco Iodice | 40471d1 | 2021-04-26 08:39:28 +0100 | [diff] [blame^] | 773 | VEC_DATA_TYPE(ACC_DATA_TYPE, VEC_SIZE) |
| 774 | data0; |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 775 | #if defined(FP_MIXED_PRECISION) |
| 776 | // In case of FP_MIXED_PRECISION, ACC_DATA_TYPE is != DATA_TYPE |
| 777 | data0 = CONVERT(VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(in_base_ptr + (x + idx_in_w) * input_stride_y + (y + idx_in_h) * input_stride_z)), VEC_DATA_TYPE(ACC_DATA_TYPE, VEC_SIZE)); |
Gian Marco Iodice | 40471d1 | 2021-04-26 08:39:28 +0100 | [diff] [blame^] | 778 | #else // defined(FP_MIXED_PRECISION) |
| 779 | data0 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(in_base_ptr + (x + idx_in_w) * input_stride_y + (y + idx_in_h) * input_stride_z)); |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 780 | #endif // defined(FP_MIXED_PRECISION) |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 781 | |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 782 | #if defined(POOL_L2) |
| 783 | // Raise to power of 2 for L2 Pooling |
| 784 | data0 *= data0; |
| 785 | #endif // defined(POOL_L2) |
| 786 | res0 = POOL_OP(res0, data0); |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 787 | } |
| 788 | } |
| 789 | |
Gian Marco Iodice | 40471d1 | 2021-04-26 08:39:28 +0100 | [diff] [blame^] | 790 | #endif // POOL_SIZE_X == SRC_WIDTH && POOL_SIZE_Y == SRC_HEIGHT |
| 791 | |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 792 | #if defined(POOL_AVG) || defined(POOL_L2) |
Gian Marco Iodice | 40471d1 | 2021-04-26 08:39:28 +0100 | [diff] [blame^] | 793 | res0 /= (VEC_DATA_TYPE(ACC_DATA_TYPE, VEC_SIZE))filter_size; |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 794 | #endif // defined(POOL_AVG) || defined(POOL_L2) |
| 795 | |
| 796 | #if defined(POOL_L2) |
| 797 | // Take square root of the result in L2 pooling |
| 798 | res0 = SQRT_OP(res0); |
| 799 | #endif // defined(POOL_L2) |
| 800 | |
| 801 | // Store result |
| 802 | #if defined(FP_MIXED_PRECISION) |
Gian Marco Iodice | 40471d1 | 2021-04-26 08:39:28 +0100 | [diff] [blame^] | 803 | VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) |
| 804 | res_converted0 = CONVERT(res0, VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)); |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 805 | STORE_VECTOR_SELECT(res_converted, DATA_TYPE, out_base_ptr, VEC_SIZE, VEC_SIZE_LEFTOVER, (VEC_SIZE_LEFTOVER != 0) && get_global_id(0) == 0); |
Gian Marco Iodice | 40471d1 | 2021-04-26 08:39:28 +0100 | [diff] [blame^] | 806 | #else // defined(FP_MIXED_PRECISION) |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 807 | STORE_VECTOR_SELECT(res, DATA_TYPE, out_base_ptr, VEC_SIZE, VEC_SIZE_LEFTOVER, (VEC_SIZE_LEFTOVER != 0) && get_global_id(0) == 0); |
| 808 | #endif // defined(FP_MIXED_PRECISION) |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 809 | } |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 810 | #endif // defined(POOL_SIZE_X) && defined(POOL_SIZE_Y) |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 811 | |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 812 | #define SELECT_TYPE SELECT_VEC_DATA_TYPE(ACC_DATA_TYPE, VEC_SIZE) |
Giorgio Arena | 69153b3 | 2020-10-23 13:14:26 +0100 | [diff] [blame] | 813 | |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 814 | /** Performs pooling layer of size equal to 2. This OpenCL kernel can perform the following pooling types: |
| 815 | * -# max, -DPOOL_MAX must be passed at compile time |
| 816 | * -# max extracting the max index, -DPOOL_MAX and -DEXTRACT_MAX_INDEX must be passed at compile time |
| 817 | * -# average, -DPOOL_AVG must be passed at compile time. If padding has to be expluded, -DEXCLUDE_PADDING should be passed at compile time |
| 818 | * -# l2 normalisation, -DPOOL_L2 must be passed at compile time |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 819 | * |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 820 | * @note Datatype must be passed at compile type using -DDATA_TYPE e.g. -DDATA_TYPE=half. Supported data types are F32/F16 |
| 821 | * @note Accumulation data type must be passed at compile time using -DACC_DATA_TYPE e.g. -DACC_DATA_TYPE=float |
| 822 | * @note If -DFP_MIXED_PRECISION is passed at compile time, the kernel will use F32 for the partial result |
| 823 | * @note Input tensor width and height must be passed at compile time using -DSRC_WIDTH and -DSRC_HEIGHT |
| 824 | * @note Output tensor height, channels and batch size must be passed at compile time using -DDST_HEIGHT, -DDST_CHANNELS and -DDST_BATCH_SIZE |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 825 | * @note Pool strides must be passed at compile time using -DSTRIDE_X and -DSTRIDE_Y which are the steps of the window along the x and y directions |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 826 | * @note Pool pads must be passed at compile time using -DPAD_X and -DPAD_Y |
| 827 | * @note Vector size must be passed at compile time using -DVEC_SIZE=size. e.g. -DVEC_SIZE=16 |
| 828 | * @note Leftover vector size must be passed at compile time using -DVEC_SIZE_LEFTOVER. e.g. -DVEC_SIZE_LEFTOVER=3. It is defined as the remainder between the input's first dimension and VEC_SIZE |
| 829 | * @note The initial value for the pooling operation must be passed at compile time using -DINITIAL_VALUE e.g. -DINITIAL_VALUE=0 |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 830 | * |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 831 | * @param[in] input_ptr Pointer to the source tensor. Supported data types: F32/F16 |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 832 | * @param[in] input_stride_x Stride of the source tensor in X dimension (in bytes) |
| 833 | * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes) |
| 834 | * @param[in] input_stride_y Stride of the source tensor in Y dimension (in bytes) |
| 835 | * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes) |
| 836 | * @param[in] input_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 837 | * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes) |
| 838 | * @param[in] input_stride_w Stride of the source tensor in W dimension (in bytes) |
| 839 | * @param[in] input_step_w input_stride_w * number of elements along W processed per workitem(in bytes) |
| 840 | * @param[in] input_offset_first_element_in_bytes The offset of the first element in the source tensor |
| 841 | * @param[out] output_ptr Pointer to the destination tensor. Supported data types: same as @p input_ptr |
| 842 | * @param[in] output_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 843 | * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes) |
| 844 | * @param[in] output_stride_y Stride of the destination tensor in Y dimension (in bytes) |
| 845 | * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes) |
| 846 | * @param[in] output_stride_z Stride of the destination tensor in Z dimension (in bytes) |
| 847 | * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes) |
| 848 | * @param[in] output_stride_w Stride of the destination tensor in W dimension (in bytes) |
| 849 | * @param[in] output_step_w output_stride_w * number of elements along W processed per workitem(in bytes) |
| 850 | * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination tensor |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 851 | * @param[in] indices_ptr (Optional) Pointer to the indices tensor. Supported data types: U32 |
| 852 | * @param[in] indices_stride_x (Optional) Stride of the indices tensor in X dimension (in bytes) |
| 853 | * @param[in] indices_step_x (Optional) indices_stride_x * number of elements along X processed per workitem(in bytes) |
| 854 | * @param[in] indices_stride_y (Optional) Stride of the indices tensor in Y dimension (in bytes) |
| 855 | * @param[in] indices_step_y (Optional) indices_stride_y * number of elements along Y processed per workitem(in bytes) |
| 856 | * @param[in] indices_stride_z (Optional) Stride of the indices tensor in Z dimension (in bytes) |
| 857 | * @param[in] indices_step_z (Optional) indices_stride_z * number of elements along Z processed per workitem(in bytes) |
| 858 | * @param[in] indices_stride_w (Optional) Stride of the indices tensor in W dimension (in bytes) |
| 859 | * @param[in] indices_step_w (Optional) indices_stride_w * number of elements along W processed per workitem(in bytes) |
| 860 | * @param[in] indices_offset_first_element_in_bytes (Optional) The offset of the first element in the indices tensor |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 861 | */ |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 862 | __kernel void pooling_layer_2x2_nhwc( |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 863 | TENSOR4D_DECLARATION(input), |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 864 | TENSOR4D_DECLARATION(output) |
| 865 | #if defined(EXTRACT_MAX_INDEX) && defined(POOL_MAX) |
| 866 | , |
| 867 | TENSOR4D_DECLARATION(indices) |
| 868 | #endif // defined(EXTRACT_MAX_INDEX) && defined(POOL_MAX) |
| 869 | ) |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 870 | { |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 871 | // Note: If C is not multiple of VEC_SIZE, we shift back of VEC_SIZE_LEFTOVER elements to compute the leftover elements for get_global_id(0) == 0 |
| 872 | // Note: If C is less than VEC_SIZE, VEC_SIZE should be SHRINKED to the closest smaller VEC_SIZE. This operation is performed on the host side |
| 873 | int idx_out_c = max((int)(get_global_id(0) * VEC_SIZE - (VEC_SIZE - VEC_SIZE_LEFTOVER) % VEC_SIZE), 0); |
| 874 | int idx_out_w = get_global_id(1); |
| 875 | #if DST_BATCH_SIZE != 1 |
| 876 | // If batch size != 1, the batch size dimension is collapsed over the height dimension |
| 877 | int idx_out_h = get_global_id(2) % DST_HEIGHT; |
| 878 | int idx_out_n = get_global_id(2) / DST_HEIGHT; |
Gian Marco Iodice | 40471d1 | 2021-04-26 08:39:28 +0100 | [diff] [blame^] | 879 | #else //SRC_BATCH_SIZE != 1 |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 880 | int idx_out_h = get_global_id(2); |
| 881 | int idx_out_n = 0; |
| 882 | #endif // SRC_BATCH_SIZE != 1 |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 883 | |
Gian Marco Iodice | 40471d1 | 2021-04-26 08:39:28 +0100 | [diff] [blame^] | 884 | int idx_in_w = idx_out_w * STRIDE_X - PAD_X; |
| 885 | int idx_in_h = idx_out_h * STRIDE_Y - PAD_Y; |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 886 | |
Gian Marco Iodice | 40471d1 | 2021-04-26 08:39:28 +0100 | [diff] [blame^] | 887 | __global unsigned char *in_base_ptr = input_ptr + input_offset_first_element_in_bytes + idx_out_c * sizeof(DATA_TYPE) + idx_out_n * input_stride_w; |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 888 | |
Gian Marco Iodice | 40471d1 | 2021-04-26 08:39:28 +0100 | [diff] [blame^] | 889 | __global unsigned char *out_base_ptr = output_ptr + output_offset_first_element_in_bytes + idx_out_c * sizeof(DATA_TYPE) + idx_out_w * output_stride_y + idx_out_h * output_stride_z + idx_out_n * |
| 890 | output_stride_w; |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 891 | |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 892 | int pool_x_s = max((int)0, -idx_in_w); |
| 893 | int pool_x_e = min((int)2, (int)SRC_WIDTH - idx_in_w); |
| 894 | int pool_y_s = max((int)0, -idx_in_h); |
| 895 | int pool_y_e = min((int)2, (int)SRC_HEIGHT - idx_in_h); |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 896 | |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 897 | int filter_size = (pool_x_e - pool_x_s) * (pool_y_e - pool_y_s); |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 898 | |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 899 | int x0 = pool_x_s + idx_in_w; |
| 900 | int y0 = pool_y_s + idx_in_h; |
| 901 | int x1 = pool_x_e - 1 + idx_in_w; |
| 902 | int y1 = pool_y_e - 1 + idx_in_h; |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 903 | |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 904 | REPEAT_VAR_INIT_TO_CONST(4, VEC_DATA_TYPE(ACC_DATA_TYPE, VEC_SIZE), data, 0); |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 905 | |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 906 | #if defined(FP_MIXED_PRECISION) |
| 907 | // In case of FP_MIXED_PRECISION, ACC_DATA_TYPE is != DATA_TYPE |
| 908 | data0 = CONVERT(VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(in_base_ptr + x0 * input_stride_y + y0 * input_stride_z)), VEC_DATA_TYPE(ACC_DATA_TYPE, VEC_SIZE)); |
| 909 | data1 = CONVERT(VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(in_base_ptr + x1 * input_stride_y + y0 * input_stride_z)), VEC_DATA_TYPE(ACC_DATA_TYPE, VEC_SIZE)); |
| 910 | data2 = CONVERT(VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(in_base_ptr + x0 * input_stride_y + y1 * input_stride_z)), VEC_DATA_TYPE(ACC_DATA_TYPE, VEC_SIZE)); |
| 911 | data3 = CONVERT(VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(in_base_ptr + x1 * input_stride_y + y1 * input_stride_z)), VEC_DATA_TYPE(ACC_DATA_TYPE, VEC_SIZE)); |
Gian Marco Iodice | 40471d1 | 2021-04-26 08:39:28 +0100 | [diff] [blame^] | 912 | #else // defined(FP_MIXED_PRECISION) |
| 913 | data0 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(in_base_ptr + x0 * input_stride_y + y0 * input_stride_z)); |
| 914 | data1 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(in_base_ptr + x1 * input_stride_y + y0 * input_stride_z)); |
| 915 | data2 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(in_base_ptr + x0 * input_stride_y + y1 * input_stride_z)); |
| 916 | data3 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(in_base_ptr + x1 * input_stride_y + y1 * input_stride_z)); |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 917 | #endif // defined(FP_MIXED_PRECISION) |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 918 | |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 919 | #if !defined(POOL_MAX) |
| 920 | if(filter_size != 4) |
| 921 | { |
Giorgio Arena | 69153b3 | 2020-10-23 13:14:26 +0100 | [diff] [blame] | 922 | SELECT_TYPE cond_w_s = (SELECT_TYPE)idx_in_w < (SELECT_TYPE)0; |
| 923 | SELECT_TYPE cond_w_e = (SELECT_TYPE)idx_in_w >= (SELECT_TYPE)(SRC_WIDTH - 1); |
| 924 | SELECT_TYPE cond_h_s = (SELECT_TYPE)idx_in_h < (SELECT_TYPE)0; |
| 925 | SELECT_TYPE cond_h_e = (SELECT_TYPE)idx_in_h >= (SELECT_TYPE)(SRC_HEIGHT - 1); |
| 926 | |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 927 | // Make invalid the values loaded if the x or y coordinate was clamped (out-of-bound) |
Giorgio Arena | 69153b3 | 2020-10-23 13:14:26 +0100 | [diff] [blame] | 928 | data0 = select(data0, (VEC_DATA_TYPE(ACC_DATA_TYPE, VEC_SIZE))INITIAL_VALUE, (SELECT_TYPE)(cond_w_s | cond_h_s)); |
| 929 | data1 = select(data1, (VEC_DATA_TYPE(ACC_DATA_TYPE, VEC_SIZE))INITIAL_VALUE, (SELECT_TYPE)(cond_w_e | cond_h_s)); |
| 930 | data2 = select(data2, (VEC_DATA_TYPE(ACC_DATA_TYPE, VEC_SIZE))INITIAL_VALUE, (SELECT_TYPE)(cond_w_s | cond_h_e)); |
| 931 | data3 = select(data3, (VEC_DATA_TYPE(ACC_DATA_TYPE, VEC_SIZE))INITIAL_VALUE, (SELECT_TYPE)(cond_w_e | cond_h_e)); |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 932 | } |
| 933 | #endif // !defined(POOL_MAX) |
| 934 | |
| 935 | #if defined(POOL_L2) |
| 936 | // Raise to power of 2 for L2 Pooling |
| 937 | data0 *= data0; |
| 938 | data1 *= data1; |
| 939 | data2 *= data2; |
| 940 | data3 *= data3; |
| 941 | #endif /* defined(POOL_L2) */ |
| 942 | |
| 943 | VEC_DATA_TYPE(ACC_DATA_TYPE, VEC_SIZE) |
| 944 | res0 = data0; |
| 945 | res0 = POOL_OP(res0, data1); |
| 946 | res0 = POOL_OP(res0, data2); |
| 947 | res0 = POOL_OP(res0, data3); |
| 948 | |
| 949 | #if defined(POOL_AVG) || defined(POOL_L2) |
| 950 | #if defined(EXCLUDE_PADDING) |
| 951 | res0 /= (VEC_DATA_TYPE(ACC_DATA_TYPE, VEC_SIZE))filter_size; |
Gian Marco Iodice | 40471d1 | 2021-04-26 08:39:28 +0100 | [diff] [blame^] | 952 | #else // !defined(EXCLUDE_PADDING) |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 953 | res0 /= (VEC_DATA_TYPE(ACC_DATA_TYPE, VEC_SIZE))4; |
| 954 | #endif // defined(EXCLUDE_PADDING) |
| 955 | #endif // defined(POOL_AVG) || defined(POOL_L2) |
| 956 | |
| 957 | #if defined(POOL_L2) |
| 958 | // Take square root of the result in L2 pooling |
| 959 | res0 = SQRT_OP(res0); |
| 960 | #endif // defined(POOL_L2) |
| 961 | |
| 962 | // Store result |
| 963 | #if defined(FP_MIXED_PRECISION) |
Gian Marco Iodice | 40471d1 | 2021-04-26 08:39:28 +0100 | [diff] [blame^] | 964 | VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) |
| 965 | res_converted0 = CONVERT(res0, VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)); |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 966 | STORE_VECTOR_SELECT(res_converted, DATA_TYPE, out_base_ptr, VEC_SIZE, VEC_SIZE_LEFTOVER, (VEC_SIZE_LEFTOVER != 0) && get_global_id(0) == 0); |
Gian Marco Iodice | 40471d1 | 2021-04-26 08:39:28 +0100 | [diff] [blame^] | 967 | #else // defined(FP_MIXED_PRECISION) |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 968 | STORE_VECTOR_SELECT(res, DATA_TYPE, out_base_ptr, VEC_SIZE, VEC_SIZE_LEFTOVER, (VEC_SIZE_LEFTOVER != 0) && get_global_id(0) == 0); |
| 969 | #endif // defined(FP_MIXED_PRECISION) |
| 970 | |
| 971 | #if defined(EXTRACT_MAX_INDEX) && defined(POOL_MAX) |
| 972 | |
| 973 | // This part is used to return the index of the maximum value |
| 974 | // Note: DST_CHANNELS and DST_BATCH_SIZE can be used for either the input and output tensor |
| 975 | |
| 976 | // note: Batch dimension does not contribute in the offset contribution |
Gian Marco Iodice | 40471d1 | 2021-04-26 08:39:28 +0100 | [diff] [blame^] | 977 | VEC_DATA_TYPE(uint, VEC_SIZE) |
| 978 | base_index = (uint)idx_out_c; |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 979 | |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 980 | base_index += VEC_OFFS(uint, VEC_SIZE); |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 981 | |
Gian Marco Iodice | 40471d1 | 2021-04-26 08:39:28 +0100 | [diff] [blame^] | 982 | VEC_DATA_TYPE(uint, VEC_SIZE) |
| 983 | index0 = base_index + (uint)x0 * DST_CHANNELS + (uint)y0 * (DST_CHANNELS * SRC_WIDTH); |
| 984 | VEC_DATA_TYPE(uint, VEC_SIZE) |
| 985 | index1 = base_index + (uint)x1 * DST_CHANNELS + (uint)y0 * (DST_CHANNELS * SRC_WIDTH); |
| 986 | VEC_DATA_TYPE(uint, VEC_SIZE) |
| 987 | index2 = base_index + (uint)x0 * DST_CHANNELS + (uint)y1 * (DST_CHANNELS * SRC_WIDTH); |
| 988 | VEC_DATA_TYPE(uint, VEC_SIZE) |
| 989 | index3 = base_index + (uint)x1 * DST_CHANNELS + (uint)y1 * (DST_CHANNELS * SRC_WIDTH); |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 990 | |
| 991 | index0 = select(index1, index0, CONVERT(isgreaterequal(data0, data1), VEC_DATA_TYPE(int, VEC_SIZE))); |
| 992 | index1 = select(index3, index2, CONVERT(isgreaterequal(data2, data3), VEC_DATA_TYPE(int, VEC_SIZE))); |
| 993 | index0 = select(index1, index0, CONVERT(isgreaterequal(max(data0, data1), max(data2, data3)), VEC_DATA_TYPE(int, VEC_SIZE))); |
| 994 | |
Gian Marco Iodice | 40471d1 | 2021-04-26 08:39:28 +0100 | [diff] [blame^] | 995 | __global unsigned char *idx_base_ptr = indices_ptr + indices_offset_first_element_in_bytes + idx_out_c * sizeof(uint) + idx_out_w * indices_stride_y + idx_out_h * indices_stride_z + idx_out_n * |
| 996 | indices_stride_w; |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 997 | |
| 998 | // Store result |
| 999 | STORE_VECTOR_SELECT(index, uint, idx_base_ptr, VEC_SIZE, VEC_SIZE_LEFTOVER, ((VEC_SIZE_LEFTOVER != 0) && get_global_id(0) == 0)); |
| 1000 | #endif // defined(EXTRACT_MAX_INDEX) && defined(POOL_MAX) |
| 1001 | } |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 1002 | #endif // defined(VEC_SIZE) && defined(VEC_SIZE_LEFTOVER) && defined(SRC_WIDTH) && defined(SRC_HEIGHT) && defined(DST_CHANNELS) && defined(DST_HEIGHT) && defined(DST_BATCH_SIZE) && defined(ACC_DATA_TYPE) |