blob: 869221364180c04aa8736819c74cb7c82897930b [file] [log] [blame]
Michalis Spyrou7e9391b2018-10-05 14:49:28 +01001/*
2 * Copyright (c) 2018 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_REDUCE_MEAN_FIXTURE
25#define ARM_COMPUTE_TEST_REDUCE_MEAN_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"
Michele Di Giorgioed5a4922018-09-13 16:22:01 +010035#include "tests/validation/Helpers.h"
Michalis Spyrou7e9391b2018-10-05 14:49:28 +010036#include "tests/validation/reference/ReductionOperation.h"
37#include "tests/validation/reference/ReshapeLayer.h"
38
39namespace arm_compute
40{
41namespace test
42{
43namespace validation
44{
45template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
46class ReduceMeanValidationFixture : public framework::Fixture
47{
48public:
49 template <typename...>
50 void setup(TensorShape shape, DataType data_type, Coordinates axis, bool keep_dims, QuantizationInfo quantization_info)
51 {
52 _target = compute_target(shape, data_type, axis, keep_dims, quantization_info);
53 _reference = compute_reference(shape, data_type, axis, keep_dims, quantization_info);
54 }
55
56protected:
57 template <typename U>
58 void fill(U &&tensor)
59 {
60 if(!is_data_type_quantized(tensor.data_type()))
61 {
62 std::uniform_real_distribution<> distribution(-1.0f, 1.0f);
63 library->fill(tensor, distribution, 0);
64 }
65 else
66 {
Michele Di Giorgioed5a4922018-09-13 16:22:01 +010067 std::pair<int, int> bounds = get_quantized_bounds(tensor.quantization_info(), -1.0f, 1.0f);
68 std::uniform_int_distribution<> distribution(bounds.first, bounds.second);
Michalis Spyrou7e9391b2018-10-05 14:49:28 +010069
70 library->fill(tensor, distribution, 0);
71 }
72 }
73
74 TensorType compute_target(TensorShape &src_shape, DataType data_type, Coordinates axis, bool keep_dims, QuantizationInfo quantization_info)
75 {
76 // Create tensors
77 TensorType src = create_tensor<TensorType>(src_shape, data_type, 1, quantization_info);
78 TensorType dst;
79
80 // Create and configure function
81 FunctionType reduction_mean;
82 reduction_mean.configure(&src, axis, keep_dims, &dst);
83
84 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
85 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
86
87 // Allocate tensors
88 src.allocator()->allocate();
89 dst.allocator()->allocate();
90
91 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
92 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
93
94 // Fill tensors
95 fill(AccessorType(src));
96
97 // Compute function
98 reduction_mean.run();
99
100 return dst;
101 }
102
103 SimpleTensor<T> compute_reference(TensorShape &src_shape, DataType data_type, Coordinates axis, bool keep_dims, QuantizationInfo quantization_info)
104 {
105 // Create reference
106 SimpleTensor<T> src{ src_shape, data_type, 1, quantization_info };
107
108 // Fill reference
109 fill(src);
110
111 SimpleTensor<T> out;
112 for(unsigned int i = 0; i < axis.num_dimensions(); ++i)
113 {
114 TensorShape output_shape = i == 0 ? src_shape : out.shape();
115 output_shape.set(axis[i], 1);
116 out = reference::reduction_operation<T>(i == 0 ? src : out, output_shape, axis[i], ReductionOperation::MEAN_SUM);
117 }
118
119 if(!keep_dims)
120 {
121 TensorShape output_shape = src_shape;
122 for(unsigned int i = 0; i < axis.num_dimensions(); ++i)
123 {
124 output_shape.remove_dimension(axis[i]);
125 }
126
127 out = reference::reshape_layer(out, output_shape);
128 }
129 return out;
130 }
131
132 TensorType _target{};
133 SimpleTensor<T> _reference{};
134};
135
136template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
137class ReduceMeanQuantizedFixture : public ReduceMeanValidationFixture<TensorType, AccessorType, FunctionType, T>
138{
139public:
140 template <typename...>
141 void setup(TensorShape shape, DataType data_type, Coordinates axis, bool keep_dims, QuantizationInfo quantization_info = QuantizationInfo())
142 {
143 ReduceMeanValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, axis, keep_dims, quantization_info);
144 }
145};
146
147template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
148class ReduceMeanFixture : public ReduceMeanValidationFixture<TensorType, AccessorType, FunctionType, T>
149{
150public:
151 template <typename...>
152 void setup(TensorShape shape, DataType data_type, Coordinates axis, bool keep_dims)
153 {
154 ReduceMeanValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, axis, keep_dims, QuantizationInfo());
155 }
156};
157} // namespace validation
158} // namespace test
159} // namespace arm_compute
160#endif /* ARM_COMPUTE_TEST_REDUCE_MEAN_FIXTURE */