Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 ARM Limited. |
| 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #include "helpers.h" |
| 25 | |
| 26 | #if defined(POOL_AVG) |
| 27 | #define POOL_OP(x, y) ((x) + (y)) |
| 28 | #else /* defined(POOL_AVG) */ |
| 29 | #define POOL_OP(x, y) (max((x), (y))) |
| 30 | #endif /* defined(POOL_AVG) */ |
| 31 | |
| 32 | #define DIV_OP(x, y) (x * (1.f / y)) |
| 33 | |
| 34 | #if defined(POOL_L2) |
| 35 | #error "L2 pooling is not supported" |
| 36 | #endif /* defined(POOL_L2) */ |
| 37 | |
| 38 | int calculate_avg_scale(const int pool_size, const int upper_bound_w, const int upper_bound_h, |
| 39 | const int pad_x, const int pad_y, const int stride_x, const int stride_y) |
| 40 | { |
| 41 | int start_x = get_global_id(0) * stride_x - pad_x; |
| 42 | int start_y = get_global_id(1) * stride_y - pad_y; |
| 43 | const int end_x = min(start_x + pool_size, upper_bound_w); |
| 44 | const int end_y = min(start_y + pool_size, upper_bound_h); |
| 45 | #if defined(EXCLUDE_PADDING) |
| 46 | start_x = max(0, start_x); |
| 47 | start_y = max(0, start_y); |
| 48 | #endif /* defined(EXCLUDE_PADDING) */ |
| 49 | return ((end_y - start_y) * (end_x - start_x)); |
| 50 | } |
| 51 | |
| 52 | /** Performs a pooling function of pool size equal to N |
| 53 | * |
| 54 | * @note Pool size must be passed using -DPOOL_SIZE e.g. -DPOOL_SIZE=13; |
| 55 | * @note In case of average pooling the following information must be passed at compile time: |
| 56 | * -DPOOL_AVG must be provided otherwise max pooling will be performed. |
| 57 | * -DMAX_WIDTH and -DMAX_HEIGHT which are the maximum accessible indeces in x and y dimensions (width + pad) |
| 58 | * -DSTRIDE_X and -DSTRIDE_Y which are the steps of the window along the x and y directions |
| 59 | * -DPAD_X and -DPAD_Y which are the pooling paddings in x and y dimension |
| 60 | * |
| 61 | * @param[in] input_ptr Pointer to the source image. Supported data types: QASYMM8 |
| 62 | * @param[in] input_stride_x Stride of the source image in X dimension (in bytes) |
| 63 | * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes) |
| 64 | * @param[in] input_stride_y Stride of the source image in Y dimension (in bytes) |
| 65 | * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes) |
| 66 | * @param[in] input_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 67 | * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes) |
| 68 | * @param[in] input_offset_first_element_in_bytes The offset of the first element in the source image |
| 69 | * @param[out] output_ptr Pointer to the destination image. Supported data types: same as @p input_ptr |
| 70 | * @param[in] output_stride_x Stride of the destination image in X dimension (in bytes) |
| 71 | * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes) |
| 72 | * @param[in] output_stride_y Stride of the destination image in Y dimension (in bytes) |
| 73 | * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes) |
| 74 | * @param[in] output_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 75 | * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes) |
| 76 | * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination image |
| 77 | */ |
| 78 | __kernel void pooling_layer_N_quantized( |
| 79 | TENSOR3D_DECLARATION(input), |
| 80 | TENSOR3D_DECLARATION(output)) |
| 81 | { |
| 82 | // Get pixels pointer |
| 83 | Tensor3D input = CONVERT_TO_TENSOR3D_STRUCT(input); |
| 84 | Tensor3D output = CONVERT_TO_TENSOR3D_STRUCT(output); |
| 85 | |
| 86 | int8 vdata = 0; |
| 87 | int sdata = 0; |
| 88 | |
| 89 | // Load data |
| 90 | for(int y = 0; y < POOL_SIZE; y++) |
| 91 | { |
| 92 | int x = 0; |
| 93 | for(; x <= ((int)POOL_SIZE - 8); x += 8) |
| 94 | { |
| 95 | uchar8 data = vload8(0, (__global uchar *)tensor3D_offset(&input, x, y, 0)); |
| 96 | int8 data0 = convert_int8(data); |
| 97 | vdata = POOL_OP(vdata, data0); |
| 98 | } |
| 99 | |
| 100 | // Leftover |
| 101 | for(; x < (int)POOL_SIZE; ++x) |
| 102 | { |
| 103 | uchar data = *((__global uchar *)tensor3D_offset(&input, x, y, 0)); |
| 104 | int data0 = convert_int(data); |
| 105 | sdata = POOL_OP(sdata, data0); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | // Reduce result |
| 110 | int4 reduce4 = POOL_OP(vdata.s0123, vdata.s4567); |
| 111 | int2 reduce2 = POOL_OP(reduce4.s01, reduce4.s23); |
| 112 | int res = POOL_OP(reduce2.s0, reduce2.s1); |
| 113 | res = POOL_OP(res, sdata); |
| 114 | |
| 115 | #if defined(POOL_AVG) |
Georgios Pinitas | 2f18579 | 2017-11-10 14:25:59 +0000 | [diff] [blame] | 116 | res = round(DIV_OP(res, calculate_avg_scale(POOL_SIZE, MAX_WIDTH, MAX_HEIGHT, PAD_X, PAD_Y, STRIDE_X, STRIDE_Y))); |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 117 | #endif /* defined(POOL_AVG) */ |
| 118 | |
| 119 | // Store result |
| 120 | *(__global uchar *)output.ptr = convert_uchar(res); |
| 121 | } |