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
FFTs are used in a variety of fields for everything from data compression to spectral analysis. Using the Cooley-Tukey FFT it should be possible to used a chunked algorithm to do the computation since the algorithm is already inherently chunked*. Both FFT and inverse FFT support would be needed for the most common use cases.
*(Note: Would need a good way to handle arrays with length that is a prime number and a good way to specify a minimum sized chunk to use)
FFT would be doable if the direction of FFT is not chunked (this might be the common case). If it is chunked however then we might find this a bit hard because FFT is a fairly non-local algorithm. I would be quite happy to learn otherwise if you can find a blocked algorithm. I'm not an expert here and would love to be proven incorrect.
On the other hand, something like the discrete wavelet transform would be very doable.
Out of curiousity are you more interested in the multi-core aspects of dasks for fast FFTs or the out-of-core nature? Do you need to block within the direction of transformation or are you in the "stack of images" case?
@mrocklin Keep in mind that my use case is only one of many for FFTs. It would be best to make this functionality as general as possible.
My specific use case is usually performing lots of FFTs on lots of relatively small data sets, usually on the scale of (2000, 1) to (100000, 1) shaped arrays. For that upper end of 100000 points it would be useful to have some splitting over cores for a single FFT, but on the lower end there wouldn't be much gained (if anything) from parallelization.
One of my most common use cases would be a graph similar to
Where f1, f2, and f3 are simple functions that don't really need multicore (mostly array slicing and a small amount of arithmetic). This could definitely benefit from some parallelization, especially considering I might call this function several dozen times in one run of my software. It's definitely not the biggest data set out there, but we're still limited to one core unless we want to mangle the code with a lot of multiprocessing logic.
The Cooley-Tukey FFT algorithm is really a divide, compute, and re-combine operation. It depends on how well you can factor the length of the array, so it works best for powers of 2 length arrays and works worst for a prime number of points (I've actually run into that problem in the past with numpy's FFT, it becomes the world's slowest prime number finder). I don't know how well dask can handle differently sized chunks, but it is essentially a chunk-then-combine algorithm.
@bheklilr if you are not already using the MKL linked version of NumPy that should give you a significant speed boost. Also I think scipy.fftpack.fft is faster than numpy's default.
In Cooley-Tukey you "chunk" an FFT of n samples into an multiple FFTs of n1, n2, n2, ... samples. Where these ns are the prime factors of the original n. These chunks can be computed in parallel.
The first problem I notice, is that these are not "chunks" in the dask array sense, they are not contiguous, but cyclic.
A few use cases I can think of:
The input data fits comfortably in memory (considering an FFT operation will make a few copies). I'm going to assume that NumPy/SciPy will beat us here.
Data does not fit in memory. Having the input data be a dask array is probably bad because each CT chunk may touch many dask.array chunks. So we would probably have to compute the whole dask.array before we FFT it. So having the data on disk would probably be a better application for dask. But we would still be doing non-contiguous reads which would probably hurt. The algorithm would look like:
We first compute CT chunking (prime factors in each dimension).
then grab the chunks we want with indexing, and FFT each one with np.fft. This can be parallized.
Write the data to disk? This can also be parallelized. Having this output a dask array seems problematic since each chunk is so non-contiguous
The thing I notice here is that this does not seem like a good fit for dask.array. But rather a custom dask graph. Like @bheklilr has, but also considering data size.
Jake Vanderplas has a nice blog post about Cooley-Tukey here. It implements Cooley-Tookey in NumPy but does not handle cases where the input data is not a power of 2.
Just in case someone else encounters this: from what I could find, the problem with handling transposes for very large files without explicitly knowing about chunk sizes, etc., is ones that has been solved (see [1], written in the context of cache misses, but it is all the same principle). For FTs in particular, [1] notes it has been implemented in the FFTW library.
I haven't looked into it. My first guess is that it would be easy if you don't want to chunk along the transformed dimensions, but hard if you want to parallelize the individual calls to fft2.
If you're in the first case then map_blocks should suffice. If you're in the second case then this would probably require looking into some research papers. I definitely have no immediate plans to work on this but would welcome efforts by others.
My first guess is that it would be easy if you don't want to chunk along the transformed dimensions...
If the chunk size is one frame in an image sequence, this is actually a surprisingly common use case. Particularly when used for rigid body image registration. Also FFTs are generally a nice way to speed up convolutions. So I think this would actually come up quite a bit and be pretty handy.
FFTW is a very powerful FFT implementation. However there are other ones some are open source like FFTPACK (used by NumPy and SciPy with some modifications), others are closed source like the MKL implementation, others work on different hardware (e.g. GPUs) like clFFT and cuFFT.
I'm not aware of FFTW having out-of-core support. AFAIK it operates all in memory. Though it does have multicore support. Not sure if this is what you are meaning.
There is a pyFFTW implementation and it has a higher level API that matches NumPy or SciPy very closely. This is a good option for using FFTW. However I should note I'm biased as I'm associated with that project.
IANAL. That said, the reason NumPy does not use FFTW is a licensing issue. NumPy is under the BSD 3-Clause license and FFTW is under GPL 2 (with some alternative license for a fee). As a consequence, combining NumPy with FFTW would force NumPy to be placed under GPL 2. Since the NumPy developers do not want to license NumPy this way, they have opted not to include FFTW.
Given that pyFFTW is available and easy to use as a drop in replacement for NumPy, I don't think that this causes much of a problem. Also Dask supplies a wrapper function which can be used trivially to wrap pyFFTW and use it with Dask. Also there are plans to provide a Dask interface in pyFFTW, which should make this already trivial wrapping step unnecessary. In short using Dask with NumPy's FFT library or pyFFTW's should be straightforward.
In any event, one can use Dask to perform FFTs on larger than memory data as long as the axis or axes along which the FFT is applied can fit in memory.
Activity
mrocklin commentedon Jul 8, 2015
FFT would be doable if the direction of FFT is not chunked (this might be the common case). If it is chunked however then we might find this a bit hard because FFT is a fairly non-local algorithm. I would be quite happy to learn otherwise if you can find a blocked algorithm. I'm not an expert here and would love to be proven incorrect.
On the other hand, something like the discrete wavelet transform would be very doable.
Out of curiousity are you more interested in the multi-core aspects of dasks for fast FFTs or the out-of-core nature? Do you need to block within the direction of transformation or are you in the "stack of images" case?
agramfort commentedon Jul 9, 2015
bheklilr commentedon Jul 9, 2015
@mrocklin Keep in mind that my use case is only one of many for FFTs. It would be best to make this functionality as general as possible.
My specific use case is usually performing lots of FFTs on lots of relatively small data sets, usually on the scale of (2000, 1) to (100000, 1) shaped arrays. For that upper end of 100000 points it would be useful to have some splitting over cores for a single FFT, but on the lower end there wouldn't be much gained (if anything) from parallelization.
One of my most common use cases would be a graph similar to
Where
f1,f2, andf3are simple functions that don't really need multicore (mostly array slicing and a small amount of arithmetic). This could definitely benefit from some parallelization, especially considering I might call this function several dozen times in one run of my software. It's definitely not the biggest data set out there, but we're still limited to one core unless we want to mangle the code with a lot of multiprocessing logic.The Cooley-Tukey FFT algorithm is really a divide, compute, and re-combine operation. It depends on how well you can factor the length of the array, so it works best for powers of 2 length arrays and works worst for a prime number of points (I've actually run into that problem in the past with numpy's FFT, it becomes the world's slowest prime number finder). I don't know how well dask can handle differently sized chunks, but it is essentially a chunk-then-combine algorithm.
mrocklin commentedon Jul 9, 2015
I'll take a closer look at Cooley Tukey, probably sometime next week.
Regarding your particular computation it looks like you could just run that through a scheduler directly and bypass dask.array entirely.
cowlicks commentedon Jul 14, 2015
@bheklilr if you are not already using the MKL linked version of NumPy that should give you a significant speed boost. Also I think
scipy.fftpack.fftis faster than numpy's default.cowlicks commentedon Jul 18, 2015
In Cooley-Tukey you "chunk" an FFT of
nsamples into an multiple FFTs ofn1,n2,n2, ... samples. Where thesens are the prime factors of the originaln. These chunks can be computed in parallel.The first problem I notice, is that these are not "chunks" in the dask array sense, they are not contiguous, but cyclic.
A few use cases I can think of:
The thing I notice here is that this does not seem like a good fit for dask.array. But rather a custom dask graph. Like @bheklilr has, but also considering data size.
Jake Vanderplas has a nice blog post about Cooley-Tukey here. It implements Cooley-Tookey in NumPy but does not handle cases where the input data is not a power of 2.
Merge pull request #522 from cowlicks/gh-405
mhvk commentedon May 22, 2016
Just in case someone else encounters this: from what I could find, the problem with handling transposes for very large files without explicitly knowing about chunk sizes, etc., is ones that has been solved (see [1], written in the context of cache misses, but it is all the same principle). For FTs in particular, [1] notes it has been implemented in the FFTW library.
[1] http://ieeexplore.ieee.org/xpl/login.jsp?tp=&arnumber=814600&url=http%3A%2F%2Fieeexplore.ieee.org%2Fiel5%2F6604%2F17631%2F00814600.pdf%3Farnumber%3D814600
anton-malakhov commentedon Sep 15, 2016
is it possible to support fft2 functions as well?
mrocklin commentedon Sep 15, 2016
I haven't looked into it. My first guess is that it would be easy if you don't want to chunk along the transformed dimensions, but hard if you want to parallelize the individual calls to fft2.
If you're in the first case then
map_blocksshould suffice. If you're in the second case then this would probably require looking into some research papers. I definitely have no immediate plans to work on this but would welcome efforts by others.jakirkham commentedon Nov 19, 2016
If the chunk size is one frame in an image sequence, this is actually a surprisingly common use case. Particularly when used for rigid body image registration. Also FFTs are generally a nice way to speed up convolutions. So I think this would actually come up quite a bit and be pretty handy.
mrocklin commentedon Nov 20, 2016
Adding fft2 functions makes sense to me. We would want to raise informative errors in non-supported cases.
jakirkham commentedon Feb 13, 2017
Opened issue ( #1980 ) to discuss making such a change.
switham commentedon Sep 1, 2017
Somewhere I heard fftw was the fft package. Fftw does have out-of-core fft. I notice there is an active pyFFTW package... Why doesn't numpy use fftw?
jakirkham commentedon Sep 3, 2017
FFTW is a very powerful FFT implementation. However there are other ones some are open source like FFTPACK (used by NumPy and SciPy with some modifications), others are closed source like the MKL implementation, others work on different hardware (e.g. GPUs) like clFFT and cuFFT.
I'm not aware of FFTW having out-of-core support. AFAIK it operates all in memory. Though it does have multicore support. Not sure if this is what you are meaning.
There is a pyFFTW implementation and it has a higher level API that matches NumPy or SciPy very closely. This is a good option for using FFTW. However I should note I'm biased as I'm associated with that project.
IANAL. That said, the reason NumPy does not use FFTW is a licensing issue. NumPy is under the BSD 3-Clause license and FFTW is under GPL 2 (with some alternative license for a fee). As a consequence, combining NumPy with FFTW would force NumPy to be placed under GPL 2. Since the NumPy developers do not want to license NumPy this way, they have opted not to include FFTW.
Given that pyFFTW is available and easy to use as a drop in replacement for NumPy, I don't think that this causes much of a problem. Also Dask supplies a wrapper function which can be used trivially to wrap pyFFTW and use it with Dask. Also there are plans to provide a Dask interface in pyFFTW, which should make this already trivial wrapping step unnecessary. In short using Dask with NumPy's FFT library or pyFFTW's should be straightforward.
switham commentedon Sep 4, 2017
@jakirkham thanks for clarifying. Yes I meant out-of-memory as in on-disk. Maybe I'm just too shy about the size of my NUMPY arrays.
jakirkham commentedon Sep 4, 2017
Sorry I don't think I understand the question.
In any event, one can use Dask to perform FFTs on larger than memory data as long as the axis or axes along which the FFT is applied can fit in memory.
Initial implementation of _rolling.Rolling (dask#405)