1/* 2 * Copyright (c) 2019-2021 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(DATA_TYPE) // Compile time constants 27 28/** Performs a tensor cropping. 29 * 30 * @param[in] in_ptr Pointer to the source tensor. Supported data types: All 31 * @param[in] in_stride_x Stride of the source tensor in X dimension (in bytes) 32 * @param[in] in_step_x input_stride_x * number of elements along X processed per workitem(in bytes) 33 * @param[in] in_stride_y Stride of the source tensor in Y dimension (in bytes) 34 * @param[in] in_step_y input_stride_y * number of elements along Y processed per workitem(in bytes) 35 * @param[in] in_stride_z Stride of the source tensor in Z dimension (in bytes) 36 * @param[in] in_step_z input_stride_z * number of elements along Z processed per workitem(in bytes) 37 * @param[in] in_offset_first_element_in_bytes The offset of the first element in the source tensor 38 * @param[out] out_ptr Pointer to the destination tensor. Supported data types: F32 39 * @param[in] out_stride_x Stride of the destination tensor in X dimension (in bytes) 40 * @param[in] out_step_x output_stride_x * number of elements along X processed per workitem(in bytes) 41 * @param[in] out_stride_y Stride of the destination tensor in Y dimension (in bytes) 42 * @param[in] out_step_y output_stride_y * number of elements along Y processed per workitem(in bytes) 43 * @param[in] out_stride_z Stride of the source tensor in Z dimension (in bytes) 44 * @param[in] out_step_z output_stride_z * number of elements along Z processed per workitem(in bytes) 45 * @param[in] out_offset_first_element_in_bytes The offset of the first element in the destination tensor 46 * @param[in] in_offset_y The initial offset of the input address along Y. 47 * @param[in] in_offset_z The initial offset of the input address along Z. 48 */ 49__kernel void crop_tensor( 50 TENSOR3D_DECLARATION(in), 51 TENSOR3D_DECLARATION(out), 52 int in_offset_y, 53 int in_offset_z) 54{ 55 Tensor3D in = CONVERT_TO_TENSOR3D_STRUCT_NO_STEP(in); 56 Tensor3D out = CONVERT_TO_TENSOR3D_STRUCT(out); 57 58 const int in_x = get_global_id(0) * (in_step_x / in_stride_x); 59 60#if defined(WIDTH_FLIPPED) 61 const int in_y = in_offset_y - get_global_id(1); 62#else // defined(WIDTH_FLIPPED) 63 const int in_y = in_offset_y + get_global_id(1); 64#endif // defined(WIDTH_FLIPPED) 65 66#if defined(HEIGHT_FLIPPED) 67 const int in_z = in_offset_z - get_global_id(2); 68#else // defined(HEIGHT_FLIPPED) 69 const int in_z = in_offset_z + get_global_id(2); 70#endif // defined(HEIGHT_FLIPPED) 71 72#if defined(VEC_SIZE) 73 74#if defined(LAST_ACCESSED_X) 75 // Check if access on width gets out of bounds 76 // If it does then shift access vector to access elements within bounds 77 const int shift = max((int)(get_global_id(0) * VEC_SIZE) - (int)LAST_ACCESSED_X, 0); 78 in.ptr -= shift * in.stride_x; 79 out.ptr -= shift * out.stride_x; 80#endif // defined(LAST_ACCESSED_X) 81 82 __global const uchar *input_addr = tensor3D_offset(&in, in_x, in_y, in_z); 83 84 // Load data 85 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) 86 data = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)input_addr); 87 88 // Store result 89 VSTORE(VEC_SIZE) 90 (CONVERT(data, VEC_DATA_TYPE(float, VEC_SIZE)), 0, (__global float *)out.ptr); 91#else // defined(VEC_SIZE) 92 *((__global float *)(out.ptr)) = CONVERT(*((__global DATA_TYPE *)tensor3D_offset(&in, in_x, in_y, in_z)), float); 93#endif // defined(VEC_SIZE) 94} 95 96#endif // defined(DATA_TYPE) && defined(LAST_ACCESSED_X)