Michalis Spyrou | 5c8e05c | 2018-03-22 11:56:01 +0000 | [diff] [blame] | 1 | /* |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame^] | 2 | * Copyright (c) 2018-2019 ARM Limited. |
Michalis Spyrou | 5c8e05c | 2018-03-22 11:56:01 +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 | |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 26 | #if defined(PAD00) && defined(PAD10) && defined(PAD20) && defined(PAD21) && defined(PAD30) && defined(DATA_TYPE) && defined(VEC_SIZE) // Compile time constants |
| 27 | |
| 28 | /** Perform a padded copy of input tensor to the output tensor. Padding values are defined at compile time |
| 29 | * |
| 30 | * @attention The following variables must be passed at compile time: |
| 31 | * -# -DPAD{d}{0,1} = padding before{0} and after{1} dimension d (d < 4) |
| 32 | * -# -DDEPTH = The third dimension (depth) of the tensor (it is needed only if d == 3) |
| 33 | * -# -DDATA_TYPE = Input and output datatypes. |
| 34 | * |
| 35 | * @param[in] in_ptr Pointer to the source tensor. Supported data types: U8/S8/QASYMM8/U16/S16/F16/U32/S32/F32 |
| 36 | * @param[in] in_stride_x Stride of the source tensor in X dimension (in bytes) |
| 37 | * @param[in] in_step_x input_stride_x * number of elements along X processed per workitem(in bytes) |
| 38 | * @param[in] in_stride_y Stride of the source tensor in Y dimension (in bytes) |
| 39 | * @param[in] in_step_y input_stride_y * number of elements along Y processed per workitem(in bytes) |
| 40 | * @param[in] in_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 41 | * @param[in] in_step_z input_stride_z * number of elements along Z processed per workitem(in bytes) |
| 42 | * @param[in] in_offset_first_element_in_bytes The offset of the first element in the source tensor |
| 43 | * @param[out] out_ptr Pointer to the destination tensor. Supported data types: same as @p in_ptr |
| 44 | * @param[in] out_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 45 | * @param[in] out_step_x output_stride_x * number of elements along X processed per workitem(in bytes) |
| 46 | * @param[in] out_stride_y Stride of the destination tensor in Y dimension (in bytes) |
| 47 | * @param[in] out_step_y output_stride_y * number of elements along Y processed per workitem(in bytes) |
| 48 | * @param[in] out_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 49 | * @param[in] out_step_z output_stride_z * number of elements along Z processed per workitem(in bytes) |
| 50 | * @param[in] out_offset_first_element_in_bytes The offset of the first element in the destination tensor |
| 51 | */ |
| 52 | __kernel void copy_pad_tensor( |
| 53 | TENSOR3D_DECLARATION(in), |
| 54 | TENSOR3D_DECLARATION(out)) |
| 55 | |
| 56 | { |
| 57 | Tensor3D in = CONVERT_TO_TENSOR3D_STRUCT(in); |
| 58 | Tensor3D out = CONVERT_TO_TENSOR3D_STRUCT(out); |
| 59 | |
| 60 | const int offset_x = PAD00; |
| 61 | const int offset_y = PAD10; |
| 62 | const int offset_z = PAD20; |
| 63 | |
| 64 | #if PAD30 > 0 |
| 65 | const size_t in_batch = get_global_id(2) / DEPTH; |
| 66 | const int total_depth = DEPTH + PAD20 + PAD21; |
| 67 | const int offset_w = PAD30 * total_depth + in_batch * (PAD20 + PAD21); |
| 68 | #else // PAD30 == 0 |
| 69 | const int offset_w = 0; |
| 70 | #endif // PAD30 |
| 71 | |
| 72 | VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) |
| 73 | data = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)in.ptr); |
| 74 | |
| 75 | VSTORE(VEC_SIZE) |
| 76 | (data, 0, (__global DATA_TYPE *)tensor3D_offset(&out, offset_x, offset_y, offset_z + offset_w)); |
| 77 | } |
| 78 | #endif // Compile time constants |
| 79 | |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame^] | 80 | #if defined(DATA_TYPE) |
Michalis Spyrou | 5c8e05c | 2018-03-22 11:56:01 +0000 | [diff] [blame] | 81 | /** Performs a copy of input tensor to the output tensor. |
| 82 | * |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 83 | * @param[in] in_ptr Pointer to the source tensor. Supported data types: U8/S8/QASYMM8/U16/S16/F16/U32/S32/F32 |
| 84 | * @param[in] in_stride_x Stride of the source tensor in X dimension (in bytes) |
| 85 | * @param[in] in_step_x input_stride_x * number of elements along X processed per workitem(in bytes) |
| 86 | * @param[in] in_stride_y Stride of the source tensor in Y dimension (in bytes) |
| 87 | * @param[in] in_step_y input_stride_y * number of elements along Y processed per workitem(in bytes) |
| 88 | * @param[in] in_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 89 | * @param[in] in_step_z input_stride_z * number of elements along Z processed per workitem(in bytes) |
| 90 | * @param[in] in_offset_first_element_in_bytes The offset of the first element in the source tensor |
| 91 | * @param[out] out_ptr Pointer to the destination tensor. Supported data types: same as @p in_ptr |
| 92 | * @param[in] out_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 93 | * @param[in] out_step_x output_stride_x * number of elements along X processed per workitem(in bytes) |
| 94 | * @param[in] out_stride_y Stride of the destination tensor in Y dimension (in bytes) |
| 95 | * @param[in] out_step_y output_stride_y * number of elements along Y processed per workitem(in bytes) |
| 96 | * @param[in] out_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 97 | * @param[in] out_step_z output_stride_z * number of elements along Z processed per workitem(in bytes) |
| 98 | * @param[in] out_offset_first_element_in_bytes The offset of the first element in the destination tensor |
Michalis Spyrou | 5c8e05c | 2018-03-22 11:56:01 +0000 | [diff] [blame] | 99 | */ |
| 100 | __kernel void copy_tensor( |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 101 | TENSOR3D_DECLARATION(in), |
| 102 | TENSOR3D_DECLARATION(out)) |
Michalis Spyrou | 5c8e05c | 2018-03-22 11:56:01 +0000 | [diff] [blame] | 103 | { |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 104 | Tensor3D in = CONVERT_TO_TENSOR3D_STRUCT(in); |
| 105 | Tensor3D out = CONVERT_TO_TENSOR3D_STRUCT(out); |
Michalis Spyrou | 5c8e05c | 2018-03-22 11:56:01 +0000 | [diff] [blame] | 106 | |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame^] | 107 | #if defined(VEC_SIZE) |
| 108 | |
| 109 | #if defined(LAST_ACCESSED_X) |
| 110 | // Check if access on width gets out of bounds |
| 111 | // If it does then shift access vector to access elements within bounds |
| 112 | const int shift = max((int)(get_global_id(0) * VEC_SIZE) - (int)LAST_ACCESSED_X, 0); |
| 113 | in.ptr -= shift * in.stride_x; |
| 114 | out.ptr -= shift * out.stride_x; |
| 115 | #endif // defined(LAST_ACCESSED_X) |
| 116 | |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 117 | // Load data |
| 118 | VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) |
| 119 | data = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)in.ptr); |
Michalis Spyrou | 5c8e05c | 2018-03-22 11:56:01 +0000 | [diff] [blame] | 120 | |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 121 | // Store result |
| 122 | VSTORE(VEC_SIZE) |
| 123 | (data, 0, (__global DATA_TYPE *)out.ptr); |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame^] | 124 | #else // defined(VEC_SIZE) |
| 125 | *((__global DATA_TYPE *)(out.ptr)) = *((__global DATA_TYPE *)(in.ptr)); |
| 126 | #endif // defined(VEC_SIZE) |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 127 | } |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame^] | 128 | #endif // defined(DATA_TYPE) |