Repository navigation
[Grappler/Kernels] Support multi-dimensional bias with leading ones in fused MatMul and Conv2D - #126737
[Grappler/Kernels] Support multi-dimensional bias with leading ones in fused MatMul and Conv2D#126737PranshulSoni wants to merge 1 commit into
Conversation
…Conv2D Fixes tensorflow#126661 When Grappler Remapper fuses an Add node into _FusedMatMul or _FusedConv2D with a broadcastable bias shape (e.g. [1, N] where leading dimensions are 1), the non-oneDNN CPU kernel (InitBiasAddArgs) and GPU kernels strictly checked bias.dims() == 1, raising: 'InvalidArgumentError: bias must be 1-dimensional'. This commit: 1. Updates InitBiasAddArgs in fused_eigen_output_kernels.h to accept bias tensors with rank >= 1 as long as all leading dimensions are 1, matching oneDNN's MklFusedMatMulOp behavior. 2. Updates GPU fused MatMul and Conv2D kernels to similarly accept bias tensors with leading 1 dimensions. 3. Guards IsBiasSemanticAdd in remapper.cc from fusing when the broadcast shape has higher rank than the contraction output tensor. 4. Adds C++ unit tests in remapper_test.cc and matmul_op_test.cc, and a Python regression test in remapper_test.py.
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
|
Hi @PranshulSoni, Can you please sign the CLA. Thank you! |
dmiltr3
left a comment
There was a problem hiding this comment.
Thank you for your contribution and for addressing multi-dimensional bias support for fused MatMul and Conv2D operations (#126661)!
We've reviewed your pull request and identified a few necessary updates and improvements:
1. Rebase Against master
A related PR (#126688) was recently merged into master, applying kernel relaxations in fused_eigen_output_kernels.h, conv_ops_fused_impl.h, and matmul_op_fused.cc. However, that change did not include your crucial Grappler Remapper rank guard in remapper.cc:711 (if (bcast_shape.dim_size() > shape.dim_size()) return false;), nor did it have your 4-way transpose tests.
Please rebase your branch on latest master and resolve the overlapping kernel edits, while preserving your Remapper fix and enhanced tests.
2. Missing Channel Dimension Validation in GPU Kernel
In tensorflow/core/kernels/matmul_op_fused.cc inside LaunchFusedMatMulOp, the GPU launch path should verify that the inner bias dimension matches matrix column size n:
OP_REQUIRES(context, bias.dim_size(bias.dims() - 1) == n,
absl::InvalidArgumentError(absl::StrCat(
"bias depth must equal matrix column size: ",
bias.shape().DebugString(), " vs. ", n)));This protects against out-of-bounds GPU reads if _FusedMatMul is invoked with mismatched inner bias dimension. Also prefer idiomatic OP_REQUIRES over if (cond) OP_REQUIRES_OK(context, ...).
3. Test Coverage
- In
tensorflow/core/kernels/matmul_op_test.cc, please ensure yourVerifyMatMulWith2DBiastest (covering all 4 transpose combinationsFF,TF,FT,TTagainst un-fused MatMul) is retained during the rebase. Please also include negative test cases verifying that invalid bias shapes (e.g.,[2, N],[0, N]) returnINVALID_ARGUMENT. - In
tensorflow/core/kernels/conv_ops_test.cc, please add a unit test exercising_FusedConv2Dwith multi-dimensional bias (e.g.,[1, 1, 1, C]or[1, C]).
Could you please rebase on latest master and update the PR with these adjustments? Thanks again!
|
This PR is stale because it has been open for 14 days with no activity. It will be closed if no further activity occurs. Thank you. |
|
This PR was closed because it has been inactive for 14 days since being marked as stale. Please reopen if you'd like to work on this further. |
Fixes #126661
Description
Adding a
[1, N]tensor to the result of aMatMul(specifically whentranspose_a=True) orConv2Dsucceeds in eager execution via broadcasting. However, when wrapped intf.function, graph execution crashes with:Root Cause
remapper.cc:705-725),IsBiasSemanticAddmatches anAddnode with an addend having shape[1, N](since all leading dimensions before the channel dimension are 1). Remapper replaces the node with_FusedMatMul(or_FusedConv2D), passing the[1, N]tensor as the bias operand.mkl_layout_pass.cc,FusedMatMulRewriteskips rewriting_FusedMatMulto_MklNativeFusedMatMulwhentranspose_a=Trueto avoid reorder overhead. On GPU or non-oneDNN builds, the op also remains_FusedMatMul.fused_eigen_output_kernels.h,InitBiasAddArgsstrictly checkedif (bias.dims() != 1)and failed withInvalidArgumentError. Similarly, the GPU fused MatMul and Conv2D kernels strictly assertedbias.dims() == 1. However, the memory layout of any[1, ..., 1, N]tensor is contiguous and identical to a 1D vector of lengthN. In oneDNN'smkl_matmul_op_fused.ccandmkl_conv_ops.h, this relaxed check had already been implemented.Solution
fused_eigen_output_kernels.h, updatedInitBiasAddArgsto allowbias.dims() >= 1as long as all leading dimensions before the last (channel) dimension are 1.matmul_op_fused.ccandconv_ops_fused_impl.h, updated GPULaunchFusedMatMulOpandFusedConv2Dbias validation to acceptbias.dims() >= 1with leading ones.remapper.cc:711, addedif (bcast_shape.dim_size() > shape.dim_size()) return false;to prevent fusing when the broadcast shape would expand the output tensor's rank.FuseMatmulTransposeAWithAddandDoNotFuseMatmulWithAddHigherRankinremapper_test.cc.VerifyMatMulWith2DBiasandMatMul2DBiasinmatmul_op_test.cctesting all 4 transpose combinations (FF,TF,FT,TT) with[1, N]bias.test_matmul_transpose_a_broadcast_bias_addinremapper_test.py.