Repository navigation
Rewrite Dask Array's pad to add only new chunks - #4152
Conversation
|
@jakirkham have you thought about having a |
pad to add only new chunkspad to add only new chunks
|
Sounds interesting. Maybe worth writing up an issue and expounding on that idea a bit. Edit: This was raised as issue ( #4154 ). |
665cfdb to
3cce100
Compare
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.
3cce100 to
09157b2
Compare
pad to add only new chunkspad to add only new chunks
|
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.
09157b2 to
1e27f42
Compare
| Helper function for padding edges. | ||
| for d in axes: | ||
| for i in range(2): | ||
| pad_shapes[i][d] = pad_width[d][i] |
There was a problem hiding this comment.
is there a reason why this is transposed?
There was a problem hiding this comment.
Yeah, it's convenient to select which padding first (e.g. left or right) and thus get the shape for that padding.
| ) | ||
| ] | ||
|
|
||
| result = concatenate([pad_arrays[0], result, pad_arrays[1]], axis=d) |
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
That's right. Yeah it seems to nicely mirror what np.pad does.
There was a problem hiding this comment.
don't be too hung up on numpy's implementation. It was optimized for a different usecase.
There was a problem hiding this comment.
This was basically the easiest way to write it in my mind at least.
|
I guess I'm a little confused as to what is happening now. 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 ). |
|
Any other thoughts on this change? |
|
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. |
|
The behavior here is consistent with the other padding cases that use |
|
yeah agreed. So i'm a go on this. |
|
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. |
|
I haven't looked at this, but generally if Mark and you are in agreement I
say go ahead and merge.
…On Mon, Nov 5, 2018 at 10:42 AM jakirkham ***@***.***> wrote:
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.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#4152 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AASszKIygcMu6WejBlM_dcATidjslr3Cks5usFxkgaJpZM4YBEaM>
.
|
|
Thanks. If you find an issue latter, please let me know and would be happy to address in a follow-up. |
Requires PR ( #4150 )
In nearly all cases Dask Array's
padwould 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, andlinear_rampmodes where NumPy'spadwas 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'spadappears 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
padin these cases to not make use of NumPy'spad. 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.flake8 daskcc @hmaarrfk