Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [diff] [blame] | 1 | /* |
Viet-Hoa Do | 6829e02 | 2024-01-16 16:23:24 +0000 | [diff] [blame^] | 2 | * Copyright (c) 2018-2021, 2023-2024 Arm Limited. |
Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +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 | |
| 26 | #if defined(DATA_TYPE) && defined(AXIS) |
| 27 | |
| 28 | /** Performs the Gather operation along the chosen axis |
| 29 | * @note Datatype should be given as a preprocessor argument using -DDATA_TYPE=type. e.g. -DDATA_TYPE=short |
| 30 | * @note Axis should be given as a preprocessor argument using -DAXIS=axis. e.g. -DAXIS=1 |
| 31 | * @attention Output tensor depth should be given as a preprocessor argument using -DOUTPUT_DIM_Z=size. e.g. -DOUTPUT_DIM_Z=16 |
Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [diff] [blame] | 32 | * |
| 33 | * |
Sheri Zhang | 0de45d0 | 2020-04-17 14:59:13 +0100 | [diff] [blame] | 34 | * @param[in] input_ptr Pointer to the source tensor. Supported data types: All |
Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [diff] [blame] | 35 | * @param[in] input_stride_x Stride of the source tensor in X dimension (in bytes) |
| 36 | * @param[in] input_step_x input_stride_x * number of elements along X processed per work item (in bytes) |
| 37 | * @param[in] input_stride_y Stride of the source tensor in Y dimension (in bytes) |
| 38 | * @param[in] input_step_y input_stride_y * number of elements along Y processed per work item (in bytes) |
| 39 | * @param[in] input_stride_z Stride of the source tensor in Y dimension (in bytes) |
| 40 | * @param[in] input_step_z input_stride_z * number of elements along Z processed per work item (in bytes) |
| 41 | * @param[in] input_stride_w Stride of the source tensor in Z dimension (in bytes) |
| 42 | * @param[in] input_step_w input_stride_w * number of elements along W processed per work item (in bytes) |
| 43 | * @param[in] input_offset_first_element_in_bytes Offset of the first element in the source tensor |
Manuel Bottini | 7ad6257 | 2019-01-16 11:18:15 +0000 | [diff] [blame] | 44 | * @param[in] indices_ptr Pointer to the indices vector. Supported data types: S32/U32. |
Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [diff] [blame] | 45 | * @param[in] indices_stride_x Stride of the indices vector in X dimension (in bytes) |
| 46 | * @param[in] indices_step_x input_stride_x * number of elements along X processed per work item (in bytes) |
| 47 | * @param[in] indices_offset_first_element_in_bytes Offset of the first element in the indices vector |
| 48 | * @param[out] output_ptr Pointer to the destination tensor. Supported data types: same as @p input_ptr |
| 49 | * @param[in] output_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 50 | * @param[in] output_step_x output_stride_x * number of elements along X processed per work item (in bytes) |
| 51 | * @param[in] output_stride_y Stride of the destination tensor in Y dimension (in bytes) |
| 52 | * @param[in] output_step_y output_stride_y * number of elements along Y processed per work item (in bytes) |
| 53 | * @param[in] output_stride_z Stride of the destination tensor in Z dimension (in bytes) |
| 54 | * @param[in] output_step_z output_stride_z * number of elements along Z processed per work item (in bytes) |
| 55 | * @param[in] output_stride_w Stride of the destination tensor in W dimension (in bytes) |
| 56 | * @param[in] output_step_w output_stride_w * number of elements along W processed per work item (in bytes) |
| 57 | * @param[in] output_offset_first_element_in_bytes Offset of the first element in the destination tensor |
| 58 | */ |
| 59 | __kernel void gather( |
| 60 | TENSOR4D_DECLARATION(input), |
Omar Al Khatib | cdd1e03 | 2023-04-26 11:31:45 +0100 | [diff] [blame] | 61 | TENSOR4D_DECLARATION(indices), |
Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [diff] [blame] | 62 | TENSOR4D_DECLARATION(output)) |
| 63 | { |
| 64 | const int px = get_global_id(0); |
| 65 | const int py = get_global_id(1); |
| 66 | const int pz = get_global_id(2) % OUTPUT_DIM_Z; |
Omar Al Khatib | cdd1e03 | 2023-04-26 11:31:45 +0100 | [diff] [blame] | 67 | const int pw = (get_global_id(2) / OUTPUT_DIM_Z ); |
Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [diff] [blame] | 68 | |
Viet-Hoa Do | 6829e02 | 2024-01-16 16:23:24 +0000 | [diff] [blame^] | 69 | const Tensor4D input = CONVERT_TO_TENSOR4D_STRUCT_NO_STEP(input); |
| 70 | const Tensor4D indices = CONVERT_TO_TENSOR4D_STRUCT_NO_STEP(indices); |
Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [diff] [blame] | 71 | Tensor4D output = CONVERT_TO_TENSOR4D_STRUCT(output, OUTPUT_DIM_Z); |
| 72 | |
| 73 | #if AXIS == 0 |
Omar Al Khatib | cdd1e03 | 2023-04-26 11:31:45 +0100 | [diff] [blame] | 74 | #if INDICES_DIMS == 1 |
| 75 | const uint index = *(__global const uint *)tensor4D_offset(&indices, px, 0, 0, 0); |
Viet-Hoa Do | a25582c | 2023-03-15 16:52:05 +0000 | [diff] [blame] | 76 | const uint safe_index = select((uint)0, index, index < INDEX_LIMIT); |
| 77 | __global const uchar *input_addr = tensor4D_offset(&input, safe_index, py, pz, pw); |
Omar Al Khatib | cdd1e03 | 2023-04-26 11:31:45 +0100 | [diff] [blame] | 78 | #elif INDICES_DIMS == 2 |
| 79 | const uint index = *(__global const uint *)tensor4D_offset(&indices, px, py, 0, 0); |
| 80 | const uint safe_index = select((uint)0, index, index < INDEX_LIMIT); |
| 81 | __global const uchar *input_addr = tensor4D_offset(&input, safe_index, pz, pw, 0); |
| 82 | #elif INDICES_DIMS == 3 |
| 83 | const uint index = *(__global const uint *)tensor4D_offset(&indices, px, py, pz, 0); |
| 84 | const uint safe_index = select((uint)0, index, index < INDEX_LIMIT); |
| 85 | __global const uchar *input_addr = tensor4D_offset(&input, safe_index, pw, 0, 0); |
| 86 | #elif INDICES_DIMS == 4 |
| 87 | const uint index = *(__global const uint *)tensor4D_offset(&indices, px, py, pz, pw); |
| 88 | const uint safe_index = select((uint)0, index, index < INDEX_LIMIT); |
| 89 | __global const uchar *input_addr = tensor4D_offset(&input, safe_index, 0, 0, 0); |
| 90 | #endif //INDICES_DIMS |
| 91 | |
Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [diff] [blame] | 92 | #elif AXIS == 1 |
Omar Al Khatib | cdd1e03 | 2023-04-26 11:31:45 +0100 | [diff] [blame] | 93 | #if INDICES_DIMS == 1 |
| 94 | const uint index = *(__global const uint *)tensor4D_offset(&indices, py, 0, 0, 0); |
Viet-Hoa Do | a25582c | 2023-03-15 16:52:05 +0000 | [diff] [blame] | 95 | const uint safe_index = select((uint)0, index, index < INDEX_LIMIT); |
Omar Al Khatib | cdd1e03 | 2023-04-26 11:31:45 +0100 | [diff] [blame] | 96 | __global const uchar *input_addr = tensor4D_offset(&input, px, safe_index, pz, pw); |
| 97 | #elif INDICES_DIMS == 2 |
| 98 | const uint index = *(__global const uint *)tensor4D_offset(&indices, py, pz, 0, 0); |
| 99 | const uint safe_index = select((uint)0, index, index < INDEX_LIMIT); |
| 100 | __global const uchar *input_addr = tensor4D_offset(&input, px, safe_index, pw, 0); |
| 101 | #elif INDICES_DIMS == 3 |
| 102 | const uint index = *(__global const uint *)tensor4D_offset(&indices, py, pz, pw, 0); |
| 103 | const uint safe_index = select((uint)0, index, index < INDEX_LIMIT); |
| 104 | __global const uchar *input_addr = tensor4D_offset(&input, px, safe_index, 0, 0); |
| 105 | #endif //INDICES_DIMS |
| 106 | |
Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [diff] [blame] | 107 | #elif AXIS == 2 |
Omar Al Khatib | cdd1e03 | 2023-04-26 11:31:45 +0100 | [diff] [blame] | 108 | #if INDICES_DIMS == 1 |
| 109 | const uint index = *(__global const uint *)tensor4D_offset(&indices, pz, 0, 0, 0); |
Viet-Hoa Do | a25582c | 2023-03-15 16:52:05 +0000 | [diff] [blame] | 110 | const uint safe_index = select((uint)0, index, index < INDEX_LIMIT); |
Omar Al Khatib | cdd1e03 | 2023-04-26 11:31:45 +0100 | [diff] [blame] | 111 | __global const uchar *input_addr = tensor4D_offset(&input, px, py, safe_index, pw); |
| 112 | #elif INDICES_DIMS == 2 |
| 113 | const uint index = *(__global const uint *)tensor4D_offset(&indices, pz, pw, 0, 0); |
| 114 | const uint safe_index = select((uint)0, index, index < INDEX_LIMIT); |
| 115 | __global const uchar *input_addr = tensor4D_offset(&input, px, py, safe_index, 0); |
| 116 | #endif //INDICES_DIMS |
| 117 | |
Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [diff] [blame] | 118 | #elif AXIS == 3 |
Omar Al Khatib | cdd1e03 | 2023-04-26 11:31:45 +0100 | [diff] [blame] | 119 | #if INDICES_DIMS == 1 |
| 120 | const uint index = *(__global const uint *)tensor4D_offset(&indices, pw, 0, 0, 0); |
Viet-Hoa Do | a25582c | 2023-03-15 16:52:05 +0000 | [diff] [blame] | 121 | const uint safe_index = select((uint)0, index, index < INDEX_LIMIT); |
Omar Al Khatib | cdd1e03 | 2023-04-26 11:31:45 +0100 | [diff] [blame] | 122 | __global const uchar *input_addr = tensor4D_offset(&input, px, py, pz, safe_index); |
| 123 | #endif //INDICES_DIMS |
| 124 | |
Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [diff] [blame] | 125 | #endif //AXIS |
| 126 | |
Viet-Hoa Do | a25582c | 2023-03-15 16:52:05 +0000 | [diff] [blame] | 127 | *(__global DATA_TYPE *)output.ptr = select((DATA_TYPE)0, *((__global const DATA_TYPE *)input_addr), (DATA_TYPE)(index < INDEX_LIMIT)); |
Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Viet-Hoa Do | 6829e02 | 2024-01-16 16:23:24 +0000 | [diff] [blame^] | 130 | #endif //defined(DATA_TYPE) && defined(AXIS) |