Skip to content

Conversation

@cangtianhuang
Copy link
Contributor

@cangtianhuang cangtianhuang commented Jul 11, 2025

PR Category

Operator Mechanism

PR Types

Bug fixes

Description

修复 paddle.linalg.det 反向报错问题:

在输入有奇异矩阵时发生:
image

问题定位:

paddle.linalg.det 的梯度计算采用以下公式:

$$ \frac{\partial \text{det}(A)}{\partial A} = \text{adj}(A)^\text{T} = \text{det}(A) \cdot A^{-\text{T}} $$

直接通过矩阵逆运算实现,但奇异矩阵不可逆( $\text{det}(A)=0$$A^{-\text{T}}$ 不存在)导致计算失败。理论上,奇异矩阵的梯度应采用伴随矩阵 $\text{adj}(A)$ 直接计算。

此外,FoundZeroFunctor 直接进行零值判定,未考虑浮点数计算的误差,导致大量奇异矩阵被误判断为非奇异矩阵。

解决方案:

  1. 将零值判断改为使用 epsilon 阈值,if 避免写入竞争:
*res_ = (x_[idx] == static_cast<T>(0));
if (abs(x_[idx]) < FoundZeroEpsilon<RealType>::value()) { *res_ = true; }
  1. 将所有 phi::Multiply 运算提升为 MPType,并通过分块 {} 减少内存占用

后续修复:

本修复仅解决反向报错问题。如果输入均为非奇异矩阵,测试全部通过:

如果需要完美计算奇异矩阵,算法需进行非常大调整,该工作将在 paddle.linalg.lu 支持复数功能后进行:

@paddle-bot
Copy link

paddle-bot bot commented Jul 11, 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.

wanghuancoder
wanghuancoder previously approved these changes Jul 15, 2025
Copy link
Contributor

@wanghuancoder wanghuancoder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 这个改动有点儿大,在PR描述里介绍清楚吧。然后让书豪帮忙看看。我看不懂。

@lshpku lshpku merged commit 5d39519 into PaddlePaddle:develop Jul 16, 2025
90 of 92 checks passed
@cangtianhuang cangtianhuang deleted the fix-det-2 branch July 17, 2025 06:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants