blob: f94bfb66406c1220e89dd880b59a7c5b990bdd0c [file] [log] [blame]
Michele Di Giorgio5daeffd2018-11-26 10:01:15 +00001/*
Adnan AlSinan704c22f2023-10-24 11:05:56 +01002* Copyright (c) 2018-2021, 2023 Arm Limited.
Michele Di Giorgiof6f78762020-07-06 11:27:21 +01003 *
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 */
Michele Di Giorgio5daeffd2018-11-26 10:01:15 +000024#include "helpers.h"
25
26#if defined(DATA_TYPE) && defined(NUM_REVERSE_DIMS)
27
28#if NUM_REVERSE_DIMS > 4
29#error("Reversing more than 4 dimensions is not currently supported")
30#endif /* NUM_REVERSE_DIMS > 4 */
31
32/** Performs reverse along the specified axis.
33 *
34 * @note The data type must be given as a preprocessor argument using -DDATA_TYPE=num. e.g. -DDATA_TYPE=uint
35 * @note The number of dimensions to reverse must be given as a preprocessor argument using -DNUM_REVERSE_DIMS=num, e.g. -DNUM_REVERSE_DIMS=3
Adnan AlSinan704c22f2023-10-24 11:05:56 +010036 * @note The number of dimensions of the source tensor must be given as a preprocessor argument using -DRANK=num, e.g. -DRANK=3
37 * @note The values in axis_tensor must be within [-rank, rank-1].
Michele Di Giorgio5daeffd2018-11-26 10:01:15 +000038 *
Michele Di Giorgiof6f78762020-07-06 11:27:21 +010039 * @param[in] src_ptr Pointer to the source tensor. Supported data types: All
Michele Di Giorgio5daeffd2018-11-26 10:01:15 +000040 * @param[in] src_stride_x Stride of the first source tensor in X dimension (in bytes)
41 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
42 * @param[in] src_stride_y Stride of the first source tensor in Y dimension (in bytes)
43 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
44 * @param[in] src_stride_z Stride of the first source tensor in Z dimension (in bytes)
45 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
46 * @param[in] src_stride_w Stride of the first source tensor in Z dimension (in bytes)
47 * @param[in] src_step_w src_stride_z * number of elements along Z processed per workitem(in bytes)
48 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the first source tensor
49 * @param[in] axis_ptr Pointer to the source vector. Supported data types: U32
50 * @param[in] axis_stride_x Stride of the first source tensor in X dimension (in bytes)
51 * @param[in] axis_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
52 * @param[in] axis_offset_first_element_in_bytes The offset of the first element in the first source tensor
53 * @param[out] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr
54 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
55 * @param[in] dst_step_x output_stride_x * number of elements along X processed per workitem(in bytes)
56 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
57 * @param[in] dst_step_y output_stride_y * number of elements along Y processed per workitem(in bytes)
58 * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes)
59 * @param[in] dst_step_z output_stride_z * number of elements along Z processed per workitem(in bytes)
60 * @param[in] dst_stride_w Stride of the destination tensor in Z dimension (in bytes)
61 * @param[in] dst_step_w output_stride_z * number of elements along Z processed per workitem(in bytes)
62 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
63 */
64__kernel void reverse(TENSOR4D_DECLARATION(src),
65 VECTOR_DECLARATION(axis),
66 TENSOR4D_DECLARATION(dst),
67 const uint width,
68 const uint height,
69 const uint depth,
70 const uint batches)
71{
72 Tensor4D src = CONVERT_TO_TENSOR4D_STRUCT(src, depth);
73 Vector axis = CONVERT_TO_VECTOR_STRUCT_NO_STEP(axis);
74 Tensor4D dst = CONVERT_TO_TENSOR4D_STRUCT_NO_STEP(dst, depth);
75
76 const uint x_in = get_global_id(0);
77 const uint y_in = get_global_id(1);
78 const uint z_in = get_global_id(2) % depth;
79 const uint w_in = get_global_id(2) / depth;
80
81 const uint4 dims = (uint4)(0, 1, 2, 3);
82 int4 to_reverse = (int4)(0, 0, 0, 0);
Adnan AlSinan704c22f2023-10-24 11:05:56 +010083
84 VEC_DATA_TYPE(int, NUM_REVERSE_DIMS) indices = VLOAD(NUM_REVERSE_DIMS)(0,(__global int *)axis.ptr);
85#if defined(USE_INVERTED_AXIS)
86 indices = select((VEC_DATA_TYPE(int, NUM_REVERSE_DIMS)) RANK - 1, -1, indices < 0) - indices;
87#else /* defined(USE_INVERTED_AXIS) */
88 indices = select(indices, indices + RANK, indices < 0);
89#endif /* defined(USE_INVERTED_AXIS) */
90
Michele Di Giorgio5daeffd2018-11-26 10:01:15 +000091#if NUM_REVERSE_DIMS == 1
Adnan AlSinan704c22f2023-10-24 11:05:56 +010092 to_reverse = ((uint4)indices == dims);
Michele Di Giorgio5daeffd2018-11-26 10:01:15 +000093#elif NUM_REVERSE_DIMS == 2
Adnan AlSinan704c22f2023-10-24 11:05:56 +010094 to_reverse = ((uint4)indices.s0 == dims) || ((uint4)indices.s1 == dims);
Michele Di Giorgio5daeffd2018-11-26 10:01:15 +000095#elif NUM_REVERSE_DIMS == 3
Adnan AlSinan704c22f2023-10-24 11:05:56 +010096 to_reverse = ((uint4)indices.s0 == dims) || ((uint4)indices.s1 == dims) || ((uint4)indices.s2 == dims);
97#else /* NUM_REVERSE_DIMS == 1 */
98 to_reverse = ((uint4)indices.s0 == dims) || ((uint4)indices.s1 == dims) || ((uint4)indices.s2 == dims) || ((uint4)indices.s3 == dims);
Michele Di Giorgio5daeffd2018-11-26 10:01:15 +000099#endif /* NUM_REVERSE_DIMS == 1 */
Adnan AlSinan704c22f2023-10-24 11:05:56 +0100100
Michele Di Giorgio5daeffd2018-11-26 10:01:15 +0000101 const uint x_out = to_reverse.s0 ? width - x_in - 1 : x_in;
102 const uint y_out = to_reverse.s1 ? height - y_in - 1 : y_in;
103 const uint z_out = to_reverse.s2 ? depth - z_in - 1 : z_in;
104 const uint w_out = to_reverse.s3 ? batches - w_in - 1 : w_in;
105
106 *((__global DATA_TYPE *)tensor4D_offset(&dst, x_out, y_out, z_out, w_out)) = *((__global DATA_TYPE *)src.ptr);
107}
108#endif // defined(DATA_TYPE) && defined(NUM_REVERSE_DIMS)