tf.broadcast_to under jit_compile=True silently tiles instead of raising when the target dim is a multiple of the input dim
Issue type
Bug
Have you reproduced the bug with TensorFlow Nightly?
Yes
Source
source
TensorFlow version
2.21.0, tf-nightly 2.22.0-dev20260905 and 2.23.0-dev20260930 (CPU)
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.broadcast_to(x, shape) with x of shape [1, 2] and shape = [2, 6] is invalid (dim 2 → 6), and eager raises Incompatible shapes: [1,2] vs. [2,6]. Under jit_compile=True, it returns a (2, 6) tensor with [1, 2] repeated three times per row.
The tf2xla BroadcastToOp calls xla::BroadcastTo directly. That helper accepts any output dim that is a multiple of the input dim and tiles to fill it (output % input == 0 in xla/hlo/builder/lib/broadcast.cc). TF's broadcasting rule (dims must match or be 1) is never checked. When shape is a constant, graph shape inference catches it while tracing. When shape is a function argument or computed, nothing does.
Expected: XLA should raise InvalidArgumentError like eager. A BCast-style compatibility check in the tf2xla BroadcastToOp before calling xla::BroadcastTo would match TF semantics.
Standalone code to reproduce the issue
import tensorflow as tf
def f(shape):
return tf.broadcast_to(tf.constant([[1., 2.]]), shape)
shape = tf.constant([2, 6])
for mode, fn in (("eager", f), ("XLA", tf.function(f, jit_compile=True))):
try:
print(mode, fn(shape).numpy().tolist())
except Exception as e:
print(mode, type(e).__name__, str(e).splitlines()[0][:120])Relevant log output
eager InvalidArgumentError {{function_node __wrapped__BroadcastTo_device_/job:localhost/replica:0/task:0/device:CPU:0}} Incompatible shapes: [1,2] vs. [2,6] [Op:BroadcastTo]
XLA [[1.0, 2.0, 1.0, 2.0, 1.0, 2.0], [1.0, 2.0, 1.0, 2.0, 1.0, 2.0]]Source: tensorflow/tensorflow