-
Notifications
You must be signed in to change notification settings - Fork 74k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
validate argument axis of tf.experimental.numpy.stack #60727
base: master
Are you sure you want to change the base?
Conversation
AT present tf.experimental.numpy.stack not validating the axis argument. If we pass out of bound values to the axis still it is not rising the Error. Hence I am proposing amendment to validate the axis argument and raise the Error in case of out of bound axis. Please find the attached gist for explaining the problem and also with proposed amendments to the code it works fine and raised intended error when invalid axis arguments provided.
It is raising an error. Even in your gist you provide in this description. So what's the issue? |
Sorry you might have gone through the Its not raising the error for
Thanks! |
That's not throwing an error because the axis is valid: it can be in the range [0, 1], and negative values work backwards from the end, so -2 corresponds to axis=0 and -1 corresponds to axis=1 in this case. Your example fails if you pass axis=-3. |
Hi @SuryanarayanaY Can you please check @cantonios's comments ? Thank you! |
For earlier case the explanation holds right. I tested with new code.Please refer below code.
Whereas with Numpy it raises error.
Please refer the new gist attached here and the proposed code change might resolve this. |
Hi @cantonios Can you please review this PR ? Thank you! |
@@ -1066,6 +1066,10 @@ def stack(arrays, axis=0): # pylint: disable=missing-function-docstring | |||
arrays = asarray(arrays) | |||
if axis == 0: | |||
return arrays | |||
elif axis < -array_ops.rank(arrays) or axis >= array_ops.rank(arrays): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The issue is actually in swapaxes
, which doesn't properly check that the axis is in the valid range.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have tested implementing range validation in swapaxes and it seems working fine. Attached gist for reference.
Validation Code and location can be found below.
def swapaxes(a, axis1, axis2): # pylint: disable=missing-docstring
a = asarray(a) #
def adjust_axes(axes, rank):
def f(x):
if isinstance(x, int):
if x < 0:
x = x + rank
else:
x = array_ops.where_v2(x < 0, np_utils.add(x, a_rank), x)
return x
return nest.map_structure(f, axes)
if axis2 < -array_ops.rank(a) or axis2 >= array_ops.rank(a):
raise ValueError(
f"Argument `axis` = {axis2} not in range "
f"[{-array_ops.rank(a)}, {array_ops.rank(a)})")
Is it good to go implementing the validation code in swapaxes
instead of stack
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The check should be in swapaxes
, which has the same issue. It currently assumes that axis
is always in the range [-rank, rank)
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still waiting for the check to be made in swapaxes
Hi @SuryanarayanaY Can you please check @cantonios's comments and keep us posted ? Thank you! |
Requested for review |
Hi @cantonios Can you please review this PR ? Thank you! |
Hi @SuryanarayanaY Any update on this PR? Please. Thank you! |
2 similar comments
Hi @SuryanarayanaY Any update on this PR? Please. Thank you! |
Hi @SuryanarayanaY Any update on this PR? Please. Thank you! |
This PR is stale because it has been open for 14 days with no activity. It will be closed if no further activity occurs. Thank you. |
Hi @SuryanarayanaY Any update on this PR? Please. Thank you! |
This PR is stale because it has been open for 14 days with no activity. It will be closed if no further activity occurs. Thank you. |
Hi @SuryanarayanaY Any update on this PR? Please. Thank you! |
This PR is stale because it has been open for 14 days with no activity. It will be closed if no further activity occurs. Thank you. |
Hi @SuryanarayanaY Any update on this PR? Please. Thank you! |
Hi @SuryanarayanaY Any update on this PR? Please. Thank you! |
Hi @SuryanarayanaY Any update on this PR? Please. Thank you! |
Hi @SuryanarayanaY Any update on this PR? Please. Thank you! |
Hi @SuryanarayanaY Any update on this PR? Please. Thank you! |
AT present
tf.experimental.numpy.stack
not validating the values foraxis
argument. If we pass out of bound values to theaxis
still it is not rising the Error. Hence I am proposing amendments to validate theaxis
argument and raise the Error in case of out of bound axis.Please find the attached gist for explaining the problem and also with proposed amendments to the code it works fine and raised intended error when invalid axis arguments provided.
Fixes #55217