blob: 564098497bec75600bb198617a091528bdcae4c5 [file] [log] [blame]
Georgios Pinitas0bc78492019-03-18 20:07:37 +00001/*
Giorgio Arenaea7de7b2020-12-10 16:49:39 +00002 * Copyright (c) 2019-2020 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
Giorgio Arenaea7de7b2020-12-10 16:49:39 +000038#include "utils/Utils.h"
39
Georgios Pinitas0bc78492019-03-18 20:07:37 +000040#include <random>
41
42namespace arm_compute
43{
44namespace test
45{
46namespace validation
47{
Georgios Pinitas8be91482019-03-26 17:23:28 +000048template <typename TensorType, typename AccessorType, typename FunctionType, typename InfoType, typename T>
Georgios Pinitas0bc78492019-03-18 20:07:37 +000049class FFTValidationFixture : public framework::Fixture
50{
51public:
52 template <typename...>
53 void setup(TensorShape shape, DataType data_type)
54 {
55 _target = compute_target(shape, data_type);
56 _reference = compute_reference(shape, data_type);
57 ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(_target.info()->tensor_shape(), _reference.shape());
58 }
59
60protected:
61 template <typename U>
62 void fill(U &&tensor)
63 {
Giorgio Arenaea7de7b2020-12-10 16:49:39 +000064 switch(tensor.data_type())
65 {
66 case DataType::F16:
67 {
68 arm_compute::utils::uniform_real_distribution_fp16 distribution(half(-5.0f), half(5.0f));
69 library->fill(tensor, distribution, 0);
70 break;
71 }
72 case DataType::F32:
73 {
74 std::uniform_real_distribution<float> distribution(-5.0f, 5.0f);
75 library->fill(tensor, distribution, 0);
76 break;
77 }
78 default:
79 library->fill_tensor_uniform(tensor, 0);
80 }
Georgios Pinitas0bc78492019-03-18 20:07:37 +000081 }
82
83 TensorType compute_target(const TensorShape &shape, DataType data_type)
84 {
85 // Create tensors
86 TensorType src = create_tensor<TensorType>(shape, data_type, 2);
87 TensorType dst = create_tensor<TensorType>(shape, data_type, 2);
88
89 // Create and configure function
Georgios Pinitas8be91482019-03-26 17:23:28 +000090 FunctionType fft;
91 fft.configure(&src, &dst, InfoType());
Georgios Pinitas0bc78492019-03-18 20:07:37 +000092
93 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
94 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
95
96 // Allocate tensors
97 src.allocator()->allocate();
98 dst.allocator()->allocate();
99
100 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
101 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
102
103 // Fill tensors
104 fill(AccessorType(src));
105
106 // Compute function
Georgios Pinitas8be91482019-03-26 17:23:28 +0000107 fft.run();
Georgios Pinitas0bc78492019-03-18 20:07:37 +0000108
109 return dst;
110 }
111
112 SimpleTensor<T> compute_reference(const TensorShape &shape, DataType data_type)
113 {
114 // Create reference
115 SimpleTensor<T> src{ shape, data_type, 2 };
116
117 // Fill reference
118 fill(src);
Georgios Pinitas8be91482019-03-26 17:23:28 +0000119 if(std::is_same<InfoType, FFT1DInfo>::value)
120 {
121 return reference::dft_1d(src, reference::FFTDirection::Forward);
122 }
123 else
124 {
125 return reference::dft_2d(src, reference::FFTDirection::Forward);
126 }
Georgios Pinitas0bc78492019-03-18 20:07:37 +0000127 }
128
129 TensorType _target{};
130 SimpleTensor<T> _reference{};
131};
Georgios Pinitas8be91482019-03-26 17:23:28 +0000132
133template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
134class FFTConvolutionValidationGenericFixture : public framework::Fixture
135{
136public:
137 template <typename...>
138 void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, PadStrideInfo info, Size2D dilation,
139 DataType data_type, DataLayout data_layout, ActivationLayerInfo act_info)
140 {
141 _data_type = data_type;
142 _data_layout = data_layout;
143
144 _target = compute_target(input_shape, weights_shape, bias_shape, output_shape, info, dilation, act_info);
145 _reference = compute_reference(input_shape, weights_shape, bias_shape, output_shape, info, dilation, act_info);
146 }
147
148protected:
149 template <typename U>
150 void fill(U &&tensor, int i)
151 {
152 switch(tensor.data_type())
153 {
Giorgio Arenaea7de7b2020-12-10 16:49:39 +0000154 case DataType::F16:
155 {
156 arm_compute::utils::uniform_real_distribution_fp16 distribution(half(-1.0f), half(1.0f));
157 library->fill(tensor, distribution, i);
158 break;
159 }
Georgios Pinitas8be91482019-03-26 17:23:28 +0000160 case DataType::F32:
161 {
Giorgio Arenaea7de7b2020-12-10 16:49:39 +0000162 std::uniform_real_distribution<float> distribution(-1.0f, 1.0f);
Georgios Pinitas8be91482019-03-26 17:23:28 +0000163 library->fill(tensor, distribution, i);
164 break;
165 }
166 default:
167 library->fill_tensor_uniform(tensor, i);
168 }
169 }
170
171 TensorType compute_target(TensorShape input_shape, TensorShape weights_shape, const TensorShape &bias_shape, TensorShape output_shape, const PadStrideInfo &info,
172 const Size2D &dilation, const ActivationLayerInfo act_info)
173 {
174 ARM_COMPUTE_UNUSED(dilation);
175 ARM_COMPUTE_ERROR_ON((input_shape[2] % weights_shape[2]) != 0);
176
177 if(_data_layout == DataLayout::NHWC)
178 {
179 permute(input_shape, PermutationVector(2U, 0U, 1U));
180 permute(weights_shape, PermutationVector(2U, 0U, 1U));
181 permute(output_shape, PermutationVector(2U, 0U, 1U));
182 }
183
184 // Create tensors
185 TensorType src = create_tensor<TensorType>(input_shape, _data_type, 1, QuantizationInfo(), _data_layout);
186 TensorType weights = create_tensor<TensorType>(weights_shape, _data_type, 1, QuantizationInfo(), _data_layout);
187 TensorType bias = create_tensor<TensorType>(bias_shape, _data_type, 1, QuantizationInfo(), _data_layout);
188 TensorType dst = create_tensor<TensorType>(output_shape, _data_type, 1, QuantizationInfo(), _data_layout);
189
190 // Create and configure function
191 FunctionType conv;
Giorgio Arenaea7de7b2020-12-10 16:49:39 +0000192 conv.configure(&src, &weights, &bias, &dst, info, act_info, _data_type == DataType::F16);
Georgios Pinitas8be91482019-03-26 17:23:28 +0000193
194 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
195 ARM_COMPUTE_EXPECT(weights.info()->is_resizable(), framework::LogLevel::ERRORS);
196 ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS);
197 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
198
199 // Allocate tensors
200 src.allocator()->allocate();
201 weights.allocator()->allocate();
202 bias.allocator()->allocate();
203 dst.allocator()->allocate();
204
205 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
206 ARM_COMPUTE_EXPECT(!weights.info()->is_resizable(), framework::LogLevel::ERRORS);
207 ARM_COMPUTE_EXPECT(!bias.info()->is_resizable(), framework::LogLevel::ERRORS);
208 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
209
210 // Fill tensors
211 fill(AccessorType(src), 0);
212 fill(AccessorType(weights), 1);
213 fill(AccessorType(bias), 2);
214
215 // Compute convolution function
216 conv.run();
217
218 return dst;
219 }
220
221 SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape, const PadStrideInfo &info,
222 const Size2D &dilation, const ActivationLayerInfo act_info)
223 {
224 ARM_COMPUTE_ERROR_ON((input_shape[2] % weights_shape[2]) != 0);
225
226 // Create reference
227 SimpleTensor<T> src{ input_shape, _data_type, 1 };
228 SimpleTensor<T> weights{ weights_shape, _data_type, 1 };
229 SimpleTensor<T> bias{ bias_shape, _data_type, 1 };
230
231 // Fill reference
232 fill(src, 0);
233 fill(weights, 1);
234 fill(bias, 2);
235
236 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,
237 weights, bias, output_shape, info, dilation);
238 }
239
240 TensorType _target{};
241 SimpleTensor<T> _reference{};
242 DataType _data_type{};
243 DataLayout _data_layout{};
244};
245
246template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
247class FFTConvolutionValidationFixture : public FFTConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
248{
249public:
250 template <typename...>
251 void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, PadStrideInfo info, Size2D dilation,
252 DataType data_type, DataLayout data_layout, ActivationLayerInfo act_info)
253 {
254 FFTConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(input_shape, weights_shape, bias_shape, output_shape, info, dilation,
255 data_type, data_layout, act_info);
256 }
257};
Georgios Pinitas0bc78492019-03-18 20:07:37 +0000258} // namespace validation
259} // namespace test
260} // namespace arm_compute
261#endif /* ARM_COMPUTE_TEST_FFT_FIXTURE */