Repository navigation
Keep the sign of zero in the eager scalar tensor cache - #129243
Merged
copybara-service[bot] merged 2 commits intoOct 23, 2026
Merged
copybara-service[bot] merged 2 commits into
copybara-service[bot] merged 2 commits into
Conversation
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
Contributor
There was a problem hiding this comment.
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.
dmiltr3
approved these changes
Oct 19, 2026
This was referenced Oct 23, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #129231.
In eager mode, Python scalars are converted through
TFE_TensorHandleCache, and the cache key compares values with Python==. Python says0.0 == -0.0and gives the two the same hash. So whichever zero is converted first gets handed back for the other one as well:It also happens in the other order: once
-0.0is cached, later0.0conversions 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 inatan2,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_RichCompareBoolmatched a NaN by identity too, thoughConvertToEagerTensordoesn'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
PyObjectPtrfrom this header against Python 3.11 in a small standalone program. It gives the expected result for0.0vs-0.0in both orders, for complex zeros with each sign combination, for the same NaN object vs two distinct NaNs, for0.0vs0, and for ordinary floats, ints, complex numbers andTrue.testScalarCacheKeepsSignOfZerointensor_test.pycovers float16/32/64, complex64/128 and the binary-op path from the issue. It fails on current tf-nightly, which is the bug.