Fix torch pad: (before, after) pairs were swapped for asymmetric pad_width - #36
Merged
Merged
Conversation
Owner
|
Looks good thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
torch_padflattenedpad_widthand then reversed the whole flat tuple, which reverses each(before, after)pair as well as the axis order — so on the torch backend padding placed before an axis ended up after it, and vice versa. Symmetric pads are unaffected, which is presumably why it went unnoticed.The fix is to reverse the axis order before flattening:
torch's flat format is
(before_last, after_last, before_secondlast, ...), i.e. axes reversed but each pair in order. The single-tuple broadcast branch (len(pad) == 2 -> pad * ndim) also becomes correct for an asymmetric((a, b),), which was previously swapped too.test_padonly checked the output shape and sum, both of which are invariant under the swap. It now also compares againstnp.padand has an asymmetricpad_widthcase; that test fails on the torch backend before this change and passes after.Fixes #35