Skip to content

【Hackathon 8th No.1】add lu_solve api for paddle -part #71030

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 36 commits into from
Mar 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
7a6ccd5
add cpu
decade-afk Feb 1, 2025
fdb233c
add test
decade-afk Feb 9, 2025
6ed2302
fix
decade-afk Feb 9, 2025
616c471
fix
decade-afk Feb 10, 2025
276955d
fix
decade-afk Feb 10, 2025
0c73108
test
decade-afk Feb 10, 2025
29acb14
test
decade-afk Feb 10, 2025
f4229ea
test
decade-afk Feb 10, 2025
3d069ca
fix
decade-afk Feb 10, 2025
6be155f
test
decade-afk Feb 11, 2025
5c5097d
test
decade-afk Feb 11, 2025
20030cf
fix
decade-afk Feb 11, 2025
033dd58
fix
decade-afk Feb 12, 2025
f14c6ee
fix
decade-afk Feb 12, 2025
bf9cce3
fix codestyle
decade-afk Feb 12, 2025
c9b8212
add cpu
decade-afk Feb 1, 2025
8f3a34b
Merge branch 'new_solve' of work:decade-afk/Paddle into new_solve
decade-afk Feb 12, 2025
c2d0d36
c++ test
decade-afk Feb 13, 2025
6c70f66
Merge branch 'PaddlePaddle:develop' into new_solve
decade-afk Feb 15, 2025
6635082
fix
decade-afk Feb 15, 2025
d51f22c
fix
decade-afk Feb 24, 2025
eec8697
Merge branch 'PaddlePaddle:develop' into new_solve
decade-afk Feb 24, 2025
384f4f8
fix
decade-afk Feb 24, 2025
fd905bc
fix codestyle
decade-afk Feb 24, 2025
c2c4000
fix
decade-afk Feb 25, 2025
3cd7595
Merge branch 'PaddlePaddle:develop' into new_solve
decade-afk Feb 25, 2025
72e961f
fix
decade-afk Feb 28, 2025
0826d57
fix
decade-afk Feb 28, 2025
edde871
Merge branch 'new_solve' of work:decade-afk/Paddle into new_solve
decade-afk Feb 28, 2025
8f19ce3
fix
decade-afk Feb 28, 2025
07ff6f4
fix
decade-afk Feb 28, 2025
9d3ae1e
fix
decade-afk Mar 5, 2025
f537bad
fix example
decade-afk Mar 7, 2025
b2784f7
fix
decade-afk Mar 7, 2025
92ef570
Update python/paddle/tensor/linalg.py
decade-afk Mar 7, 2025
28275fd
Update test/legacy_test/test_lu_solve_op.py
decade-afk Mar 7, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3475,6 +3475,17 @@ bool LstmOpInferSymbolicShape(pir::Operation *op,

return true;
}
bool LuSolveOpInferSymbolicShape(
pir::Operation *op, pir::InferSymbolicShapeContext *infer_context) {
const auto &b_shape_or_data =
infer_context->GetShapeOrDataForValue(op->operand_source(0));
const std::vector<symbol::DimExpr> &b_shape = b_shape_or_data.shape();
infer_context->SetShapeOrDataForValue(
op->result(0),
symbol::ShapeOrDataDimExprs{symbol::TensorShapeOrDataDimExprs(b_shape)});

return true;
}

