Skip to content

tf.math.bincount with negative values raises in eager but silently returns an empty tensor with jit_compile=True #117960

Closed

Description

@rookieLiu2018

Issue type

Bug

Have you reproduced the bug with TensorFlow Nightly?

Yes

Source

source

TensorFlow version

tf 2.21

Custom code

Yes

OS platform and distribution

No response

Mobile device

No response

Python version

No response

Bazel version

No response

GCC/compiler version

No response

CUDA/cuDNN version

No response

GPU model and memory

No response

Current behavior?

tf.math.bincount correctly raises an error in eager mode when the input contains negative values. However, when the same computation is compiled with tf.function(jit_compile=True), the XLA-compiled function silently returns an empty tensor instead of raising an error.

This is an eager/XLA legality mismatch. The compiled path appears to skip or mishandle the input validation for negative values.

Standalone code to reproduce the issue

import tensorflow as tf

print("tf version:", tf.__version__)

x = tf.constant([0, -1, 2], dtype=tf.int32)

def fn(x):
    return tf.math.bincount(
        x,
        minlength=3,
        maxlength=3,
        dtype=tf.int32,
    )

print("\n=== eager ===")
try:
    y = fn(x)
    print("eager ok:")
    print(y)
except Exception as e:
    print("eager error:")
    print(type(e).__name__)
    print(e)

print("\n=== XLA compiled ===")
try:
    compiled_fn = tf.function(fn, jit_compile=True)
    y = compiled_fn(x)
    print("compiled ok:")
    print(y)
    print("compiled numpy:", y.numpy())
except Exception as e:
    print("compiled error:")
    print(type(e).__name__)
    print(e)

Relevant log output

tf version: 2.21.0

=== eager ===
eager error:
InvalidArgumentError
{{function_node __wrapped__DenseBincount_device_/job:localhost/replica:0/task:0/device:CPU:0}} Input arr must be non-negative! [Op:DenseBincount] name: 

=== XLA compiled ===
compiled ok:
tf.Tensor([1 0 1], shape=(3,), dtype=int32)
compiled numpy: [1 0 1]

Activity

  1. google-ml-butler commented on Jul 27, 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

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions