blob: e263b25bf292630035ccdcc65564f9b65296209d [file] [log] [blame]
Michalis Spyrou7930db42018-11-22 17:36:28 +00001/*
Michalis Spyrouaea14c62019-01-03 11:10:25 +00002 * Copyright (c) 2018-2019 ARM Limited.
Michalis Spyrou7930db42018-11-22 17:36:28 +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_ARG_MIN_MAX_FIXTURE
25#define ARM_COMPUTE_TEST_ARG_MIN_MAX_FIXTURE
26
27#include "arm_compute/core/TensorShape.h"
28#include "arm_compute/core/Types.h"
29#include "arm_compute/runtime/Tensor.h"
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/ReductionOperation.h"
37
38namespace arm_compute
39{
40namespace test
41{
42namespace validation
43{
44template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Michalis Spyrouaea14c62019-01-03 11:10:25 +000045class ArgMinMaxValidationBaseFixture : public framework::Fixture
Michalis Spyrou7930db42018-11-22 17:36:28 +000046{
47public:
48 template <typename...>
Michalis Spyrouaea14c62019-01-03 11:10:25 +000049 void setup(TensorShape shape, DataType data_type, int axis, ReductionOperation op, QuantizationInfo q_info)
Michalis Spyrou7930db42018-11-22 17:36:28 +000050 {
Michalis Spyrouaea14c62019-01-03 11:10:25 +000051 _target = compute_target(shape, data_type, axis, op, q_info);
52 _reference = compute_reference(shape, data_type, axis, op, q_info);
Michalis Spyrou7930db42018-11-22 17:36:28 +000053 }
54
55protected:
56 template <typename U>
57 void fill(U &&tensor)
58 {
Michalis Spyrouaea14c62019-01-03 11:10:25 +000059 if(!is_data_type_quantized(tensor.data_type()))
60 {
61 std::uniform_real_distribution<> distribution(-1.0f, 1.0f);
62 library->fill(tensor, distribution, 0);
63 }
64 else
65 {
66 std::pair<int, int> bounds = get_quantized_bounds(tensor.quantization_info(), -1.0f, 1.0f);
67 std::uniform_int_distribution<uint8_t> distribution(bounds.first, bounds.second);
68
69 library->fill(tensor, distribution, 0);
70 }
Michalis Spyrou7930db42018-11-22 17:36:28 +000071 }
72
Michalis Spyrouaea14c62019-01-03 11:10:25 +000073 TensorType compute_target(TensorShape &src_shape, DataType data_type, int axis, ReductionOperation op, QuantizationInfo q_info)
Michalis Spyrou7930db42018-11-22 17:36:28 +000074 {
75 // Create tensors
Michalis Spyrouaea14c62019-01-03 11:10:25 +000076 TensorType src = create_tensor<TensorType>(src_shape, data_type, 1, q_info);
Michalis Spyrou7930db42018-11-22 17:36:28 +000077 TensorType dst;
78
79 // Create and configure function
80 FunctionType arg_min_max_layer;
81 arg_min_max_layer.configure(&src, axis, &dst, op);
82
83 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
84 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
85
86 // Allocate tensors
87 src.allocator()->allocate();
88 dst.allocator()->allocate();
89
90 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
91 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
92
93 // Fill tensors
94 fill(AccessorType(src));
95
96 // Compute function
97 arg_min_max_layer.run();
98
99 return dst;
100 }
101
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000102 SimpleTensor<uint32_t> compute_reference(TensorShape &src_shape, DataType data_type, int axis, ReductionOperation op, QuantizationInfo q_info)
Michalis Spyrou7930db42018-11-22 17:36:28 +0000103 {
104 // Create reference
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000105 SimpleTensor<T> src{ src_shape, data_type, 1, q_info };
Michalis Spyrou7930db42018-11-22 17:36:28 +0000106
107 // Fill reference
108 fill(src);
109
110 TensorShape output_shape = src_shape;
111 output_shape.set(axis, 1);
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000112 return reference::reduction_operation<T, uint32_t>(src, output_shape, axis, op);
Michalis Spyrou7930db42018-11-22 17:36:28 +0000113 }
114
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000115 TensorType _target{};
116 SimpleTensor<uint32_t> _reference{};
117};
118
119template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
120class ArgMinMaxValidationQuantizedFixture : public ArgMinMaxValidationBaseFixture<TensorType, AccessorType, FunctionType, T>
121{
122public:
123 template <typename...>
124 void setup(const TensorShape &shape, DataType data_type, int axis, ReductionOperation op, QuantizationInfo quantization_info)
125 {
126 ArgMinMaxValidationBaseFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, axis, op, quantization_info);
127 }
128};
129
130template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
131class ArgMinMaxValidationFixture : public ArgMinMaxValidationBaseFixture<TensorType, AccessorType, FunctionType, T>
132{
133public:
134 template <typename...>
135 void setup(const TensorShape &shape, DataType data_type, int axis, ReductionOperation op)
136 {
137 ArgMinMaxValidationBaseFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, axis, op, QuantizationInfo());
138 }
Michalis Spyrou7930db42018-11-22 17:36:28 +0000139};
140} // namespace validation
141} // namespace test
142} // namespace arm_compute
143#endif /* ARM_COMPUTE_TEST_ARG_MIN_MAX_FIXTURE */