blob: 6debd4a03873c3cb05f9019d23410389c1ec5725 [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"
35#include "tests/validation/reference/ReductionOperation.h"
36#include "tests/validation/reference/ReshapeLayer.h"
37
38namespace arm_compute
39{
40namespace test
41{
42namespace validation
43{
44template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
45class ReduceMeanValidationFixture : public framework::Fixture
46{
47public:
48 template <typename...>
49 void setup(TensorShape shape, DataType data_type, Coordinates axis, bool keep_dims, QuantizationInfo quantization_info)
50 {
51 _target = compute_target(shape, data_type, axis, keep_dims, quantization_info);
52 _reference = compute_reference(shape, data_type, axis, keep_dims, quantization_info);
53 }
54
55protected:
56 template <typename U>
57 void fill(U &&tensor)
58 {
59 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 const QuantizationInfo quant_info = tensor.quantization_info();
67 const int min_bound = quant_info.quantize(-1.f, RoundingPolicy::TO_NEAREST_UP);
68 const int max_bound = quant_info.quantize(1.f, RoundingPolicy::TO_NEAREST_UP);
69 std::uniform_int_distribution<> distribution(min_bound, max_bound);
70
71 library->fill(tensor, distribution, 0);
72 }
73 }
74
75 TensorType compute_target(TensorShape &src_shape, DataType data_type, Coordinates axis, bool keep_dims, QuantizationInfo quantization_info)
76 {
77 // Create tensors
78 TensorType src = create_tensor<TensorType>(src_shape, data_type, 1, quantization_info);
79 TensorType dst;
80
81 // Create and configure function
82 FunctionType reduction_mean;
83 reduction_mean.configure(&src, axis, keep_dims, &dst);
84
85 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
86 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
87
88 // Allocate tensors
89 src.allocator()->allocate();
90 dst.allocator()->allocate();
91
92 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
93 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
94
95 // Fill tensors
96 fill(AccessorType(src));
97
98 // Compute function
99 reduction_mean.run();
100
101 return dst;
102 }
103
104 SimpleTensor<T> compute_reference(TensorShape &src_shape, DataType data_type, Coordinates axis, bool keep_dims, QuantizationInfo quantization_info)
105 {
106 // Create reference
107 SimpleTensor<T> src{ src_shape, data_type, 1, quantization_info };
108
109 // Fill reference
110 fill(src);
111
112 SimpleTensor<T> out;
113 for(unsigned int i = 0; i < axis.num_dimensions(); ++i)
114 {
115 TensorShape output_shape = i == 0 ? src_shape : out.shape();
116 output_shape.set(axis[i], 1);
117 out = reference::reduction_operation<T>(i == 0 ? src : out, output_shape, axis[i], ReductionOperation::MEAN_SUM);
118 }
119
120 if(!keep_dims)
121 {
122 TensorShape output_shape = src_shape;
123 for(unsigned int i = 0; i < axis.num_dimensions(); ++i)
124 {
125 output_shape.remove_dimension(axis[i]);
126 }
127
128 out = reference::reshape_layer(out, output_shape);
129 }
130 return out;
131 }
132
133 TensorType _target{};
134 SimpleTensor<T> _reference{};
135};
136
137template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
138class ReduceMeanQuantizedFixture : public ReduceMeanValidationFixture<TensorType, AccessorType, FunctionType, T>
139{
140public:
141 template <typename...>
142 void setup(TensorShape shape, DataType data_type, Coordinates axis, bool keep_dims, QuantizationInfo quantization_info = QuantizationInfo())
143 {
144 ReduceMeanValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, axis, keep_dims, quantization_info);
145 }
146};
147
148template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
149class ReduceMeanFixture : public ReduceMeanValidationFixture<TensorType, AccessorType, FunctionType, T>
150{
151public:
152 template <typename...>
153 void setup(TensorShape shape, DataType data_type, Coordinates axis, bool keep_dims)
154 {
155 ReduceMeanValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, axis, keep_dims, QuantizationInfo());
156 }
157};
158} // namespace validation
159} // namespace test
160} // namespace arm_compute
161#endif /* ARM_COMPUTE_TEST_REDUCE_MEAN_FIXTURE */