Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2019 Arm Limited. |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +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_asymm.h" |
| 25 | |
| 26 | /** Generate all the region of interests based on the image size and the anchors passed in. For each element (x,y) of the |
| 27 | * grid, it will generate NUM_ANCHORS rois, given by shifting the grid position to match the anchor. |
| 28 | * |
| 29 | * @attention The following variables must be passed at compile time: |
| 30 | * -# -DDATA_TYPE= Tensor data type. Supported data types: QASYMM8 |
| 31 | * -# -DHEIGHT= Height of the feature map on which this kernel is applied |
| 32 | * -# -DWIDTH= Width of the feature map on which this kernel is applied |
| 33 | * -# -DNUM_ANCHORS= Number of anchors to be used to generate the rois per each pixel |
| 34 | * -# -DSTRIDE= Stride to be applied at each different pixel position (i.e., x_range = (1:WIDTH)*STRIDE and y_range = (1:HEIGHT)*STRIDE |
| 35 | * -# -DNUM_ROI_FIELDS= Number of fields used to represent a roi |
| 36 | * |
| 37 | * @param[in] anchors_ptr Pointer to the anchors tensor. Supported data types: QASYMM8 |
| 38 | * @param[in] anchors_stride_x Stride of the anchors tensor in X dimension (in bytes) |
| 39 | * @param[in] anchors_step_x anchors_stride_x * number of elements along X processed per workitem(in bytes) |
| 40 | * @param[in] anchors_stride_y Stride of the anchors tensor in Y dimension (in bytes) |
| 41 | * @param[in] anchors_step_y anchors_stride_y * number of elements along Y processed per workitem(in bytes) |
| 42 | * @param[in] anchors_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 43 | * @param[in] anchors_step_z anchors_stride_z * number of elements along Z processed per workitem(in bytes) |
| 44 | * @param[in] anchors_offset_first_element_in_bytes The offset of the first element in the boxes tensor |
| 45 | * @param[out] rois_ptr Pointer to the rois. Supported data types: same as @p in_ptr |
| 46 | * @param[out] rois_stride_x Stride of the rois in X dimension (in bytes) |
| 47 | * @param[out] rois_step_x pred_boxes_stride_x * number of elements along X processed per workitem(in bytes) |
| 48 | * @param[out] rois_stride_y Stride of the rois in Y dimension (in bytes) |
| 49 | * @param[out] rois_step_y pred_boxes_stride_y * number of elements along Y processed per workitem(in bytes) |
| 50 | * @param[out] rois_stride_z Stride of the rois in Z dimension (in bytes) |
| 51 | * @param[out] rois_step_z pred_boxes_stride_z * number of elements along Z processed per workitem(in bytes) |
| 52 | * @param[out] rois_offset_first_element_in_bytes The offset of the first element in the rois |
| 53 | */ |
| 54 | #if defined(DATA_TYPE) && defined(WIDTH) && defined(HEIGHT) && defined(NUM_ANCHORS) && defined(STRIDE) && defined(NUM_ROI_FIELDS) && defined(OFFSET) && defined(SCALE) |
| 55 | __kernel void generate_proposals_compute_all_anchors_quantized( |
| 56 | VECTOR_DECLARATION(anchors), |
| 57 | VECTOR_DECLARATION(rois)) |
| 58 | { |
| 59 | Vector anchors = CONVERT_TO_VECTOR_STRUCT_NO_STEP(anchors); |
| 60 | Vector rois = CONVERT_TO_VECTOR_STRUCT(rois); |
| 61 | |
| 62 | const size_t idx = get_global_id(0); |
| 63 | // Find the index of the anchor |
| 64 | const size_t anchor_idx = idx % NUM_ANCHORS; |
| 65 | |
| 66 | // Find which shift is this thread using |
| 67 | const size_t shift_idx = idx / NUM_ANCHORS; |
| 68 | |
| 69 | // Compute the shift on the X and Y direction (the shift depends exclusively by the index thread id) |
| 70 | const float shift_x = (float)(shift_idx % WIDTH) * STRIDE; |
| 71 | const float shift_y = (float)(shift_idx / WIDTH) * STRIDE; |
| 72 | |
| 73 | VEC_DATA_TYPE(float, NUM_ROI_FIELDS) |
| 74 | shift = (VEC_DATA_TYPE(float, NUM_ROI_FIELDS))(shift_x, shift_y, shift_x, shift_y); |
| 75 | |
| 76 | // Read the given anchor |
| 77 | VEC_DATA_TYPE(float, NUM_ROI_FIELDS) |
| 78 | anchor = DEQUANTIZE(VLOAD(NUM_ROI_FIELDS)(0, (__global DATA_TYPE *)vector_offset(&anchors, anchor_idx * NUM_ROI_FIELDS)), OFFSET, SCALE, DATA_TYPE, NUM_ROI_FIELDS); |
| 79 | |
| 80 | // Apply the shift to the anchor |
| 81 | VEC_DATA_TYPE(float, NUM_ROI_FIELDS) |
| 82 | shifted_anchor = anchor + shift; |
| 83 | |
| 84 | VSTORE(NUM_ROI_FIELDS) |
| 85 | (QUANTIZE(shifted_anchor, OFFSET, SCALE, DATA_TYPE, NUM_ROI_FIELDS), 0, (__global DATA_TYPE *)rois.ptr); |
| 86 | } |
| 87 | #endif //defined(DATA_TYPE) && defined(WIDTH) && defined(HEIGHT) && defined(NUM_ANCHORS) && defined(STRIDE) && defined(NUM_ROI_FIELDS) && defined(OFFSET) && defined(SCALE) |