1 /*
2 * Copyright (c) 2019-2020 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 #include "arm_compute/core/TensorShape.h"
25 #include "arm_compute/core/Types.h"
26 #include "tests/AssetsLibrary.h"
27 #include "tests/Globals.h"
28 #include "tests/SimpleTensor.h"
29 #include "tests/SimpleTensorAccessor.h"
30 #include "tests/framework/Asserts.h"
31 #include "tests/framework/Macros.h"
32 #include "tests/framework/datasets/Datasets.h"
33
34 #include "tests/validation/Validation.h"
35 #include "tests/validation/reference/ConvolutionLayer.h"
36 #include "tests/validation/reference/DFT.h"
37
38 #include <random>
39
40 namespace arm_compute
41 {
42 namespace test
43 {
44 namespace validation
45 {
46 namespace
47 {
48 auto shapes_1d_dft = framework::dataset::make("TensorShape", { TensorShape(33U),
49 TensorShape(8U),
50 TensorShape(23U, 7U),
51 TensorShape(16U, 8U, 4U)
52 });
53
54 auto shapes_2d_dft = framework::dataset::make("TensorShape", { TensorShape(33U, 14U),
55 TensorShape(8U, 9U),
56 TensorShape(23U, 7U, 3U),
57 TensorShape(16U, 8U, 4U)
58 });
59
60 auto conv_dataset_dft = framework::dataset::zip(framework::dataset::zip(framework::dataset::make("InputShape", { TensorShape(8U, 7U, 3U, 2U),
61 TensorShape(18U, 22U, 4U),
62 TensorShape(32U, 48U, 8U)
63 }),
64 framework::dataset::make("WeightShape", { TensorShape(3U, 3U, 3U, 6U),
65 TensorShape(5U, 5U, 4U, 3U),
66 TensorShape(9U, 9U, 8U, 3U)
67 })),
68 framework::dataset::make("ConvInfo", { PadStrideInfo(1, 1, 1, 1),
69 PadStrideInfo(1, 1, 2, 2),
70 PadStrideInfo(1, 1, 4, 4)
71 }));
72 } // namespace
73 TEST_SUITE(CPP)
TEST_SUITE(DFT)74 TEST_SUITE(DFT)
75
76 TEST_SUITE(DFT1D)
77 DATA_TEST_CASE(Real, framework::DatasetMode::ALL, shapes_1d_dft,
78 shape)
79 {
80 SimpleTensor<float> src{ shape, DataType::F32, 1 };
81 std::uniform_real_distribution<float> distribution(-5.f, 5.f);
82 library->fill(src, distribution, 0);
83
84 const bool is_odd = shape.x() % 2;
85
86 // Forward pass
87 auto forward = reference::rdft_1d(src);
88 // Backward pass
89 auto backward = reference::ridft_1d(forward, is_odd);
90
91 // Validate with input
92 validate(SimpleTensorAccessor<float>(src), backward, RelativeTolerance<float>(0.1f));
93 }
94
DATA_TEST_CASE(Complex,framework::DatasetMode::ALL,shapes_1d_dft,shape)95 DATA_TEST_CASE(Complex, framework::DatasetMode::ALL, shapes_1d_dft,
96 shape)
97 {
98 SimpleTensor<float> src{ shape, DataType::F32, 2 };
99 std::uniform_real_distribution<float> distribution(-5.f, 5.f);
100 library->fill(src, distribution, 0);
101
102 // Forward pass
103 auto forward = reference::dft_1d(src, reference::FFTDirection::Forward);
104 // Backward pass
105 auto backward = reference::dft_1d(forward, reference::FFTDirection::Inverse);
106
107 // Validate with input
108 validate(SimpleTensorAccessor<float>(src), backward, RelativeTolerance<float>(0.1f));
109 }
110 TEST_SUITE_END() // DFT1D
111
TEST_SUITE(DFT2D)112 TEST_SUITE(DFT2D)
113 DATA_TEST_CASE(Real, framework::DatasetMode::ALL, shapes_2d_dft,
114 shape)
115 {
116 SimpleTensor<float> src{ shape, DataType::F32, 1 };
117 std::uniform_real_distribution<float> distribution(-5.f, 5.f);
118 library->fill(src, distribution, 0);
119
120 const bool is_odd = shape.x() % 2;
121
122 // Forward pass
123 auto forward = reference::rdft_2d(src);
124 // Backward pass
125 auto backward = reference::ridft_2d(forward, is_odd);
126
127 // Validate with input
128 validate(SimpleTensorAccessor<float>(src), backward, RelativeTolerance<float>(0.1f));
129 }
130
DATA_TEST_CASE(Complex,framework::DatasetMode::ALL,shapes_2d_dft,shape)131 DATA_TEST_CASE(Complex, framework::DatasetMode::ALL, shapes_2d_dft,
132 shape)
133 {
134 SimpleTensor<float> src{ shape, DataType::F32, 2 };
135 std::uniform_real_distribution<float> distribution(-5.f, 5.f);
136 library->fill(src, distribution, 0);
137
138 // Forward pass
139 auto forward = reference::dft_2d(src, reference::FFTDirection::Forward);
140 // Backward pass
141 auto backward = reference::dft_2d(forward, reference::FFTDirection::Inverse);
142
143 // Validate with input
144 validate(SimpleTensorAccessor<float>(src), backward, RelativeTolerance<float>(0.1f), 0.f, AbsoluteTolerance<float>(0.001f));
145 }
146 TEST_SUITE_END() // DFT2D
147
TEST_SUITE(Conv)148 TEST_SUITE(Conv)
149 DATA_TEST_CASE(Real2Real, framework::DatasetMode::ALL, conv_dataset_dft,
150 shape_in, shape_w, conv_info)
151 {
152 std::uniform_real_distribution<float> distribution(-1.f, 1.f);
153 std::uniform_real_distribution<float> distribution_b(0.f, 0.f);
154
155 SimpleTensor<float> src{ shape_in, DataType::F32, 1 };
156 SimpleTensor<float> w{ shape_w, DataType::F32, 1 };
157 SimpleTensor<float> b{ TensorShape(shape_w[3]), DataType::F32, 1 };
158
159 library->fill(src, distribution, 0);
160 library->fill(w, distribution, 1);
161 library->fill(b, distribution_b, 2);
162
163 const auto output_wh = arm_compute::scaled_dimensions(shape_in.x(), shape_in.y(), shape_w.x(), shape_w.y(), conv_info);
164 TensorShape dst_shape = shape_in;
165 dst_shape.set(0, output_wh.first);
166 dst_shape.set(1, output_wh.second);
167 dst_shape.set(2, shape_w[3]);
168
169 // FFT based convolution
170 auto dst = reference::conv2d_dft(src, w, conv_info);
171 // Reference convolution
172 auto dst_ref = reference::convolution_layer(src, w, b, dst_shape, conv_info);
173
174 // Validate with input
175 validate(SimpleTensorAccessor<float>(dst), dst_ref, RelativeTolerance<float>(0.1f), 0.f, AbsoluteTolerance<float>(0.001f));
176 }
177 TEST_SUITE_END() // Conv
178
179 TEST_SUITE_END() // DFT
180 TEST_SUITE_END() // CPP
181 } // namespace validation
182 } // namespace test
183 } // namespace arm_compute
184