torch-dct icon indicating copy to clipboard operation
torch-dct copied to clipboard

module 'torch' has no attribute 'rfft'

Open aoliao12138 opened this issue 2 years ago • 5 comments

When I try to run the example, I get a runtime error

>>> X = dct.dct(x)   # DCT-II done through the last dimension
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.8/dist-packages/torch_dct/_dct.py", line 48, in dct
    Vc = torch.rfft(v, 1, onesided=False)
AttributeError: module 'torch' has no attribute 'rfft'

my torch version is '1.10.2+cu113'. Did I miss something?

Also, I want to ask what is scaled DCT? Does the input of scaled DCT need to be in range [0,1]?

Thank you very much!

aoliao12138 avatar Jun 05 '22 15:06 aoliao12138

'torch.rfft' is removed after torch1.9

yrj1409 avatar Jun 08 '22 01:06 yrj1409

if you can read Chinese, here is the solution: https://aitechtogether.com/article/37264.html

CollinWeightman avatar Jun 13 '22 06:06 CollinWeightman

Found the solution hidden in another post: https://github.com/zh217/torch-dct/issues/15#issuecomment-851439294

To migrate the DCT code to the newest pytorch version, you only have to change two lines; one in dct, one in idct.

dct:

#Vc = torch.rfft(v, 1, onesided=False)           # comment this line
Vc = torch.view_as_real(torch.fft.fft(v, dim=1))  # add this line

idct:

# v = torch.irfft(V, 1, onesided=False)                             # comment this line
v= torch.fft.irfft(torch.view_as_complex(V), n=V.shape[1], dim=1)   # add this line

Took me wayyy too long to figure out. Hope it helps you!

MaloShady avatar Jun 22 '22 13:06 MaloShady

Thanks for the solution @MaloShady . But I have a question. In dct1(): return torch.rfft(torch.cat([x, x.flip([1])[:, 1:-1]], dim=1), 1)[:, :, 0].view(*x_shape). I think new torch use fft to replace rfft, so we need change torch.rfft to torch.fft.rfft. If we don't use the dct1() and its inverse, there will be no bug report for this one.

RobinHistory avatar Nov 02 '22 11:11 RobinHistory

This can be closed now. Fixed in PR https://github.com/zh217/torch-dct/pull/24

jonnor avatar Nov 07 '22 21:11 jonnor