Skip to content

performance of map_overlap with boundary='none' is hindered by add_dummy_padding #3947

Closed

Description

@hmaarrfk

map_overlap provides a few boundary conditions. The performance of the 'none' boundary condition is seriously affected when the array is trimmed since dummy_padding is added to the array which forces a whole copy of the array.

To illustrate the problem, I define a convenience function that implements the trim logic at the end of map_overlap.

Benchmark:

from dask import array as da
from dask.array.overlap import (coerce_boundary, 
                                coerce_depth, 
                                trim_internal, 
                                add_dummy_padding)
shape=(150, 1024, 1024)
chunks=(-1, 512, 512)
dtype='float32'
z = da.zeros(shape=shape, chunks=chunks, dtype=dtype).persist()

# convenience function, inverse of da.overlap.overlap
# logic taken from map_overlap
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)

# Call once to force python to load all the necessary modules
_ = trim(z, 10, boundary='nearest').persist()
%time _ = trim(z, 10, boundary='none').persist()
%time _ = trim(z, 10, boundary='nearest').persist()
%time _ = trim(z, 10, boundary='none').persist()
%time _ = trim(z, 10, boundary='nearest').persist()
%time _ = trim(z, 10, boundary='none').persist()
%time _ = trim(z, 10, boundary='nearest').persist()

Results:

  • 'nearest' finishes in 30 ms
  • 'none' finishes in 500 ms

Computation graph

Trimming with 'nearest'

trim(z, 10, boundary='nearest').visualize()

image

Trimming with 'none'

trim(z, 10, boundary='none').visualize()

mydask

This is very likely the issue I was seeing in Issue #3938

cc: @jni

Activity

  1. mrocklin commented on Sep 5, 2018

    @mrocklin
    Member

    boundary condition is seriously affected when the array is trimmed since dummy_padding is added to the array which forces a whole copy of the array.

    It looks like it only affects the outer boundary chunks, but I suspect that in your case that this is the entire array.

    Do you have a recommendation on how to improve the implementation?

  2. hmaarrfk commented on Sep 5, 2018

    @hmaarrfk
    ContributorAuthor

    I agree that in my case it is "all boundary", but you may only want to overlap in a specific dimensions (a use case that was envisioned in map_overlap) in which case the trim functionality, that you would assume should be a no-op in those dimensions, is actually coming at a big cost. this is probably not true.

    I think the logic of add_dummy_padding can be moved in chunk.trim (or whatever function actually returns the sliced array), to allow the slicing to be aware of the boundary condition. map_blocks can pass block_info to the trimming function so that it becomes aware what block is being trimmed.

    the slicing can then check if

    1. If it is located on boundary.
    2. If that boundary was originally passed as 'none'.

    Then it can choose to slice the axis as :, overlap:, overlap:-overlap or :-overlap accordingly.

  3. added a commit that references this issue on Sep 5, 2018
    df32d4a
  4. mrocklin commented on Sep 5, 2018

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

    @hmaarrfk
    ContributorAuthor

    PR #3950 implemented the "solution" where the logic is moved down to chunk.trim but I'm not convinced that it is the best solution. I mostly wrote it so I could benchmark my own work without incurring large memory copies and to learn about block_info.

    My specific issue with PR #3950 is that chunk.trim now needs knowledge of options that are coming from higher level code. This makes the api of chunk.trim very cluttered.

    I think a better solution would be to somehow:

    1. Allow chunk.trim to handle asymmetric trimming
    2. Call chunk.trim with the appropriate trimming parameters on each axis.

    For now, chunk.trim supports different trimmings on a per axis level, but doesn't support asymmetric trimming.

    I can probably help implement this, but I would need guidance on the correct choice for the API of chunk.trim:

    Maybe:
    axes could take:

    1. An iterable of tuples. The inner tuple would be trim_front, trim_back. trim_back would have to be a positive number or None
    2. If the iterable returns a non-iterable, then it assumes symmetric slicing.
    3. If axis is a single value, symmetric trimming on all axes is assumed.
  6. mrocklin commented on Sep 5, 2018

    @mrocklin
    Member

    I think that it doesn't matter that much. We can make some new function like chunk.trim that is only used in this context. This inner function doesn't have to be public API.

  7. added 2 commits that reference this issue on Sep 8, 2018
    6fa092d
    80629e0
  8. added a commit that references this issue on Sep 18, 2018
    885bbb2
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