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

[cmake] support MacOS arm liblapack #63093

Merged
merged 4 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
41 changes: 28 additions & 13 deletions cmake/external/lapack.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,34 @@ elseif(WIN32)
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_FILE
"lapack_mac_v3.10.0.20210628.tar.gz"
CACHE STRING "" FORCE)
set(LAPACK_URL
"https://paddlepaddledeps.bj.bcebos.com/${LAPACK_FILE}"
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")
else() # MacOS
if(APPLE AND WITH_ARM)
set(LAPACK_FILE
"lapack_mac_arm64_v0.3.26.tar.gz"
CACHE STRING "" FORCE)
set(LAPACK_URL
"https://paddlepaddledeps.bj.bcebos.com/${LAPACK_FILE}"
CACHE STRING "" FORCE)
set(LAPACK_URL_MD5 3f6412105ae2b7465e5ee90c8673e6d4)
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.dylib")
set(LAPACK_LIB "${LAPACK_LIB_DIR}/liblapack.dylib")
else()
set(LAPACK_FILE
"lapack_mac_v3.10.0.20210628.tar.gz"
CACHE STRING "" FORCE)
set(LAPACK_URL
"https://paddlepaddledeps.bj.bcebos.com/${LAPACK_FILE}"
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()
endif()

function(download_lapack)
Expand Down
44 changes: 43 additions & 1 deletion paddle/phi/backends/dynload/dynamic_loader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ 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/dynload/dynamic_loader.h"
#include <dirent.h>

#include <cstdlib>
#include <string>
Expand Down Expand Up @@ -182,6 +183,34 @@ static inline void* GetDsoHandleFromSpecificPath(const std::string& spec_path,
return dso_handle;
}

static inline std::string FindLibAbsolutePath(const std::string& directory,
const std::string& filename) {
DIR* dir;
struct dirent* ent;

if ((dir = opendir(directory.c_str())) != nullptr) {
while ((ent = readdir(dir)) != nullptr) {
if (ent->d_type == DT_REG || ent->d_type == DT_LNK) {
if (filename == std::string(ent->d_name)) {
closedir(dir);
return join(directory, ent->d_name);
}
} else if (ent->d_type == DT_DIR) {
if (strcmp(ent->d_name, ".") != 0 && strcmp(ent->d_name, "..") != 0) {
std::string res =
FindLibAbsolutePath(join(directory, ent->d_name) + "/", filename);
if (!res.empty()) {
closedir(dir);
return res;
}
}
}
}
closedir(dir);
}
return "";
}

static inline void* GetDsoHandleFromDefaultPath(const std::string& dso_path,
int dynload_flags) {
// default search from LD_LIBRARY_PATH/DYLD_LIBRARY_PATH
Expand All @@ -195,10 +224,19 @@ static inline void* GetDsoHandleFromDefaultPath(const std::string& dso_path,
// bring System Integrity Projection (SIP), if dso_handle
// is null, search from default package path in Mac OS.
#if defined(__APPLE__) || defined(__OSX__)
#if defined(__arm__) || defined(__aarch64__)
if (nullptr == dso_handle) {
dso_handle =
dlopen(FindLibAbsolutePath("/opt/homebrew/Cellar/", dso_path).c_str(),
dynload_flags);
}
#else
if (nullptr == dso_handle) {
dso_handle =
dlopen(join("/usr/local/cuda/lib/", dso_path).c_str(), dynload_flags);
dlopen(FindLibAbsolutePath("/usr/local/cuda/lib/", dso_path).c_str(),
dynload_flags);
}
#endif
#endif

return dso_handle;
Expand Down Expand Up @@ -618,7 +656,11 @@ void* GetMKLMLDsoHandle() {

void* GetLAPACKDsoHandle() {
#if defined(__APPLE__) || defined(__OSX__)
#if defined(__arm__) || defined(__aarch64__)
return GetDsoHandleFromSearchPath(FLAGS_lapack_dir, "liblapack.dylib");
#else
return GetDsoHandleFromSearchPath(FLAGS_lapack_dir, "liblapack.3.dylib");
#endif
#elif defined(_WIN32)
return GetDsoHandleFromSearchPath(FLAGS_lapack_dir, "liblapack.dll");
#else
Expand Down