blob: 091000175cac6ec865cb6fdc9393c1b0df427467 [file] [log] [blame]
Ioan-Cristian Szabo432a7d42017-10-12 09:25:19 +01001/*
2 * Copyright (c) 2017 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_ACCUMULATE_FIXTURE
25#define ARM_COMPUTE_TEST_ACCUMULATE_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"
Ioan-Cristian Szabo432a7d42017-10-12 09:25:19 +010034#include "tests/validation/Helpers.h"
Georgios Pinitas5a7e7762017-12-01 16:27:29 +000035#include "tests/validation/reference/Accumulate.h"
Ioan-Cristian Szabo432a7d42017-10-12 09:25:19 +010036
37namespace arm_compute
38{
39namespace test
40{
41namespace validation
42{
43template <typename TensorType, typename AccessorType, typename FunctionType, typename T1, typename T2>
44class AccumulateBaseValidationFixture : public framework::Fixture
45{
46public:
47 template <typename...>
48 void setup(TensorShape shape, DataType data_type, DataType output_data_type)
49 {
50 _target = compute_target(shape, data_type, output_data_type);
51 _reference = compute_reference(shape, data_type, output_data_type);
52 }
53
54protected:
55 template <typename U, typename D>
56 void fill(U &&tensor, int i, D max)
57 {
58 library->fill_tensor_uniform(tensor, i, static_cast<D>(0), max);
59 }
60
61 TensorType compute_target(const TensorShape &shape, DataType data_type, DataType output_data_type)
62 {
63 // Create tensors
64 TensorType ref_src = create_tensor<TensorType>(shape, data_type);
65 TensorType dst = create_tensor<TensorType>(shape, output_data_type);
66
67 // Create and configure function
68 FunctionType accum;
69 accum_conf(accum, ref_src, dst);
70
71 ARM_COMPUTE_EXPECT(ref_src.info()->is_resizable(), framework::LogLevel::ERRORS);
72 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
73
74 // Allocate tensors
75 ref_src.allocator()->allocate();
76 dst.allocator()->allocate();
77
78 ARM_COMPUTE_EXPECT(!ref_src.info()->is_resizable(), framework::LogLevel::ERRORS);
79 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
80
81 const T1 max = std::numeric_limits<T1>::max();
82
83 // Fill tensors
84 fill(AccessorType(ref_src), 0, max);
85 fill(AccessorType(dst), 1, static_cast<T2>(max));
86
87 // Compute function
88 accum.run();
89
90 return dst;
91 }
92
93 SimpleTensor<T2> compute_reference(const TensorShape &shape, DataType data_type, DataType output_data_type)
94 {
95 // Create reference
96 SimpleTensor<T1> ref_src{ shape, data_type };
97
98 const T1 max = std::numeric_limits<T1>::max();
99
100 // Fill reference
101 fill(ref_src, 0, max);
102
103 return accum_ref(ref_src, output_data_type);
104 }
105
106 virtual void accum_conf(FunctionType &func, const TensorType &input, TensorType &accum) = 0;
107
108 virtual SimpleTensor<T2> accum_ref(const SimpleTensor<T1> &input, DataType output_data_type) = 0;
109
110 TensorType _target{};
111 SimpleTensor<T2> _reference{};
112};
113
114template <typename TensorType, typename AccessorType, typename FunctionType, typename T1, typename T2>
115class AccumulateValidationFixture : public AccumulateBaseValidationFixture<TensorType, AccessorType, FunctionType, T1, T2>
116{
117public:
118 template <typename...>
119 void setup(TensorShape shape, DataType data_type, DataType output_data_type)
120 {
121 AccumulateBaseValidationFixture<TensorType, AccessorType, FunctionType, T1, T2>::setup(shape, data_type, output_data_type);
122 }
123
124 virtual void accum_conf(FunctionType &func, const TensorType &input, TensorType &accum) override
125 {
126 func.configure(&input, &accum);
127 }
128
129 virtual SimpleTensor<T2> accum_ref(const SimpleTensor<T1> &input, DataType output_data_type) override
130 {
131 return reference::accumulate<T1, T2>(input, output_data_type);
132 }
133};
134
135template <typename TensorType, typename AccessorType, typename FunctionType, typename T1, typename T2>
136class AccumulateWeightedValidationFixture : public AccumulateBaseValidationFixture<TensorType, AccessorType, FunctionType, T1, T2>
137{
138public:
139 template <typename...>
140 void setup(TensorShape shape, DataType data_type, DataType output_data_type)
141 {
142 std::mt19937 gen(library->seed());
143 std::uniform_real_distribution<> float_dist(0, 1);
144
145 _alpha = float_dist(gen);
146
147 AccumulateBaseValidationFixture<TensorType, AccessorType, FunctionType, T1, T2>::setup(shape, data_type, output_data_type);
148 }
149
150 virtual void accum_conf(FunctionType &func, const TensorType &input, TensorType &accum) override
151 {
152 func.configure(&input, _alpha, &accum);
153 }
154
155 virtual SimpleTensor<T2> accum_ref(const SimpleTensor<T1> &input, DataType output_data_type) override
156 {
157 return reference::accumulate_weighted<T1, T2>(input, _alpha, output_data_type);
158 }
159
160 float _alpha{ 0.f };
161};
162
163template <typename TensorType, typename AccessorType, typename FunctionType, typename T1, typename T2>
164class AccumulateSquaredValidationFixture : public AccumulateBaseValidationFixture<TensorType, AccessorType, FunctionType, T1, T2>
165{
166public:
167 template <typename...>
168 void setup(TensorShape shape, DataType data_type, DataType output_data_type)
169 {
170 std::mt19937 gen(library->seed());
171 std::uniform_int_distribution<uint32_t> int_dist(0, 15);
172
173 _shift = int_dist(gen);
174
175 AccumulateBaseValidationFixture<TensorType, AccessorType, FunctionType, T1, T2>::setup(shape, data_type, output_data_type);
176 }
177
178 virtual void accum_conf(FunctionType &func, const TensorType &input, TensorType &accum) override
179 {
180 func.configure(&input, _shift, &accum);
181 }
182
183 virtual SimpleTensor<T2> accum_ref(const SimpleTensor<T1> &input, DataType output_data_type) override
184 {
185 return reference::accumulate_squared<T1, T2>(input, _shift, output_data_type);
186 }
187
188 uint32_t _shift{ 0U };
189};
190} // namespace validation
191} // namespace test
192} // namespace arm_compute
193#endif /* ARM_COMPUTE_TEST_ACCUMULATE_FIXTURE */