Repository navigation
Fix tf.experimental.numpy.isclose for integer inputs - #108702
gamila-wisam wants to merge 4 commits into
Conversation
7968287 to
c3827cf
Compare
|
The requested change (removing trailing whitespace) has been addressed and pushed. |
|
No longer at Google, no longer can approve PRs |
|
Thanks for letting me know. |
|
Hi @cantonios , |
|
Hi @cantonios , thanks for pointing this out! well the intent here is to match NumPy’s isclose behavior by promoting integer inputs to floating point, rather than comparing integers exactly. As you mentioned, this does mean that very large int64 values (above 2^53) can lose precision when cast to float64, and distinct integers may then compare as close. This is a known limitation of floating-point–based isclose behavior and is consistent with NumPy’s approach. The previous TensorFlow behavior (a == b) diverged from NumPy and produced incorrect results for integer inputs even in common cases. I can add a clarifying comment or a test documenting this precision limitation if that would be helpful. |
What's an example of an incorrect behavior? When the atol/rtol are large? I prefer correctness over strict adherence to numpy's results. If numpy suffers the same issue for large integers, then that's a numpy bug that should be addressed - we have encountered (and fixed) these kinds of things before in numpy. You might try taking the difference as an integer prior to casting to double. You'll need to check for overflows first though. The other concern is performance. Comparing integers is fast and easy on most hardware platforms. Doubles are expensive, especially if these tensors are large. Imagine an int8 tensor of billions of elements, then all of a sudden you cast two of them to double type. This is also something that we care a lot more about than numpy does because of the sheer scale of some of the TF models. I think I would err on the side of keeping it as is with the straight integer comparison. |
|
Hi @cantonios, can you please take a look when you have a chance? |
|
Hi @cantonios , Can you please review this PR? Thanks! |
- Handle integer difference first; cast to float64 only when needed - Match NumPy semantics; added tests for int32, int64, bool, inf - Shortened long lines for PyLint compliance
| a = math_ops.cast(a, dtypes.int8) | ||
| b = math_ops.cast(b, dtypes.int8) | ||
|
|
||
| diff = math_ops.abs(a - b) |
There was a problem hiding this comment.
∣a − b∣ ≤ atol + rtol * ∣b∣
you see in the integer world addition is commutative so
∣a - b ∣- atol ≤ rtol * ∣b∣
so you can do the atol without loosing precision, which will handle many cases
if this goes through then you might want to submit an issue on numpy to match this.
There was a problem hiding this comment.
you might want to do rtol * ∣b∣as doubles and cast them back to whatever dtype they originaly had so that it is a int comparison
I agree with cantonios here, however this is a compatibility layer and we don't expect it to be as performant, but correctness is more important than compatibility, so try doing the fix I asked above and fix the pylint issue |
I do care about performance here. Casting to double is incredibly expensive and can cause a lot of large models to blow up their memory usage. That would not be acceptable. |
|
Following internal discussion, we have determined that bypassing explicit type conversions in favor of the implementation below covers all intended use cases: diff = tf.abs(a - b)
rhs = atol + rtol * tf.abs(b)
out = (diff <= rhs)Key Considerations: This change introduces a shift in behavior; updated release notes and tests should be included to reflect these adjustments. |
|
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. |
|
This PR was closed because it has been inactive for 14 days since being marked as stale. Please reopen if you'd like to work on this further. |
isclose compared integer inputs with pure equality, ignoring the tolerances entirely, so inputs well within rtol or atol of each other reported False where NumPy reports True. Apply the tolerance formula to integer inputs using the operands' own arithmetic, following the implementation agreed on by maintainers in PR #108702: no unconditional cast to floating point, so integer tolerances stay in integer arithmetic and floating-point tolerances require type promotion, keeping the cost of promotion opt-in. The difference is computed as max - min because unsigned subtraction wraps around and tf.abs does not support unsigned dtypes. When promotion yields a floating-point tolerance side, the difference is cast to match it, since comparison ops do not promote mixed operands. Verified against NumPy across int32, int64, and uint8 inputs with integer and float tolerances, with type promotion disabled, in legacy mode, and in all mode. Also updates release notes as requested by maintainers. Fixes #108657
Fixes #108657
ready for review
Fix tf.experimental.numpy.isclose for integer inputs.
Changes include:
Fixes incorrect results for integer inputs while keeping floating-point behavior unchanged.
Matches NumPy semantics for integer comparisons, including support for broadcasting and tolerances.
Adds unit tests covering integer tensors, large integers, booleans, and edge cases.
Note:
Integer comparisons now cast to float64 only when necessary for rtol/atol.
Exact equality (rtol=0, atol=0) is handled efficiently in integer domain.
Large integers may still lose precision when casting to float64, consistent with NumPy behavior.