Skip to content

lazy blocking for final results #3938

Closed

Description

@hmaarrfk

I was going over an example of yours where you and the scikit-image team showed that it was hard to get good performance from scikit-image algorithms and dask.

I decided to jump down the rabbit hole and found that there were probably 2 places that could probably be optimized:

  1. Serialization/serialization of numpy arrays. You seem to have identified this one, and might come in later versions of numpy.
  2. The final blocking of the result.

I think it many cases, making the final result a C_continguous numpy array is probably not necessary.

For the example here: (i7-7700HQ, 16GB of RAM, 4 cores + hyperthreading)
https://gist.github.com/hmaarrfk/ea6d364c2ce097f249b5c23a93214f31

Passing the arrays 4 arrays 50MB each, of around a threaded scheduler takes a few ms. while blocking the final result (200 MB) in a contiguous array takes 100ms+.

Have you considered returned a result as a blocked matrix that is not c-contiguous? Is this possible at the moment? I don't think I saw the option in the docs, but maybe I'm blind.

I don't know if getting rid of blocking might help map_overlaped as well.

CC: @jni

Activity

  1. mrocklin commented on Sep 4, 2018

    @mrocklin
    Member

    Have you considered returned a result as a blocked matrix that is not c-contiguous? Is this possible at the moment? I don't think I saw the option in the docs, but maybe I'm blind.

    Did you have a target data structure in mind? The way I would represent such a blocked matrix in Python is probably with a dask array. You might consider looking at timings that use dask.persist, which returns a dask array rather than dask.compute, which returns a numpy array

  2. mrocklin commented on Sep 4, 2018

    @mrocklin
    Member

    Serialization shouldn't be a problem if you use a threaded scheduler

  3. hmaarrfk commented on Sep 4, 2018

    @hmaarrfk
    ContributorAuthor

    Thanks you for the info. Persist does mostly what is needed for the question I had. I guess I wrote off persist when I first read through the docs a few months back.

    I was talking with @jni about parallelizing the computation of a Gaussian filter using Dask and the question of blocked operations came up. It made use of map_overlap which seems to a convenience function that calls overlap, map_blocks and trim.

    Going a step further, it seems that trim returns blocks that are C-contiguous.

    • Any chance of returning memory views of the original arrays? (See benchmark below)
    • Any chance of having dask arrays with more transparent overlaps, ie. they would hold extra memory for the overlapping regions, but a call to np.asarray would omit copies of the overlapped parts?
    from dask import array as da
    import numpy as np
    
    from skimage.filters import gaussian as gaussian_filter
    
    
    shape=(150, 1024, 1024)
    chunks=(-1, 512, 512)
    dtype='float32'
    z = da.zeros(shape=shape, chunks=chunks, dtype=dtype)
    # This takes time 407 ms
    %time _ = z.compute()
    # This is fast. (24 ms) Thank you for hint!
    %time _ = z.persist()
    z_overlaped = da.overlap.overlap(z, 10, boundary='none')
    # This is slow (450 ms)
    %time _ = z_overlaped.compute()
    # This is kinda slow (160 ms) but I understand why a memory copy is necessary.
    %time z_overalaped_persisted = z_overlaped.persist()
    # convenience function, inverse of da.overlap.overlap
    # logic taken from map_overlap
    def trim(x, depth, boundary=None):
        from dask.array.overlap import coerce_boundary, coerce_depth, trim_internal, add_dummy_padding
        boundary2 = coerce_boundary(x.ndim, boundary)
        depth2 = coerce_depth(x.ndim, depth)
        
        x2 = add_dummy_padding(x, depth2, boundary2)
        return trim_internal(x2, depth2)
    # This takes 775 ms
    %time trim(z_overalaped_persisted, 10, boundary='none').compute()
    # This takes 488 ms, I wish it returned a memory view so as to return in 1ms.
    %time trim(z_overalaped_persisted, 10, boundary='none').persist(scheduler='threads')
  4. mrocklin commented on Sep 4, 2018

    @mrocklin
    Member
  5. hmaarrfk commented on Sep 4, 2018

    @hmaarrfk
    ContributorAuthor

    @mrocklin thank you for replying so quickly. Benchmarking on the plane kept me busy.

    I think I need to rerun benchmarks with a hacked up version of dask that doesn't hash every call to from_array. See Issue #3946

  6. hmaarrfk commented on Sep 5, 2018

    @hmaarrfk
    ContributorAuthor

    @mrocklin I'm mostly convinced that it is the issue outlined #3947

    from dask import array as da
    
    from dask.array.overlap import (coerce_boundary, 
                                    coerce_depth, 
                                    trim_internal, 
                                    add_dummy_padding)
    
    shape=(50, 1024, 1024)
    chunks=(-1, 512, 512)
    dtype='float32'
    
    def trim(x, depth, boundary=None):
        boundary2 = coerce_boundary(x.ndim, boundary)
        depth2 = coerce_depth(x.ndim, depth)
        
        x2 = add_dummy_padding(x, depth2, boundary2)
        return trim_internal(x2, depth2)
    
    
    a00 = da.ones(shape=shape, chunks=-1, dtype=dtype).persist()
    a01 = da.ones(shape=shape, chunks=-1, dtype=dtype).persist()
    a10 = da.ones(shape=shape, chunks=-1, dtype=dtype).persist()
    a11 = da.ones(shape=shape, chunks=-1, dtype=dtype).persist()
    
    b = da.block([[a00, a01], [a10, a11]]).persist()
    c = da.block([[a00[10:-10, 10:-10], a01[10:-10, 10:-10]], [a10[10:-10, 10:-10], a11[10:-10, 10:-10]]]).persist()
    d = trim(b, 10, boundary='nearest').persist()
    %timeit b = da.block([[a00, a01], [a10, a11]]).persist()
    %timeit c = da.block([[a00[10:-10, 10:-10], a01[10:-10, 10:-10]], [a10[10:-10, 10:-10], a11[10:-10, 10:-10]]]).persist()
    %timeit d = trim(b, 10, boundary='nearest').persist()

    returns

    2.25 ms ± 47.9 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
    3.83 ms ± 46.4 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
    1.72 ms ± 24.7 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
    

    meaning that the cost of trim_internal approximately the same as just calling persist on a dask array, but the call to add_dummy_padding should be removed, instead the slicing logic should probably be moved to trim_internal.

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