blob: 86a97272a06813f257bd73461e921130321af45c [file] [log] [blame]
Georgios Pinitas0bc78492019-03-18 20:07:37 +00001/*
Giorgio Arenab309fc22021-01-05 09:46:16 +00002 * Copyright (c) 2019-2021 Arm Limited.
Georgios Pinitas0bc78492019-03-18 20:07:37 +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_FFT_FIXTURE
25#define ARM_COMPUTE_TEST_FFT_FIXTURE
26
27#include "arm_compute/core/Types.h"
28#include "arm_compute/runtime/FunctionDescriptors.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"
Georgios Pinitas8be91482019-03-26 17:23:28 +000034#include "tests/validation/reference/ActivationLayer.h"
35#include "tests/validation/reference/ConvolutionLayer.h"
Georgios Pinitas0bc78492019-03-18 20:07:37 +000036#include "tests/validation/reference/DFT.h"
37
38#include <random>
39
40namespace arm_compute
41{
42namespace test
43{
44namespace validation
45{
Georgios Pinitas8be91482019-03-26 17:23:28 +000046template <typename TensorType, typename AccessorType, typename FunctionType, typename InfoType, typename T>
Georgios Pinitas0bc78492019-03-18 20:07:37 +000047class FFTValidationFixture : public framework::Fixture
48{
49public:
50 template <typename...>
51 void setup(TensorShape shape, DataType data_type)
52 {
53 _target = compute_target(shape, data_type);
54 _reference = compute_reference(shape, data_type);
55 ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(_target.info()->tensor_shape(), _reference.shape());
56 }
57
58protected:
59 template <typename U>
60 void fill(U &&tensor)
61 {
Giorgio Arenaea7de7b2020-12-10 16:49:39 +000062 switch(tensor.data_type())
63 {
64 case DataType::F16:
65 {
Giorgio Arenaa8e2aeb2021-01-06 11:34:57 +000066 arm_compute::utils::uniform_real_distribution_16bit<half> distribution{ -5.0f, 5.0f };
Giorgio Arenaea7de7b2020-12-10 16:49:39 +000067 library->fill(tensor, distribution, 0);
68 break;
69 }
70 case DataType::F32:
71 {
72 std::uniform_real_distribution<float> distribution(-5.0f, 5.0f);
73 library->fill(tensor, distribution, 0);
74 break;
75 }
76 default:
77 library->fill_tensor_uniform(tensor, 0);
78 }
Georgios Pinitas0bc78492019-03-18 20:07:37 +000079 }
80
81 TensorType compute_target(const TensorShape &shape, DataType data_type)
82 {
83 // Create tensors
84 TensorType src = create_tensor<TensorType>(shape, data_type, 2);
85 TensorType dst = create_tensor<TensorType>(shape, data_type, 2);
86
87 // Create and configure function
Georgios Pinitas8be91482019-03-26 17:23:28 +000088 FunctionType fft;
89 fft.configure(&src, &dst, InfoType());
Georgios Pinitas0bc78492019-03-18 20:07:37 +000090
91 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
92 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
93
94 // Allocate tensors
95 src.allocator()->allocate();
96 dst.allocator()->allocate();
97
98 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
99 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
100
101 // Fill tensors
102 fill(AccessorType(src));
103
104 // Compute function
Georgios Pinitas8be91482019-03-26 17:23:28 +0000105 fft.run();
Georgios Pinitas0bc78492019-03-18 20:07:37 +0000106
107 return dst;
108 }
109
110 SimpleTensor<T> compute_reference(const TensorShape &shape, DataType data_type)
111 {
112 // Create reference
113 SimpleTensor<T> src{ shape, data_type, 2 };
114
115 // Fill reference
116 fill(src);
Georgios Pinitas8be91482019-03-26 17:23:28 +0000117 if(std::is_same<InfoType, FFT1DInfo>::value)
118 {
119 return reference::dft_1d(src, reference::FFTDirection::Forward);
120 }
121 else
122 {
123 return reference::dft_2d(src, reference::FFTDirection::Forward);
124 }
Georgios Pinitas0bc78492019-03-18 20:07:37 +0000125 }
126
127 TensorType _target{};
128 SimpleTensor<T> _reference{};
129};
Georgios Pinitas8be91482019-03-26 17:23:28 +0000130
131template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
132class FFTConvolutionValidationGenericFixture : public framework::Fixture
133{
134public:
135 template <typename...>
136 void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, PadStrideInfo info, Size2D dilation,
137 DataType data_type, DataLayout data_layout, ActivationLayerInfo act_info)
138 {
139 _data_type = data_type;
140 _data_layout = data_layout;
141
142 _target = compute_target(input_shape, weights_shape, bias_shape, output_shape, info, dilation, act_info);
143 _reference = compute_reference(input_shape, weights_shape, bias_shape, output_shape, info, dilation, act_info);
144 }
145
146protected:
147 template <typename U>
148 void fill(U &&tensor, int i)
149 {
150 switch(tensor.data_type())
151 {
Giorgio Arenaea7de7b2020-12-10 16:49:39 +0000152 case DataType::F16:
153 {
Giorgio Arenaa8e2aeb2021-01-06 11:34:57 +0000154 arm_compute::utils::uniform_real_distribution_16bit<half> distribution{ -1.0f, 1.0f };
Giorgio Arenaea7de7b2020-12-10 16:49:39 +0000155 library->fill(tensor, distribution, i);
156 break;
157 }
Georgios Pinitas8be91482019-03-26 17:23:28 +0000158 case DataType::F32:
159 {
Giorgio Arenaea7de7b2020-12-10 16:49:39 +0000160 std::uniform_real_distribution<float> distribution(-1.0f, 1.0f);
Georgios Pinitas8be91482019-03-26 17:23:28 +0000161 library->fill(tensor, distribution, i);
162 break;
163 }
164 default:
165 library->fill_tensor_uniform(tensor, i);
166 }
167 }
168
169 TensorType compute_target(TensorShape input_shape, TensorShape weights_shape, const TensorShape &bias_shape, TensorShape output_shape, const PadStrideInfo &info,
170 const Size2D &dilation, const ActivationLayerInfo act_info)
171 {
172 ARM_COMPUTE_UNUSED(dilation);
173 ARM_COMPUTE_ERROR_ON((input_shape[2] % weights_shape[2]) != 0);
174
175 if(_data_layout == DataLayout::NHWC)
176 {
177 permute(input_shape, PermutationVector(2U, 0U, 1U));
178 permute(weights_shape, PermutationVector(2U, 0U, 1U));
179 permute(output_shape, PermutationVector(2U, 0U, 1U));
180 }
181
182 // Create tensors
183 TensorType src = create_tensor<TensorType>(input_shape, _data_type, 1, QuantizationInfo(), _data_layout);
184 TensorType weights = create_tensor<TensorType>(weights_shape, _data_type, 1, QuantizationInfo(), _data_layout);
185 TensorType bias = create_tensor<TensorType>(bias_shape, _data_type, 1, QuantizationInfo(), _data_layout);
186 TensorType dst = create_tensor<TensorType>(output_shape, _data_type, 1, QuantizationInfo(), _data_layout);
187
188 // Create and configure function
189 FunctionType conv;
Giorgio Arenaea7de7b2020-12-10 16:49:39 +0000190 conv.configure(&src, &weights, &bias, &dst, info, act_info, _data_type == DataType::F16);
Georgios Pinitas8be91482019-03-26 17:23:28 +0000191
192 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
193 ARM_COMPUTE_EXPECT(weights.info()->is_resizable(), framework::LogLevel::ERRORS);
194 ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS);
195 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
196
197 // Allocate tensors
198 src.allocator()->allocate();
199 weights.allocator()->allocate();
200 bias.allocator()->allocate();
201 dst.allocator()->allocate();
202
203 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
204 ARM_COMPUTE_EXPECT(!weights.info()->is_resizable(), framework::LogLevel::ERRORS);
205 ARM_COMPUTE_EXPECT(!bias.info()->is_resizable(), framework::LogLevel::ERRORS);
206 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
207
208 // Fill tensors
209 fill(AccessorType(src), 0);
210 fill(AccessorType(weights), 1);
211 fill(AccessorType(bias), 2);
212
213 // Compute convolution function
214 conv.run();
215
216 return dst;
217 }
218
219 SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape, const PadStrideInfo &info,
220 const Size2D &dilation, const ActivationLayerInfo act_info)
221 {
222 ARM_COMPUTE_ERROR_ON((input_shape[2] % weights_shape[2]) != 0);
223
224 // Create reference
225 SimpleTensor<T> src{ input_shape, _data_type, 1 };
226 SimpleTensor<T> weights{ weights_shape, _data_type, 1 };
227 SimpleTensor<T> bias{ bias_shape, _data_type, 1 };
228
229 // Fill reference
230 fill(src, 0);
231 fill(weights, 1);
232 fill(bias, 2);
233
234 return (act_info.enabled()) ? reference::activation_layer<T>(reference::convolution_layer<T>(src, weights, bias, output_shape, info, dilation), act_info) : reference::convolution_layer<T>(src,
235 weights, bias, output_shape, info, dilation);
236 }
237
238 TensorType _target{};
239 SimpleTensor<T> _reference{};
240 DataType _data_type{};
241 DataLayout _data_layout{};
242};
243
244template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
245class FFTConvolutionValidationFixture : public FFTConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
246{
247public:
248 template <typename...>
249 void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, PadStrideInfo info, Size2D dilation,
250 DataType data_type, DataLayout data_layout, ActivationLayerInfo act_info)
251 {
252 FFTConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(input_shape, weights_shape, bias_shape, output_shape, info, dilation,
253 data_type, data_layout, act_info);
254 }
255};
Georgios Pinitas0bc78492019-03-18 20:07:37 +0000256} // namespace validation
257} // namespace test
258} // namespace arm_compute
259#endif /* ARM_COMPUTE_TEST_FFT_FIXTURE */