xref: /aosp_15_r20/external/ComputeLibrary/tests/validation/fixtures/SliceOperationsFixtures.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2018-2021 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 #ifndef ARM_COMPUTE_TEST_SLICE_OPERATIONS_FIXTURE
25 #define ARM_COMPUTE_TEST_SLICE_OPERATIONS_FIXTURE
26 
27 #include "arm_compute/core/TensorShape.h"
28 #include "arm_compute/core/Types.h"
29 
30 #include "tests/AssetsLibrary.h"
31 #include "tests/Globals.h"
32 #include "tests/IAccessor.h"
33 #include "tests/framework/Asserts.h"
34 #include "tests/framework/Fixture.h"
35 #include "tests/validation/Helpers.h"
36 #include "tests/validation/reference/SliceOperations.h"
37 
38 namespace arm_compute
39 {
40 namespace test
41 {
42 namespace validation
43 {
44 template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
45 class SliceFixture : public framework::Fixture
46 {
47 public:
48     template <typename...>
setup(TensorShape shape,Coordinates starts,Coordinates ends,DataType data_type)49     void setup(TensorShape shape, Coordinates starts, Coordinates ends, DataType data_type)
50     {
51         _target    = compute_target(shape, starts, ends, data_type);
52         _reference = compute_reference(shape, starts, ends, data_type);
53     }
54 
55 protected:
56     template <typename U>
fill(U && tensor,int i)57     void fill(U &&tensor, int i)
58     {
59         library->fill_tensor_uniform(tensor, i);
60     }
61 
compute_target(const TensorShape & shape,const Coordinates & starts,const Coordinates & ends,DataType data_type)62     TensorType compute_target(const TensorShape &shape, const Coordinates &starts, const Coordinates &ends, DataType data_type)
63     {
64         // Create tensors
65         TensorType src = create_tensor<TensorType>(shape, data_type);
66         TensorType dst;
67 
68         // Create and configure function
69         FunctionType slice;
70         slice.configure(&src, &dst, starts, ends);
71 
72         ARM_COMPUTE_ASSERT(src.info()->is_resizable());
73         ARM_COMPUTE_ASSERT(dst.info()->is_resizable());
74 
75         // Allocate tensors
76         src.allocator()->allocate();
77         dst.allocator()->allocate();
78 
79         ARM_COMPUTE_ASSERT(!src.info()->is_resizable());
80         ARM_COMPUTE_ASSERT(!dst.info()->is_resizable());
81 
82         // Fill tensors
83         fill(AccessorType(src), 0);
84         fill(AccessorType(dst), 1);
85 
86         // Compute function
87         slice.run();
88 
89         return dst;
90     }
91 
compute_reference(const TensorShape & shape,const Coordinates & starts,const Coordinates & ends,DataType data_type)92     SimpleTensor<T> compute_reference(const TensorShape &shape, const Coordinates &starts, const Coordinates &ends, DataType data_type)
93     {
94         // Create reference
95         SimpleTensor<T> src{ shape, data_type };
96 
97         // Fill reference
98         fill(src, 0);
99 
100         return reference::slice(src, starts, ends);
101     }
102 
103     TensorType      _target{};
104     SimpleTensor<T> _reference{};
105 };
106 
107 template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
108 class StridedSliceFixture : public framework::Fixture
109 {
110 public:
111     template <typename...>
setup(TensorShape shape,Coordinates starts,Coordinates ends,BiStrides strides,int32_t begin_mask,int32_t end_mask,int32_t shrink_mask,DataType data_type)112     void setup(TensorShape shape,
113                Coordinates starts, Coordinates ends, BiStrides strides,
114                int32_t begin_mask, int32_t end_mask, int32_t shrink_mask,
115                DataType data_type)
116     {
117         _target    = compute_target(shape, starts, ends, strides, begin_mask, end_mask, shrink_mask, data_type);
118         _reference = compute_reference(shape, starts, ends, strides, begin_mask, end_mask, shrink_mask, data_type);
119     }
120 
121 protected:
122     template <typename U>
fill(U && tensor,int i)123     void fill(U &&tensor, int i)
124     {
125         library->fill_tensor_uniform(tensor, i);
126     }
127 
compute_target(const TensorShape & shape,const Coordinates & starts,const Coordinates & ends,const BiStrides & strides,int32_t begin_mask,int32_t end_mask,int32_t shrink_mask,DataType data_type)128     TensorType compute_target(const TensorShape &shape,
129                               const Coordinates &starts, const Coordinates &ends, const BiStrides &strides,
130                               int32_t begin_mask, int32_t end_mask, int32_t shrink_mask,
131                               DataType data_type)
132     {
133         // Create tensors
134         TensorType src = create_tensor<TensorType>(shape, data_type);
135         TensorType dst;
136 
137         // Create and configure function
138         FunctionType strided_slice;
139         strided_slice.configure(&src, &dst, starts, ends, strides, begin_mask, end_mask, shrink_mask);
140 
141         ARM_COMPUTE_ASSERT(src.info()->is_resizable());
142         ARM_COMPUTE_ASSERT(dst.info()->is_resizable());
143 
144         // Allocate tensors
145         src.allocator()->allocate();
146         dst.allocator()->allocate();
147 
148         ARM_COMPUTE_ASSERT(!src.info()->is_resizable());
149         ARM_COMPUTE_ASSERT(!dst.info()->is_resizable());
150 
151         // Fill tensors
152         fill(AccessorType(src), 0);
153         fill(AccessorType(dst), 1);
154 
155         // Compute function
156         strided_slice.run();
157 
158         return dst;
159     }
160 
compute_reference(const TensorShape & shape,const Coordinates & starts,const Coordinates & ends,const BiStrides & strides,int32_t begin_mask,int32_t end_mask,int32_t shrink_mask,DataType data_type)161     SimpleTensor<T> compute_reference(const TensorShape &shape,
162                                       const Coordinates &starts, const Coordinates &ends, const BiStrides &strides,
163                                       int32_t begin_mask, int32_t end_mask, int32_t shrink_mask,
164                                       DataType data_type)
165     {
166         // Create reference
167         SimpleTensor<T> src{ shape, data_type };
168 
169         // Fill reference
170         fill(src, 0);
171 
172         return reference::strided_slice(src, starts, ends, strides, begin_mask, end_mask, shrink_mask);
173     }
174 
175     TensorType      _target{};
176     SimpleTensor<T> _reference{};
177 };
178 } // namespace validation
179 } // namespace test
180 } // namespace arm_compute
181 #endif /* ARM_COMPUTE_TEST_SLICE_OPERATIONS_FIXTURE */
182