[PHI][CINN] Align linalg.solve kernel with torch #72608
Merged
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
与torch对齐
paddle.linalg.solve的调用逻辑,提升数百倍性能,并修复大Tensor下的报错简介
linalg.solve用于求解矩阵方程
A @ X = B,其中A[batch_size, n, n],X[batch_size, n, nrhs],B[batch_size, n, nrhs](batch_size可以没有或者是多维)求解方法
首先对A做PLU分解,得到
P @ A = L @ U;其中P是一个permutation矩阵(实际实现中使用pivot数组记录),L是一个对角线全为1的下三角矩阵,U是一个上三角矩阵然后将
P @ A = L @ U做等价变换得到A = P^T @ L @ U,然后将A带入原来的方程A @ X = B中,得到P^T @ L @ U @ X = B接下来把P^T移到右边,得到
L @ U @ X = P @ B,由于P是一个permutation矩阵,因此这步只需要scatter操作即可,不需要真的矩阵乘最后进行两次triangular求解,将L和U依次移动到右边,即
L @ U @ X = P @ B==第1次求解=>U @ X = L^-1 @ P @ B==第2次求解=>X = U^-1 @ L^-1 @ P @ B此时
X = U^-1 @ L^-1 @ P @ B即是方程的解注:由于cublas使用列优先存储,而paddle使用行优先,出于性能考虑并没有对A在输入时进行转置(和torch保持一致),因此上述公式中A实际上是A^T,实际求解方程是
X = P^T @ L^T^-1 @ U^T^-1 @ B,cublas调用方法大致相同,只是顺序不同执行过程
参考torch实现:lu_solve_kernel
与torch对齐情况
关于int使用的说明
注意到,我在kernel中很多地方没有把int转成int64_t,包括n、nrhs,这是因为:
Pcard-85711