Skip to content

tf.dtypes.saturate_cast: int64 input clamps to INT64_MIN on x86; uint32/uint64 input raises NotFoundError #129838

Closed

Description

@bodapatisaikrishna

Issue type

Bug

Have you reproduced the bug with TensorFlow Nightly?

Yes. The uint32/uint64 crash reproduces on tf-nightly 2.23.0.dev20261028 (macOS arm64). For the x86 int64 problem, see "How this was verified" below.

Source

binary

TensorFlow version

tf-nightly 2.23.0.dev20261028; the code is unchanged on master

Custom code

Yes

OS platform and distribution

macOS 15.7.2 (arm64), plus x86_64 Python and NumPy under Rosetta

Python version

3.11

Current behavior?

tf.dtypes.saturate_cast has two problems with wide integer inputs.

1. int64 input gets a wrong clamp limit on x86. saturate_cast in tensorflow/python/ops/math_ops.py computes the upper limit like this:

max_limit = np_dtype(
    np.minimum(float(in_dtype.max), float(out_real_dtype.max))
)

float(2**63 - 1) rounds up to 2**63, which int64 cannot hold, so np.int64(2.0**63) is an out-of-range conversion (NumPy warns: invalid value encountered in cast). The result depends on the CPU: ARM saturates to INT64_MAX, but x86 gives INT64_MIN. The limit then goes into ClipByValue, so with int64 input and a float32, float64, int64 or uint64 output dtype, every value is clamped to INT64_MIN and the correct result is lost with no error.

Expected output for [0, 5, -7, 2**40], compared with what the code computes on x86:

Cast Expected x86
int64 -> float32 [0, 5, -7, 1.0995e12] [-9.22e18, -9.22e18, -9.22e18, -9.22e18]
int64 -> float64 [0, 5, -7, 1.0995e12] [-9.22e18, -9.22e18, -9.22e18, -9.22e18]
int64 -> int64 [0, 5, -7, 1099511627776] [-9223372036854775808, ...] (all four)
int64 -> uint64 [0, 5, 0, 1099511627776] [9223372036854775808, ...] (all four)

No test catches this because cast_op_test.py never passes integer inputs to saturate_cast.

2. uint32 and uint64 inputs crash on every platform. ClipByValue has no kernels for them, so saturate_cast raises NotFoundError. This also breaks tf.image.convert_image_dtype(x, ..., saturate=True) for uint32 and uint64 images.

How this was verified

I don't have an x86 Linux machine, and the x86 TensorFlow wheel can't run under Rosetta here because it needs AVX. So I have not run problem 1 end to end on a real x86 TensorFlow build. What I did run:

  • The bad limit itself, on real x86 code: np.int64(np.minimum(float(2**63 - 1), float(np.finfo(np.float32).max))) under Rosetta returns -9223372036854775808 with NumPy 1.26.4 and 2.4.6, and 9223372036854775807 on arm64.
  • The unmodified saturate_cast source on x86 Python with x86 NumPy, with only the TensorFlow ops stubbed (ClipByValue as min/max, cast as astype). That produced the x86 column above.
  • ClipByValue on tf-nightly (arm64) with both limits set to INT64_MIN, which returns INT64_MIN for every input.

If you have an x86 Linux machine, the snippet below checks problem 1 directly. It should print the correct values if I'm wrong.

Standalone code to reproduce the issue

import tensorflow as tf

x = tf.constant([0, 5, -7, 2**40], tf.int64)
# On x86: every value is -9.22e18. On arm64: correct.
print(tf.dtypes.saturate_cast(x, tf.float32).numpy())
print(tf.dtypes.saturate_cast(x, tf.int64).numpy())

# On every platform: NotFoundError.
print(tf.dtypes.saturate_cast(tf.constant([300], tf.uint32), tf.uint8).numpy())

Relevant log output

# tf-nightly 2.23.0.dev20261028, macOS arm64
RuntimeWarning: invalid value encountered in cast
  max_limit = np_dtype(
[ 0.0000000e+00  5.0000000e+00 -7.0000000e+00  1.0995116e+12]
[            0             5            -7 1099511627776]
tensorflow.python.framework.errors_impl.NotFoundError: Could not find device for node: {{node ClipByValue}} = ClipByValue[T=DT_UINT32]

I have a fix ready and will open a PR that links here.

Activity

  1. added
    comp:ops
    awaiting PR merge
    2.21.0
    on Nov 3, 2026
  2. Venkat6871 commented on Nov 5, 2026

    @Venkat6871
    Contributor

    Hi @bodapatisaikrishna ,
    Thank you for reporting this issue and raising the PR to address it. A fix has been proposed in PR, and the issue will be resolved once it is merged.
    We truly appreciate your initiative and effort in contributing this fix.

  3. google-ml-butler commented on Nov 9, 2026

    @google-ml-butler

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

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:opstype:bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions