You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Dec 29, 2025. It is now read-only.
Repository navigation
This repository was archived by the owner on Dec 29, 2025. It is now read-only.
Work with pre-allocated arrays and mul! when doing FFT #14
In the docs of AbstractFFTs.jl, it says that it is preferrable to apply the FFT plans to pre-allocated arrays using mul! from the LinearAlgebra package.
As this step is taken for every pass in the network, this should be well worthwile investigating.
Note: as explained here in section 3.3, mul! should transfer to the GPU effortlessly since mul! either calls BLAS or cuBLAS depending on the input types.
using LinearAlgebra, CUDA
mul!(CUDA.zeros(Float32,2,2),
cu(rand(Float32,2,2)),
cu(rand(Float32,2,2)))
2×2 CuArray{Float32, 2}:0.6092151.27570.8137150.897835mul!(zeros(Float32,2,2),
rand(Float32,2,2),
rand(Float32,2,2))
2×2 Matrix{Float32}:0.7533830.2632151.168420.309142
Related to #11. Resolving the Diff problem likely takes care of both issues, maybe even #12.
Problem seems to be that Zygote needs to be supplied with custom adjoints since FFTW.jl calls the external FFTW library - which Zygote has no insight to and thus doesn't know what to do. But, as explained here, AD potentially isn't even necessary.
A custom implementation of NNlib's batched_mul! is given in 096bab9. It is however a huge pain to get the batching working efficiently with CUDA.
Maybe this should be left aside for now. Doing regular FFT comes with the additional advantage that AD works well.
It appears that the subsequent caching of the FFT arrays doesn't happen within julia, but rather after FFTW is called via unsafe_execute! and ccall. Hence, it's probably not worthwile working around these.
For completeness sake, the same benchmarks but using a 3D Array of shape (200, 128, 100) using CUDA:
@benchmark f * n
BechmarkTools.Trial:10000 samples with 3 evaluations.
Range (min … max):7.358 μs …3.094 ms ┊ GC (min … max):0.00%…0.00%
Time (median):63.611 μs ┊ GC (median):0.00%
Time (mean ± σ):62.548 μs ±57.216 μs ┊ GC (mean ± σ):0.63%±0.84%
█
▃▂▂▂▂▁▂▂▁▁▁▂▂▁▂▁▁▁▁▁▁▁▁▁▁▂▂▂▂▁▂▁▁▂▁▂▁▂▁▂▁▂▂▂▂▁▂▁▂▂▂▁▂▂▂▂▂▅█ ▂
7.36 μs Histogram: frequency by time 64.4 μs <
Memory estimate:816 bytes, allocs estimate:22.
julia>@benchmarkmul!(pref, f, n)
BechmarkTools.Trial:10000 samples with 5 evaluations.
Range (min … max):6.135 μs …900.818 μs ┊ GC (min … max):0.00%…0.00%
Time (median):63.982 μs ┊ GC (median):0.00%
Time (mean ± σ):63.333 μs ±11.964 μs ┊ GC (mean ± σ):0.00%±0.00%
▁ ▁█ ▁
█▃▁▁▁▁▁▁▁▁▁▁▃▁▄▁▃▁▁▁▁▁▁▁▁▁▁▁▁▁▁▃▁▁▁▁▁▁▁▁▁▁▁▁▁▃▄▁▃▃▃▁▁▁▄▃▅▆██ █
6.14 μs Histogram:log(frequency) by time 64.5 μs <
Memory estimate:496 bytes, allocs estimate:14.
julia>@benchmark fi * (f * n)
BechmarkTools.Trial:10000 samples with 1 evaluations.
Range (min … max):17.151 μs …4.731 ms ┊ GC (min … max):0.00%…30.70%
Time (median):153.542 μs ┊ GC (median):0.00%
Time (mean ± σ):150.429 μs ±113.811 μs ┊ GC (mean ± σ):0.58%±0.76%
█
▃▂▂▂▂▂▁▂▁▁▁▁▁▁▁▁▁▂▁▁▂▁▁▁▁▁▂▂▁▁▁▂▂▁▁▁▁▁▁▁▁▁▂▁▂▂▁▂▂▂▂▂▂▂▂▂▁▂▂██ ▂
17.2 μs Histogram: frequency by time 155 μs <
Memory estimate:1.52 KiB, allocs estimate:44.
julia>@benchmarkmul!(prefi, fi, mul!(pref, f, n))
BechmarkTools.Trial:10000 samples with 1 evaluations.
Range (min … max):15.249 μs …225.914 μs ┊ GC (min … max):0.00%…0.00%
Time (median):153.672 μs ┊ GC (median):0.00%
Time (mean ± σ):150.568 μs ±20.788 μs ┊ GC (mean ± σ):0.00%±0.00%
▃█
▃▂▂▁▁▁▁▁▁▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▂▁▁▁▁▂▁▁▁▂▁▁▁▁▁▂▁▁▁▂▂▂▁▂▂▂▂▂▂▂▂██ ▂
15.2 μs Histogram: frequency by time 156 μs <
Memory estimate:1.38 KiB, allocs estimate:36.
julia>@benchmarkrfft(n,3)
BechmarkTools.Trial:10000 samples with 1 evaluations.
Range (min … max):81.273 μs …7.313 ms ┊ GC (min … max):0.00%…24.65%
Time (median):84.526 μs ┊ GC (median):0.00%
Time (mean ± σ):90.103 μs ±172.512 μs ┊ GC (mean ± σ):1.44%±0.75%
▁▅█▇█▇▇▆▃▃
▂▂▂▃▄▆██████████▇▆▅▃▃▃▃▃▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▁▂▁▁▂▂▂▂▂▂▂▂ ▃
81.3 μs Histogram: frequency by time 98.3 μs <
Memory estimate:1.91 KiB, allocs estimate:48.
julia>@benchmarkirfft(rfft(n,3), size(n,3), 3)
BechmarkTools.Trial:10000 samples with 1 evaluations.
Range (min … max):167.979 μs …7.797 ms ┊ GC (min … max):0.00%…23.84%
Time (median):172.844 μs ┊ GC (median):0.00%
Time (mean ± σ):184.898 μs ±259.530 μs ┊ GC (mean ± σ):1.48%±1.03%
▁██▃
▂▂▄████▅▃▃▃▃▃▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▁▂▂▁▂▂▂▁▁▂▁▁▂▂▁▁▁▂▂▁▁▁▁▂▁▁▂ ▃
168 μs Histogram: frequency by time 224 μs <
Memory estimate:4.09 KiB, allocs estimate:101.
julia>@benchmarkirfft(rfft(n,3), 100, 3)
BechmarkTools.Trial:10000 samples with 1 evaluations.
Range (min … max):168.265 μs …7.826 ms ┊ GC (min … max):0.00%…23.62%
Time (median):172.484 μs ┊ GC (median):0.00%
Time (mean ± σ):184.944 μs ±259.261 μs ┊ GC (mean ± σ):1.48%±1.03%
▃▆█▇▆▃
▁▁▂▃▅▇███████▅▃▃▂▂▂▂▂▂▃▂▂▂▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁ ▂
168 μs Histogram: frequency by time 195 μs <
Memory estimate:4.09 KiB, allocs estimate:101.
Bottom line: Speedup using pre-allocated arrays is somewhat debatable. We allocate a little less memory, similar to the CPU implementation. The largest improvement can be seen when doing the whole pipeline x -> rfft -> irfft. Here, we can slice the amount of allocated memory in third.
Activity
pzimbrod commentedon Sep 1, 2021
All in all, there's some mem allocation up for grabs and also a moderate speed improvement (~25 %).
pzimbrod commentedon Sep 1, 2021
This however breaks AD, which is absolutely necessary:
pzimbrod commentedon Sep 1, 2021
Note: as explained here in section 3.3,
mul!should transfer to the GPU effortlessly sincemul!either calls BLAS or cuBLAS depending on the input types.pzimbrod commentedon Sep 17, 2021
Related to #11. Resolving the Diff problem likely takes care of both issues, maybe even #12.
Problem seems to be that
Zygoteneeds to be supplied with custom adjoints sinceFFTW.jlcalls the external FFTW library - whichZygotehas no insight to and thus doesn't know what to do. But, as explained here, AD potentially isn't even necessary.pzimbrod commentedon Nov 5, 2021
A custom implementation of
NNlib'sbatched_mul!is given in 096bab9. It is however a huge pain to get the batching working efficiently with CUDA.Maybe this should be left aside for now. Doing regular FFT comes with the additional advantage that AD works well.
It appears that the subsequent caching of the FFT arrays doesn't happen within julia, but rather after
FFTWis called viaunsafe_execute!andccall. Hence, it's probably not worthwile working around these.pzimbrod commentedon Nov 8, 2021
For completeness sake, the same benchmarks but using a 3D Array of shape
(200, 128, 100)using CUDA:Bottom line: Speedup using pre-allocated arrays is somewhat debatable. We allocate a little less memory, similar to the CPU implementation. The largest improvement can be seen when doing the whole pipeline x -> rfft -> irfft. Here, we can slice the amount of allocated memory in third.