The time complexity of fft function

username: sh0416
cooked: <p>Hello,</p>
<p>I am curious about the fft built-in function.</p>
<p>Is this function calculate the DFT coefficient directly?</p>
<p>What is the time complexity of fft function if we do not use GPU?</p>
<p>Is this function use divide-and-conquer algorithm for calculating fft?</p>
<p>Thanks,</p>

username: KFrank
cooked: <p>Hi 성현!</p>
<aside class="quote no-group quote-modified" data-username="sh0416" data-post="1" data-topic="47945" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/sh0416/48/38708_2.png" class="avatar"> sh0416:</div>
<blockquote>
<p>…<br>
What is the time complexity of fft function if we do not use GPU?</p>
<p>Is this function use divide-and-conquer algorithm for calculating fft?</p>
</blockquote>
</aside>
<p>I haven’t actually looked at the code, but the time complexity<br>
should be <code>n log n</code>.</p>
<p>After all, the function in question is <code>torch.fft</code>, where “fft”<br>
stands for “fast Fourier transform,” which uses what you call<br>
the “divide-and-conquer” algorithm and runs in <code>n log n</code>.</p>
<p>It would be false advertising if <code>torch.fft</code> instead used the<br>
sft* algorithm.</p>
<p>*) “Slow Fourier transform”</p>
<p>Best regards.</p>
<p>K. Frank</p>

username: sh0416
cooked: <p>So much thanks for reply <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>
<p>I have one more question.</p>
<p>Can we get the gradient of fft function with respect to input?</p>
<p>I think it is possible, but I want to make sure things.</p>

username: KFrank
cooked: <p>Hello 성현!</p>
<aside class="quote no-group quote-modified" data-username="sh0416" data-post="3" data-topic="47945" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/sh0416/48/38708_2.png" class="avatar"> sh0416:</div>
<blockquote>
<p>…<br>
Can we get the gradient of fft function with respect to input?</p>
<p>I think it is possible, but I want to make sure things.</p>
</blockquote>
</aside>
<p>It looks like the gradient is supported.  Try:</p>
<pre><code class="lang-plaintext">&gt;&gt;&gt; import torch
&gt;&gt;&gt; t = torch.randn (1, 8, 2)
&gt;&gt;&gt; t.requires_grad = True
&gt;&gt;&gt; torch.fft (t, 1)
tensor([[[-2.7232,  3.8741],
         [-2.9743, -2.1404],
         [ 1.1234,  4.4275],
         [ 1.7661,  1.6113],
         [-3.6401,  3.6872],
         [ 0.0582, -3.4854],
         [-1.6034,  0.3976],
         [ 0.3413,  0.8839]]], grad_fn=&lt;FftWithSizeBackward&gt;)
</code></pre>
<p>(I haven’t actually tried this, but I would imagine that<br>
<code>FftWithSizeBackward</code> works correctly.)</p>
<p>Best.</p>
<p>K. Frank</p>

username: SimonW
cooked: <p>Yes, backward of fft is implemented (and for rfft, ifft, irfft as well).</p>

username: SimonW
cooked: <p>About the speed, on CUDA, the cufft package is used, on CPU, MKL is used. They are both highly-optimized libraries, so it should be very fast.</p>