Skip to content

Implement numpy.fft functionality #405

Closed

Description

@bheklilr

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)

Activity

  1. mrocklin commented on Jul 8, 2015

    @mrocklin
    Member

    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?

  2. agramfort commented on Jul 9, 2015

    @agramfort
  3. bheklilr commented on Jul 9, 2015

    @bheklilr
    Author

    @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

    def build_graph(x, y):
        return {
            'x': x,
            'y': y,
    
            'x1': (f1, 'x'),
            'x2': (f2, 'x'),
            'X1': (np.fft.rfft, 'x1'),
            'X2': (np.fft.rfft, 'x2'),
    
            'y1': (f1, 'y'),
            'y2': (f2, 'y'),
            'Y1': (np.fft.rfft, 'y1'),
            'Y2': (np.fft.rfft, 'y2'),
    
            'X': (f3, 'X1', 'X2'),
            'Y': (f3, 'Y1', 'Y2'),
    
            'output': (operator.div, 'X', 'Y')
        }
    

    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.

  4. mrocklin commented on Jul 9, 2015

    @mrocklin
    Member

    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.

    from dask.threaded import get
    dsk = build_graph(x, y)
    output = get(dsk, 'output')  # executes with multiple threads
  5. cowlicks commented on Jul 14, 2015

    @cowlicks
    Member

    @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.

  6. cowlicks commented on Jul 18, 2015

    @cowlicks
    Member

    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.

  7. added a commit that references this issue on Aug 6, 2015
  8. mhvk commented on May 22, 2016

    @mhvk

    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

  9. anton-malakhov commented on Sep 15, 2016

    @anton-malakhov

    is it possible to support fft2 functions as well?

  10. mrocklin commented on Sep 15, 2016

    @mrocklin
    Member

    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.

  11. jakirkham commented on Nov 19, 2016

    @jakirkham
    Member

    is it possible to support fft2 functions as well?

    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.

  12. mrocklin commented on Nov 20, 2016

    @mrocklin
    Member

    Adding fft2 functions makes sense to me. We would want to raise informative errors in non-supported cases.

  13. jakirkham commented on Feb 13, 2017

    @jakirkham
    Member

    Opened issue ( #1980 ) to discuss making such a change.

  14. switham commented on Sep 1, 2017

    @switham

    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?

  15. jakirkham commented on Sep 3, 2017

    @jakirkham
    Member

    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.

  16. switham commented on Sep 4, 2017

    @switham

    @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.

  17. jakirkham commented on Sep 4, 2017

    @jakirkham
    Member

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions