Pull Request已成功合入, 合并人@CANN-robot
(感谢 dx 的贡献)变更摘要
此 PR 新增了 matrix_inverse 算子的 golden 参考实现,通过在 examples/common/golden/ 目录下添加 matrix_inverse.hpp 头文件,提供了基于 LU 分解(带部分主元选取)的矩阵求逆模板函数,用于为 MatrixInverse 算子生成预期输出结果。同时在 golden.hpp 中引入了对新头文件的包含。
主要改动
- 新增
matrix_inverse.hpp头文件:在examples/common/golden/matrix_inverse.hpp中定义了Catlass::golden命名空间下的三个模板函数——SgetrfRowMajor(行主序 LU 分解)、SgetrsRowMajor(基于 LU 因子求解线性方程组)和ComputeInverseInplace(原地矩阵求逆),作为 matrix_inverse 算子的 golden 参考实现。 SgetrfRowMajor实现带部分主元选取的 LU 分解:遍历矩阵对角线,逐列选取最大绝对值主元并进行行交换,若主元小于阈值则判定矩阵奇异并返回非零信息码;随后计算乘子并更新尾随子矩阵,结果存储在原地。SgetrsRowMajor支持求解A*X=B与A^T*X=B:根据trans参数区分是否转置,分别执行主元置换、前代(L 求解)和回代(U 求解)三步,结果覆盖写入B矩阵。ComputeInverseInplace组合 LU 分解与求解实现矩阵求逆:先调用SgetrfRowMajor进行 LU 分解,再构造单位矩阵作为右端项,通过SgetrsRowMajor求解得到逆矩阵并回写至输入矩阵。- 更新
golden.hpp:将版权年份从 2025 更新为 2026,并新增#include "golden/matrix_inverse.hpp"以注册新的 golden 头文件。


代码审查
现在进行最终扫描(boundary values、hot-path costs、co-located defects):
- N=0 / N=1 / nrhs=0 边界:所有循环在 N=0 时正确跳过(
k < 0为 false 的 for 循环不执行,k >= 0在k = -1时立即退出)。ipiv.resize(0)安全。work(N*N)当 N=0 时是空 vector。正确。 - 热路径开销:内层循环(三重嵌套)为标准 O(N³),循环内部无额外分配,每次迭代成本合理。
- 同位置二次缺陷:第 80 行周围未发现独立于空指针问题的其他逻辑错误(比较方向、索引/字段选取均正确)。
审查小结
| 优先级 | 数量 |
|---|---|
| P0 | 0 |
| P1 | 0 |
| P2 | 1 |
| P3 | 0 |
已审查的文件:
examples/common/golden.hpp— 无问题(仅 copyright 年份更新 + 新增 include,变更正确)examples/common/golden/matrix_inverse.hpp— 1 个问题(SgetrsRowMajor的trans参数空指针解引用风险)
整体风险评估:此变更风险较低。新增的 LU 分解与矩阵求逆算法逻辑正确,仅有 trans 参数缺少空指针防御这一处隐患——当前唯一调用点传入字符串字面量是安全的,但如果未来外部代码以 nullptr 调用,会触发 UB。建议在合并前加入一个简单的断言保护。
| 类型 | 数量 |
|---|---|
| 🔴 阻塞 | 1 |
| 🟡 建议 | 0 |
⛔ 需要修改


Thanks for your pull-request.
The full list of commands accepted by me can be found at here。
You can get sig-info at here
PR Approval Progress
✅ Congratulations! All modules have met the lgtm and approve requirements.
Module Approval Details
| module | lgtm status | approve status |
|---|---|---|
| repo-cann/catlass | ✅ sunhao_hw, yuantao_ (2/2) | ✅ yuantao_, sunhao_hw (2/1) |
💡 Tip:
- Committer can comment
/approveor/lgtm- Commenting
/approveimplies both code review (lgtm) and intent to merge (approve)
CLA Signature Pass
weixin_63557130, thanks for your pull request. All authors of the commits have signed the CLA. 👍


The MR is merging by another one
If you want to solve this problem, you can click here to do it in the FAQs.


Pull Request 已合并或已关闭。
If you want to solve this problem, you can click here to do it in the FAQs.


Pull Request 已合并或已关闭。
If you want to solve this problem, you can click here to do it in the FAQs.


描述
这个文件实现了 矩阵求逆(Matrix Inverse) 算子,属于 Catlass::golden 命名空间下的 CPU 端黄金参考实现(用于验证 Ascend NPU 上算子结果的正确性)。
关联的Issue
https://gitcode.com/cann/catlass/issues/331
原因
测试
文档更新
类型标签