xref: /aosp_15_r20/external/ComputeLibrary/tests/validation/CPP/DFT.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1*c217d954SCole Faust /*
2*c217d954SCole Faust  * Copyright (c) 2019-2020 Arm Limited.
3*c217d954SCole Faust  *
4*c217d954SCole Faust  * SPDX-License-Identifier: MIT
5*c217d954SCole Faust  *
6*c217d954SCole Faust  * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust  * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust  * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust  * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust  * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust  *
13*c217d954SCole Faust  * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust  * copies or substantial portions of the Software.
15*c217d954SCole Faust  *
16*c217d954SCole Faust  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust  * SOFTWARE.
23*c217d954SCole Faust  */
24*c217d954SCole Faust #include "arm_compute/core/TensorShape.h"
25*c217d954SCole Faust #include "arm_compute/core/Types.h"
26*c217d954SCole Faust #include "tests/AssetsLibrary.h"
27*c217d954SCole Faust #include "tests/Globals.h"
28*c217d954SCole Faust #include "tests/SimpleTensor.h"
29*c217d954SCole Faust #include "tests/SimpleTensorAccessor.h"
30*c217d954SCole Faust #include "tests/framework/Asserts.h"
31*c217d954SCole Faust #include "tests/framework/Macros.h"
32*c217d954SCole Faust #include "tests/framework/datasets/Datasets.h"
33*c217d954SCole Faust 
34*c217d954SCole Faust #include "tests/validation/Validation.h"
35*c217d954SCole Faust #include "tests/validation/reference/ConvolutionLayer.h"
36*c217d954SCole Faust #include "tests/validation/reference/DFT.h"
37*c217d954SCole Faust 
38*c217d954SCole Faust #include <random>
39*c217d954SCole Faust 
40*c217d954SCole Faust namespace arm_compute
41*c217d954SCole Faust {
42*c217d954SCole Faust namespace test
43*c217d954SCole Faust {
44*c217d954SCole Faust namespace validation
45*c217d954SCole Faust {
46*c217d954SCole Faust namespace
47*c217d954SCole Faust {
48*c217d954SCole Faust auto shapes_1d_dft = framework::dataset::make("TensorShape", { TensorShape(33U),
49*c217d954SCole Faust                                                                TensorShape(8U),
50*c217d954SCole Faust                                                                TensorShape(23U, 7U),
51*c217d954SCole Faust                                                                TensorShape(16U, 8U, 4U)
52*c217d954SCole Faust                                                              });
53*c217d954SCole Faust 
54*c217d954SCole Faust auto shapes_2d_dft = framework::dataset::make("TensorShape", { TensorShape(33U, 14U),
55*c217d954SCole Faust                                                                TensorShape(8U, 9U),
56*c217d954SCole Faust                                                                TensorShape(23U, 7U, 3U),
57*c217d954SCole Faust                                                                TensorShape(16U, 8U, 4U)
58*c217d954SCole Faust                                                              });
59*c217d954SCole Faust 
60*c217d954SCole Faust auto conv_dataset_dft = framework::dataset::zip(framework::dataset::zip(framework::dataset::make("InputShape", { TensorShape(8U, 7U, 3U, 2U),
61*c217d954SCole Faust                                                                                                                  TensorShape(18U, 22U, 4U),
62*c217d954SCole Faust                                                                                                                  TensorShape(32U, 48U, 8U)
63*c217d954SCole Faust                                                                                                                }),
64*c217d954SCole Faust                                                                         framework::dataset::make("WeightShape", { TensorShape(3U, 3U, 3U, 6U),
65*c217d954SCole Faust                                                                                                                   TensorShape(5U, 5U, 4U, 3U),
66*c217d954SCole Faust                                                                                                                   TensorShape(9U, 9U, 8U, 3U)
67*c217d954SCole Faust                                                                                                                 })),
68*c217d954SCole Faust                                                 framework::dataset::make("ConvInfo", { PadStrideInfo(1, 1, 1, 1),
69*c217d954SCole Faust                                                                                        PadStrideInfo(1, 1, 2, 2),
70*c217d954SCole Faust                                                                                        PadStrideInfo(1, 1, 4, 4)
71*c217d954SCole Faust                                                                                      }));
72*c217d954SCole Faust } // namespace
73*c217d954SCole Faust TEST_SUITE(CPP)
TEST_SUITE(DFT)74*c217d954SCole Faust TEST_SUITE(DFT)
75*c217d954SCole Faust 
76*c217d954SCole Faust TEST_SUITE(DFT1D)
77*c217d954SCole Faust DATA_TEST_CASE(Real, framework::DatasetMode::ALL, shapes_1d_dft,
78*c217d954SCole Faust                shape)
79*c217d954SCole Faust {
80*c217d954SCole Faust     SimpleTensor<float>                   src{ shape, DataType::F32, 1 };
81*c217d954SCole Faust     std::uniform_real_distribution<float> distribution(-5.f, 5.f);
82*c217d954SCole Faust     library->fill(src, distribution, 0);
83*c217d954SCole Faust 
84*c217d954SCole Faust     const bool is_odd = shape.x() % 2;
85*c217d954SCole Faust 
86*c217d954SCole Faust     // Forward pass
87*c217d954SCole Faust     auto forward = reference::rdft_1d(src);
88*c217d954SCole Faust     // Backward pass
89*c217d954SCole Faust     auto backward = reference::ridft_1d(forward, is_odd);
90*c217d954SCole Faust 
91*c217d954SCole Faust     // Validate with input
92*c217d954SCole Faust     validate(SimpleTensorAccessor<float>(src), backward, RelativeTolerance<float>(0.1f));
93*c217d954SCole Faust }
94*c217d954SCole Faust 
DATA_TEST_CASE(Complex,framework::DatasetMode::ALL,shapes_1d_dft,shape)95*c217d954SCole Faust DATA_TEST_CASE(Complex, framework::DatasetMode::ALL, shapes_1d_dft,
96*c217d954SCole Faust                shape)
97*c217d954SCole Faust {
98*c217d954SCole Faust     SimpleTensor<float>                   src{ shape, DataType::F32, 2 };
99*c217d954SCole Faust     std::uniform_real_distribution<float> distribution(-5.f, 5.f);
100*c217d954SCole Faust     library->fill(src, distribution, 0);
101*c217d954SCole Faust 
102*c217d954SCole Faust     // Forward pass
103*c217d954SCole Faust     auto forward = reference::dft_1d(src, reference::FFTDirection::Forward);
104*c217d954SCole Faust     // Backward pass
105*c217d954SCole Faust     auto backward = reference::dft_1d(forward, reference::FFTDirection::Inverse);
106*c217d954SCole Faust 
107*c217d954SCole Faust     // Validate with input
108*c217d954SCole Faust     validate(SimpleTensorAccessor<float>(src), backward, RelativeTolerance<float>(0.1f));
109*c217d954SCole Faust }
110*c217d954SCole Faust TEST_SUITE_END() // DFT1D
111*c217d954SCole Faust 
TEST_SUITE(DFT2D)112*c217d954SCole Faust TEST_SUITE(DFT2D)
113*c217d954SCole Faust DATA_TEST_CASE(Real, framework::DatasetMode::ALL, shapes_2d_dft,
114*c217d954SCole Faust                shape)
115*c217d954SCole Faust {
116*c217d954SCole Faust     SimpleTensor<float>                   src{ shape, DataType::F32, 1 };
117*c217d954SCole Faust     std::uniform_real_distribution<float> distribution(-5.f, 5.f);
118*c217d954SCole Faust     library->fill(src, distribution, 0);
119*c217d954SCole Faust 
120*c217d954SCole Faust     const bool is_odd = shape.x() % 2;
121*c217d954SCole Faust 
122*c217d954SCole Faust     // Forward pass
123*c217d954SCole Faust     auto forward = reference::rdft_2d(src);
124*c217d954SCole Faust     // Backward pass
125*c217d954SCole Faust     auto backward = reference::ridft_2d(forward, is_odd);
126*c217d954SCole Faust 
127*c217d954SCole Faust     // Validate with input
128*c217d954SCole Faust     validate(SimpleTensorAccessor<float>(src), backward, RelativeTolerance<float>(0.1f));
129*c217d954SCole Faust }
130*c217d954SCole Faust 
DATA_TEST_CASE(Complex,framework::DatasetMode::ALL,shapes_2d_dft,shape)131*c217d954SCole Faust DATA_TEST_CASE(Complex, framework::DatasetMode::ALL, shapes_2d_dft,
132*c217d954SCole Faust                shape)
133*c217d954SCole Faust {
134*c217d954SCole Faust     SimpleTensor<float>                   src{ shape, DataType::F32, 2 };
135*c217d954SCole Faust     std::uniform_real_distribution<float> distribution(-5.f, 5.f);
136*c217d954SCole Faust     library->fill(src, distribution, 0);
137*c217d954SCole Faust 
138*c217d954SCole Faust     // Forward pass
139*c217d954SCole Faust     auto forward = reference::dft_2d(src, reference::FFTDirection::Forward);
140*c217d954SCole Faust     // Backward pass
141*c217d954SCole Faust     auto backward = reference::dft_2d(forward, reference::FFTDirection::Inverse);
142*c217d954SCole Faust 
143*c217d954SCole Faust     // Validate with input
144*c217d954SCole Faust     validate(SimpleTensorAccessor<float>(src), backward, RelativeTolerance<float>(0.1f), 0.f, AbsoluteTolerance<float>(0.001f));
145*c217d954SCole Faust }
146*c217d954SCole Faust TEST_SUITE_END() // DFT2D
147*c217d954SCole Faust 
TEST_SUITE(Conv)148*c217d954SCole Faust TEST_SUITE(Conv)
149*c217d954SCole Faust DATA_TEST_CASE(Real2Real, framework::DatasetMode::ALL, conv_dataset_dft,
150*c217d954SCole Faust                shape_in, shape_w, conv_info)
151*c217d954SCole Faust {
152*c217d954SCole Faust     std::uniform_real_distribution<float> distribution(-1.f, 1.f);
153*c217d954SCole Faust     std::uniform_real_distribution<float> distribution_b(0.f, 0.f);
154*c217d954SCole Faust 
155*c217d954SCole Faust     SimpleTensor<float> src{ shape_in, DataType::F32, 1 };
156*c217d954SCole Faust     SimpleTensor<float> w{ shape_w, DataType::F32, 1 };
157*c217d954SCole Faust     SimpleTensor<float> b{ TensorShape(shape_w[3]), DataType::F32, 1 };
158*c217d954SCole Faust 
159*c217d954SCole Faust     library->fill(src, distribution, 0);
160*c217d954SCole Faust     library->fill(w, distribution, 1);
161*c217d954SCole Faust     library->fill(b, distribution_b, 2);
162*c217d954SCole Faust 
163*c217d954SCole Faust     const auto  output_wh = arm_compute::scaled_dimensions(shape_in.x(), shape_in.y(), shape_w.x(), shape_w.y(), conv_info);
164*c217d954SCole Faust     TensorShape dst_shape = shape_in;
165*c217d954SCole Faust     dst_shape.set(0, output_wh.first);
166*c217d954SCole Faust     dst_shape.set(1, output_wh.second);
167*c217d954SCole Faust     dst_shape.set(2, shape_w[3]);
168*c217d954SCole Faust 
169*c217d954SCole Faust     // FFT based convolution
170*c217d954SCole Faust     auto dst = reference::conv2d_dft(src, w, conv_info);
171*c217d954SCole Faust     // Reference convolution
172*c217d954SCole Faust     auto dst_ref = reference::convolution_layer(src, w, b, dst_shape, conv_info);
173*c217d954SCole Faust 
174*c217d954SCole Faust     // Validate with input
175*c217d954SCole Faust     validate(SimpleTensorAccessor<float>(dst), dst_ref, RelativeTolerance<float>(0.1f), 0.f, AbsoluteTolerance<float>(0.001f));
176*c217d954SCole Faust }
177*c217d954SCole Faust TEST_SUITE_END() // Conv
178*c217d954SCole Faust 
179*c217d954SCole Faust TEST_SUITE_END() // DFT
180*c217d954SCole Faust TEST_SUITE_END() // CPP
181*c217d954SCole Faust } // namespace validation
182*c217d954SCole Faust } // namespace test
183*c217d954SCole Faust } // namespace arm_compute
184