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_SELECT_FIXTURE
25 #define ARM_COMPUTE_TEST_SELECT_FIXTURE
26
27 #include "arm_compute/core/TensorShape.h"
28 #include "arm_compute/core/Types.h"
29 #include "tests/AssetsLibrary.h"
30 #include "tests/Globals.h"
31 #include "tests/IAccessor.h"
32 #include "tests/framework/Asserts.h"
33 #include "tests/framework/Fixture.h"
34 #include "tests/validation/reference/Select.h"
35
36 namespace arm_compute
37 {
38 namespace test
39 {
40 namespace validation
41 {
42 namespace detail
43 {
44 /** Get the expected shape of the condition tensor
45 *
46 * @param shape Shape of the other input tensor to select
47 * @param has_same_same_rank Boolean that flags if the condition has the same rank with the other tensors
48 *
49 * @return the expected condition shape
50 */
select_condition_shape(TensorShape shape,bool has_same_same_rank)51 inline TensorShape select_condition_shape(TensorShape shape, bool has_same_same_rank)
52 {
53 TensorShape condition_shape = shape;
54 if(!has_same_same_rank)
55 {
56 condition_shape = TensorShape(shape[shape.num_dimensions() - 1]);
57 }
58 return condition_shape;
59 }
60 } // namespace detail
61
62 template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
63 class SelectValidationFixture : public framework::Fixture
64 {
65 public:
66 template <typename...>
setup(TensorShape shape,bool has_same_same_rank,DataType data_type)67 void setup(TensorShape shape, bool has_same_same_rank, DataType data_type)
68 {
69 TensorShape condition_shape = detail::select_condition_shape(shape, has_same_same_rank);
70
71 _target = compute_target(shape, condition_shape, data_type);
72 _reference = compute_reference(shape, condition_shape, data_type);
73 }
74
75 protected:
76 template <typename U>
fill(U && tensor,int i)77 void fill(U &&tensor, int i)
78 {
79 library->fill_tensor_uniform(tensor, i);
80 }
81 template <typename U>
fill_bool(U && tensor,int i)82 void fill_bool(U &&tensor, int i)
83 {
84 ARM_COMPUTE_ERROR_ON(tensor.data_type() != DataType::U8);
85 library->fill_tensor_uniform(tensor, i, static_cast<uint8_t>(0), static_cast<uint8_t>(1));
86 }
87
compute_target(const TensorShape & shape,const TensorShape & condition_shape,DataType data_type)88 TensorType compute_target(const TensorShape &shape, const TensorShape &condition_shape, DataType data_type)
89 {
90 // Create tensors
91 TensorType c_t = create_tensor<TensorType>(condition_shape, DataType::U8);
92 TensorType x_t = create_tensor<TensorType>(shape, data_type);
93 TensorType y_t = create_tensor<TensorType>(shape, data_type);
94 TensorType dst_t = create_tensor<TensorType>(shape, data_type);
95
96 // Create and configure function
97 FunctionType select;
98 select.configure(&c_t, &x_t, &y_t, &dst_t);
99
100 ARM_COMPUTE_ASSERT(c_t.info()->is_resizable());
101 ARM_COMPUTE_ASSERT(x_t.info()->is_resizable());
102 ARM_COMPUTE_ASSERT(y_t.info()->is_resizable());
103 ARM_COMPUTE_ASSERT(dst_t.info()->is_resizable());
104
105 // Allocate tensors
106 c_t.allocator()->allocate();
107 x_t.allocator()->allocate();
108 y_t.allocator()->allocate();
109 dst_t.allocator()->allocate();
110
111 ARM_COMPUTE_ASSERT(!c_t.info()->is_resizable());
112 ARM_COMPUTE_ASSERT(!x_t.info()->is_resizable());
113 ARM_COMPUTE_ASSERT(!y_t.info()->is_resizable());
114 ARM_COMPUTE_ASSERT(!dst_t.info()->is_resizable());
115
116 // Fill tensors
117 fill_bool(AccessorType(c_t), 0);
118 fill(AccessorType(x_t), 1);
119 fill(AccessorType(y_t), 2);
120
121 // Compute function
122 select.run();
123
124 return dst_t;
125 }
126
compute_reference(const TensorShape & shape,const TensorShape & condition_shape,DataType data_type)127 SimpleTensor<T> compute_reference(const TensorShape &shape, const TensorShape &condition_shape, DataType data_type)
128 {
129 // Create reference
130 SimpleTensor<uint8_t> ref_c{ condition_shape, DataType::U8 };
131 SimpleTensor<T> ref_x{ shape, data_type };
132 SimpleTensor<T> ref_y{ shape, data_type };
133
134 // Fill reference
135 fill_bool(ref_c, 0);
136 fill(ref_x, 1);
137 fill(ref_y, 2);
138
139 return reference::select<T>(ref_c, ref_x, ref_y);
140 }
141
142 TensorType _target{};
143 SimpleTensor<T> _reference{};
144 };
145 } // namespace validation
146 } // namespace test
147 } // namespace arm_compute
148 #endif /* ARM_COMPUTE_TEST_SELECT_FIXTURE */
149