theano.gpuarray.fft – Fast Fourier Transforms

Performs Fast Fourier Transforms (FFT) on the GPU.

FFT gradients are implemented as the opposite Fourier transform of the output gradients.

Note

You must install scikit-cuda to compute Fourier transforms on the GPU.

Warning

The real and imaginary parts of the Fourier domain arrays are stored as a pair of float32 arrays, emulating complex64. Since theano has limited support for complex number operations, care must be taken to manually implement operations such as gradients.

theano.gpuarray.fft.curfft(inp, norm=None)[source]

Performs the fast Fourier transform of a real-valued input on the GPU.

The input must be a real-valued float32 variable of dimensions (m, …, n). It performs FFTs of size (…, n) on m batches.

The output is a GpuArray of dimensions (m, …, n//2+1, 2). The second to last dimension of the output contains the n//2+1 non-trivial elements of the real-valued FFTs. The real and imaginary parts are stored as a pair of float32 arrays.

Parameters
  • inp – Array of real-valued float32 of size (m, …, n), containing m inputs of size (…, n).

  • norm ({None, 'ortho', 'no_norm'}) – Normalization of transform. Following numpy, default None normalizes only the inverse transform by n, ‘ortho’ yields the unitary transform (

    System Message: WARNING/2 (1/\sqrt n)

    latex exited with error [stderr] kpathsea: Running mktexfmt latex.fmt mktexfmt: mktexfmt is using the following fmtutil.cnf files (in precedence order): mktexfmt: mktexfmt is using the following fmtutil.cnf file for writing changes: mktexfmt: /builddir/.texlive2020/texmf-config/web2c/fmtutil.cnf mktexfmt [INFO]: writing formats under /builddir/.texlive2020/texmf-var/web2c mktexfmt [INFO]: did not find entry for byfmt=latex, skipped mktexfmt [INFO]: total formats: 0 mktexfmt [INFO]: exiting with status 0 [stdout] This is pdfTeX, Version 3.14159265-2.6-1.40.21 (TeX Live 2020) (preloaded format=latex) restricted \write18 enabled. I can't find the format file `latex.fmt'!

    forward and inverse). In addition, ‘no_norm’ leaves the transform unnormalized.

theano.gpuarray.fft.cuirfft(inp, norm=None, is_odd=False)[source]

Performs the inverse fast Fourier Transform with real-valued output on the GPU.

The input is a variable of dimensions (m, …, n//2+1, 2) with type float32 representing the non-trivial elements of m real-valued Fourier transforms of initial size (…, n). The real and imaginary parts are stored as a pair of float32 arrays.

The output is a real-valued float32 variable of dimensions (m, …, n) giving the m inverse FFTs.

Parameters
  • inp – Array of float32 of size (m, …, n//2+1, 2), containing m inputs with n//2+1 non-trivial elements on the last dimension and real and imaginary parts stored as separate arrays.

  • norm ({None, 'ortho', 'no_norm'}) – Normalization of transform. Following numpy, default None normalizes only the inverse transform by n, ‘ortho’ yields the unitary transform (

    System Message: WARNING/2 (1/\sqrt n)

    latex exited with error [stderr] kpathsea: Running mktexfmt latex.fmt mktexfmt: mktexfmt is using the following fmtutil.cnf files (in precedence order): mktexfmt: mktexfmt is using the following fmtutil.cnf file for writing changes: mktexfmt: /builddir/.texlive2020/texmf-config/web2c/fmtutil.cnf mktexfmt [INFO]: writing formats under /builddir/.texlive2020/texmf-var/web2c mktexfmt [INFO]: did not find entry for byfmt=latex, skipped mktexfmt [INFO]: total formats: 0 mktexfmt [INFO]: exiting with status 0 [stdout] This is pdfTeX, Version 3.14159265-2.6-1.40.21 (TeX Live 2020) (preloaded format=latex) restricted \write18 enabled. I can't find the format file `latex.fmt'!

    forward and inverse). In addition, ‘no_norm’ leaves the transform unnormalized.

  • is_odd ({True, False}) – Set to True to get a real inverse transform output with an odd last dimension of length (N-1)*2 + 1 for an input last dimension of length N.

For example, the code below performs the real input FFT of a box function, which is a sinc function. The absolute value is plotted, since the phase oscillates due to the box function being shifted to the middle of the array. The Theano flag device=cuda{0,1...} must be used.

import numpy as np
import theano
import theano.tensor as T
from theano.gpuarray import fft

x = T.matrix('x', dtype='float32')

rfft = fft.curfft(x, norm='ortho')
f_rfft = theano.function([x], rfft)

N = 1024
box = np.zeros((1, N), dtype='float32')
box[:, N/2-10: N/2+10] = 1

out = f_rfft(box)
c_out = np.asarray(out[0, :, 0] + 1j*out[0, :, 1])
abs_out = abs(c_out)
../../_images/plot_fft.png