Skip to content

[FusedOps] Support broadcast-shaped bias with leading 1s in fused MatMul and Conv2D - #126688

Merged
copybara-service[bot] merged 1 commit into
tensorflow:masterfrom
adi-IL:fix/fused-matmul-multidim-bias
Sep 25, 2026
Merged

copybara-service[bot] merged 1 commit into
tensorflow:masterfrom
adi-IL:fix/fused-matmul-multidim-bias

Conversation

@adi-IL

@adi-IL adi-IL commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

Fixes #126661

Description

When Grappler remapper fuses MatMul or Conv2D with an Add whose broadcasted bias tensor has leading 1s (such as [1, N] or [1, 1, N]), the fused node wires the multi-dimensional bias tensor directly into the fused op.

While MklFusedMatMulOp in mkl_matmul_op_fused.cc already supports bias_dims > 1 when all leading dimensions are 1, InitBiasAddArgs in fused_eigen_output_kernels.h, LaunchFusedMatMulOp in matmul_op_fused.cc (GPU), and LaunchFusedConv2DOp in conv_ops_fused_impl.h strictly enforced bias.dims() == 1. When _FusedMatMul executed on the Eigen CPU path (such as inside a tf.function), it failed with InvalidArgumentError: bias must be 1-dimensional.

This patch updates InitBiasAddArgs, LaunchFusedMatMulOp (GPU), and LaunchFusedConv2DOp to validate that for bias tensors with rank >= 1, all dimensions except the last are 1. This aligns the Eigen and GPU kernels with the remapper and MKL implementations.

Verification

…d Conv2D

When Grappler remapper fuses MatMul or Conv2D with Add, the bias tensor
can have broadcast dimensions where leading axes are 1 (e.g. [1, N]).
While mkl_matmul_op_fused allows bias_dims > 1 with leading 1s,
InitBiasAddArgs in fused_eigen_output_kernels.h, GPU LaunchFusedMatMulOp,
and LaunchFusedConv2DOp strictly required bias.dims() == 1. This caused
InvalidArgumentError: bias must be 1-dimensional during graph execution
in tf.function.

Allow bias tensors with rank >= 1 whose leading dimensions are 1 in
InitBiasAddArgs, GPU LaunchFusedMatMulOp, and LaunchFusedConv2DOp,
aligning the Eigen and GPU kernels with the remapper and MKL implementations.
Add test coverage in matmul_op_test.cc and matmul_op_test.py.

Fixes tensorflow#126661
@google-ml-butler google-ml-butler Bot added the size:M CL Change Size: Medium label Sep 2, 2026
@nithyak0204
nithyak0204 requested a review from a team September 3, 2026 08:54
@google-ml-butler google-ml-butler Bot added the awaiting review Pull request awaiting review label Sep 3, 2026
@nithyak0204 nithyak0204 added the prtype:bugfix PR to fix a bug label Sep 3, 2026
@github-project-automation github-project-automation Bot moved this to Assigned Reviewer in PR Queue Sep 3, 2026
@nithyak0204 nithyak0204 added the comp:core issues related to core part of tensorflow label Sep 3, 2026
@dmiltr3
dmiltr3 self-requested a review September 3, 2026 16:03
@google-ml-butler google-ml-butler Bot added kokoro:force-run Tests on submitted change ready to pull PR ready for merge process labels Sep 3, 2026
@github-project-automation github-project-automation Bot moved this from Assigned Reviewer to Approved by Reviewer in PR Queue Sep 3, 2026
@nithyak0204 nithyak0204 removed awaiting review Pull request awaiting review kokoro:force-run Tests on submitted change labels Sep 4, 2026
@copybara-service
copybara-service Bot merged commit 908220b into tensorflow:master Sep 25, 2026
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp:core issues related to core part of tensorflow prtype:bugfix PR to fix a bug ready to pull PR ready for merge process size:M CL Change Size: Medium

Projects

Status: Merged

Development

Successfully merging this pull request may close these issues.

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

4 participants