Skip to content
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

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

Open
wants to merge 21 commits into
base: develop
Choose a base branch
from
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) 2024 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) 2024 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/funcs/lapack/lapack_function.h"
#include "paddle/phi/kernels/impl/lu_kernel_impl.h"
#include "paddle/phi/kernels/lu_solve_kernel.h"

namespace phi {

template <typename T, typename Context>
void LuSolveKernel(const Context& dev_ctx,
const DenseTensor& x,
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 = x.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, x);
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 = reinterpret_cast<T*>(const_cast<T*>(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) 2022 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