Skip to content

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

Closed

Description

@bodapatisaikrishna

Issue type

Bug

Have you reproduced the bug with TensorFlow Nightly?

Yes

Source

binary

TensorFlow version

tf-nightly 2.23.0-dev20261014 (git v1.12.1-146947-gfcb676979cd)

Custom code

Yes

OS platform and distribution

macOS 15.7.2 (arm64)

Mobile device

No response

Python version

3.11.7

Bazel version

No response

GCC/compiler version

No response

CUDA/cuDNN version

No response

GPU model and memory

No response

Current behavior?

In eager mode, Python scalars converted to a tensor with an explicit dtype go through TFE_TensorHandleCache. Its key equality (PyObjectPtr::operator==) uses Python ==, and 0.0 == -0.0 is True with equal hashes. So whichever signed zero gets converted first is returned for both later on.

The result is silently wrong signed zeros. After x * 0.0 has run once, x * -0.0 gives +0.0, and 1 / (x * -0.0) gives +inf where it should be -inf. The reverse order is affected too: once tf.constant(-0.0, dtype=...) has been cached, tf.constant(0.0, dtype=...) comes back as -0.0. Anything that depends on the sign of zero is affected, including atan2, copysign-style logic, 1/x and signbit, and results depend on what code happened to run earlier in the process.

ConvertToEagerTensor already skips the cache for non-finite floats. It probably needs to skip (or key on the sign bit for) floats equal to zero, and the same goes for complex values with a zero real or imaginary part.

Expected: x * -0.0 == [-0., -0.] and 1 / (x * -0.0) == [-inf, -inf] regardless of earlier conversions; 1.0 / tf.constant(0.0, tf.float64) == inf.

Standalone code to reproduce the issue

import tensorflow as tf

x = tf.constant([1.0, 2.0])
print(x * 0.0)           # [0. 0.]
print(x * -0.0)          # expected [-0. -0.]
print(1.0 / (x * -0.0))  # expected [-inf -inf]

# The reverse order is also affected: once -0.0 is cached, 0.0 comes back negative.
a = tf.constant(-0.0, dtype=tf.float64)
b = tf.constant(0.0, dtype=tf.float64)
print(1.0 / b)           # expected inf

Relevant log output

tf.Tensor([0. 0.], shape=(2,), dtype=float32)
tf.Tensor([0. 0.], shape=(2,), dtype=float32)
tf.Tensor([inf inf], shape=(2,), dtype=float32)
tf.Tensor(-inf, shape=(), dtype=float64)

Activity

  1. added
    comp:core
    2.21.0
    on Oct 19, 2026
  2. Kayyuri commented on Oct 19, 2026

    @Kayyuri

    Hi @bodapatisaikrishna,
    Thanks for reporting this issue and submitting a fix for TensorFlow. Once the PR is merged, the issue will be resolved. We appreciate your contribution.

  3. google-ml-butler commented on Oct 23, 2026

    @google-ml-butler

    Are you satisfied with the resolution of your issue?
    Yes
    No

  4. added a commit that references this issue on Oct 23, 2026
    8489c3e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

2.21.0awaiting PR mergecomp:coretype:bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions