-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Support extern third_party lapack API on Linux/Windows/Mac #35690
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| # Copyright (c) 2021 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 (ExternalProject) | ||
|
|
||
| SET(LAPACK_PREFIX_DIR ${THIRD_PARTY_PATH}/lapack) | ||
| SET(LAPACK_SOURCE_DIR ${THIRD_PARTY_PATH}/lapack/src/extern_lapack) | ||
| SET(LAPACK_INSTALL_DIR ${THIRD_PARTY_PATH}/install/lapack) | ||
| SET(LAPACK_INCLUDE_DIR ${LAPACK_SOURCE_DIR}) | ||
| SET(LAPACK_LIB_DIR ${LAPACK_INSTALL_DIR}/lib) | ||
|
|
||
| # Note(zhouwei): lapack need fortan compiler which many machines don't have, so use precompiled library. | ||
| # use lapack tag v3.10.0 on 06/28/2021 https://github.com/Reference-LAPACK/lapack | ||
| if(LINUX) | ||
| SET(LAPACK_VER "lapack_lnx_v3.10.0.20210628" CACHE STRING "" FORCE) | ||
| SET(LAPACK_URL "https://paddlepaddledeps.bj.bcebos.com/${LAPACK_VER}.tar.gz" CACHE STRING "" FORCE) | ||
| SET(LAPACK_URL_MD5 71f8cc8237a8571692f3e07f9a4f25f6) | ||
| SET(GNU_RT_LIB_1 "${LAPACK_LIB_DIR}/libquadmath.so.0") | ||
| SET(GFORTRAN_LIB "${LAPACK_LIB_DIR}/libgfortran.so.3") | ||
| SET(BLAS_LIB "${LAPACK_LIB_DIR}/libblas.so.3") | ||
| SET(LAPACK_LIB "${LAPACK_LIB_DIR}/liblapack.so.3") | ||
| elseif(WIN32) | ||
| # Refer to [lapack-for-windows] http://icl.cs.utk.edu/lapack-for-windows/lapack/#lapacke | ||
| SET(LAPACK_VER "lapack_win_v3.10.0.20210628" CACHE STRING "" FORCE) | ||
| SET(LAPACK_URL "https://paddlepaddledeps.bj.bcebos.com/${LAPACK_VER}.zip" CACHE STRING "" FORCE) | ||
| SET(LAPACK_URL_MD5 590d080392dcd5abbd5dca767a50b63a) | ||
| SET(GNU_RT_LIB_1 "${LAPACK_LIB_DIR}/libquadmath-0.dll") | ||
| SET(GNU_RT_LIB_2 "${LAPACK_LIB_DIR}/libgcc_s_seh-1.dll") | ||
| SET(GFORTRAN_LIB "${LAPACK_LIB_DIR}/libgfortran-3.dll") | ||
| SET(BLAS_LIB "${LAPACK_LIB_DIR}/libblas.dll") | ||
| SET(LAPACK_LIB "${LAPACK_LIB_DIR}/liblapack.dll") | ||
| else() | ||
| SET(LAPACK_VER "lapack_mac_v3.10.0.20210628" CACHE STRING "" FORCE) | ||
| SET(LAPACK_URL "https://paddlepaddledeps.bj.bcebos.com/${LAPACK_VER}.tar.gz" CACHE STRING "" FORCE) | ||
| SET(LAPACK_URL_MD5 427aecf8dee8523de3566ca8e47944d7) | ||
| SET(GNU_RT_LIB_1 "${LAPACK_LIB_DIR}/libquadmath.0.dylib") | ||
| SET(GNU_RT_LIB_2 "${LAPACK_LIB_DIR}/libgcc_s.1.dylib") | ||
| SET(GFORTRAN_LIB "${LAPACK_LIB_DIR}/libgfortran.5.dylib") | ||
| SET(BLAS_LIB "${LAPACK_LIB_DIR}/libblas.3.dylib") | ||
| SET(LAPACK_LIB "${LAPACK_LIB_DIR}/liblapack.3.dylib") | ||
| endif() | ||
|
|
||
| ExternalProject_Add( | ||
| extern_lapack | ||
| ${EXTERNAL_PROJECT_LOG_ARGS} | ||
| URL ${LAPACK_URL} | ||
| URL_MD5 ${LAPACK_URL_MD5} | ||
| PREFIX ${LAPACK_PREFIX_DIR} | ||
| DOWNLOAD_DIR ${LAPACK_SOURCE_DIR} | ||
| DOWNLOAD_NO_PROGRESS 1 | ||
| PATCH_COMMAND "" | ||
| UPDATE_COMMAND "" | ||
| CONFIGURE_COMMAND "" | ||
| BUILD_COMMAND "" | ||
| INSTALL_COMMAND ${CMAKE_COMMAND} -E copy_directory ${LAPACK_SOURCE_DIR} ${LAPACK_LIB_DIR} | ||
| BUILD_BYPRODUCTS ${BLAS_LIB} | ||
| BUILD_BYPRODUCTS ${LAPACK_LIB} | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| // Copyright (c) 2021 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/fluid/operators/math/lapack_function.h" | ||
| #include "paddle/fluid/platform/dynload/lapack.h" | ||
|
|
||
| namespace paddle { | ||
| namespace operators { | ||
| namespace math { | ||
|
|
||
| // LU (for example) | ||
| template <> | ||
| void lapackLu<double>(int m, int n, double *a, int lda, int *ipiv, int *info) { | ||
| platform::dynload::dgetrf_(&m, &n, a, &lda, ipiv, info); | ||
| } | ||
|
|
||
| template <> | ||
| void lapackLu<float>(int m, int n, float *a, int lda, int *ipiv, int *info) { | ||
|
||
| platform::dynload::sgetrf_(&m, &n, a, &lda, ipiv, info); | ||
| } | ||
|
|
||
| } // namespace math | ||
| } // namespace operators | ||
| } // namespace paddle | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| // Copyright (c) 2021 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. | ||
|
|
||
| #pragma once | ||
|
|
||
| namespace paddle { | ||
| namespace operators { | ||
| namespace math { | ||
|
|
||
| // LU (for example) | ||
| template <typename T> | ||
| void lapackLu(int m, int n, T *a, int lda, int *ipiv, int *info); | ||
|
|
||
| } // namespace math | ||
| } // namespace operators | ||
| } // namespace paddle |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /* Copyright (c) 2016 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/fluid/platform/dynload/lapack.h" | ||
| #include <mutex> | ||
|
|
||
| namespace paddle { | ||
| namespace platform { | ||
| namespace dynload { | ||
|
|
||
| std::once_flag lapack_dso_flag; | ||
| void* lapack_dso_handle = nullptr; | ||
|
|
||
| #define DEFINE_WRAP(__name) DynLoad__##__name __name | ||
|
|
||
| LAPACK_ROUTINE_EACH(DEFINE_WRAP); | ||
|
|
||
| } // namespace dynload | ||
| } // namespace platform | ||
| } // namespace paddle |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| /* Copyright (c) 2016 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. */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <mutex> | ||
| #include "paddle/fluid/platform/dynload/dynamic_loader.h" | ||
| #include "paddle/fluid/platform/port.h" | ||
|
|
||
| // Note(zhouwei): because lapack doesn't provide appropriate header file. | ||
| // should expose API statement yourself. | ||
|
|
||
| // getrf_(For example) | ||
| extern "C" void dgetrf_(int *m, int *n, double *a, int *lda, int *ipiv, | ||
| int *info); | ||
| extern "C" void sgetrf_(int *m, int *n, float *a, int *lda, int *ipiv, | ||
| int *info); | ||
|
|
||
| namespace paddle { | ||
| namespace platform { | ||
| namespace dynload { | ||
|
|
||
| extern std::once_flag lapack_dso_flag; | ||
| extern void *lapack_dso_handle; | ||
|
|
||
| /** | ||
| * The following macro definition can generate structs | ||
| * (for each function) to dynamic load lapack routine | ||
| * via operator overloading. | ||
| */ | ||
| #define DYNAMIC_LOAD_LAPACK_WRAP(__name) \ | ||
| struct DynLoad__##__name { \ | ||
| template <typename... Args> \ | ||
| auto operator()(Args... args) -> DECLARE_TYPE(__name, args...) { \ | ||
| using lapackFunc = decltype(&::__name); \ | ||
| std::call_once(lapack_dso_flag, []() { \ | ||
| lapack_dso_handle = paddle::platform::dynload::GetLAPACKDsoHandle(); \ | ||
| }); \ | ||
| static void *p_##_name = dlsym(lapack_dso_handle, #__name); \ | ||
| return reinterpret_cast<lapackFunc>(p_##_name)(args...); \ | ||
| } \ | ||
| }; \ | ||
| extern DynLoad__##__name __name | ||
|
|
||
| #define DECLARE_DYNAMIC_LOAD_LAPACK_WRAP(__name) \ | ||
| DYNAMIC_LOAD_LAPACK_WRAP(__name) | ||
|
|
||
| #define LAPACK_ROUTINE_EACH(__macro) \ | ||
| __macro(dgetrf_); \ | ||
| __macro(sgetrf_); | ||
|
|
||
| LAPACK_ROUTINE_EACH(DECLARE_DYNAMIC_LOAD_LAPACK_WRAP); | ||
|
|
||
| #undef DYNAMIC_LOAD_LAPACK_WRAP | ||
|
|
||
| } // namespace dynload | ||
| } // namespace platform | ||
| } // namespace paddle |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need any other dtype support?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
还有两种复数类型,这里只是做示例,API同学可以继续往上加,数量非常多没有一一列举