Skip to content

Fix tf.experimental.numpy.isclose for integer inputs - #108702

Closed
gamila-wisam wants to merge 4 commits into
tensorflow:masterfrom
gamila-wisam:fix-isclose-int-clean
Closed

gamila-wisam wants to merge 4 commits into
tensorflow:masterfrom
gamila-wisam:fix-isclose-int-clean

Conversation

@gamila-wisam

@gamila-wisam gamila-wisam commented Jan 23, 2026 •

Copy link
Copy Markdown

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.

@google-ml-butler google-ml-butler Bot added the size:S CL Change Size: Small label Jan 23, 2026
@google-ml-butler
google-ml-butler Bot requested a review from cantonios January 23, 2026 01:43
@google-ml-butler google-ml-butler Bot added the awaiting review Pull request awaiting review label Jan 23, 2026
@keerthanakadiri keerthanakadiri added the prtype:bugfix PR to fix a bug label Jan 23, 2026
@github-project-automation github-project-automation Bot moved this to Assigned Reviewer in PR Queue Jan 23, 2026
@keerthanakadiri keerthanakadiri added the comp:ops OPs related issues label Jan 23, 2026
@github-project-automation github-project-automation Bot moved this from Assigned Reviewer to Reviewer Requested Changes in PR Queue Jan 23, 2026
@gamila-wisam
gamila-wisam force-pushed the fix-isclose-int-clean branch from 7968287 to c3827cf Compare January 26, 2026 17:47
@keerthanakadiri keerthanakadiri added stat:awaiting response Status - Awaiting response from author and removed awaiting review Pull request awaiting review labels Jan 27, 2026
@gamila-wisam

Copy link
Copy Markdown
Author

The requested change (removing trailing whitespace) has been addressed and pushed.
@mihaimaruseac PTAL

@google-ml-butler google-ml-butler Bot removed the stat:awaiting response Status - Awaiting response from author label Jan 29, 2026
@mihaimaruseac

Copy link
Copy Markdown
Contributor

No longer at Google, no longer can approve PRs

@gamila-wisam

Copy link
Copy Markdown
Author

Thanks for letting me know.

@gamila-wisam

Copy link
Copy Markdown
Author

Hi @cantonios ,
The requested change (removing trailing whitespace) has been addressed and pushed, Could you please review and approve when you get a chance? Thanks

@gamila-wisam

Copy link
Copy Markdown
Author

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.

@cantonios

Copy link
Copy Markdown
Collaborator

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.

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.

@gamila-wisam

Copy link
Copy Markdown
Author

Hi @cantonios, can you please take a look when you have a chance?

@google-ml-butler google-ml-butler Bot added the awaiting review Pull request awaiting review label Feb 15, 2026
@keerthanakadiri
keerthanakadiri removed the request for review from mihaimaruseac February 24, 2026 06:05
@keerthanakadiri

Copy link
Copy Markdown
Contributor

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)

@ratgr ratgr Apr 23, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

∣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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@ratgr

ratgr commented Apr 23, 2026 •

Copy link
Copy Markdown
Contributor

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 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

@cantonios

Copy link
Copy Markdown
Collaborator

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 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.

@ratgr

ratgr commented Apr 24, 2026 •

Copy link
Copy Markdown
Contributor

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 will work if rtol and atol are integers.
Type promotion is disabled by default so this will fail on int values and float tolerances.
However if automatic promotion is enabled, the logic will handle promotions as expected.
This makes bad performance an opt-in not default

This change introduces a shift in behavior; updated release notes and tests should be included to reflect these adjustments.

@keerthanakadiri keerthanakadiri added stat:awaiting response Status - Awaiting response from author and removed awaiting review Pull request awaiting review labels Apr 28, 2026
@github-actions

Copy link
Copy Markdown

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.

@github-actions github-actions Bot added the stale This label marks the issue/pr stale - to be closed automatically if no activity label May 13, 2026
@github-actions

Copy link
Copy Markdown

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.

@github-actions github-actions Bot closed this May 27, 2026
@github-project-automation github-project-automation Bot moved this from Reviewer Requested Changes to Closed/Rejected in PR Queue May 27, 2026
copybara-service Bot pushed a commit that referenced this pull request Jul 28, 2026
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp:ops OPs related issues prtype:bugfix PR to fix a bug size:S CL Change Size: Small stale This label marks the issue/pr stale - to be closed automatically if no activity stat:awaiting response Status - Awaiting response from author

Projects

Status: Closed/Rejected

Development

Successfully merging this pull request may close these issues.

tf.experimental.numpy.isclose returns incorrect results for integer inputs

6 participants