XLA still silently executes MatMul with an incompatible size-1 contracting dimension after #125677 (follow-up to #117771)
Issue type
Bug
Have you reproduced the bug with TensorFlow Nightly?
Yes
Source
source
TensorFlow version
2.21.0 and tf-nightly 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?
#117771 reported that jit_compile=True silently executes a MatMul whose inner dimensions don't match. PR #125677 fixed this by checking that contracting dimensions "must match or be 1". When one of the contracting dimensions is 1, XLA still broadcasts it instead of raising. TF's MatMul does not allow this: eager and non-XLA tf.function raise Matrix size-incompatible. Under XLA, a [1,4] x [1,1] matmul returns [[10.]], which is sum(x) * w.
The same thing happens with tf.linalg.matvec and tf.tensordot, and when the size-1 dimension comes from a data-dependent shape (e.g. tf.math.confusion_matrix, tf.ragged.boolean_mask).
On 2.21.0 both XLA calls in the repro return values ([[42.0]] and [[10.0]]). On tf-nightly 2.23.0-dev20260930 the #117771 case now raises, but [1,4] x [1,1] still returns [[10.0]].
Expected: XLA should raise for [1,4] x [1,1] like eager does. xla::Einsum / BatchDot explicitly allow size-1 contracting dimensions (EinsumBroadcastingContractingDims in matrix_test.cc), so the TF-semantics check probably belongs in the tf2xla MatMul / BatchMatMul kernels.
Standalone code to reproduce the issue
import tensorflow as tf
x = tf.constant([[2.0, 4.0, 6.0, 8.0]]) # [1, 4]
def check(name, w_init):
w = tf.Variable(w_init, shape=tf.TensorShape(None)) # unknown static shape, as in #117771
f = lambda: tf.matmul(x, w)
for mode, fn in (("eager", f), ("XLA", tf.function(f, jit_compile=True))):
try:
print(f"{name} {mode}: {fn().numpy().tolist()}")
except Exception as e:
print(f"{name} {mode}: {type(e).__name__}: {str(e).splitlines()[0][:100]}")
check("[1,4]x[6,1] (#117771)", [[0.1], [0.2], [0.3], [0.4], [0.5], [0.6]])
check("[1,4]x[1,1]", [[0.5]])Relevant log output
[1,4]x[6,1] (#117771) eager: InvalidArgumentError: ... Matrix size-incompatible ...
[1,4]x[6,1] (#117771) XLA: InvalidArgumentError: Contracting dimensions 1 of LHS and 0 of RHS must match or be 1. Got 4 and 6.
[1,4]x[1,1] eager: InvalidArgumentError: ... Matrix size-incompatible ...
[1,4]x[1,1] XLA: [[10.0]]Source: tensorflow/tensorflow