ejmahler / RustFFT

RustFFT is a high-performance FFT library written in pure Rust.
Apache License 2.0
706 stars 49 forks source link

Difference between RustFFT vs PyTorch implementation #142

Closed phudtran closed 3 months ago

phudtran commented 3 months ago

Hi,

In my attempt to implement STFT and ISTFT, I noticed a difference between the output of RustFFT vs PyTorch.

The code to run the test can be found here. https://github.com/phudtran/rustfft_vs_torch

The results for convenience.

Testing with signal size: 1024
Average Rust roundtrip difference: 1.0789151622353326e-16
Average PyTorch roundtrip difference: 1.1709378294661755e-16
Average roundtrip difference (Rust FFT -> PyTorch IFFT): 0.004872985718332864
Average roundtrip difference (PyTorch FFT -> Rust IFFT): 4.989937375572853

Testing with signal size: 4096
Average Rust roundtrip difference: 1.194176726956002e-16
Average PyTorch roundtrip difference: 1.3139718159156325e-16
Average roundtrip difference (Rust FFT -> PyTorch IFFT): 0.0050154637837731
Average roundtrip difference (PyTorch FFT -> Rust IFFT): 20.543339658334617

Testing with signal size: 16384
Average Rust roundtrip difference: 1.2692186583368034e-16
Average PyTorch roundtrip difference: 1.4479730753893462e-16
Average roundtrip difference (Rust FFT -> PyTorch IFFT): 0.0050241207559530736
Average roundtrip difference (PyTorch FFT -> Rust IFFT): 82.31519446553516
phudtran commented 3 months ago

Some more background, my application is running the spectrogram through an ONNX model (trained with PyTorch) then reconstructing the signal with the ISTFT. Since RustFFT's IFFT is not equivalent to PyTorch's IFFT, the signal is not reconstructed correctly.

phudtran commented 3 months ago

Reopening after fixing tests. Feel free to close if this is not planned.

phudtran commented 3 months ago

I didn't normalize after the ISTFT. Didn't match PyTorch's behavior before. Fixed now.

Testing with signal size: 1024
Average Rust roundtrip difference: 1.0426809562242775e-16
Average PyTorch roundtrip difference: 1.0637285391174207e-16
Average roundtrip difference (Rust FFT -> PyTorch IFFT): 1.0524879883908826e-16
Average roundtrip difference (PyTorch FFT -> Rust IFFT): 1.0640585855053184e-16

Testing with signal size: 4096
Average Rust roundtrip difference: 1.1929012237158682e-16
Average PyTorch roundtrip difference: 1.2068799818214623e-16
Average roundtrip difference (Rust FFT -> PyTorch IFFT): 1.2049052833236164e-16
Average roundtrip difference (PyTorch FFT -> Rust IFFT): 1.2169140730809795e-16

Testing with signal size: 16384
Average Rust roundtrip difference: 1.2780922228069706e-16
Average PyTorch roundtrip difference: 1.3177517016782907e-16
Average roundtrip difference (Rust FFT -> PyTorch IFFT): 1.3015744457352966e-16
Average roundtrip difference (PyTorch FFT -> Rust IFFT): 1.3157975132891694e-16