Skip to content

Conversation

@ooooo-create
Copy link
Contributor

@ooooo-create ooooo-create commented Jul 27, 2025

PR Category

Operator Mechanism

PR Types

Bug fixes

Description

fix einsum_grad when contraction with broadcast
在遇到 AB 类型需要广播的时候,einsum('ij, ij -> j',Tensor(shape=[2,2]),Tensor(shape=[1,2]) 其中 i 是 AB 类型且发生了广播,在前向的 PerformContraction 阶段会 cache 在 matual 的输入结果(没有 resize 成可乘形状之前),给反向复用。
在反向调用 enisum("ij, i, -> ij") 时,shape 是 [1,2] 和 [2],后续 PerformContraction 阶段会直接使用之前的缓存结果,之后 Tensor 会被 resize 成 mul_dims,这个 mul_dims 是按照 labelshape 来计算的,直接使用 labelshape 的结果就会把 i 的 dim 认为是 1,但是其实前向 cache 的时候是 2,导致了后续错误,数据计算错误,计算梯度的 shape 是[1,2] 导致后续为了梯度形状还原到输入形状 [2,2] 直接 resize 被访问就报错了。
这里直接把反向的 labelshape 传入 EinsumKernelImpl(前向默认都是 0,不会触发后面的条件),在推导 labelshape 时对比形状是否一致,如果传入的比推出的大时(推出的为1,不一致的类型为 AO 或者 BO),就采用传入的形状

图片
@paddle-bot
Copy link

paddle-bot bot commented Jul 27, 2025

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@paddle-bot paddle-bot bot added the contributor External developers label Jul 27, 2025
@ooooo-create ooooo-create changed the title fix einsum_grad when contraction with broadcast [Accuracy diff No.84-85] Fix accuracy diff for paddle.einsum API Jul 27, 2025
@ooooo-create
Copy link
Contributor Author

/re-run all-failed

@luotao1 luotao1 added the HappyOpenSource Pro 进阶版快乐开源活动,更具挑战性的任务 label Jul 28, 2025
@ooooo-create
Copy link
Contributor Author

/re-run all-failed

@lshpku lshpku merged commit d650414 into PaddlePaddle:develop Jul 29, 2025
88 of 92 checks passed
@ooooo-create ooooo-create deleted the fix_einsum branch September 29, 2025 09:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

contributor External developers HappyOpenSource Pro 进阶版快乐开源活动,更具挑战性的任务

3 participants