blob: b98bc77b1dbaeeec8ea5f53beef213473b23d8f8 [file] [log] [blame]
Georgios Pinitasdef2a852019-02-21 14:47:56 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2019-2020 Arm Limited.
Georgios Pinitasdef2a852019-02-21 14:47:56 +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#include "DFT.h"
25
26#include "PadLayer.h"
27#include "Permute.h"
28#include "Reverse.h"
29#include "SliceOperations.h"
30
31#include <cmath>
32
33namespace arm_compute
34{
35namespace test
36{
37namespace validation
38{
39namespace reference
40{
41namespace
42{
43/** Performs an one dimensional DFT on a given real sequence.
44 *
45 * @param[in] src_ptr Pointer to the real input sequence.
46 * @param[in] N Size of input sequence.
47 * @param[out] dst_ptr Pointer to the complex output sequence.
48 * @param[out] K Size of the output sequence
49 */
50template <typename T>
51void rdft_1d_step(const T *src_ptr, size_t N, T *dst_ptr, size_t K)
52{
Michalis Spyroud1d77222020-04-08 14:10:15 +010053#if defined(_OPENMP)
54 #pragma omp parallel for
55#endif /* _OPENMP */
Georgios Pinitasdef2a852019-02-21 14:47:56 +000056 for(unsigned int k = 0; k < K; ++k)
57 {
58 float Xr = 0;
59 float Xi = 0;
60 for(unsigned int n = 0; n < N; ++n)
61 {
62 const float alpha = (2 * M_PI * k * n) / N;
63 const float val_r = src_ptr[n];
64 // Assuming DFT from the R domain thus skipping imaginary calculations
65 Xr += val_r * cos(alpha);
66 Xi -= val_r * sin(alpha);
67 }
68
69 dst_ptr[k * 2] = Xr;
70 dst_ptr[k * 2 + 1] = Xi;
71 }
72}
73
74/** Performs an one dimensional DFT on a given complex sequence.
75 *
76 * @param[in] src_ptr Pointer to the complex input sequence.
77 * @param[out] dst_ptr Pointer to the complex output sequence.
78 * @param[in] N Size of the sequences
79 */
80template <typename T>
81void dft_1d_step(const T *src_ptr, T *dst_ptr, size_t N)
82{
Michalis Spyroud1d77222020-04-08 14:10:15 +010083#if defined(_OPENMP)
84 #pragma omp parallel for
85#endif /* _OPENMP */
Georgios Pinitasdef2a852019-02-21 14:47:56 +000086 for(unsigned int k = 0; k < N; ++k)
87 {
88 float Xr = 0;
89 float Xi = 0;
90 for(unsigned int n = 0; n < N; ++n)
91 {
92 const float alpha = (2 * M_PI * k * n) / N;
93 const float val_r = src_ptr[2 * n];
94 const float val_i = src_ptr[2 * n + 1];
95 const float cos_alpha = cos(alpha);
96 const float sin_alpha = sin(alpha);
97
98 Xr += val_r * cos_alpha + val_i * sin_alpha;
99 Xi += val_i * cos_alpha - val_r * sin_alpha;
100 }
101
102 dst_ptr[k * 2] = Xr;
103 dst_ptr[k * 2 + 1] = Xi;
104 }
105}
106
107/** Performs an one dimensional inverse DFT on a given real sequence.
108 *
109 * @param[in] src_ptr Pointer to the real input sequence.
110 * @param[in] K Size of input sequence.
111 * @param[out] dst_ptr Pointer to the complex output sequence.
112 * @param[out] N Size of the output sequence
113 */
114template <typename T>
115void irdft_1d_step(const T *src_ptr, size_t K, T *dst_ptr, size_t N)
116{
117 const bool is_odd = N % 2;
118 const unsigned int Nleft = N - K;
119 const int tail_start = is_odd ? K - 1 : K - 2;
Michalis Spyroud1d77222020-04-08 14:10:15 +0100120#if defined(_OPENMP)
121 #pragma omp parallel for
122#endif /* _OPENMP */
Georgios Pinitasdef2a852019-02-21 14:47:56 +0000123 for(unsigned int n = 0; n < N; ++n)
124 {
125 float xr = 0;
126 for(unsigned int k = 0; k < K; ++k)
127 {
128 const float alpha = (2 * M_PI * k * n) / N;
129 xr += src_ptr[2 * k] * cos(alpha) - src_ptr[2 * k + 1] * sin(alpha);
130 }
131
132 unsigned int j = tail_start;
133 for(unsigned int k = 0; k < Nleft; ++k)
134 {
135 const float alpha = (2 * M_PI * (k + K) * n) / N;
136 xr += src_ptr[2 * j] * cos(alpha) + src_ptr[2 * j + 1] * sin(alpha);
137 --j;
138 }
139
140 dst_ptr[n] = xr;
141 }
142}
143
144/** Performs an one dimensional inverse DFT on a given complex sequence.
145 *
146 * @param[in] src_ptr Pointer to the complex input sequence.
147 * @param[out] dst_ptr Pointer to the complex output sequence.
148 * @param[in] N Size of the sequences
149 */
150template <typename T>
151void idft_1d_step(const T *src_ptr, T *dst_ptr, size_t N)
152{
Michalis Spyroud1d77222020-04-08 14:10:15 +0100153#if defined(_OPENMP)
154 #pragma omp parallel for
155#endif /* _OPENMP */
Georgios Pinitasdef2a852019-02-21 14:47:56 +0000156 for(unsigned int n = 0; n < N; ++n)
157 {
158 float xr = 0;
159 float xi = 0;
160 for(unsigned int k = 0; k < N; ++k)
161 {
162 const float alpha = (2 * M_PI * k * n) / N;
163 const float cos_alpha = cos(alpha);
164 const float sin_alpha = sin(alpha);
165 const float val_r = src_ptr[2 * k];
166 const float val_i = src_ptr[2 * k + 1];
167
168 xr += val_r * cos_alpha - val_i * sin_alpha;
169 xi += val_i * cos_alpha + val_r * sin_alpha;
170 }
171
172 dst_ptr[2 * n] = xr;
173 dst_ptr[2 * n + 1] = xi;
174 }
175}
176
177template <typename T>
178SimpleTensor<T> rdft_1d_core(const SimpleTensor<T> &src, FFTDirection direction, bool is_odd)
179{
180 // Performs only rdft
181 ARM_COMPUTE_ERROR_ON(direction == FFTDirection::Forward && src.num_channels() != 1);
182 ARM_COMPUTE_ERROR_ON(direction == FFTDirection::Inverse && src.num_channels() != 2);
183
184 const unsigned int inverse_tail = is_odd ? 1 : 0;
185 const unsigned int N = src.shape()[0];
186 const unsigned int K = direction == FFTDirection::Forward ? N / 2 + 1 : (N - 1) * 2 + inverse_tail;
187 const unsigned int num_channels = direction == FFTDirection::Forward ? 2 : 1;
188
189 TensorShape dst_shape = src.shape();
190 dst_shape.set(0, K);
191
192 SimpleTensor<T> dst(dst_shape, src.data_type(), num_channels);
193
194 const unsigned int upper_dims = src.shape().total_size_upper(1);
Michalis Spyroud1d77222020-04-08 14:10:15 +0100195#if defined(_OPENMP)
196 #pragma omp parallel for
197#endif /* _OPENMP */
Georgios Pinitasdef2a852019-02-21 14:47:56 +0000198 for(unsigned int du = 0; du < upper_dims; ++du)
199 {
200 const T *src_row_ptr = src.data() + du * N * src.num_channels();
201 T *dst_row_ptr = dst.data() + du * K * dst.num_channels();
202 direction == FFTDirection::Forward ? rdft_1d_step(src_row_ptr, N, dst_row_ptr, K) : irdft_1d_step(src_row_ptr, N, dst_row_ptr, K);
203 }
204
205 return dst;
206}
207
208template <typename T>
209SimpleTensor<T> dft_1d_core(const SimpleTensor<T> &src, FFTDirection direction)
210{
211 ARM_COMPUTE_ERROR_ON(src.num_channels() != 2);
212
213 const unsigned int N = src.shape()[0];
214
215 SimpleTensor<T> dst(src.shape(), src.data_type(), src.num_channels());
216
217 const unsigned int upper_dims = src.shape().total_size_upper(1);
Michalis Spyroud1d77222020-04-08 14:10:15 +0100218#if defined(_OPENMP)
219 #pragma omp parallel for
220#endif /* _OPENMP */
Georgios Pinitasdef2a852019-02-21 14:47:56 +0000221 for(unsigned int du = 0; du < upper_dims; ++du)
222 {
223 const T *src_row_ptr = src.data() + du * N * src.num_channels();
224 T *dst_row_ptr = dst.data() + du * N * dst.num_channels();
225 direction == FFTDirection::Forward ? dft_1d_step(src_row_ptr, dst_row_ptr, N) : idft_1d_step(src_row_ptr, dst_row_ptr, N);
226 }
227
228 return dst;
229}
230
231/** Scale a tensor by a given scaling factor.
232 *
233 * @param[in,out] tensor Tensor to scale.
234 * @param[in] scaling_factor Scaling to scale the tensor data with.
235 */
236template <typename T>
237void scale(SimpleTensor<T> &tensor, T scaling_factor)
238{
239 const int total_elements = tensor.num_elements() * tensor.num_channels();
240 T *data_ptr = tensor.data();
Michalis Spyroud1d77222020-04-08 14:10:15 +0100241#if defined(_OPENMP)
242 #pragma omp parallel for
243#endif /* _OPENMP */
Georgios Pinitasdef2a852019-02-21 14:47:56 +0000244 for(int i = 0; i < total_elements; ++i)
245 {
246 data_ptr[i] /= scaling_factor;
247 }
248}
249
250/** Performs a complex element-wise multiplication with reduction across the channels axis.
251 *
252 * @param[in] input Input tensor.
253 * @param[in] weights Weights tensor.
254 *
255 * @return Output tensor.
256 */
257template <typename T>
258SimpleTensor<T> complex_mul_and_reduce(const SimpleTensor<T> &input, const SimpleTensor<T> &weights)
259{
Michalis Spyroufae513c2019-10-16 17:41:33 +0100260 const uint32_t W = input.shape().x();
261 const uint32_t H = input.shape().y();
262 const uint32_t Ci = input.shape().z();
263 const uint32_t Co = weights.shape()[3];
264 const uint32_t N = input.shape().total_size() / (W * H * Ci);
Georgios Pinitasdef2a852019-02-21 14:47:56 +0000265
266 TensorShape output_shape = input.shape();
267 output_shape.set(2, Co);
268 SimpleTensor<T> dst(output_shape, input.data_type(), input.num_channels());
269
270 // MemSet dst memory to zero
271 std::memset(dst.data(), 0, dst.size());
Manuel Bottinif25e2952020-04-23 12:40:08 +0100272
Michalis Spyroufae513c2019-10-16 17:41:33 +0100273 for(uint32_t b = 0; b < N; ++b)
Georgios Pinitasdef2a852019-02-21 14:47:56 +0000274 {
Michalis Spyroufae513c2019-10-16 17:41:33 +0100275 for(uint32_t co = 0; co < Co; ++co)
Georgios Pinitasdef2a852019-02-21 14:47:56 +0000276 {
Michalis Spyroufae513c2019-10-16 17:41:33 +0100277 for(uint32_t ci = 0; ci < Ci; ++ci)
Georgios Pinitasdef2a852019-02-21 14:47:56 +0000278 {
Michalis Spyroufae513c2019-10-16 17:41:33 +0100279 for(uint32_t h = 0; h < H; ++h)
Georgios Pinitasdef2a852019-02-21 14:47:56 +0000280 {
Michalis Spyroufae513c2019-10-16 17:41:33 +0100281 for(uint32_t w = 0; w < W; ++w)
Georgios Pinitasdef2a852019-02-21 14:47:56 +0000282 {
Michalis Spyroufae513c2019-10-16 17:41:33 +0100283 const uint32_t i_index = w + h * W + ci * H * W + b * H * W * Ci;
284 const uint32_t w_index = w + h * W + ci * H * W + co * H * W * Ci;
285 const uint32_t o_index = w + h * W + co * H * W + b * H * W * Co;
Georgios Pinitasdef2a852019-02-21 14:47:56 +0000286 const Coordinates i_coords = index2coords(input.shape(), i_index);
287 const Coordinates w_coords = index2coords(weights.shape(), w_index);
288 const Coordinates o_coords = index2coords(dst.shape(), o_index);
289
290 auto i_ptr = static_cast<const T *>(input(i_coords));
291 auto w_ptr = static_cast<const T *>(weights(w_coords));
292 auto o_ptr = static_cast<T *>(dst(o_coords));
293
294 const T Rin = i_ptr[0];
295 const T Iin = i_ptr[1];
296 const T Rw = w_ptr[0];
297 const T Iw = w_ptr[1];
298
299 o_ptr[0] += Rin * Rw - Iin * Iw;
300 o_ptr[1] += Rin * Iw + Rw * Iin;
301 }
302 }
303 }
304 }
305 }
306 return dst;
307}
308} // namespace
309
310template <typename T>
311SimpleTensor<T> rdft_1d(const SimpleTensor<T> &src)
312{
313 return rdft_1d_core(src, FFTDirection::Forward, false);
314}
315
316template <typename T>
317SimpleTensor<T> ridft_1d(const SimpleTensor<T> &src, bool is_odd)
318{
319 auto dst = rdft_1d_core(src, FFTDirection::Inverse, is_odd);
320
Giorgio Arenaea7de7b2020-12-10 16:49:39 +0000321 const T scaling_factor = T(dst.shape()[0]);
Georgios Pinitasdef2a852019-02-21 14:47:56 +0000322 scale(dst, scaling_factor);
323
324 return dst;
325}
326
327template <typename T>
328SimpleTensor<T> dft_1d(const SimpleTensor<T> &src, FFTDirection direction)
329{
330 auto dst = dft_1d_core(src, direction);
331 if(direction == FFTDirection::Inverse)
332 {
Giorgio Arenaea7de7b2020-12-10 16:49:39 +0000333 const T scaling_factor = T(dst.shape()[0]);
Georgios Pinitasdef2a852019-02-21 14:47:56 +0000334 scale(dst, scaling_factor);
335 }
336 return dst;
337}
338
339template <typename T>
340SimpleTensor<T> rdft_2d(const SimpleTensor<T> &src)
341{
342 ARM_COMPUTE_ERROR_ON(src.num_channels() != 1);
343 constexpr FFTDirection direction = FFTDirection::Forward;
344
345 auto first_pass = rdft_1d_core(src, direction, false);
346 auto transposed = permute(first_pass, PermutationVector(1U, 0U));
347 auto second_pass = dft_1d_core(transposed, direction);
348 return permute(second_pass, PermutationVector(1U, 0U));
349}
350
351template <typename T>
352SimpleTensor<T> ridft_2d(const SimpleTensor<T> &src, bool is_odd)
353{
354 ARM_COMPUTE_ERROR_ON(src.num_channels() != 2);
355 constexpr FFTDirection direction = FFTDirection::Inverse;
356
357 auto transposed = permute(src, PermutationVector(1U, 0U));
358 auto first_pass = dft_1d_core(transposed, direction);
359 auto transposed_2 = permute(first_pass, PermutationVector(1U, 0U));
360 auto dst = rdft_1d_core(transposed_2, direction, is_odd);
361
Giorgio Arenaea7de7b2020-12-10 16:49:39 +0000362 const T scaling_factor = T(dst.shape()[0] * dst.shape()[1]);
Georgios Pinitasdef2a852019-02-21 14:47:56 +0000363 scale(dst, scaling_factor);
364 return dst;
365}
366
367template <typename T>
368SimpleTensor<T> dft_2d(const SimpleTensor<T> &src, FFTDirection direction)
369{
370 ARM_COMPUTE_ERROR_ON(src.num_channels() != 2);
371
372 if(direction == FFTDirection::Forward)
373 {
374 auto first_pass = dft_1d_core(src, direction);
375 auto transposed = permute(first_pass, PermutationVector(1U, 0U));
376 auto second_pass = dft_1d_core(transposed, direction);
377 return permute(second_pass, PermutationVector(1U, 0U));
378 }
379 else
380 {
381 auto transposed = permute(src, PermutationVector(1U, 0U));
382 auto first_pass = dft_1d_core(transposed, direction);
383 auto transposed_2 = permute(first_pass, PermutationVector(1U, 0U));
384 auto dst = dft_1d_core(transposed_2, direction);
385
Giorgio Arenaea7de7b2020-12-10 16:49:39 +0000386 const T scaling_factor = T(dst.shape()[0] * dst.shape()[1]);
Georgios Pinitasdef2a852019-02-21 14:47:56 +0000387 scale(dst, scaling_factor);
388
389 return dst;
390 }
391}
392
393template <typename T>
394SimpleTensor<T> conv2d_dft(const SimpleTensor<T> &src, const SimpleTensor<T> &w, const PadStrideInfo &conv_info)
395{
396 // Pad input to full padding
397 const PaddingList padding_in = { { 0, w.shape()[0] - 1 }, { 0, w.shape()[1] - 1 } };
398 auto padded_src = pad_layer(src, padding_in);
399
400 // Flip weights
401 std::vector<uint32_t> axis_v = { 0, 1 };
402 SimpleTensor<uint32_t> axis{ TensorShape(2U), DataType::U32 };
403 std::copy(axis_v.begin(), axis_v.begin() + axis.shape().x(), axis.data());
404 auto flipped_w = reverse(w, axis);
405
406 // Pad weights to have the same size as input
407 const PaddingList paddings_w = { { 0, src.shape()[0] - 1 }, { 0, src.shape()[1] - 1 } };
408 auto padded_w = pad_layer(flipped_w, paddings_w);
409
410 // Transform input and weights to frequency domain
411 auto Fsrc = rdft_2d(padded_src);
412 auto Fw = rdft_2d(padded_w);
413
414 // Perform dot product
415 auto Fdst = complex_mul_and_reduce(Fsrc, Fw);
416
417 // Transform output back to frequency domain
418 auto conv_res = ridft_2d(Fdst);
419
420 // Slice output
421 const int start_left = w.shape().x() - conv_info.pad_left() - 1;
422 const int start_top = w.shape().y() - conv_info.pad_top() - 1;
423 const int end_right = conv_res.shape().x() - (w.shape().x() - conv_info.pad_right() - 1);
424 const int end_botton = conv_res.shape().y() - (w.shape().y() - conv_info.pad_bottom() - 1);
425 return slice(conv_res, Coordinates(start_left, start_top), Coordinates(end_right, end_botton));
426}
427
Giorgio Arenaea7de7b2020-12-10 16:49:39 +0000428// FP32
Georgios Pinitasdef2a852019-02-21 14:47:56 +0000429template SimpleTensor<float> rdft_1d(const SimpleTensor<float> &src);
430template SimpleTensor<float> ridft_1d(const SimpleTensor<float> &src, bool is_odd);
431template SimpleTensor<float> dft_1d(const SimpleTensor<float> &src, FFTDirection direction);
432
433template SimpleTensor<float> rdft_2d(const SimpleTensor<float> &src);
434template SimpleTensor<float> ridft_2d(const SimpleTensor<float> &src, bool is_odd);
435template SimpleTensor<float> dft_2d(const SimpleTensor<float> &src, FFTDirection direction);
436
437template SimpleTensor<float> conv2d_dft(const SimpleTensor<float> &src, const SimpleTensor<float> &w, const PadStrideInfo &conv_info);
Giorgio Arenaea7de7b2020-12-10 16:49:39 +0000438
439// FP16
440template SimpleTensor<half> rdft_1d(const SimpleTensor<half> &src);
441template SimpleTensor<half> ridft_1d(const SimpleTensor<half> &src, bool is_odd);
442template SimpleTensor<half> dft_1d(const SimpleTensor<half> &src, FFTDirection direction);
443
444template SimpleTensor<half> rdft_2d(const SimpleTensor<half> &src);
445template SimpleTensor<half> ridft_2d(const SimpleTensor<half> &src, bool is_odd);
446template SimpleTensor<half> dft_2d(const SimpleTensor<half> &src, FFTDirection direction);
447
448template SimpleTensor<half> conv2d_dft(const SimpleTensor<half> &src, const SimpleTensor<half> &w, const PadStrideInfo &conv_info);
Georgios Pinitasdef2a852019-02-21 14:47:56 +0000449} // namespace reference
450} // namespace validation
451} // namespace test
452} // namespace arm_compute