Repository navigation
[FusedOps] Support broadcast-shaped bias with leading 1s in fused MatMul and Conv2D - #126688
Merged
copybara-service[bot] merged 1 commit intoSep 25, 2026
Conversation
…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
dmiltr3
approved these changes
Sep 3, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
MklFusedMatMulOpinmkl_matmul_op_fused.ccalready supportsbias_dims > 1when all leading dimensions are 1,InitBiasAddArgsinfused_eigen_output_kernels.h,LaunchFusedMatMulOpinmatmul_op_fused.cc(GPU), andLaunchFusedConv2DOpinconv_ops_fused_impl.hstrictly enforcedbias.dims() == 1. When_FusedMatMulexecuted on the Eigen CPU path (such as inside atf.function), it failed withInvalidArgumentError: bias must be 1-dimensional.This patch updates
InitBiasAddArgs,LaunchFusedMatMulOp(GPU), andLaunchFusedConv2DOpto 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
tf.functionand confirmed the shape and numerical output match eager execution.FusedMatMulWithBiasOpTest.MatMulWithBroadcastBiasinmatmul_op_test.cc.MatMulAddFusionTest.testMatMulAddBroadcastInTfFunctioninmatmul_op_test.py.