Skip to content

Commit ae65257

Browse files
authored
[2.2]support extern third_party lapack API on Linux/Windows/Mac (#35690)
* support extern third_party lapack on Linux/Windows/Mac * fix ci
1 parent 5af6081 commit ae65257

File tree

12 files changed

+270
-6
lines changed

12 files changed

+270
-6
lines changed

cmake/external/lapack.cmake

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
INCLUDE (ExternalProject)
16+
17+
SET(LAPACK_PREFIX_DIR ${THIRD_PARTY_PATH}/lapack)
18+
SET(LAPACK_SOURCE_DIR ${THIRD_PARTY_PATH}/lapack/src/extern_lapack)
19+
SET(LAPACK_INSTALL_DIR ${THIRD_PARTY_PATH}/install/lapack)
20+
SET(LAPACK_INCLUDE_DIR ${LAPACK_SOURCE_DIR})
21+
SET(LAPACK_LIB_DIR ${LAPACK_INSTALL_DIR}/lib)
22+
23+
# Note(zhouwei): lapack need fortan compiler which many machines don't have, so use precompiled library.
24+
# use lapack tag v3.10.0 on 06/28/2021 https://github.com/Reference-LAPACK/lapack
25+
if(LINUX)
26+
SET(LAPACK_VER "lapack_lnx_v3.10.0.20210628" CACHE STRING "" FORCE)
27+
SET(LAPACK_URL "https://paddlepaddledeps.bj.bcebos.com/${LAPACK_VER}.tar.gz" CACHE STRING "" FORCE)
28+
SET(LAPACK_URL_MD5 71f8cc8237a8571692f3e07f9a4f25f6)
29+
SET(GNU_RT_LIB_1 "${LAPACK_LIB_DIR}/libquadmath.so.0")
30+
SET(GFORTRAN_LIB "${LAPACK_LIB_DIR}/libgfortran.so.3")
31+
SET(BLAS_LIB "${LAPACK_LIB_DIR}/libblas.so.3")
32+
SET(LAPACK_LIB "${LAPACK_LIB_DIR}/liblapack.so.3")
33+
elseif(WIN32)
34+
# Refer to [lapack-for-windows] http://icl.cs.utk.edu/lapack-for-windows/lapack/#lapacke
35+
SET(LAPACK_VER "lapack_win_v3.10.0.20210628" CACHE STRING "" FORCE)
36+
SET(LAPACK_URL "https://paddlepaddledeps.bj.bcebos.com/${LAPACK_VER}.zip" CACHE STRING "" FORCE)
37+
SET(LAPACK_URL_MD5 590d080392dcd5abbd5dca767a50b63a)
38+
SET(GNU_RT_LIB_1 "${LAPACK_LIB_DIR}/libquadmath-0.dll")
39+
SET(GNU_RT_LIB_2 "${LAPACK_LIB_DIR}/libgcc_s_seh-1.dll")
40+
SET(GFORTRAN_LIB "${LAPACK_LIB_DIR}/libgfortran-3.dll")
41+
SET(BLAS_LIB "${LAPACK_LIB_DIR}/libblas.dll")
42+
SET(LAPACK_LIB "${LAPACK_LIB_DIR}/liblapack.dll")
43+
else()
44+
SET(LAPACK_VER "lapack_mac_v3.10.0.20210628" CACHE STRING "" FORCE)
45+
SET(LAPACK_URL "https://paddlepaddledeps.bj.bcebos.com/${LAPACK_VER}.tar.gz" CACHE STRING "" FORCE)
46+
SET(LAPACK_URL_MD5 427aecf8dee8523de3566ca8e47944d7)
47+
SET(GNU_RT_LIB_1 "${LAPACK_LIB_DIR}/libquadmath.0.dylib")
48+
SET(GNU_RT_LIB_2 "${LAPACK_LIB_DIR}/libgcc_s.1.dylib")
49+
SET(GFORTRAN_LIB "${LAPACK_LIB_DIR}/libgfortran.5.dylib")
50+
SET(BLAS_LIB "${LAPACK_LIB_DIR}/libblas.3.dylib")
51+
SET(LAPACK_LIB "${LAPACK_LIB_DIR}/liblapack.3.dylib")
52+
endif()
53+
54+
ExternalProject_Add(
55+
extern_lapack
56+
${EXTERNAL_PROJECT_LOG_ARGS}
57+
URL ${LAPACK_URL}
58+
URL_MD5 ${LAPACK_URL_MD5}
59+
PREFIX ${LAPACK_PREFIX_DIR}
60+
DOWNLOAD_DIR ${LAPACK_SOURCE_DIR}
61+
DOWNLOAD_NO_PROGRESS 1
62+
PATCH_COMMAND ""
63+
UPDATE_COMMAND ""
64+
CONFIGURE_COMMAND ""
65+
BUILD_COMMAND ""
66+
INSTALL_COMMAND ${CMAKE_COMMAND} -E copy_directory ${LAPACK_SOURCE_DIR} ${LAPACK_LIB_DIR}
67+
BUILD_BYPRODUCTS ${BLAS_LIB}
68+
BUILD_BYPRODUCTS ${LAPACK_LIB}
69+
)

cmake/third_party.cmake

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,10 @@ include(external/threadpool)# download threadpool
210210
include(external/dlpack) # download dlpack
211211
include(external/xxhash) # download, build, install xxhash
212212
include(external/warpctc) # download, build, install warpctc
213+
include(external/lapack) # download, build, install lapack
213214

214215
list(APPEND third_party_deps extern_eigen3 extern_gflags extern_glog extern_boost extern_xxhash)
215-
list(APPEND third_party_deps extern_zlib extern_dlpack extern_warpctc extern_threadpool)
216+
list(APPEND third_party_deps extern_zlib extern_dlpack extern_warpctc extern_threadpool extern_lapack)
216217

217218
include(cblas) # find first, then download, build, install openblas
218219

paddle/fluid/operators/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ endif()
118118

119119
cc_library(common_infer_shape_functions SRCS common_infer_shape_functions.cc DEPS operator)
120120

121-
set(COMMON_OP_DEPS ${COMMON_OP_DEPS} selected_rows_functor selected_rows
121+
set(COMMON_OP_DEPS ${COMMON_OP_DEPS} selected_rows_functor selected_rows lapack_function
122122
lod_tensor maxouting unpooling pooling lod_rank_table context_project
123123
sequence_pooling segment_pooling executor device_memory_aligment generator)
124124
set(COMMON_OP_DEPS ${COMMON_OP_DEPS} dynload_warpctc)

paddle/fluid/operators/math/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ else()
7878
math_library(beam_search DEPS math_function)
7979
endif()
8080
math_library(fc DEPS blas)
81+
math_library(lapack_function DEPS dynload_lapack)
8182

8283
math_library(matrix_bit_code)
8384

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "paddle/fluid/operators/math/lapack_function.h"
16+
#include "paddle/fluid/platform/dynload/lapack.h"
17+
18+
namespace paddle {
19+
namespace operators {
20+
namespace math {
21+
22+
// LU (for example)
23+
template <>
24+
void lapackLu<double>(int m, int n, double *a, int lda, int *ipiv, int *info) {
25+
platform::dynload::dgetrf_(&m, &n, a, &lda, ipiv, info);
26+
}
27+
28+
template <>
29+
void lapackLu<float>(int m, int n, float *a, int lda, int *ipiv, int *info) {
30+
platform::dynload::sgetrf_(&m, &n, a, &lda, ipiv, info);
31+
}
32+
33+
} // namespace math
34+
} // namespace operators
35+
} // namespace paddle
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#pragma once
16+
17+
namespace paddle {
18+
namespace operators {
19+
namespace math {
20+
21+
// LU (for example)
22+
template <typename T>
23+
void lapackLu(int m, int n, T *a, int lda, int *ipiv, int *info);
24+
25+
} // namespace math
26+
} // namespace operators
27+
} // namespace paddle

paddle/fluid/platform/dynload/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,7 @@ endif()
4545
if (WITH_MKLML)
4646
cc_library(dynload_mklml SRCS mklml.cc DEPS dynamic_loader mklml)
4747
endif()
48+
49+
cc_library(dynload_lapack SRCS lapack.cc DEPS dynamic_loader)
50+
add_dependencies(dynload_lapack extern_lapack)
4851
# TODO(TJ): add iomp, mkldnn?

paddle/fluid/platform/dynload/dynamic_loader.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ DEFINE_string(
5151

5252
DEFINE_string(mklml_dir, "", "Specify path for loading libmklml_intel.so.");
5353

54+
DEFINE_string(lapack_dir, "", "Specify path for loading liblapack.so.");
55+
5456
DEFINE_string(op_dir, "", "Specify path for loading user-defined op library.");
5557

5658
#ifdef PADDLE_WITH_HIP
@@ -478,6 +480,16 @@ void* GetMKLMLDsoHandle() {
478480
#endif
479481
}
480482

483+
void* GetLAPACKDsoHandle() {
484+
#if defined(__APPLE__) || defined(__OSX__)
485+
return GetDsoHandleFromSearchPath(FLAGS_lapack_dir, "liblapack.3.dylib");
486+
#elif defined(_WIN32)
487+
return GetDsoHandleFromSearchPath(FLAGS_lapack_dir, "liblapack.dll");
488+
#else
489+
return GetDsoHandleFromSearchPath(FLAGS_lapack_dir, "liblapack.so.3");
490+
#endif
491+
}
492+
481493
void* GetOpDsoHandle(const std::string& dso_name) {
482494
return GetDsoHandleFromSearchPath(FLAGS_op_dir, dso_name);
483495
}

paddle/fluid/platform/dynload/dynamic_loader.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ void* GetNCCLDsoHandle();
3939
void* GetHCCLDsoHandle();
4040
void* GetTensorRtDsoHandle();
4141
void* GetMKLMLDsoHandle();
42+
void* GetLAPACKDsoHandle();
4243
void* GetOpDsoHandle(const std::string& dso_name);
4344
void* GetNvtxDsoHandle();
4445
void* GetCUFFTDsoHandle();
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License. */
14+
15+
#include "paddle/fluid/platform/dynload/lapack.h"
16+
#include <mutex>
17+
18+
namespace paddle {
19+
namespace platform {
20+
namespace dynload {
21+
22+
std::once_flag lapack_dso_flag;
23+
void* lapack_dso_handle = nullptr;
24+
25+
#define DEFINE_WRAP(__name) DynLoad__##__name __name
26+
27+
LAPACK_ROUTINE_EACH(DEFINE_WRAP);
28+
29+
} // namespace dynload
30+
} // namespace platform
31+
} // namespace paddle

0 commit comments

Comments
 (0)