Empty reshapes legalization (issue #45589) #46011
Open
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.
In
reshape
, one dimension from the output can be specified as:
, and then it's inferred from the input size and the remaining specified dimensions.If any specified output dimension is
zero
, the function today raises a zero division error. On the case of an empty input, though, we might simply infer anything consistent with another empty matrix. It's more of a case of "0/0"... And indeed, you can do#45589 points this out, and this patch proposes a solution.
If both input and output sizes contain a
0
, we compute a dimension from products of both sides ignoring zeros. This preserves the property that the input is recovered if the output contains the same values, except for one:
(as long as the input has a single0
). The size of the remaining dimensions may also be relevant in applications where arrays are being concatenated, and this makes it more likely that the produced array still matches some application constraint.