blob: 024227b22ab397fd229faba53c0597d39a6dce8b [file] [log] [blame]
Georgios Pinitas0bc78492019-03-18 20:07:37 +00001/*
Matthew Bentham945b8da2023-07-12 11:54:59 +00002 * Copyright (c) 2019-2023 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:
Georgios Pinitas0bc78492019-03-18 20:07:37 +000050 void setup(TensorShape shape, DataType data_type)
51 {
52 _target = compute_target(shape, data_type);
53 _reference = compute_reference(shape, data_type);
54 ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(_target.info()->tensor_shape(), _reference.shape());
55 }
56
57protected:
58 template <typename U>
59 void fill(U &&tensor)
60 {
Giorgio Arenaea7de7b2020-12-10 16:49:39 +000061 switch(tensor.data_type())
62 {
63 case DataType::F16:
64 {
Giorgio Arenaa8e2aeb2021-01-06 11:34:57 +000065 arm_compute::utils::uniform_real_distribution_16bit<half> distribution{ -5.0f, 5.0f };
Giorgio Arenaea7de7b2020-12-10 16:49:39 +000066 library->fill(tensor, distribution, 0);
67 break;
68 }
69 case DataType::F32:
70 {
71 std::uniform_real_distribution<float> distribution(-5.0f, 5.0f);
72 library->fill(tensor, distribution, 0);
73 break;
74 }
75 default:
76 library->fill_tensor_uniform(tensor, 0);
77 }
Georgios Pinitas0bc78492019-03-18 20:07:37 +000078 }
79
80 TensorType compute_target(const TensorShape &shape, DataType data_type)
81 {
82 // Create tensors
83 TensorType src = create_tensor<TensorType>(shape, data_type, 2);
84 TensorType dst = create_tensor<TensorType>(shape, data_type, 2);
85
86 // Create and configure function
Georgios Pinitas8be91482019-03-26 17:23:28 +000087 FunctionType fft;
88 fft.configure(&src, &dst, InfoType());
Georgios Pinitas0bc78492019-03-18 20:07:37 +000089
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +010090 ARM_COMPUTE_ASSERT(src.info()->is_resizable());
91 ARM_COMPUTE_ASSERT(dst.info()->is_resizable());
Georgios Pinitas0bc78492019-03-18 20:07:37 +000092
Manuel Bottini9a81cd82021-04-15 17:44:55 +010093 add_padding_x({ &src, &dst });
Giorgio Arena63825e82021-03-25 14:54:50 +000094
Georgios Pinitas0bc78492019-03-18 20:07:37 +000095 // Allocate tensors
96 src.allocator()->allocate();
97 dst.allocator()->allocate();
98
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +010099 ARM_COMPUTE_ASSERT(!src.info()->is_resizable());
100 ARM_COMPUTE_ASSERT(!dst.info()->is_resizable());
Georgios Pinitas0bc78492019-03-18 20:07:37 +0000101
102 // Fill tensors
103 fill(AccessorType(src));
104
105 // Compute function
Georgios Pinitas8be91482019-03-26 17:23:28 +0000106 fft.run();
Georgios Pinitas0bc78492019-03-18 20:07:37 +0000107
108 return dst;
109 }
110
111 SimpleTensor<T> compute_reference(const TensorShape &shape, DataType data_type)
112 {
113 // Create reference
114 SimpleTensor<T> src{ shape, data_type, 2 };
115
116 // Fill reference
117 fill(src);
Georgios Pinitas8be91482019-03-26 17:23:28 +0000118 if(std::is_same<InfoType, FFT1DInfo>::value)
119 {
120 return reference::dft_1d(src, reference::FFTDirection::Forward);
121 }
122 else
123 {
124 return reference::dft_2d(src, reference::FFTDirection::Forward);
125 }
Georgios Pinitas0bc78492019-03-18 20:07:37 +0000126 }
127
128 TensorType _target{};
129 SimpleTensor<T> _reference{};
130};
Georgios Pinitas8be91482019-03-26 17:23:28 +0000131
132template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
133class FFTConvolutionValidationGenericFixture : public framework::Fixture
134{
135public:
Georgios Pinitas8be91482019-03-26 17:23:28 +0000136 void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, PadStrideInfo info, Size2D dilation,
Manuel Bottinica62c6f2021-03-23 11:50:34 +0000137 DataType data_type, DataLayout data_layout, ActivationLayerInfo act_info, bool mixed_layout = false)
Georgios Pinitas8be91482019-03-26 17:23:28 +0000138 {
Manuel Bottinica62c6f2021-03-23 11:50:34 +0000139 _mixed_layout = mixed_layout;
Giorgio Arena63825e82021-03-25 14:54:50 +0000140 _data_type = data_type;
141 _data_layout = data_layout;
Georgios Pinitas8be91482019-03-26 17:23:28 +0000142
143 _target = compute_target(input_shape, weights_shape, bias_shape, output_shape, info, dilation, act_info);
144 _reference = compute_reference(input_shape, weights_shape, bias_shape, output_shape, info, dilation, act_info);
145 }
146
147protected:
Manuel Bottinica62c6f2021-03-23 11:50:34 +0000148 void mix_layout(FunctionType &layer, TensorType &src, TensorType &dst)
149 {
150 // Test Multi DataLayout graph cases, when the data layout changes after configure
151 src.info()->set_data_layout(_data_layout == DataLayout::NCHW ? DataLayout::NHWC : DataLayout::NCHW);
152 dst.info()->set_data_layout(_data_layout == DataLayout::NCHW ? DataLayout::NHWC : DataLayout::NCHW);
153
154 // Compute Convolution function
155 layer.run();
156
157 // Reinstating original data layout for the test suite to properly check the values
158 src.info()->set_data_layout(_data_layout);
159 dst.info()->set_data_layout(_data_layout);
160 }
161
Georgios Pinitas8be91482019-03-26 17:23:28 +0000162 template <typename U>
163 void fill(U &&tensor, int i)
164 {
165 switch(tensor.data_type())
166 {
Giorgio Arenaea7de7b2020-12-10 16:49:39 +0000167 case DataType::F16:
168 {
Giorgio Arenaa8e2aeb2021-01-06 11:34:57 +0000169 arm_compute::utils::uniform_real_distribution_16bit<half> distribution{ -1.0f, 1.0f };
Giorgio Arenaea7de7b2020-12-10 16:49:39 +0000170 library->fill(tensor, distribution, i);
171 break;
172 }
Georgios Pinitas8be91482019-03-26 17:23:28 +0000173 case DataType::F32:
174 {
Giorgio Arenaea7de7b2020-12-10 16:49:39 +0000175 std::uniform_real_distribution<float> distribution(-1.0f, 1.0f);
Georgios Pinitas8be91482019-03-26 17:23:28 +0000176 library->fill(tensor, distribution, i);
177 break;
178 }
179 default:
180 library->fill_tensor_uniform(tensor, i);
181 }
182 }
183
184 TensorType compute_target(TensorShape input_shape, TensorShape weights_shape, const TensorShape &bias_shape, TensorShape output_shape, const PadStrideInfo &info,
185 const Size2D &dilation, const ActivationLayerInfo act_info)
186 {
187 ARM_COMPUTE_UNUSED(dilation);
188 ARM_COMPUTE_ERROR_ON((input_shape[2] % weights_shape[2]) != 0);
189
190 if(_data_layout == DataLayout::NHWC)
191 {
192 permute(input_shape, PermutationVector(2U, 0U, 1U));
193 permute(weights_shape, PermutationVector(2U, 0U, 1U));
194 permute(output_shape, PermutationVector(2U, 0U, 1U));
195 }
196
197 // Create tensors
198 TensorType src = create_tensor<TensorType>(input_shape, _data_type, 1, QuantizationInfo(), _data_layout);
199 TensorType weights = create_tensor<TensorType>(weights_shape, _data_type, 1, QuantizationInfo(), _data_layout);
200 TensorType bias = create_tensor<TensorType>(bias_shape, _data_type, 1, QuantizationInfo(), _data_layout);
201 TensorType dst = create_tensor<TensorType>(output_shape, _data_type, 1, QuantizationInfo(), _data_layout);
202
Viet-Hoa Do0a36f582022-10-18 15:23:46 +0100203 add_padding_x({ &src, &weights, &bias, &dst }, _data_layout);
204
Georgios Pinitas8be91482019-03-26 17:23:28 +0000205 // Create and configure function
206 FunctionType conv;
Giorgio Arenaea7de7b2020-12-10 16:49:39 +0000207 conv.configure(&src, &weights, &bias, &dst, info, act_info, _data_type == DataType::F16);
Georgios Pinitas8be91482019-03-26 17:23:28 +0000208
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +0100209 ARM_COMPUTE_ASSERT(src.info()->is_resizable());
210 ARM_COMPUTE_ASSERT(weights.info()->is_resizable());
211 ARM_COMPUTE_ASSERT(bias.info()->is_resizable());
212 ARM_COMPUTE_ASSERT(dst.info()->is_resizable());
Georgios Pinitas8be91482019-03-26 17:23:28 +0000213
214 // Allocate tensors
215 src.allocator()->allocate();
216 weights.allocator()->allocate();
217 bias.allocator()->allocate();
218 dst.allocator()->allocate();
219
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +0100220 ARM_COMPUTE_ASSERT(!src.info()->is_resizable());
221 ARM_COMPUTE_ASSERT(!weights.info()->is_resizable());
222 ARM_COMPUTE_ASSERT(!bias.info()->is_resizable());
223 ARM_COMPUTE_ASSERT(!dst.info()->is_resizable());
Georgios Pinitas8be91482019-03-26 17:23:28 +0000224
225 // Fill tensors
226 fill(AccessorType(src), 0);
227 fill(AccessorType(weights), 1);
228 fill(AccessorType(bias), 2);
Giorgio Arena63825e82021-03-25 14:54:50 +0000229
Manuel Bottinica62c6f2021-03-23 11:50:34 +0000230 if(_mixed_layout)
231 {
232 mix_layout(conv, src, dst);
233 }
234 else
235 {
236 // Compute Convolution function
237 conv.run();
238 }
Georgios Pinitas8be91482019-03-26 17:23:28 +0000239 return dst;
240 }
241
242 SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape, const PadStrideInfo &info,
243 const Size2D &dilation, const ActivationLayerInfo act_info)
244 {
245 ARM_COMPUTE_ERROR_ON((input_shape[2] % weights_shape[2]) != 0);
246
247 // Create reference
248 SimpleTensor<T> src{ input_shape, _data_type, 1 };
249 SimpleTensor<T> weights{ weights_shape, _data_type, 1 };
250 SimpleTensor<T> bias{ bias_shape, _data_type, 1 };
251
252 // Fill reference
253 fill(src, 0);
254 fill(weights, 1);
255 fill(bias, 2);
256
257 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,
258 weights, bias, output_shape, info, dilation);
259 }
260
261 TensorType _target{};
262 SimpleTensor<T> _reference{};
263 DataType _data_type{};
264 DataLayout _data_layout{};
Giorgio Arena63825e82021-03-25 14:54:50 +0000265 bool _mixed_layout{ false };
Georgios Pinitas8be91482019-03-26 17:23:28 +0000266};
267
Manuel Bottinica62c6f2021-03-23 11:50:34 +0000268template <typename TensorType, typename AccessorType, typename FunctionType, typename T, bool mixed_layout = false>
Georgios Pinitas8be91482019-03-26 17:23:28 +0000269class FFTConvolutionValidationFixture : public FFTConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
270{
271public:
Georgios Pinitas8be91482019-03-26 17:23:28 +0000272 void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, PadStrideInfo info, Size2D dilation,
273 DataType data_type, DataLayout data_layout, ActivationLayerInfo act_info)
274 {
275 FFTConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(input_shape, weights_shape, bias_shape, output_shape, info, dilation,
Manuel Bottinica62c6f2021-03-23 11:50:34 +0000276 data_type, data_layout, act_info, mixed_layout);
Georgios Pinitas8be91482019-03-26 17:23:28 +0000277 }
278};
Georgios Pinitas0bc78492019-03-18 20:07:37 +0000279} // namespace validation
280} // namespace test
281} // namespace arm_compute
282#endif /* ARM_COMPUTE_TEST_FFT_FIXTURE */