aboutsummaryrefslogtreecommitdiff
path: root/tests/validation/CPP/DFT.cpp
blob: d4020f2d99aceb8912b9c1f04e9205428f71e2e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
 * Copyright (c) 2019-2020 ARM Limited.
 *
 * SPDX-License-Identifier: MIT
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#include "arm_compute/core/TensorShape.h"
#include "arm_compute/core/Types.h"
#include "tests/AssetsLibrary.h"
#include "tests/Globals.h"
#include "tests/SimpleTensor.h"
#include "tests/SimpleTensorAccessor.h"
#include "tests/framework/Asserts.h"
#include "tests/framework/Macros.h"
#include "tests/framework/datasets/Datasets.h"

#include "tests/validation/Validation.h"
#include "tests/validation/reference/ConvolutionLayer.h"
#include "tests/validation/reference/DFT.h"

#include <random>

namespace arm_compute
{
namespace test
{
namespace validation
{
namespace
{
auto shapes_1d_dft = framework::dataset::make("TensorShape", { TensorShape(33U),
                                                               TensorShape(8U),
                                                               TensorShape(23U, 7U),
                                                               TensorShape(16U, 8U, 4U)
                                                             });

auto shapes_2d_dft = framework::dataset::make("TensorShape", { TensorShape(33U, 14U),
                                                               TensorShape(8U, 9U),
                                                               TensorShape(23U, 7U, 3U),
                                                               TensorShape(16U, 8U, 4U)
                                                             });

auto conv_dataset_dft = framework::dataset::zip(framework::dataset::zip(framework::dataset::make("InputShape", { TensorShape(8U, 7U, 3U, 2U),
                                                                                                                 TensorShape(18U, 22U, 4U),
                                                                                                                 TensorShape(32U, 48U, 8U)
                                                                                                               }),
                                                                        framework::dataset::make("WeightShape", { TensorShape(3U, 3U, 3U, 6U),
                                                                                                                  TensorShape(5U, 5U, 4U, 3U),
                                                                                                                  TensorShape(9U, 9U, 8U, 3U)
                                                                                                                })),
                                                framework::dataset::make("ConvInfo", { PadStrideInfo(1, 1, 1, 1),
                                                                                       PadStrideInfo(1, 1, 2, 2),
                                                                                       PadStrideInfo(1, 1, 4, 4)
                                                                                     }));
} // namespace
TEST_SUITE(CPP)
TEST_SUITE(DFT)

TEST_SUITE(DFT1D)
DATA_TEST_CASE(Real, framework::DatasetMode::ALL, shapes_1d_dft,
               shape)
{
    SimpleTensor<float>                   src{ shape, DataType::F32, 1 };
    std::uniform_real_distribution<float> distribution(-5.f, 5.f);
    library->fill(src, distribution, 0);

    const bool is_odd = shape.x() % 2;

    // Forward pass
    auto forward = reference::rdft_1d(src);
    // Backward pass
    auto backward = reference::ridft_1d(forward, is_odd);

    // Validate with input
    validate(SimpleTensorAccessor<float>(src), backward, RelativeTolerance<float>(0.1f));
}

DATA_TEST_CASE(Complex, framework::DatasetMode::ALL, shapes_1d_dft,
               shape)
{
    SimpleTensor<float>                   src{ shape, DataType::F32, 2 };
    std::uniform_real_distribution<float> distribution(-5.f, 5.f);
    library->fill(src, distribution, 0);

    // Forward pass
    auto forward = reference::dft_1d(src, reference::FFTDirection::Forward);
    // Backward pass
    auto backward = reference::dft_1d(forward, reference::FFTDirection::Inverse);

    // Validate with input
    validate(SimpleTensorAccessor<float>(src), backward, RelativeTolerance<float>(0.1f));
}
TEST_SUITE_END() // DFT1D

TEST_SUITE(DFT2D)
DATA_TEST_CASE(Real, framework::DatasetMode::ALL, shapes_2d_dft,
               shape)
{
    SimpleTensor<float>                   src{ shape, DataType::F32, 1 };
    std::uniform_real_distribution<float> distribution(-5.f, 5.f);
    library->fill(src, distribution, 0);

    const bool is_odd = shape.x() % 2;

    // Forward pass
    auto forward = reference::rdft_2d(src);
    // Backward pass
    auto backward = reference::ridft_2d(forward, is_odd);

    // Validate with input
    validate(SimpleTensorAccessor<float>(src), backward, RelativeTolerance<float>(0.1f));
}

DATA_TEST_CASE(Complex, framework::DatasetMode::ALL, shapes_2d_dft,
               shape)
{
    SimpleTensor<float>                   src{ shape, DataType::F32, 2 };
    std::uniform_real_distribution<float> distribution(-5.f, 5.f);
    library->fill(src, distribution, 0);

    // Forward pass
    auto forward = reference::dft_2d(src, reference::FFTDirection::Forward);
    // Backward pass
    auto backward = reference::dft_2d(forward, reference::FFTDirection::Inverse);

    // Validate with input
    validate(SimpleTensorAccessor<float>(src), backward, RelativeTolerance<float>(0.1f), 0.f, AbsoluteTolerance<float>(0.001f));
}
TEST_SUITE_END() // DFT2D

TEST_SUITE(Conv)
DATA_TEST_CASE(Real2Real, framework::DatasetMode::ALL, conv_dataset_dft,
               shape_in, shape_w, conv_info)
{
    std::uniform_real_distribution<float> distribution(-1.f, 1.f);
    std::uniform_real_distribution<float> distribution_b(0.f, 0.f);

    SimpleTensor<float> src{ shape_in, DataType::F32, 1 };
    SimpleTensor<float> w{ shape_w, DataType::F32, 1 };
    SimpleTensor<float> b{ TensorShape(shape_w[3]), DataType::F32, 1 };

    library->fill(src, distribution, 0);
    library->fill(w, distribution, 1);
    library->fill(b, distribution_b, 2);

    const auto  output_wh = arm_compute::scaled_dimensions(shape_in.x(), shape_in.y(), shape_w.x(), shape_w.y(), conv_info);
    TensorShape dst_shape = shape_in;
    dst_shape.set(0, output_wh.first);
    dst_shape.set(1, output_wh.second);
    dst_shape.set(2, shape_w[3]);

    // FFT based convolution
    auto dst = reference::conv2d_dft(src, w, conv_info);
    // Reference convolution
    auto dst_ref = reference::convolution_layer(src, w, b, dst_shape, conv_info);

    // Validate with input
    validate(SimpleTensorAccessor<float>(dst), dst_ref, RelativeTolerance<float>(0.1f), 0.f, AbsoluteTolerance<float>(0.001f));
}
TEST_SUITE_END() // Conv

TEST_SUITE_END() // DFT
TEST_SUITE_END() // CPP
} // namespace validation
} // namespace test
} // namespace arm_compute