Skip to content

dask.array: Don't use dummy expansion for trim_internal in overlapped - #3964

Merged
mrocklin merged 2 commits into
dask:masterfrom
hmaarrfk:trim_no_expand_redux
Sep 18, 2018
Merged

mrocklin merged 2 commits into
dask:masterfrom
hmaarrfk:trim_no_expand_redux

Conversation

@hmaarrfk

@hmaarrfk hmaarrfk commented Sep 8, 2018

Copy link
Copy Markdown
Contributor

… computations

Fixes #3947

  • Tests added / passed
  • Passes flake8 dask

@hmaarrfk hmaarrfk mentioned this pull request Sep 8, 2018
2 tasks
@hmaarrfk

hmaarrfk commented Sep 8, 2018

Copy link
Copy Markdown
Contributor Author

Note to self. I guess i need to specify the partial arguments as kwargs for python 2.7 to take them in.

@mrocklin

mrocklin commented Sep 9, 2018

Copy link
Copy Markdown
Member

Except for the Python 2 errors things here seems sensible to me. @jakirkham might find this PR interesting.

@hmaarrfk if I recall correctly you have a small benchmark where this showed significant speed improvement. Would you might including a small example as a comment in this PR? We might also include the speed difference in the commit message.

@hmaarrfk

hmaarrfk commented Sep 9, 2018

Copy link
Copy Markdown
Contributor Author

2.7 bug is fixed in #3966

@hmaarrfk

hmaarrfk commented Sep 9, 2018

Copy link
Copy Markdown
Contributor Author

Benchmark

Adapted from #3947 (comment)

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, 
                                trim_overlap,
                                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 (before this patch)
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_overlap(z, 10, boundary='none').persist()
%time _ = trim_overlap(z, 10, boundary='nearest').persist()

%time _ = trim(z, 10, boundary='none').persist()
%time _ = trim(z, 10, boundary='nearest').persist()
%time _ = trim_overlap(z, 10, boundary='none').persist()
%time _ = trim_overlap(z, 10, boundary='nearest').persist()

Results:

  • 'nearest' finishes in 30 ms
  • 'none' finishes in 500 ms
  • new trim_overlap boundary='none' finishes in 2ms
  • new trim_overlap boundary='nearest' finishes in 2ms

Computation graph

Trimming with 'nearest'

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

image

Trimming with 'none'

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

mydask

cc: @jni

Much to my surprise, this speeds up boundary='nearest' as well.

@hmaarrfk

hmaarrfk commented Sep 9, 2018

Copy link
Copy Markdown
Contributor Author

Also cc'ing @emmanuelle

@hmaarrfk
hmaarrfk force-pushed the trim_no_expand_redux branch from 950e328 to 80629e0 Compare September 9, 2018 15:43
@hmaarrfk

hmaarrfk commented Sep 9, 2018

Copy link
Copy Markdown
Contributor Author

oh i didnt see that you had merged into my branch i guess i rebased and forced pushed...

@hmaarrfk

hmaarrfk commented Sep 9, 2018

Copy link
Copy Markdown
Contributor Author

@mrocklin do you have any insight as to why this sped up normal bdy conditions too? 30ms -> 2ms

@mrocklin

mrocklin commented Sep 9, 2018 via email

Copy link
Copy Markdown
Member

@hmaarrfk

hmaarrfk commented Sep 9, 2018

Copy link
Copy Markdown
Contributor Author

What do you want to do with the function add_dummy_padding. Here is the docstring from that function

    Pads an array which has 'none' as the boundary type.
    Used to simplify trimming arrays which use 'none'.

    >>> import dask.array as da
    >>> x = da.arange(6, chunks=3)
    >>> add_dummy_padding(x, {0: 1}, {0: 'none'}).compute()  # doctest: +NORMALIZE_WHITESPACE
    array([..., 0, 1, 2, 3, 4, 5, ...])

Had you done any changes to it before I erased yours?

Also, the results of that benchmark still baffle me. I thought that it was serialization thing where because I defined the function from the REPL dask was having a hard time making the bytecode for it. Unfortunately, even if I move the old and new code into the benchmark script, I get the same results :S.

I even modified it with:

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)
    if boundary == 'nearest':
        print('got here')
        assert x is x2
    return trim_internal(x2, depth2)

and in fact, nearest just passes x2 as x, so I'm really confused.

@hmaarrfk

hmaarrfk commented Sep 9, 2018

Copy link
Copy Markdown
Contributor Author

