Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2020 Arm Limited. |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [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" |
| 25 | |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame] | 26 | #if defined(DATA_TYPE) && defined(INITIAL_VALUE) |
| 27 | #define VEC_TYPE(VEC_SIZE) VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) |
| 28 | |
Pablo Tello | a52e4cf | 2019-04-01 14:55:18 +0100 | [diff] [blame] | 29 | #if defined(OFFSET_IN1) && defined(OFFSET_OUT) && defined(SCALE_IN1) && defined(SCALE_OUT) |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame] | 30 | #define VEC_FLOAT(VEC_SIZE) VEC_DATA_TYPE(float, VEC_SIZE) |
Michalis Spyrou | 4335a8c | 2019-04-05 16:41:30 +0100 | [diff] [blame] | 31 | #define VEC_INT(VEC_SIZE) VEC_DATA_TYPE(int, VEC_SIZE) |
Michalis Spyrou | 4335a8c | 2019-04-05 16:41:30 +0100 | [diff] [blame] | 32 | #define CONVERT_RTE(x, type) (convert_##type##_rte((x))) |
Pablo Tello | a52e4cf | 2019-04-01 14:55:18 +0100 | [diff] [blame] | 33 | #define CONVERT_DOWN(x, type) CONVERT_RTE(x, type) |
| 34 | #define REQUANTIZE(VEC_SIZE, input, in_offset, out_offset, in_scale, out_scale, res) \ |
| 35 | { \ |
| 36 | const VEC_FLOAT(VEC_SIZE) in_f32 = (CONVERT(input, VEC_FLOAT(VEC_SIZE)) - (VEC_FLOAT(VEC_SIZE))((float)in_offset)) * (VEC_FLOAT(VEC_SIZE))((float)in_scale); \ |
| 37 | const VEC_FLOAT(VEC_SIZE) out_f32 = in_f32 / ((VEC_FLOAT(VEC_SIZE))(float)out_scale) + ((VEC_FLOAT(VEC_SIZE))((float)out_offset)); \ |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame] | 38 | res = CONVERT_SAT(CONVERT_DOWN(out_f32, VEC_INT(VEC_SIZE)), VEC_TYPE(VEC_SIZE)); \ |
Pablo Tello | a52e4cf | 2019-04-01 14:55:18 +0100 | [diff] [blame] | 39 | } |
| 40 | #endif /* defined(OFFSET_IN1) && defined(OFFSET_OUT) && defined(SCALE_IN1) && defined(SCALE_OUT) */ |
| 41 | |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 42 | #if defined(POOL_AVG) |
| 43 | #define POOL_OP(x, y) ((x) + (y)) |
| 44 | #else /* defined(POOL_AVG) */ |
| 45 | #define POOL_OP(x, y) (max((x), (y))) |
| 46 | #endif /* defined(POOL_AVG) */ |
| 47 | |
| 48 | #define DIV_OP(x, y) (x * (1.f / y)) |
| 49 | |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 50 | #define DIV_OP_NHWC(x, y) (convert_float8(x) * (float8)(1.f / y)) |
| 51 | |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 52 | #if defined(POOL_L2) |
| 53 | #error "L2 pooling is not supported" |
| 54 | #endif /* defined(POOL_L2) */ |
| 55 | |
Isabella Gottardi | a527e8c | 2018-01-31 17:49:25 +0000 | [diff] [blame] | 56 | int calculate_avg_scale(const int pool_size_x, const int pool_size_y, const int upper_bound_w, const int upper_bound_h, |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 57 | const int pad_x, const int pad_y, const int stride_x, const int stride_y) |
| 58 | { |
| 59 | int start_x = get_global_id(0) * stride_x - pad_x; |
| 60 | int start_y = get_global_id(1) * stride_y - pad_y; |
Isabella Gottardi | a527e8c | 2018-01-31 17:49:25 +0000 | [diff] [blame] | 61 | const int end_x = min(start_x + pool_size_x, upper_bound_w); |
| 62 | const int end_y = min(start_y + pool_size_y, upper_bound_h); |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 63 | #if defined(EXCLUDE_PADDING) |
| 64 | start_x = max(0, start_x); |
| 65 | start_y = max(0, start_y); |
| 66 | #endif /* defined(EXCLUDE_PADDING) */ |
| 67 | return ((end_y - start_y) * (end_x - start_x)); |
| 68 | } |
| 69 | |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 70 | /** Performs a pooling function of pool size equal to N (NCHW) |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 71 | * |
Isabella Gottardi | a527e8c | 2018-01-31 17:49:25 +0000 | [diff] [blame] | 72 | * @note Pool sizes must be passed using -DPOOL_SIZE_X and -DPOOL_SIZE_Y e.g. -DPOOL_SIZE_X=13; |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 73 | * @note In case of average pooling the following information must be passed at compile time: |
| 74 | * -DPOOL_AVG must be provided otherwise max pooling will be performed. |
| 75 | * -DMAX_WIDTH and -DMAX_HEIGHT which are the maximum accessible indeces in x and y dimensions (width + pad) |
| 76 | * -DSTRIDE_X and -DSTRIDE_Y which are the steps of the window along the x and y directions |
| 77 | * -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] | 78 | * @note Input data type must be passed at compile time using -DDAT_TYPE=type, e.g. -DDATA_TYPE=uchar |
| 79 | * @note The initial value for the pooling operation must be passed at compile time using -DINITIAL_VALUE e.g. -DINITIAL_VALUE=0 |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 80 | * |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame] | 81 | * @param[in] input_ptr Pointer to the source image. Supported data types: QASYMM8/QASYMM8_SIGNED |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 82 | * @param[in] input_stride_x Stride of the source image in X dimension (in bytes) |
| 83 | * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes) |
| 84 | * @param[in] input_stride_y Stride of the source image in Y dimension (in bytes) |
| 85 | * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes) |
| 86 | * @param[in] input_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 87 | * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes) |
| 88 | * @param[in] input_offset_first_element_in_bytes The offset of the first element in the source image |
| 89 | * @param[out] output_ptr Pointer to the destination image. Supported data types: same as @p input_ptr |
| 90 | * @param[in] output_stride_x Stride of the destination image in X dimension (in bytes) |
| 91 | * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes) |
| 92 | * @param[in] output_stride_y Stride of the destination image in Y dimension (in bytes) |
| 93 | * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes) |
| 94 | * @param[in] output_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 95 | * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes) |
| 96 | * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination image |
| 97 | */ |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 98 | __kernel void pooling_layer_MxN_quantized_nchw( |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 99 | TENSOR3D_DECLARATION(input), |
| 100 | TENSOR3D_DECLARATION(output)) |
| 101 | { |
| 102 | // Get pixels pointer |
| 103 | Tensor3D input = CONVERT_TO_TENSOR3D_STRUCT(input); |
| 104 | Tensor3D output = CONVERT_TO_TENSOR3D_STRUCT(output); |
| 105 | |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame] | 106 | int8 vdata = INITIAL_VALUE; |
| 107 | int sdata = INITIAL_VALUE; |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 108 | |
| 109 | // Load data |
Isabella Gottardi | a527e8c | 2018-01-31 17:49:25 +0000 | [diff] [blame] | 110 | for(int y = 0; y < POOL_SIZE_Y; y++) |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 111 | { |
| 112 | int x = 0; |
Isabella Gottardi | a527e8c | 2018-01-31 17:49:25 +0000 | [diff] [blame] | 113 | for(; x <= ((int)POOL_SIZE_X - 8); x += 8) |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 114 | { |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame] | 115 | VEC_TYPE(8) |
| 116 | data = vload8(0, (__global DATA_TYPE *)tensor3D_offset(&input, x, y, 0)); |
| 117 | int8 data0 = convert_int8(data); |
| 118 | vdata = POOL_OP(vdata, data0); |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | // Leftover |
Isabella Gottardi | a527e8c | 2018-01-31 17:49:25 +0000 | [diff] [blame] | 122 | for(; x < (int)POOL_SIZE_X; ++x) |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 123 | { |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame] | 124 | DATA_TYPE data = *((__global DATA_TYPE *)tensor3D_offset(&input, x, y, 0)); |
| 125 | int data0 = convert_int(data); |
| 126 | sdata = POOL_OP(sdata, data0); |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
| 130 | // Reduce result |
| 131 | int4 reduce4 = POOL_OP(vdata.s0123, vdata.s4567); |
| 132 | int2 reduce2 = POOL_OP(reduce4.s01, reduce4.s23); |
| 133 | int res = POOL_OP(reduce2.s0, reduce2.s1); |
| 134 | res = POOL_OP(res, sdata); |
| 135 | |
| 136 | #if defined(POOL_AVG) |
Isabella Gottardi | a527e8c | 2018-01-31 17:49:25 +0000 | [diff] [blame] | 137 | res = round(DIV_OP(res, calculate_avg_scale(POOL_SIZE_X, POOL_SIZE_Y, MAX_WIDTH, MAX_HEIGHT, PAD_X, PAD_Y, STRIDE_X, STRIDE_Y))); |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 138 | #endif /* defined(POOL_AVG) */ |
| 139 | |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame] | 140 | DATA_TYPE result_q8 = CONVERT(res, DATA_TYPE); |
Pablo Tello | a52e4cf | 2019-04-01 14:55:18 +0100 | [diff] [blame] | 141 | |
| 142 | #if defined(OFFSET_IN1) && defined(OFFSET_OUT) && defined(SCALE_IN1) && defined(SCALE_OUT) |
| 143 | |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame] | 144 | const float result_f32 = convert_float(result_q8); |
Pablo Tello | a52e4cf | 2019-04-01 14:55:18 +0100 | [diff] [blame] | 145 | const float input_offset = (float)OFFSET_IN1; |
| 146 | const float input_scale = (float)SCALE_IN1; |
| 147 | const float scale_out = (float)SCALE_OUT; |
| 148 | const float offset_out = (float)OFFSET_OUT; |
| 149 | const float in_f32 = (result_f32 - input_offset) * input_scale; |
| 150 | const float out_f32 = in_f32 / scale_out + offset_out; |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame] | 151 | result_q8 = CONVERT_SAT(convert_int_rte(out_f32), DATA_TYPE); |
Pablo Tello | a52e4cf | 2019-04-01 14:55:18 +0100 | [diff] [blame] | 152 | |
| 153 | #endif /* defined(OFFSET_IN1) && defined(OFFSET_OUT) && defined(SCALE_IN1) && defined(SCALE_OUT) */ |
| 154 | |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame] | 155 | *(__global DATA_TYPE *)output.ptr = result_q8; |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 156 | } |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 157 | |
| 158 | int calculate_avg_scale_nhwc(const int pool_size_x, const int pool_size_y, int upper_bound_w, int upper_bound_h, |
| 159 | const int pad_x, const int pad_y, const int stride_x, const int stride_y) |
| 160 | { |
| 161 | int start_x = get_global_id(1) * stride_x - pad_x; |
Georgios Pinitas | 89d7173 | 2018-10-29 20:07:15 +0000 | [diff] [blame] | 162 | #if defined(DST_DEPTH) |
| 163 | int start_y = (get_global_id(2) % DST_DEPTH) * stride_y - pad_y; |
| 164 | #else /* defined(DST_DEPTH) */ |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame] | 165 | int start_y = get_global_id(2) * stride_y - pad_y; |
Georgios Pinitas | 89d7173 | 2018-10-29 20:07:15 +0000 | [diff] [blame] | 166 | #endif /* defined(DST_DEPTH) */ |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 167 | |
| 168 | const int end_x = min(start_x + pool_size_x, upper_bound_w); |
| 169 | const int end_y = min(start_y + pool_size_y, upper_bound_h); |
| 170 | |
| 171 | start_x = max(0, start_x); |
| 172 | start_y = max(0, start_y); |
| 173 | |
| 174 | return ((end_y - start_y) * (end_x - start_x)); |
| 175 | } |
| 176 | |
| 177 | /** Performs a pooling function of pool size equal to N (NHWC) |
| 178 | * |
| 179 | * @note Pool sizes must be passed using -DPOOL_SIZE_X and -DPOOL_SIZE_Y e.g. -DPOOL_SIZE_X=13; |
| 180 | * @note Tensors width and height must be passed at compile time using -DMAX_WIDTH and -DMAX_HEIGHT |
| 181 | * @note 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 |
| 182 | * @note Pad values must be passed at compile time using -DPAD_X and -DPAD_Y which are the pooling paddings in x and y dimension |
| 183 | * @note In case of average pooling the following information must be passed at compile time: |
| 184 | * -DPOOL_AVG must be provided otherwise max pooling will be performed. |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame] | 185 | * @note The initial value for the pooling operation must be passed at compile time using -DINITIAL_VALUE e.g. -DINITIAL_VALUE=0 |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 186 | * |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame] | 187 | * @param[in] input_ptr Pointer to the source image. Supported data types: QASYMM8/QASYMM8_SIGNED |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 188 | * @param[in] input_stride_x Stride of the source image in X dimension (in bytes) |
| 189 | * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes) |
| 190 | * @param[in] input_stride_y Stride of the source image in Y dimension (in bytes) |
| 191 | * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes) |
| 192 | * @param[in] input_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 193 | * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes) |
Georgios Pinitas | 89d7173 | 2018-10-29 20:07:15 +0000 | [diff] [blame] | 194 | * @param[in] input_stride_w Stride of the source tensor in W dimension (in bytes) |
| 195 | * @param[in] input_step_w input_stride_w * number of elements along W processed per workitem(in bytes) |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 196 | * @param[in] input_offset_first_element_in_bytes The offset of the first element in the source image |
| 197 | * @param[out] output_ptr Pointer to the destination image. Supported data types: same as @p input_ptr |
Georgios Pinitas | 89d7173 | 2018-10-29 20:07:15 +0000 | [diff] [blame] | 198 | * @param[in] output_stride_x Stride of the destination tensor in X dimension (in bytes) |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 199 | * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes) |
Georgios Pinitas | 89d7173 | 2018-10-29 20:07:15 +0000 | [diff] [blame] | 200 | * @param[in] output_stride_y Stride of the destination tensor in Y dimension (in bytes) |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 201 | * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes) |
Georgios Pinitas | 89d7173 | 2018-10-29 20:07:15 +0000 | [diff] [blame] | 202 | * @param[in] output_stride_z Stride of the destination tensor in Z dimension (in bytes) |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 203 | * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes) |
Georgios Pinitas | 89d7173 | 2018-10-29 20:07:15 +0000 | [diff] [blame] | 204 | * @param[in] output_stride_w Stride of the destination tensor in W dimension (in bytes) |
| 205 | * @param[in] output_step_w output_stride_w * number of elements along W processed per workitem(in bytes) |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 206 | * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination image |
| 207 | */ |
| 208 | __kernel void pooling_layer_MxN_quantized_nhwc( |
Georgios Pinitas | 89d7173 | 2018-10-29 20:07:15 +0000 | [diff] [blame] | 209 | TENSOR4D_DECLARATION(input), |
| 210 | TENSOR4D_DECLARATION(output)) |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 211 | { |
| 212 | // Get pixels pointer |
Georgios Pinitas | 89d7173 | 2018-10-29 20:07:15 +0000 | [diff] [blame] | 213 | #if defined(DST_DEPTH) |
| 214 | Tensor4D input = CONVERT_TO_TENSOR4D_STRUCT(input, DST_DEPTH); |
| 215 | Tensor4D output = CONVERT_TO_TENSOR4D_STRUCT(output, DST_DEPTH); |
| 216 | #else /* defined(DST_DEPTH) */ |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame] | 217 | Tensor3D input = CONVERT_TO_TENSOR3D_STRUCT(input); |
| 218 | Tensor3D output = CONVERT_TO_TENSOR3D_STRUCT(output); |
Georgios Pinitas | 89d7173 | 2018-10-29 20:07:15 +0000 | [diff] [blame] | 219 | #endif /* defined(DST_DEPTH) */ |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 220 | |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame] | 221 | int8 vdata = INITIAL_VALUE; |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 222 | |
Georgios Pinitas | 89d7173 | 2018-10-29 20:07:15 +0000 | [diff] [blame] | 223 | const int idx_width = get_global_id(1) * STRIDE_X; |
| 224 | #if defined(DST_DEPTH) |
| 225 | const int idx_height = (get_global_id(2) % DST_DEPTH) * STRIDE_Y; |
| 226 | #else /* defined(DST_DEPTH) */ |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame] | 227 | const int idx_height = get_global_id(2) * STRIDE_Y; |
Georgios Pinitas | 89d7173 | 2018-10-29 20:07:15 +0000 | [diff] [blame] | 228 | #endif /* defined(DST_DEPTH) */ |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 229 | |
| 230 | for(int y = 0; y < POOL_SIZE_Y; ++y) |
| 231 | { |
Michalis Spyrou | 645e837 | 2018-10-30 16:41:21 +0000 | [diff] [blame] | 232 | int y1 = select(y, PAD_Y - idx_height, y + idx_height - PAD_Y < 0 || y + idx_height - PAD_Y >= MAX_HEIGHT); |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 233 | for(int x = 0; x < POOL_SIZE_X; ++x) |
| 234 | { |
Michalis Spyrou | 645e837 | 2018-10-30 16:41:21 +0000 | [diff] [blame] | 235 | int x1 = select(x, PAD_X - idx_width - 1, x + idx_width - PAD_X < 0 || x + idx_width - PAD_X >= MAX_WIDTH); |
Georgios Pinitas | 89d7173 | 2018-10-29 20:07:15 +0000 | [diff] [blame] | 236 | x1 = select(x1, PAD_X - idx_width - 1, y != y1); |
| 237 | |
| 238 | #if defined(DST_DEPTH) |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame] | 239 | VEC_TYPE(8) |
| 240 | data = vload8(0, (__global DATA_TYPE *)tensor4D_offset(&input, 0, x1 - PAD_X, y1 - PAD_Y, 0)); |
Georgios Pinitas | 89d7173 | 2018-10-29 20:07:15 +0000 | [diff] [blame] | 241 | #else /* defined(DST_DEPTH) */ |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame] | 242 | VEC_TYPE(8) |
| 243 | data = vload8(0, (__global DATA_TYPE *)tensor3D_offset(&input, 0, x1 - PAD_X, y1 - PAD_Y)); |
Georgios Pinitas | 89d7173 | 2018-10-29 20:07:15 +0000 | [diff] [blame] | 244 | #endif /* defined(DST_DEPTH) */ |
| 245 | |
| 246 | int8 data0 = convert_int8(data); |
| 247 | vdata = POOL_OP(vdata, data0); |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | |
| 251 | #if defined(POOL_AVG) |
| 252 | // Divide by pool region in case of average pooling |
| 253 | vdata = convert_int8(round(DIV_OP_NHWC(vdata, calculate_avg_scale_nhwc(POOL_SIZE_X, POOL_SIZE_Y, MAX_WIDTH, MAX_HEIGHT, PAD_X, PAD_Y, STRIDE_X, STRIDE_Y)))); |
| 254 | #endif /* defined(POOL_AVG) */ |
| 255 | |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame] | 256 | VEC_TYPE(8) |
| 257 | out_q8 = CONVERT(vdata, VEC_TYPE(8)); |
Pablo Tello | a52e4cf | 2019-04-01 14:55:18 +0100 | [diff] [blame] | 258 | #if defined(OFFSET_IN1) && defined(OFFSET_OUT) && defined(SCALE_IN1) && defined(SCALE_OUT) |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame] | 259 | REQUANTIZE(8, out_q8, OFFSET_IN1, OFFSET_OUT, SCALE_IN1, SCALE_OUT, out_q8); |
Pablo Tello | a52e4cf | 2019-04-01 14:55:18 +0100 | [diff] [blame] | 260 | #endif /* defined(OFFSET_IN1) && defined(OFFSET_OUT) && defined(SCALE_IN1) && defined(SCALE_OUT) */ |
| 261 | |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 262 | // Store result |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame] | 263 | vstore8(out_q8, 0, (__global DATA_TYPE *)output.ptr); |
Pablo Tello | a52e4cf | 2019-04-01 14:55:18 +0100 | [diff] [blame] | 264 | } |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame] | 265 | #endif /* defined(DATA_TYPE) && defined(INITIAL_VALUE) */ |