Skip to content

Rewrite Dask Array's pad to add only new chunks - #4152

Merged
jakirkham merged 4 commits into
dask:masterfrom
jakirkham:dont_use_np_pad_in_da_pad
Nov 5, 2018
Merged

jakirkham merged 4 commits into
dask:masterfrom
jakirkham:dont_use_np_pad_in_da_pad

Conversation

@jakirkham

@jakirkham jakirkham commented Oct 30, 2018 •

Copy link
Copy Markdown
Member

Requires PR ( #4150 )

In nearly all cases Dask Array's pad would handle the intended padding in new chunks concatenated around the input array. This is nice as it ensure the original array is passed through unaltered with padding chunks being generated (possibly using the original arrays data) as needed by downstream computations.

However this was not the case for the constant, edge, and linear_ramp modes where NumPy's pad was called on the edge chunks instead. This could be bad for a few reasons. First the edge chunks would increase in size based on the padding width in these specific cases. Second NumPy's pad appears to have performance penalties (due to temporaries and copying) if these array chunks are quite large. Third inconsistent handling of chunking in modes causes unnecessary confusion.

To fix these issues, this rewrites Dask Array's pad in these cases to not make use of NumPy's pad. Instead it performs the padding operations itself by building up the chunks for each dimension on either end and concatenating them with the original array. Thus the original array is simply passed through while padded chunks are generated around the edges.

  • Tests added / passed
  • Passes flake8 dask

cc @hmaarrfk

@hmaarrfk

Copy link
Copy Markdown
Contributor

@jakirkham have you thought about having a da.overlapped_from_array as a way of creating an overlapped dask array from a numpy array already in memory? Small optimization, but seems like a common use case where you have a large numpy array that you would want to perform distributed computation on.

@jakirkham jakirkham changed the title Rewrite Dask Array's pad to add only new chunks WIP: Rewrite Dask Array's pad to add only new chunks Oct 30, 2018
@jakirkham

jakirkham commented Oct 30, 2018 •

Copy link
Copy Markdown
Member Author

Sounds interesting. Maybe worth writing up an issue and expounding on that idea a bit.

Edit: This was raised as issue ( #4154 ).

@jakirkham
jakirkham force-pushed the dont_use_np_pad_in_da_pad branch 9 times, most recently from 665cfdb to 3cce100 Compare October 31, 2018 19:02
Adds a helper function for getting the shapes and chunks for the arrays
to be padded on either end of the result. Useful in conjunction with
broadcasting operations like those in `pad_edge` or `pad_stats`.
Adds a helper function to be used for chunks in the Dask graph in the
`linear_ramp` case of `pad` to build border chunks.
@jakirkham
jakirkham force-pushed the dont_use_np_pad_in_da_pad branch from 3cce100 to 09157b2 Compare November 1, 2018 22:27
@jakirkham jakirkham changed the title WIP: Rewrite Dask Array's pad to add only new chunks Rewrite Dask Array's pad to add only new chunks Nov 1, 2018
@jakirkham

Copy link
Copy Markdown
Member Author

Ok, this seems more or less fine to me. Would be great to have a review if anyone has time. 😄

In nearly all cases Dask Array's `pad` would handle the intended padding
in new chunks concatenated around the input array. This is nice as it
ensure the original array is passed through unaltered with padding
chunks being generated (possibly using the original arrays data) as
needed by downstream computations.

However this was not the case for the `constant`, `edge`, and
`linear_ramp` modes where NumPy's `pad` was called on the edge chunks
instead. This could be bad for a few reasons. First the edge chunks
would increase in size based on the padding width in these specific
cases. Second NumPy's `pad` appears to have performance penalties (due
to temporaries and copying) if these array chunks are quite large. Third
inconsistent handling of chunking in modes causes unnecessary confusion.

To fix these issues, this rewrites Dask Array's `pad` in these cases to
not make use of NumPy's `pad`. Instead it performs the padding
operations itself by building up the chunks for each dimension on either
end and concatenating them with the original array. Thus the original
array is simply passed through while padded chunks are generated around
the edges.
As we no longer use the `np_pad` wrapper function in, `pad_edge` or any
of the other padding functions, go ahead and drop it.
@jakirkham
jakirkham force-pushed the dont_use_np_pad_in_da_pad branch from 09157b2 to 1e27f42 Compare November 1, 2018 23:03
Comment thread dask/array/creation.py
Helper function for padding edges.
for d in axes:
for i in range(2):
pad_shapes[i][d] = pad_width[d][i]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a reason why this is transposed?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it's convenient to select which padding first (e.g. left or right) and thus get the shape for that padding.

Comment thread dask/array/creation.py
)
]

result = concatenate([pad_arrays[0], result, pad_arrays[1]], axis=d)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess your algorithm is calling concatenate on each axis right? I guess you are just setting up the computation graph. The cool things you can do with compilation!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's right. Yeah it seems to nicely mirror what np.pad does.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't be too hung up on numpy's implementation. It was optimized for a different usecase.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was basically the easiest way to write it in my mind at least.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, that i believe.

@hmaarrfk

hmaarrfk commented Nov 1, 2018

Copy link
Copy Markdown
Contributor

I guess I'm a little confused as to what is happening now. is concatenate a nop when the pads are 0 in size?

@jakirkham

Copy link
Copy Markdown
Member Author

is concatenate a nop when the pads are 0 in size?

Hmm...that's a good point. They probably should be. Though they don't seem to be currently. Working on a fix in PR ( #4167 ).

@jakirkham

Copy link
Copy Markdown
Member Author

Any other thoughts on this change?

@hmaarrfk

hmaarrfk commented Nov 3, 2018

Copy link
Copy Markdown
Contributor

Yeah, i'm just worried that you are blowing up the computation graph by not having implemented the no-op concatenate.

Otherwise it makes sense.

@jakirkham

Copy link
Copy Markdown
Member Author

The behavior here is consistent with the other padding cases that use block currently and also do not optimize pad width zero cases. While I agree the optimization would be nice, I think that is orthogonal to the change in this PR.

@hmaarrfk

hmaarrfk commented Nov 3, 2018

Copy link
Copy Markdown
Contributor

yeah agreed. So i'm a go on this.

@jakirkham

Copy link
Copy Markdown
Member Author

Thanks for the review. If anyone else wants to take a look, that would be welcome.

Planning on merging tomorrow if there are no further comments.

@mrocklin

mrocklin commented Nov 5, 2018 via email

Copy link
Copy Markdown
Member

@jakirkham
jakirkham merged commit 68a7f08 into dask:master Nov 5, 2018
@jakirkham
jakirkham deleted the dont_use_np_pad_in_da_pad branch November 5, 2018 16:31
@jakirkham

Copy link
Copy Markdown
Member Author

Thanks. If you find an issue latter, please let me know and would be happy to address in a follow-up.

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