I wouldn't trust the 30ms -> 2ms speedup. Trying a few different combination out, I think z.persist doesn't do what I thought it did. I thought it moved the memory to the workers, but guess it doesn't.

There is definitely some memory issues (page faulting), that are causing the 30ms number to appear.

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

z = da.from_array(np.random.random(size=shape).astype(dtype), chunks=chunks, name=False).persist()

# convenience function, inverse of da.overlap.overlap
# logic taken from map_overlap (before this patch)
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_overlap(z, 10, boundary='nearest').persist()
%time _ = trim(z, 10, boundary='nearest').persist()
%time _ = trim_overlap(z, 10, boundary='none').persist()
# Don't put this benchmark first, as it will trash the RAM
%time _ = trim(z, 10, boundary='none').persist()

%time _ = trim(z, 10, boundary='nearest').persist()
%time _ = trim_overlap(z, 10, boundary='nearest').persist()
%time _ = trim_overlap(z, 10, boundary='none').persist()
%time _ = trim(z, 10, boundary='none').persist()

A best case time of 2ms can be achieved on nearest for the old code and new code. A worst case time of 30 ms can also be acheived for the nearest computation. (2400MHz memory, 16 GB of RAM, dual channel, i7 7700HQ, Ubuntu 16.04 64 bit)

The 500 ms number seems consistent.

@mrocklin

mrocklin commented Sep 9, 2018

Copy link
Copy Markdown
Member

Trying a few different combination out, I think z.persist doesn't do what I thought it did. I thought it moved the memory to the workers, but guess it doesn't.

Looking at your code above it looks like you never create a Dask.distributed Client, so by default you're running everything in your local process using a local Thread Pool. There is no serialization, and no data movement. You will be chopping up the array with normal numpy slicing, but that's it.

If you wanted to see how this would function with separate processes, you would run the following two lines:

from dask.distributed import Client
client = Client()

But for your case where you're looking to accelerate computations on small datasets on a single computer the in-process ThreadPool is likely to be a much better solution.

@hmaarrfk

hmaarrfk commented Sep 9, 2018

Copy link
Copy Markdown
Contributor Author

Right thanks for pointing that out.

Still I don't undertand the huge difference in performance from 30ms to 2ms for operations that should effective call the dask infrastructure and a graph with no-ops

@mrocklin

mrocklin commented Sep 9, 2018 via email

Copy link
Copy Markdown
Member

@mrocklin mrocklin mentioned this pull request Sep 10, 2018
2 tasks done
@mrocklin

Copy link
Copy Markdown
Member

@jakirkham and @hmaarrfk you two might want to trade reviewing each others' PRs at #3958 and #3964

@hmaarrfk

Copy link
Copy Markdown
Contributor Author

@jakirkham I've added a new api function trim_overlaped do I need to add it to: docs/source/array-api.rst manually?

@mrocklin

Copy link
Copy Markdown
Member

I've added a new api function trim_overlaped do I need to add it to: docs/source/array-api.rst manually?

Sorry for the delay in response. Yes, we handle API docs manually. I think that there is a section in the array api docs that is about overlapped. Presumably this would go there.

@mrocklin

Copy link
Copy Markdown
Member

If there are no further comments on this I plan to merge this at the end of the day.

@hmaarrfk

Copy link
Copy Markdown
Contributor Author

Did you want me to add things to the docs first? or were you going to do it?

@mrocklin

Copy link
Copy Markdown
Member

@jakirkham if you're around before then to look at things that would be welcome

@mrocklin

Copy link
Copy Markdown
Member

Did you want me to add things to the docs first? or were you going to do it?

I don't plan to do this, no. I'm somewhat busy with other things. I just wanted to make sure that this PR didn't linger for too long. If you have time to add docs that would be welcome.

@hmaarrfk

Copy link
Copy Markdown
Contributor Author

Just a ping. I think I added the required info to the docs. Let me know if you need anything else.

@jakirkham

Copy link
Copy Markdown
Member

Sorry to be absent here. I'll try to look, but unfortunately can't promise anything before Friday. If other people are happy with this, would encourage you to move ahead without me.

@mrocklin

Copy link
Copy Markdown
Member

OK, I'm going to go ahead and merge. Retroactive reviewing would still be welcome @jakirkham if you have time.

@mrocklin
mrocklin merged commit 885bbb2 into dask:master Sep 18, 2018
@mrocklin

Copy link
Copy Markdown
Member

Thanks for the work here @hmaarrfk . It's nice to see someone take on performance for in-memory use cases.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants