Skip to content

Add(MatMul(...), [1, N]) fails in tf.function with "bias must be 1-dimensional" while eager works #126661

Closed

Description

@laolvfan

Issue type

Bug

Have you reproduced the bug with TensorFlow Nightly?

Yes. I reproduced this with:

tf-nightly 2.22.0-dev20260830 (v1.12.1-144425-gba73aa33d57)

Source

binary

TensorFlow version

tf-nightly 2.22.0-dev20260830 (v1.12.1-144425-gba73aa33d57), also reproduced on 2.21.0 (v2.21.0-rc1-5-ga481b10260d)

Custom code

Yes

OS platform and distribution

Linux Ubuntu 20.04.4 LTS (x86_64)

Python version

3.11

Current behavior?

Adding a [1, N] tensor to the result of a MatMul runs fine in eager, but raises InvalidArgumentError: bias must be 1-dimensional once the same function is wrapped in tf.function.

tf.add broadcasts [N, N] with [1, N] to [N, N], which is what eager does. In graph mode the remapper fuses Add(MatMul(...), y) into a fused MatMul-with-bias, and that fused form requires the bias operand to be 1-D. The fusion appears to fire whenever the addend is broadcast-compatible, without checking that it is actually rank 1, so the resulting graph contains an op whose contract the operand does not satisfy and the failure only surfaces at execution time.

The MatMul is required to trigger it: tf.add(a, b) on its own with the same shapes traces and runs correctly. A 1-D bias [N] and a column [N, 1] are both fine as well, so it is specifically the [1, N] case that is mis-fused.

Note that jit_compile=True also runs correctly, so this affects the default graph path rather than XLA.

Standalone code to reproduce the issue

import tensorflow as tf


def f(x):
    return tf.add(tf.matmul(x, x, transpose_a=True), tf.matmul(tf.ones([1, 1]), x))


x = tf.random.normal([1, 7])

print("eager :", tuple(f(x).shape))
print("traced:", tuple(tf.function(f)(x).shape))

Relevant log output

eager : (7, 7)
Traceback (most recent call last):
  ...
tensorflow.python.framework.errors_impl.InvalidArgumentError: Graph execution error:

bias must be 1-dimensional[1,7]
	 [[{{node Add}}]] [Op:__inference_f_29]

Expected: traced: (7, 7), matching eager.

Activity

  1. added
    comp:grappler
    comp:ops
    2.21.0
    on Sep 4, 2026
  2. Venkat6871 commented on Sep 4, 2026

    @Venkat6871
    Contributor

    Hi @laolvfan ,
    Thank you for raising your concern here.
    A fix for this issue has been proposed in this PR1, PR2. Once it is merged, this issue will be resolved.

    Hi @adi-IL , @PranshulSoni,
    Thank you for raising the PR to address this issue. We truly appreciate your initiative and the effort in contributing the fix.

  3. google-ml-butler commented on Sep 25, 2026

    @google-ml-butler

    Are you satisfied with the resolution of your issue?
    Yes
    No

  4. added a commit that references this issue on Sep 25, 2026
    24c6e61
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

2.21.0awaiting PR mergecomp:grapplercomp:opstype:bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions