Fix infinite norm negative axis mismatch bug for matrices with 2 or more dimensions.#3756
Open
danlee2002 wants to merge 3 commits into
Open
Fix infinite norm negative axis mismatch bug for matrices with 2 or more dimensions.#3756danlee2002 wants to merge 3 commits into
danlee2002 wants to merge 3 commits into
Conversation
Author
|
@zcbenz Could you review this? |
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.
Proposed changes
The following changes aims to fix an axis mismatch bug that occurs when one tries to calculate the$\pm\infty$ norm of some matrix A with 2 or more dimensions by passing in a series of negative axes and when
keepdimis false.As an example consider the following snippet of code:
Based on the numpy implementation, one should expect the following to return the float value of 21.0. However, we get an index error and the culprit of this error is how the matrix_norm is calculated when
keepdimsis false and we receive negative index.If we take a look at the matrix_norm code. It's clear that the norm method for pos/neg infinite norm is implemented series of chained reductions:
max(sum(abs(a, s), col_axis, keepdims, s), row_axis, keepdims, s)min(sum(abs(a, s), col_axis, keepdims, s), row_axis, keepdims, s)Hence, when we specify that we want to reduce along
(row_axis, col_axis)and keepdims is false, we need to account for the reduction in dimensions when we call max or min. Otherwise, we either end up with an index out of bound error or we reduce the wrong axis by one due to an off by one error.The non-negative cases handle this via:
row_axis -= (!keepdims && row_axis > col_axis && row_axis > 0);However, this is skipped for negative cases due to the
row_axis > 0. To address this issue, this pr converts the respective for(row_axis, col_axis)to the equivalent non-negative values when negative axes values. This ensures proper behavior is achieved.Testing
(3, 3), (2, 3, 3), (2, 3, 3, 3)where outputs were verified against expected numpy outputs.Checklist
Put an
xin the boxes that apply.pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes