Skip to content

Commit 4155e48

Browse files
Fix XLA DenseBincount to ignore negative indices in runtime tensors
PR #121118 added compile-time constant validation for negative inputs in the XLA DenseBincount kernel, but only for the case where the input is a compile-time constant. For runtime (non-constant) tensors -- the common case in @tf.function(jit_compile=True) where inputs come from placeholders, variables, or upstream ops -- negative values were passed directly to xla::Scatter, silently producing wrong results instead of raising InvalidArgumentError as eager mode does. Since XLA cannot raise runtime errors from within a compiled computation, map negative indices to output_size (out of bounds) via xla::Select so that XLA Scatter silently ignores them. Using OOB indices instead of clamping to 0 avoids a subtle bug with binary_output=True: when scatter assigns (rather than adds), clamping negative values to index 0 would overwrite legitimate bin 0 values with 0. OOB indices are simply dropped by XLA Scatter, preserving correct results. The existing compile-time constant validation from #121118 is preserved unchanged -- OP_REQUIRES raises InvalidArgumentError at compile time for constant negative inputs. Also add an UnknownShape() fallback to the DenseBincount shape function in math_ops.cc for unknown-rank inputs, which previously crashed with 'Check failed: output(i).IsSet()' when tracing the new runtime-negative tests. Adds three XLA tests covering runtime negative inputs (1D, 1D with binary_output, and 2D), all wrapped in with self.session(): per XLATestCase requirements. Closes #117960
1 parent 4f1b7de commit 4155e48

4 files changed

Lines changed: 110 additions & 1 deletion

File tree

‎tensorflow/compiler/tests/BUILD‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2613,7 +2613,9 @@ tf_xla_py_strict_test(
26132613
":xla_test",
26142614
"//tensorflow/python/compat:v2_compat",
26152615
"//tensorflow/python/eager:def_function",
2616+
"//tensorflow/python/framework:dtypes",
26162617
"//tensorflow/python/framework:errors",
2618+
"//tensorflow/python/framework:tensor_spec",
26172619
"//tensorflow/python/ops:array_ops",
26182620
"//tensorflow/python/ops:math_ops_gen",
26192621
"//tensorflow/python/platform:test",

‎tensorflow/compiler/tests/bincount_op_test.py‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
from tensorflow.compiler.tests import xla_test
1717
from tensorflow.python.compat import v2_compat
1818
from tensorflow.python.eager import def_function
19+
from tensorflow.python.framework import dtypes
1920
from tensorflow.python.framework import errors
21+
from tensorflow.python.framework import tensor_spec
2022
from tensorflow.python.ops import array_ops
2123
from tensorflow.python.ops import gen_math_ops
2224
from tensorflow.python.platform import googletest
@@ -68,6 +70,69 @@ def f():
6870
):
6971
self.evaluate(f())
7072

73+
def testNegativeInputRuntimeIgnored(self):
74+
"""Runtime (non-constant) negative values should be silently ignored.
75+
76+
When the input is not a compile-time constant, we cannot raise a runtime
77+
error from within the XLA computation. Instead, negative values should be
78+
ignored (not counted) so they don't corrupt the output. This tests the
79+
fix for b/117960 where negative values under jit_compile=True silently
80+
produced wrong results.
81+
"""
82+
with self.session():
83+
with self.test_scope():
84+
85+
@def_function.function(
86+
jit_compile=True,
87+
input_signature=[tensor_spec.TensorSpec([5], dtypes.int32)])
88+
def f(arr):
89+
return gen_math_ops.dense_bincount(
90+
input=arr, size=5, weights=[]
91+
)
92+
93+
# Input with negative values — they should be ignored, not counted.
94+
# Expected: [1, 1, 1, 0, 0] (counts of 0, 1, 2; -1 and -2 ignored)
95+
result = self.evaluate(
96+
f(array_ops.constant([0, -1, 1, -2, 2], dtype=dtypes.int32)))
97+
self.assertAllEqual([1, 1, 1, 0, 0], result)
98+
99+
def testNegativeInputRuntimeIgnoredBinary(self):
100+
"""Runtime negative values with binary_output should be ignored."""
101+
with self.session():
102+
with self.test_scope():
103+
104+
@def_function.function(
105+
jit_compile=True,
106+
input_signature=[tensor_spec.TensorSpec([5], dtypes.int32)])
107+
def f(arr):
108+
return gen_math_ops.dense_bincount(
109+
input=arr, size=5, weights=[], binary_output=True
110+
)
111+
112+
result = self.evaluate(
113+
f(array_ops.constant([0, -1, 0, -2, 4], dtype=dtypes.int32)))
114+
self.assertAllEqual([1, 0, 0, 0, 1], result)
115+
116+
def testNegativeInputRuntime2d(self):
117+
"""Runtime negative values in 2D input should be ignored."""
118+
with self.session():
119+
with self.test_scope():
120+
121+
@def_function.function(
122+
jit_compile=True,
123+
input_signature=[tensor_spec.TensorSpec([2, 3], dtypes.int32)])
124+
def f(arr):
125+
return gen_math_ops.dense_bincount(
126+
input=arr, size=4, weights=[]
127+
)
128+
129+
# Row 0: [0, -1, 2] -> counts: [1, 0, 1, 0]
130+
# Row 1: [1, -3, 3] -> counts: [0, 1, 0, 1]
131+
result = self.evaluate(
132+
f(array_ops.constant(
133+
[[0, -1, 2], [1, -3, 3]], dtype=dtypes.int32)))
134+
self.assertAllEqual([[1, 0, 1, 0], [0, 1, 0, 1]], result)
135+
71136

