Skip to content

Apply tolerance formula for integer inputs in isclose - #109328

Closed
ayushozha wants to merge 1 commit into
tensorflow:masterfrom
ayushozha:fix/isclose-integer-inputs
Closed

ayushozha wants to merge 1 commit into
tensorflow:masterfrom
ayushozha:fix/isclose-integer-inputs

Conversation

@ayushozha

Copy link
Copy Markdown
Contributor

Summary

  • tf.experimental.numpy.isclose returns incorrect results for integer inputs
  • Integer dtypes fail the np.issubdtype(dtype, np.inexact) check and fall through to strict equality (a == b), completely ignoring atol and rtol parameters
  • NumPy handles this by casting integers to float before applying the tolerance formula
  • Fixed by casting integer inputs to float64 and applying |a-b| <= atol + rtol * |b|

Reproducer

import tensorflow as tf
a = tf.constant([1, 2, 3], dtype=tf.int32)
b = tf.constant([1, 3, 5], dtype=tf.int32)
# With atol=1.5, values within 1.5 of each other should be "close"
result = tf.experimental.numpy.isclose(a, b, atol=1.5, rtol=0)
print(result)  # Returns [True, False, False] on master, should be [True, True, False]

Test plan

  • Verify the reproducer matches NumPy behavior
  • Run bazel test //tensorflow/python/ops/numpy_ops:np_math_ops_test

Fixes #108657

…sclose

Integer inputs were falling through to strict equality (a == b), ignoring
atol and rtol parameters entirely. NumPy handles this by casting integers
to float before applying the tolerance formula. Match that behavior by
casting to float64 and computing |a-b| <= atol + rtol * |b|.

Fixes tensorflow#108657
@google-ml-butler google-ml-butler Bot added the size:S CL Change Size: Small label Feb 1, 2026
@google-ml-butler
google-ml-butler Bot requested a review from cantonios February 1, 2026 08:39
@google-ml-butler google-ml-butler Bot added the awaiting review Pull request awaiting review label Feb 1, 2026
@github-project-automation github-project-automation Bot moved this to Assigned Reviewer in PR Queue Feb 2, 2026
@keerthanakadiri keerthanakadiri added the comp:ops OPs related issues label Feb 2, 2026
@keerthanakadiri keerthanakadiri added the prtype:bugfix PR to fix a bug label Apr 22, 2026
@cantonios

Copy link
Copy Markdown
Collaborator

This is a duplicate of #108702. See the discussions on that PR - we cannot cast to float64 due to performance/memory issues.

@keerthanakadiri

Copy link
Copy Markdown
Contributor

This is a duplicate of #108702. Hence, closing this PR. Please feel free to raise a new PR if you'd like to revisit these changes later.

@github-project-automation github-project-automation Bot moved this from Assigned Reviewer to Closed/Rejected in PR Queue Apr 27, 2026
@google-ml-butler google-ml-butler Bot removed the awaiting review Pull request awaiting review label Apr 27, 2026
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

Projects

Status: Closed/Rejected

Development

Successfully merging this pull request may close these issues.

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

4 participants