-
Notifications
You must be signed in to change notification settings - Fork 74.9k
Add stricter type checking for tf.math.real #60540
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
Conversation
Fix for tf.math.real so that it only accepts tensors with numeric entries as input. This makes it consistent with its documentation at https://www.tensorflow.org/api_docs/python/tf/math/real and raises a TypeError saying input must have numeric entries when called incorrectly.
tensorflow/python/ops/math_ops.py
Outdated
real_dtype = input.dtype.real_dtype | ||
return gen_math_ops.real(input, Tout=real_dtype, name=name) | ||
else: | ||
elif tf.debugging.is_numeric_tensor(input): |
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.
We can't use tf.
from within here, since it's not imported (and can't be, since that would be a circular dependency).
You can import the function itself from check_ops.py
where it actually lives. Or, we could add a property to dtype itself for is_numeric
, which is probably the most general solution. Those properties are defined in dtypes.cc
, and we already have a kNumberTypes
set in types.h
that can be leveraged (see DataTypeIsComplex(...)
in that file as an example).
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 just implemented these changes. Does it look fine now? Also, is there any specific ordering to functions in dtypes.cc or types.h to keep it consistent with other files? For the time being, I placed the code for is_numeric and IsNumericDataType right above the code for is_complex and IsComplexDataType.
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.
Thanks, looks good now. No special ordering, as long as its somewhat consistent.
Will run through further testing now.
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.
Hi. I'm still waiting on Py+CPP Test Suite - Ubuntu CPU, Python 3.9 and Code Check - Changed Files workflows to be run and approved. It seems odd that it's taking this long. Any ideas why?
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.
Yes, sorry, it broke our API compatibility tests. It shouldn't be a big issue because you only added a read-only attribute, but I'll need to go in and update the API checks, and do some more testing to make sure save/load models doesn't break.
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.
Okay, thanks. Let me know if I need to change anything else.
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.
Can you add some tests in tensorflow/python/kernel_tests/math_ops/cwise_ops_test.py
?
Fix for tf.math.real so that it only accepts tensors with numeric entries as input. This makes it consistent with its documentation at https://www.tensorflow.org/api_docs/python/tf/math/real and raises a TypeError saying input must have numeric entries when called incorrectly. For more details, see issue #60526.