72137
if __name__ == "__main__":
73138
v2_compat.enable_v2_behavior()

‎tensorflow/compiler/tf2xla/kernels/bincount_op.cc‎

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,28 @@ class DenseBincountOp : public XlaOpKernel {
7373
OP_REQUIRES(ctx, rank <= 2,
7474
absl::InvalidArgumentError(absl::StrCat(
7575
"Shape must be at most rank 2 but is rank ", rank)));
76+
// When the input is a compile-time constant, validate that all values are
77+
// non-negative at compile time (matching the eager kernel's behavior).
78+
// When the input is a runtime tensor, we cannot raise a runtime error from
79+
// within the XLA computation. Instead, we set negative indices to
80+
// output_size (out of bounds) so that XLA Scatter silently ignores them.
7681
std::vector<int64_t> input_values;
77-
if (ctx->ConstantInputReshapedToIntVector(0, &input_values).ok()) {
82+
bool is_constant_input =
83+
ctx->ConstantInputReshapedToIntVector(0, &input_values).ok();
84+
if (is_constant_input) {
7885
for (int64_t value : input_values) {
7986
OP_REQUIRES(
8087
ctx, value >= 0,
8188
absl::InvalidArgumentError("Input arr must be non-negative!"));
8289
}
8390
}
91+
// For runtime (non-constant) inputs, set negative indices to output_size
92+
// (out of bounds) so that XLA Scatter silently ignores them.
93+
xla::XlaOp nonneg_mask;
94+
if (!is_constant_input) {
95+
auto input_zero = xla::Zero(ctx->builder(), input_xla_type);
96+
nonneg_mask = xla::Ge(input, input_zero);
97+
}
8498
xla::XlaOp weights = ctx->Input(2);
8599
absl::StatusOr<xla::Shape> weights_shape_or =
86100
ctx->builder()->GetShape(weights);
@@ -130,6 +144,22 @@ class DenseBincountOp : public XlaOpKernel {
130144
i, {input_shape.dimensions(0) * input_shape.dimensions(1), 1});
131145
auto j = xla::Reshape(
132146
input, {input_shape.dimensions(0) * input_shape.dimensions(1), 1});
147+
// For runtime inputs, set negative indices to output_size (out of
148+
// bounds) so XLA Scatter silently ignores them. This is better than
149+
// clamping to 0 because it avoids overwriting legitimate bin 0 values
150+
// when binary_output=True (where scatter assigns rather than adds).
151+
if (!is_constant_input) {
152+
auto flat_mask = xla::Reshape(
153+
nonneg_mask,
154+
{input_shape.dimensions(0) * input_shape.dimensions(1), 1});
155+
auto oob_value =
156+
xla::ConstantR0(ctx->builder(), static_cast<int64_t>(output_size));
157+
oob_value = xla::ConvertElementType(oob_value, input_xla_type);
158+
auto oob_broadcast = xla::Broadcast(
159+
oob_value,
160+
{input_shape.dimensions(0) * input_shape.dimensions(1), 1});
161+
j = xla::Select(flat_mask, j, oob_broadcast);
162+
}
133163
std::vector<xla::XlaOp> iotas_to_concat;
134164
iotas_to_concat.push_back(i);
135165
iotas_to_concat.push_back(j);
@@ -145,6 +175,16 @@ class DenseBincountOp : public XlaOpKernel {
145175
}
146176
} else {
147177
input = xla::Reshape(input, {size, 1});
178+
// For runtime inputs, set negative indices to output_size (out of
179+
// bounds) so XLA Scatter silently ignores them.
180+
if (!is_constant_input) {
181+
auto oob_value =
182+
xla::ConstantR0(ctx->builder(), static_cast<int64_t>(output_size));
183+
oob_value = xla::ConvertElementType(oob_value, input_xla_type);
184+
auto oob_broadcast = xla::Broadcast(oob_value, {size, 1});
185+
input = xla::Select(xla::Reshape(nonneg_mask, {size, 1}), input,
186+
oob_broadcast);
187+
}
148188
idx = xla::Reshape(input, {size, 1});
149189
updates = xla::Broadcast(one, {size});
150190
output = xla::Broadcast(zero, {output_size});

‎tensorflow/core/ops/math_ops.cc‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1828,6 +1828,8 @@ REGISTER_OP("DenseBincount")
18281828
c->set_output(0, c->MakeShape({size_val}));
18291829
} else if (c->Rank(c->input(0)) == 2) {
18301830
c->set_output(0, c->MakeShape({c->Dim(c->input(0), 0), size_val}));
1831+
} else {
1832+
c->set_output(0, c->UnknownShape());
18311833
}
18321834
return absl::OkStatus();
18331835
});

0 commit comments

Comments
 (0)