xref: /aosp_15_r20/external/ComputeLibrary/tests/validation/reference/SliceOperations.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2018-2020 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 "SliceOperations.h"
25 
26 #include "arm_compute/core/utils/helpers/tensor_transform.h"
27 #include "arm_compute/core/utils/misc/ShapeCalculator.h"
28 
29 namespace arm_compute
30 {
31 namespace test
32 {
33 namespace validation
34 {
35 namespace reference
36 {
37 template <typename T>
slice(const SimpleTensor<T> & src,Coordinates starts,Coordinates ends)38 SimpleTensor<T> slice(const SimpleTensor<T> &src, Coordinates starts, Coordinates ends)
39 {
40     using namespace arm_compute::helpers::tensor_transform;
41 
42     // Validation checks
43     ARM_COMPUTE_ERROR_ON(src.shape().num_dimensions() > 4);
44     ARM_COMPUTE_ERROR_ON(starts.num_dimensions() > src.shape().num_dimensions());
45     ARM_COMPUTE_ERROR_ON(std::any_of(starts.cbegin(), starts.cbegin() + starts.num_dimensions(), [](int i)
46     {
47         return i < 0;
48     }));
49     ARM_COMPUTE_ERROR_ON(ends.num_dimensions() > src.shape().num_dimensions());
50 
51     // Get source shape
52     const TensorShape &src_shape = src.shape();
53 
54     // Get destination shape
55     TensorShape dst_shape = arm_compute::misc::shape_calculator::compute_slice_shape(src_shape, starts, ends);
56 
57     // Create destination tensor
58     SimpleTensor<T> dst{ dst_shape, src.data_type(), 1 };
59 
60     // Perform slice
61     Window win;
62     win.use_tensor_dimensions(dst_shape);
63     execute_window_loop(win, [&](const Coordinates & id)
64     {
65         Coordinates offset;
66         for(unsigned int i = 0; i < id.num_dimensions(); ++i)
67         {
68             offset.set(i, starts[i] + id[i]);
69         }
70         *reinterpret_cast<T *>(dst(id)) = *reinterpret_cast<const T *>(src(offset));
71     });
72 
73     return dst;
74 }
75 
76 template SimpleTensor<float> slice(const SimpleTensor<float> &src, Coordinates starts, Coordinates ends);
77 template SimpleTensor<half_float::half> slice(const SimpleTensor<half_float::half> &src, Coordinates starts, Coordinates ends);
78 
79 template <typename T>
strided_slice(const SimpleTensor<T> & src,Coordinates starts,Coordinates ends,BiStrides strides,int32_t begin_mask,int32_t end_mask,int32_t shrink_axis_mask)80 SimpleTensor<T> strided_slice(const SimpleTensor<T> &src,
81                               Coordinates starts, Coordinates ends, BiStrides strides,
82                               int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask)
83 {
84     using namespace arm_compute::helpers::tensor_transform;
85 
86     // Validation checks
87     ARM_COMPUTE_ERROR_ON(src.shape().num_dimensions() > 4);
88     ARM_COMPUTE_ERROR_ON(starts.num_dimensions() > src.shape().num_dimensions());
89     ARM_COMPUTE_ERROR_ON(ends.num_dimensions() > src.shape().num_dimensions());
90     ARM_COMPUTE_ERROR_ON(strides.num_dimensions() > src.shape().num_dimensions());
91     ARM_COMPUTE_ERROR_ON(std::any_of(strides.cbegin(), strides.cbegin() + strides.num_dimensions(), [](int i)
92     {
93         return i == 0;
94     }));
95 
96     // Get source shape
97     const TensorShape &src_shape = src.shape();
98 
99     // Get destination shape
100     const TensorShape dst_shape = compute_strided_slice_output_shape(src_shape, starts, ends, strides, begin_mask, end_mask, shrink_axis_mask);
101 
102     // Create destination tensor
103     SimpleTensor<T> dst{ dst_shape, src.data_type(), 1 };
104 
105     // Get coordinates
106     Coordinates starts_abs{};
107     Coordinates ends_abs{};
108     Coordinates final_strides{};
109     std::tie(starts_abs, ends_abs, final_strides) = calculate_strided_slice_coords(src_shape,
110                                                                                    starts, ends, strides,
111                                                                                    begin_mask, end_mask, shrink_axis_mask);
112 
113     // Perform strided slice
114     unsigned int idx = 0;
115     Window       win;
116     win.use_tensor_dimensions(compute_strided_slice_output_shape(src_shape,
117                                                                  starts, ends, strides,
118                                                                  begin_mask, end_mask, shrink_axis_mask, true));
119     execute_window_loop(win, [&](const Coordinates & id)
120     {
121         Coordinates offset;
122         for(unsigned int i = 0; i < id.num_dimensions(); ++i)
123         {
124             offset.set(i, starts_abs[i] + id[i] * final_strides[i]);
125         }
126         dst.data()[idx++] = *reinterpret_cast<const T *>(src(offset));
127     });
128 
129     return dst;
130 }
131 
132 template SimpleTensor<float> strided_slice(const SimpleTensor<float> &src,
133                                            Coordinates starts, Coordinates ends, BiStrides strides,
134                                            int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask);
135 template SimpleTensor<half_float::half> strided_slice(const SimpleTensor<half_float::half> &src,
136                                                       Coordinates starts, Coordinates ends, BiStrides strides,
137                                                       int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask);
138 } // namespace reference
139 } // namespace validation
140 } // namespace test
141 } // namespace arm_compute
142