【UnitTestFix No.14】fix test_matmul_v2_op.py#75909
Merged
luotao1 merged 1 commit intoPaddlePaddle:developfrom Oct 21, 2025
Merged
【UnitTestFix No.14】fix test_matmul_v2_op.py#75909luotao1 merged 1 commit intoPaddlePaddle:developfrom
luotao1 merged 1 commit intoPaddlePaddle:developfrom
Conversation
…t shape for 1-D tensors in both forward and backward passes. This includes adjustments in the `details.h` and `matmul_grad_kernel_impl.h` files to handle reshaping of gradients appropriately.
|
你的PR提交成功,感谢你对开源项目的贡献! |
Contributor
Author
|
/re-run all-failed |
A-nnonymous
approved these changes
Oct 20, 2025
Contributor
A-nnonymous
left a comment
There was a problem hiding this comment.
LGTM in squeezing logics
YqGe585
approved these changes
Oct 20, 2025
luotao1
approved these changes
Oct 21, 2025
31 tasks
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.
PR Category
Operator Mechanism
PR Types
Bug fixes
Description
This PR fixes a critical issue where the
matmul_v2gradient operation produces inconsistent gradient shapes between eager mode and static compilation mode when the inputs are 1-D tensors.Problem
(n,)for 1-D inputs.check_prim_pir=True) incorrectly returns 2-D gradients(n, 1)or(1, n)for 1-D inputs.Root Cause
The shape inconsistency occurs in two places:
Composite implementation (
paddle/fluid/primitive/decomp_rule/decomp_vjp/details.h):When
check_prim_pir=True, the compositematmul_gradfunction unsqueezes 1-D inputs to 2-D for computation but does not convert the resulting 2-D gradients back to 1-D.Kernel implementation (
paddle/phi/kernels/impl/matmul_grad_kernel_impl.h):Internal matrix operations produce 2-D gradients that are not reshaped back to 1-D for 1-D inputs.
Fix
Changes
matmul_gradindetails.hto handle 1-D gradient shapes.MatmulGradKernelinmatmul_grad_kernel_impl.hfor consistency.This fix ensures consistent behavior between eager and static compilation modes, resolving gradient shape mismatch errors in
matmul_v2backpropagation.问题描述
(n,)。check_prim_pir=True):错误地返回 2 维梯度(n,1)或(1,n)。根本原因
问题出现在 两个地方:
复合实现(
paddle/fluid/primitive/decomp_rule/decomp_vjp/details.h):当
check_prim_pir=True时,matmul_grad复合函数会将 1 维输入 unsqueeze 为 2 维 来进行矩阵运算,但 没有在梯度返回时再 squeeze 回 1 维。内核实现(
paddle/phi/kernels/impl/matmul_grad_kernel_impl.h):内部矩阵计算同样会生成 2 维梯度,未根据原始输入形状进行还原。
解决方案
修改内容
details.h中的matmul_grad复合函数,支持 1 维梯度形状处理。matmul_grad_kernel_impl.h中的MatmulGradKernel,保证与复合逻辑一致。此修复确保了 eager 与静态编译模式下梯度计算行为一致,彻底解决了
matmul_v2反向传播中的形状不匹配问题。@luotao1 @YqGe585