Repository navigation
Description
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-9223372036854775808with NumPy 1.26.4 and 2.4.6, and9223372036854775807on arm64. - The unmodified
saturate_castsource on x86 Python with x86 NumPy, with only the TensorFlow ops stubbed (ClipByValueas min/max,castasastype). That produced the x86 column above. ClipByValueon tf-nightly (arm64) with both limits set toINT64_MIN, which returnsINT64_MINfor 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
Venkat6871 commentedon Nov 5, 2026
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.
google-ml-butler commentedon Nov 9, 2026
Are you satisfied with the resolution of your issue?
Yes
No