Skip to content

Keep the sign of zero in the eager scalar tensor cache - #129243

Merged
copybara-service[bot] merged 2 commits into
tensorflow:masterfrom
bodapatisaikrishna:fix-eager-scalar-cache-signed-zero
Oct 23, 2026
Merged

copybara-service[bot] merged 2 commits into
tensorflow:masterfrom
bodapatisaikrishna:fix-eager-scalar-cache-signed-zero

Conversation

@bodapatisaikrishna

Copy link
Copy Markdown
Contributor

Fixes #129231.

In eager mode, Python scalars are converted through TFE_TensorHandleCache, and the cache key compares values with Python ==. Python says 0.0 == -0.0 and gives the two the same hash. So whichever zero is converted first gets handed back for the other one as well:

x = tf.constant([1.0, 2.0])
x * 0.0          # [0., 0.]
x * -0.0         # [0., 0.]    should be [-0., -0.]
1 / (x * -0.0)   # [inf, inf]  should be [-inf, -inf]

It also happens in the other order: once -0.0 is cached, later 0.0 conversions come back negative. The result depends on what happened to run earlier in the process, which makes this hard to track down when it shows up in atan2, 1 / x, or anything that copies a sign.

PyObjectPtr::operator== now compares floats by value and sign bit, and compares complex numbers one part at a time the same way. I didn't touch the hash, so both zeros land in the same bucket as separate entries. I also added an identity check at the top so an object still always matches itself. That keeps NaN behaving as before: PyObject_RichCompareBool matched a NaN by identity too, though ConvertToEagerTensor doesn't cache non-finite floats in the first place.

Testing

I can't do a full TensorFlow build on my machine, so CI will be the first real build of the C++ change. To check the comparison logic on its own, I compiled PyObjectPtr from this header against Python 3.11 in a small standalone program. It gives the expected result for 0.0 vs -0.0 in both orders, for complex zeros with each sign combination, for the same NaN object vs two distinct NaNs, for 0.0 vs 0, and for ordinary floats, ints, complex numbers and True.

testScalarCacheKeepsSignOfZero in tensor_test.py covers float16/32/64, complex64/128 and the binary-op path from the issue. It fails on current tf-nightly, which is the bug.

TFE_TensorHandleCache keys Python scalars with Python equality, and
Python has 0.0 == -0.0 with equal hashes (likewise for the parts of a
complex number). Whichever zero was converted first was therefore
returned for both: after `x * 0.0`, `x * -0.0` came back as +0.0 and
`1 / (x * -0.0)` as +inf, and the reverse order turned 0.0 into -0.0.

Compare floats and complex values by value and sign bit in
PyObjectPtr::operator==. Hashes are unchanged, so the two zeros simply
land in the same bucket as distinct entries. An identity check keeps
the existing behavior of an object always matching itself.

Fixes tensorflow#129231
@google-ml-butler google-ml-butler Bot added size:S CL Change Size: Small awaiting review Pull request awaiting review labels Oct 17, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

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.

Code Review

This pull request ensures that the Python scalar cache correctly distinguishes between 0.0 and -0.0 for float and complex types by implementing sign-aware comparisons in PyObjectPtr::operator== and adding comprehensive unit tests. The review feedback recommends using PyFloat_CheckExact and PyComplex_CheckExact instead of PyFloat_Check and PyComplex_Check to prevent bypassing custom comparison logic in subclasses of float and complex.

np.float64 and np.complex128 subclass float and complex and reach the
scalar cache through PyArray_IsPythonNumber, so restricting the check
to exact types would leave np.float64(-0.0) returning +0.0. Add a
comment saying so and cover both NumPy types in the test.
@keerthanakadiri keerthanakadiri self-assigned this Oct 19, 2026
@keerthanakadiri
keerthanakadiri requested a review from a team October 19, 2026 05:17
@keerthanakadiri keerthanakadiri added the python Pull requests that update Python code label Oct 19, 2026
@github-project-automation github-project-automation Bot moved this to Assigned Reviewer in PR Queue Oct 19, 2026
@keerthanakadiri keerthanakadiri added the prtype:bugfix PR to fix a bug label Oct 19, 2026
@google-ml-butler google-ml-butler Bot added kokoro:force-run Tests on submitted change ready to pull PR ready for merge process labels Oct 19, 2026
@github-project-automation github-project-automation Bot moved this from Assigned Reviewer to Approved by Reviewer in PR Queue Oct 19, 2026
@nithyak0204 nithyak0204 removed awaiting review Pull request awaiting review kokoro:force-run Tests on submitted change labels Oct 23, 2026
@copybara-service
copybara-service Bot merged commit 6206e40 into tensorflow:master Oct 23, 2026
19 checks passed
@github-project-automation github-project-automation Bot moved this from Approved by Reviewer to Merged in PR Queue Oct 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

prtype:bugfix PR to fix a bug python Pull requests that update Python code ready to pull PR ready for merge process size:S CL Change Size: Small

Projects

Status: Merged

Development

Successfully merging this pull request may close these issues.

Eager scalar cache treats 0.0 and -0.0 as the same key, so x * -0.0 returns +0.0

4 participants