blob: f25d5abb737859bdedc79e10442d4bdf8673e88b [file] [log] [blame]
Georgios Pinitas7900a9e2018-11-23 11:44:58 +00001/*
Matthew Bentham945b8da2023-07-12 11:54:59 +00002 * Copyright (c) 2018-2021, 2023 Arm Limited.
Georgios Pinitas7900a9e2018-11-23 11:44:58 +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#ifndef ARM_COMPUTE_TEST_COMPARISON_FIXTURE
25#define ARM_COMPUTE_TEST_COMPARISON_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/Helpers.h"
35#include "tests/validation/reference/Comparisons.h"
36
37namespace arm_compute
38{
39namespace test
40{
41namespace validation
42{
43template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
44class ComparisonValidationGenericFixture : public framework::Fixture
45{
46public:
Georgios Pinitas7900a9e2018-11-23 11:44:58 +000047 void setup(ComparisonOperation op, const TensorShape &shape0, const TensorShape &shape1, DataType data_type, QuantizationInfo qinfo0, QuantizationInfo qinfo1)
48 {
49 _target = compute_target(op, shape0, shape1, data_type, qinfo0, qinfo1);
50 _reference = compute_reference(op, shape0, shape1, data_type, qinfo0, qinfo1);
51 }
52
53protected:
54 template <typename U>
55 void fill(U &&tensor, int i)
56 {
57 library->fill_tensor_uniform(tensor, i);
58 }
59
60 TensorType compute_target(ComparisonOperation op,
61 const TensorShape &shape0, const TensorShape &shape1, DataType data_type,
62 QuantizationInfo qinfo0, QuantizationInfo qinfo1)
63 {
64 // Create tensors
65 TensorType ref_src1 = create_tensor<TensorType>(shape0, data_type, 1, qinfo0);
66 TensorType ref_src2 = create_tensor<TensorType>(shape1, data_type, 1, qinfo1);
67 TensorType dst = create_tensor<TensorType>(TensorShape::broadcast_shape(shape0, shape1), DataType::U8);
68
69 // Create and configure function
70 FunctionType comp_op;
71 comp_op.configure(&ref_src1, &ref_src2, &dst, op);
72
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +010073 ARM_COMPUTE_ASSERT(ref_src1.info()->is_resizable());
74 ARM_COMPUTE_ASSERT(ref_src2.info()->is_resizable());
75 ARM_COMPUTE_ASSERT(dst.info()->is_resizable());
Georgios Pinitas7900a9e2018-11-23 11:44:58 +000076
77 // Allocate tensors
78 ref_src1.allocator()->allocate();
79 ref_src2.allocator()->allocate();
80 dst.allocator()->allocate();
81
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +010082 ARM_COMPUTE_ASSERT(!ref_src1.info()->is_resizable());
83 ARM_COMPUTE_ASSERT(!ref_src2.info()->is_resizable());
84 ARM_COMPUTE_ASSERT(!dst.info()->is_resizable());
Georgios Pinitas7900a9e2018-11-23 11:44:58 +000085
86 // Fill tensors
87 fill(AccessorType(ref_src1), 0);
88 fill(AccessorType(ref_src2), 1);
89
90 // Compute function
91 comp_op.run();
92
93 return dst;
94 }
95
96 SimpleTensor<uint8_t> compute_reference(ComparisonOperation op,
97 const TensorShape &shape0, const TensorShape &shape1, DataType data_type,
98 QuantizationInfo qinfo0, QuantizationInfo qinfo1)
99 {
100 // Create reference
101 SimpleTensor<T> ref_src1{ shape0, data_type, 1, qinfo0 };
102 SimpleTensor<T> ref_src2{ shape1, data_type, 1, qinfo1 };
103
104 // Fill reference
105 fill(ref_src1, 0);
106 fill(ref_src2, 1);
107
108 return reference::compare<T>(op, ref_src1, ref_src2);
109 }
110
111 TensorType _target{};
112 SimpleTensor<uint8_t> _reference{};
113};
114
115template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
116class ComparisonBroadcastValidationFixture : public ComparisonValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
117{
118public:
Georgios Pinitas7900a9e2018-11-23 11:44:58 +0000119 void setup(ComparisonOperation op, const TensorShape &shape0, const TensorShape &shape1, DataType data_type)
120 {
121 ComparisonValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(op, shape0, shape1, data_type, QuantizationInfo(), QuantizationInfo());
122 }
123};
124
125template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
126class ComparisonValidationFixture : public ComparisonValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
127{
128public:
Georgios Pinitas7900a9e2018-11-23 11:44:58 +0000129 void setup(ComparisonOperation op, const TensorShape &shape, DataType data_type)
130 {
131 ComparisonValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(op, shape, shape, data_type, QuantizationInfo(), QuantizationInfo());
132 }
133};
134
135template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
136class ComparisonValidationQuantizedFixture : public ComparisonValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
137{
138public:
Georgios Pinitas7900a9e2018-11-23 11:44:58 +0000139 void setup(ComparisonOperation op, const TensorShape &shape, DataType data_type, QuantizationInfo qinfo0, QuantizationInfo qinfo1)
140
141 {
142 ComparisonValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(op, shape, shape, data_type, qinfo0, qinfo1);
143 }
144};
Michele Di Giorgio81870c02020-04-30 12:02:20 +0100145
146template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
147class ComparisonQuantizedBroadcastValidationFixture : public ComparisonValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
148{
149public:
Michele Di Giorgio81870c02020-04-30 12:02:20 +0100150 void setup(ComparisonOperation op, const TensorShape &shape0, const TensorShape &shape1, DataType data_type, QuantizationInfo qinfo0, QuantizationInfo qinfo1)
151 {
152 ComparisonValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(op, shape0, shape1, data_type, qinfo0, qinfo1);
153 }
154};
Georgios Pinitas7900a9e2018-11-23 11:44:58 +0000155} // namespace validation
156} // namespace test
157} // namespace arm_compute
158#endif /* ARM_COMPUTE_TEST_COMPARISON_FIXTURE */