blob: ed6b51abe597a8bc5a7cb28e03e5be8e1c5d0794 [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 Spyroub9626ab2019-05-13 17:41:01 +010059 switch(tensor.data_type())
Michalis Spyrouaea14c62019-01-03 11:10:25 +000060 {
Michalis Spyroub9626ab2019-05-13 17:41:01 +010061 case DataType::F32:
62 case DataType::F16:
63 {
64 std::uniform_real_distribution<> distribution(-1.0f, 1.0f);
65 library->fill(tensor, distribution, 0);
66 break;
67 }
68 case DataType::S32:
69 {
70 std::uniform_int_distribution<int32_t> distribution(-100, 100);
71 library->fill(tensor, distribution, 0);
72 break;
73 }
74 case DataType::QASYMM8:
75 {
76 std::pair<int, int> bounds = get_quantized_bounds(tensor.quantization_info(), -1.0f, 1.0f);
77 std::uniform_int_distribution<uint8_t> distribution(bounds.first, bounds.second);
Michalis Spyrouaea14c62019-01-03 11:10:25 +000078
Michalis Spyroub9626ab2019-05-13 17:41:01 +010079 library->fill(tensor, distribution, 0);
80 break;
81 }
82 default:
83 ARM_COMPUTE_ERROR("DataType for Elementwise Negation Not implemented");
Michalis Spyrouaea14c62019-01-03 11:10:25 +000084 }
Michalis Spyrou7930db42018-11-22 17:36:28 +000085 }
86
Michalis Spyrouaea14c62019-01-03 11:10:25 +000087 TensorType compute_target(TensorShape &src_shape, DataType data_type, int axis, ReductionOperation op, QuantizationInfo q_info)
Michalis Spyrou7930db42018-11-22 17:36:28 +000088 {
89 // Create tensors
Michalis Spyrouaea14c62019-01-03 11:10:25 +000090 TensorType src = create_tensor<TensorType>(src_shape, data_type, 1, q_info);
Michalis Spyrou7930db42018-11-22 17:36:28 +000091 TensorType dst;
92
93 // Create and configure function
94 FunctionType arg_min_max_layer;
95 arg_min_max_layer.configure(&src, axis, &dst, op);
96
97 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
98 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
99
100 // Allocate tensors
101 src.allocator()->allocate();
102 dst.allocator()->allocate();
103
104 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
105 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
106
107 // Fill tensors
108 fill(AccessorType(src));
109
110 // Compute function
111 arg_min_max_layer.run();
112
113 return dst;
114 }
115
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000116 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 +0000117 {
118 // Create reference
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000119 SimpleTensor<T> src{ src_shape, data_type, 1, q_info };
Michalis Spyrou7930db42018-11-22 17:36:28 +0000120
121 // Fill reference
122 fill(src);
123
124 TensorShape output_shape = src_shape;
125 output_shape.set(axis, 1);
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000126 return reference::reduction_operation<T, uint32_t>(src, output_shape, axis, op);
Michalis Spyrou7930db42018-11-22 17:36:28 +0000127 }
128
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000129 TensorType _target{};
130 SimpleTensor<uint32_t> _reference{};
131};
132
133template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
134class ArgMinMaxValidationQuantizedFixture : public ArgMinMaxValidationBaseFixture<TensorType, AccessorType, FunctionType, T>
135{
136public:
137 template <typename...>
138 void setup(const TensorShape &shape, DataType data_type, int axis, ReductionOperation op, QuantizationInfo quantization_info)
139 {
140 ArgMinMaxValidationBaseFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, axis, op, quantization_info);
141 }
142};
143
144template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
145class ArgMinMaxValidationFixture : public ArgMinMaxValidationBaseFixture<TensorType, AccessorType, FunctionType, T>
146{
147public:
148 template <typename...>
149 void setup(const TensorShape &shape, DataType data_type, int axis, ReductionOperation op)
150 {
151 ArgMinMaxValidationBaseFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, axis, op, QuantizationInfo());
152 }
Michalis Spyrou7930db42018-11-22 17:36:28 +0000153};
154} // namespace validation
155} // namespace test
156} // namespace arm_compute
157#endif /* ARM_COMPUTE_TEST_ARG_MIN_MAX_FIXTURE */