blob: e16f4bf315bebfe74f40ace314fd1c05389e282a [file] [log] [blame]
Manuel Bottini8529bd62018-11-21 11:53:04 +00001/*
Viet-Hoa Do6829e022024-01-16 16:23:24 +00002 * Copyright (c) 2018-2021, 2023-2024 Arm Limited.
Manuel Bottini8529bd62018-11-21 11:53:04 +00003 *
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 Bottini8529bd62018-11-21 11:53:04 +000032 *
33 *
Sheri Zhang0de45d02020-04-17 14:59:13 +010034 * @param[in] input_ptr Pointer to the source tensor. Supported data types: All
Manuel Bottini8529bd62018-11-21 11:53:04 +000035 * @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 Bottini7ad62572019-01-16 11:18:15 +000044 * @param[in] indices_ptr Pointer to the indices vector. Supported data types: S32/U32.
Manuel Bottini8529bd62018-11-21 11:53:04 +000045 * @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 Khatibcdd1e032023-04-26 11:31:45 +010061 TENSOR4D_DECLARATION(indices),
Manuel Bottini8529bd62018-11-21 11:53:04 +000062 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 Khatibcdd1e032023-04-26 11:31:45 +010067 const int pw = (get_global_id(2) / OUTPUT_DIM_Z );
Manuel Bottini8529bd62018-11-21 11:53:04 +000068
Viet-Hoa Do6829e022024-01-16 16:23:24 +000069 const Tensor4D input = CONVERT_TO_TENSOR4D_STRUCT_NO_STEP(input);
70 const Tensor4D indices = CONVERT_TO_TENSOR4D_STRUCT_NO_STEP(indices);
Manuel Bottini8529bd62018-11-21 11:53:04 +000071 Tensor4D output = CONVERT_TO_TENSOR4D_STRUCT(output, OUTPUT_DIM_Z);
72
73#if AXIS == 0
Omar Al Khatibcdd1e032023-04-26 11:31:45 +010074#if INDICES_DIMS == 1
75 const uint index = *(__global const uint *)tensor4D_offset(&indices, px, 0, 0, 0);
Viet-Hoa Doa25582c2023-03-15 16:52:05 +000076 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 Khatibcdd1e032023-04-26 11:31:45 +010078#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 Bottini8529bd62018-11-21 11:53:04 +000092#elif AXIS == 1
Omar Al Khatibcdd1e032023-04-26 11:31:45 +010093#if INDICES_DIMS == 1
94 const uint index = *(__global const uint *)tensor4D_offset(&indices, py, 0, 0, 0);
Viet-Hoa Doa25582c2023-03-15 16:52:05 +000095 const uint safe_index = select((uint)0, index, index < INDEX_LIMIT);
Omar Al Khatibcdd1e032023-04-26 11:31:45 +010096 __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 Bottini8529bd62018-11-21 11:53:04 +0000107#elif AXIS == 2
Omar Al Khatibcdd1e032023-04-26 11:31:45 +0100108#if INDICES_DIMS == 1
109 const uint index = *(__global const uint *)tensor4D_offset(&indices, pz, 0, 0, 0);
Viet-Hoa Doa25582c2023-03-15 16:52:05 +0000110 const uint safe_index = select((uint)0, index, index < INDEX_LIMIT);
Omar Al Khatibcdd1e032023-04-26 11:31:45 +0100111 __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 Bottini8529bd62018-11-21 11:53:04 +0000118#elif AXIS == 3
Omar Al Khatibcdd1e032023-04-26 11:31:45 +0100119#if INDICES_DIMS == 1
120 const uint index = *(__global const uint *)tensor4D_offset(&indices, pw, 0, 0, 0);
Viet-Hoa Doa25582c2023-03-15 16:52:05 +0000121 const uint safe_index = select((uint)0, index, index < INDEX_LIMIT);
Omar Al Khatibcdd1e032023-04-26 11:31:45 +0100122 __global const uchar *input_addr = tensor4D_offset(&input, px, py, pz, safe_index);
123#endif //INDICES_DIMS
124
Manuel Bottini8529bd62018-11-21 11:53:04 +0000125#endif //AXIS
126
Viet-Hoa Doa25582c2023-03-15 16:52:05 +0000127 *(__global DATA_TYPE *)output.ptr = select((DATA_TYPE)0, *((__global const DATA_TYPE *)input_addr), (DATA_TYPE)(index < INDEX_LIMIT));
Manuel Bottini8529bd62018-11-21 11:53:04 +0000128}
129
Viet-Hoa Do6829e022024-01-16 16:23:24 +0000130#endif //defined(DATA_TYPE) && defined(AXIS)