// bool MergedAdamOpInferSymbolicShape(pir::Operation *op,
// pir::InferSymbolicShapeContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ OP_DECLARE_INFER_SYMBOLIC_SHAPE(LinearInterp)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(LlmInt8Linear)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(Logspace)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(Lstm)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(LuSolve)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(MatchMatrixTensor)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(MaskedMultiheadAttention_)
// OP_DECLARE_INFER_SYMBOLIC_SHAPE(MergedAdam)
Expand Down
2 changes: 2 additions & 0 deletions paddle/phi/backends/dynload/cusolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ CUSOLVER_ROUTINE_EACH(DECLARE_DYNAMIC_LOAD_CUSOLVER_WRAP);
__macro(cusolverDnSgesvdj); \
__macro(cusolverDnDgesvdj); \
__macro(cusolverDnSgetrf); \
__macro(cusolverDnSgetrs); \
__macro(cusolverDnDgetrs); \
__macro(cusolverDnDgetrf); \
__macro(cusolverDnCgetrf); \
__macro(cusolverDnZgetrf); \
Expand Down
22 changes: 22 additions & 0 deletions paddle/phi/backends/dynload/lapack.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,26 @@ extern "C" void dgetrf_(
extern "C" void sgetrf_(
int *m, int *n, float *a, int *lda, int *ipiv, int *info);

// getrs_
extern "C" void sgetrs_(char *trans,
int *n,
int *nrhs,
float *a,
int *lda,
int *ipiv,
float *b,
int *ldb,
int *info);
extern "C" void dgetrs_(char *trans,
int *n,
int *nrhs,
double *a,
int *lda,
int *ipiv,
double *b,
int *ldb,
int *info);

// evd
extern "C" void zheevd_(char *jobz,
char *uplo,
Expand Down Expand Up @@ -339,6 +359,8 @@ extern void *lapack_dso_handle;
#define LAPACK_ROUTINE_EACH(__macro) \
__macro(dgetrf_); \
__macro(sgetrf_); \
__macro(sgetrs_); \
__macro(dgetrs_); \
__macro(zheevd_); \
__macro(cheevd_); \
__macro(dsyevd_); \
Expand Down
2 changes: 2 additions & 0 deletions paddle/phi/backends/dynload/rocsolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ extern void *rocsolver_dso_handle;
__macro(rocsolver_dpotrs); \
__macro(rocsolver_cpotrs); \
__macro(rocsolver_zpotrs); \
__macro(rocsolver_sgetrs); \
__macro(rocsolver_dgetrs); \
__macro(rocsolver_sgetrf); \
__macro(rocsolver_dgetrf); \
__macro(rocsolver_cgetrf); \
Expand Down
23 changes: 23 additions & 0 deletions paddle/phi/kernels/cpu/lu_solve_grad_kernel.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "paddle/phi/backends/cpu/cpu_context.h"
#include "paddle/phi/core/kernel_registry.h"

#include "paddle/phi/kernels/impl/lu_solve_grad_kernel_impl.h"
#include "paddle/phi/kernels/lu_solve_grad_kernel.h"

// Register the CPU backward kernel
PD_REGISTER_KERNEL(
lu_solve_grad, CPU, ALL_LAYOUT, phi::LuSolveGradKernel, float, double) {}
82 changes: 82 additions & 0 deletions paddle/phi/kernels/cpu/lu_solve_kernel.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "paddle/phi/kernels/lu_solve_kernel.h"
#include "paddle/phi/backends/cpu/cpu_context.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/funcs/lapack/lapack_function.h"
#include "paddle/phi/kernels/impl/lu_kernel_impl.h"

namespace phi {

template <typename T, typename Context>
void LuSolveKernel(const Context& dev_ctx,
const DenseTensor& b,
const DenseTensor& lu,
const DenseTensor& pivots,
const std::string& trans,
DenseTensor* out) {
// Get lu matrix dimensions
auto lu_dims = lu.dims();
// Get x matrix dimensions
auto x_dims = b.dims();

// Allocate output tensor
dev_ctx.template Alloc<T>(out);
// Copy RHS data to output (will be overwritten with solution)
*out = Transpose2DTo6D<Context, T>(dev_ctx, b);
DenseTensor tem_lu = Transpose2DTo6D<Context, T>(dev_ctx, lu);

// Prepare LAPACK parameters
char trans_char = (trans == "N") ? 'N' : ((trans == "T") ? 'T' : 'C');
int n_int = lu_dims[lu_dims.size() - 1];
int nrhs_int = x_dims[x_dims.size() - 1];
int lda = std::max(1, n_int); // Leading dimension of A (LU matrix)
int ldb = std::max(1, n_int); // Leading dimension of B (RHS/solution matrix)
int info = 0;

auto outdims = out->dims();
auto outrank = outdims.size();
int batchsize = product(common::slice_ddim(outdims, 0, outrank - 2));
auto out_data = out->data<T>();
auto lu_data = tem_lu.data<T>();
auto pivots_data =
reinterpret_cast<int*>(const_cast<int*>(pivots.data<int>()));

for (int i = 0; i < batchsize; i++) {
auto* out_data_item = &out_data[i * lda * nrhs_int];
auto* lu_data_item = &lu_data[i * ldb * n_int];
auto* pivots_data_item = &pivots_data[i * n_int];
phi::funcs::lapackLuSolve<T>(trans_char,
n_int,
nrhs_int,
lu_data_item,
lda,
pivots_data_item,
out_data_item,
ldb,
&info);
PADDLE_ENFORCE_EQ(
info,
0,
phi::errors::PreconditionNotMet(
"LU solve failed with error code %d. Check if matrix is singular.",
info));
}
*out = Transpose2DTo6D<Context, T>(dev_ctx, *out);
}
} // namespace phi

PD_REGISTER_KERNEL(
lu_solve, CPU, ALL_LAYOUT, phi::LuSolveKernel, float, double) {}
27 changes: 27 additions & 0 deletions paddle/phi/kernels/funcs/lapack/lapack_function.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,33 @@ void lapackLu<float>(int m, int n, float *a, int lda, int *ipiv, int *info) {
dynload::sgetrf_(&m, &n, a, &lda, ipiv, info);
}

// lu_solve
template <>
void lapackLuSolve<double>(char trans,
int n,
int nrhs,
double *a,
int lda,
int *ipiv,
double *b,
int ldb,
int *info) {
dynload::dgetrs_(&trans, &n, &nrhs, a, &lda, ipiv, b, &ldb, info);
}

template <>
void lapackLuSolve<float>(char trans,
int n,
int nrhs,
float *a,
int lda,
int *ipiv,
float *b,
int ldb,
int *info) {
dynload::sgetrs_(&trans, &n, &nrhs, a, &lda, ipiv, b, &ldb, info);
}

// eigh
template <>
void lapackEigh<float>(char jobz,
Expand Down
12 changes: 12 additions & 0 deletions paddle/phi/kernels/funcs/lapack/lapack_function.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ namespace funcs {
template <typename T>
void lapackLu(int m, int n, T *a, int lda, int *ipiv, int *info);

// Lu_solve
template <typename T>
void lapackLuSolve(char trans,
int n,
int nrhs,
T *a,
int lda,
int *ipiv,
T *b,
int ldb,
int *info);

// Eigh
template <typename T, typename ValueType = T>
void lapackEigh(char jobz,
Expand Down
22 changes: 22 additions & 0 deletions paddle/phi/kernels/gpu/lu_solve_grad_kernel.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "paddle/phi/backends/gpu/gpu_context.h"
#include "paddle/phi/core/kernel_registry.h"

#include "paddle/phi/kernels/impl/lu_solve_grad_kernel_impl.h"
#include "paddle/phi/kernels/lu_solve_grad_kernel.h"

PD_REGISTER_KERNEL(
lu_solve_grad, GPU, ALL_LAYOUT, phi::LuSolveGradKernel, float, double) {}
Loading