未验证 提交 f291abfc 编写于 作者: Y Yu Yang 提交者: GitHub

Add HasCUDNN to detect if CUDNN is installed or not (#6349)

* Add HasCUDNN to detect if CUDNN is installed or not

* Fix CI
上级 a34fc8b3
...@@ -12,7 +12,8 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ...@@ -12,7 +12,8 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. */ limitations under the License. */
#include <paddle/platform/dynload/cudnn.h> #include "paddle/platform/dynload/cudnn.h"
#include "paddle/platform/enforce.h"
namespace paddle { namespace paddle {
namespace platform { namespace platform {
...@@ -41,6 +42,21 @@ CUDNN_DNN_ROUTINE_EACH_R5(DEFINE_WRAP); ...@@ -41,6 +42,21 @@ CUDNN_DNN_ROUTINE_EACH_R5(DEFINE_WRAP);
CUDNN_DNN_ROUTINE_EACH_R7(DEFINE_WRAP); CUDNN_DNN_ROUTINE_EACH_R7(DEFINE_WRAP);
#endif #endif
#ifdef PADDLE_USE_DSO
bool HasCUDNN() {
std::call_once(cudnn_dso_flag, GetCudnnDsoHandle, &cudnn_dso_handle);
return cudnn_dso_handle != nullptr;
}
void EnforceCUDNNLoaded(const char* fn_name) {
PADDLE_ENFORCE(cudnn_dso_handle != nullptr,
"Cannot load cudnn shared library. Cannot invoke method %s",
fn_name);
}
#else
bool HasCUDNN() { return true; }
#endif
} // namespace dynload } // namespace dynload
} // namespace platform } // namespace platform
} // namespace paddle } // namespace paddle
...@@ -25,9 +25,11 @@ namespace dynload { ...@@ -25,9 +25,11 @@ namespace dynload {
extern std::once_flag cudnn_dso_flag; extern std::once_flag cudnn_dso_flag;
extern void* cudnn_dso_handle; extern void* cudnn_dso_handle;
extern bool HasCUDNN();
#ifdef PADDLE_USE_DSO #ifdef PADDLE_USE_DSO
extern void EnforceCUDNNLoaded(const char* fn_name);
#define DECLARE_DYNAMIC_LOAD_CUDNN_WRAP(__name) \ #define DECLARE_DYNAMIC_LOAD_CUDNN_WRAP(__name) \
struct DynLoad__##__name { \ struct DynLoad__##__name { \
template <typename... Args> \ template <typename... Args> \
...@@ -36,6 +38,7 @@ extern void* cudnn_dso_handle; ...@@ -36,6 +38,7 @@ extern void* cudnn_dso_handle;
std::call_once(cudnn_dso_flag, \ std::call_once(cudnn_dso_flag, \
paddle::platform::dynload::GetCudnnDsoHandle, \ paddle::platform::dynload::GetCudnnDsoHandle, \
&cudnn_dso_handle); \ &cudnn_dso_handle); \
EnforceCUDNNLoaded(#__name); \
void* p_##__name = dlsym(cudnn_dso_handle, #__name); \ void* p_##__name = dlsym(cudnn_dso_handle, #__name); \
return reinterpret_cast<cudnn_func>(p_##__name)(args...); \ return reinterpret_cast<cudnn_func>(p_##__name)(args...); \
} \ } \
......
...@@ -78,12 +78,11 @@ static inline void GetDsoHandleFromDefaultPath(std::string& dso_path, ...@@ -78,12 +78,11 @@ static inline void GetDsoHandleFromDefaultPath(std::string& dso_path,
*dso_handle = dlopen(dso_path.c_str(), dynload_flags); *dso_handle = dlopen(dso_path.c_str(), dynload_flags);
if (nullptr == *dso_handle) { if (nullptr == *dso_handle) {
if (dso_path == "libcudnn.dylib") { if (dso_path == "libcudnn.dylib") {
PADDLE_ENFORCE(true, LOG(WARNING) << "Note: [Recommend] copy cudnn into /usr/local/cuda/ \n "
"Note: [Recommend] copy cudnn into /usr/local/cuda/ \n "
"For instance, sudo tar -xzf " "For instance, sudo tar -xzf "
"cudnn-7.5-osx-x64-v5.0-ga.tgz -C /usr/local \n sudo " "cudnn-7.5-osx-x64-v5.0-ga.tgz -C /usr/local \n sudo "
"chmod a+r /usr/local/cuda/include/cudnn.h " "chmod a+r /usr/local/cuda/include/cudnn.h "
"/usr/local/cuda/lib/libcudnn*"); "/usr/local/cuda/lib/libcudnn*";
} }
} }
} }
...@@ -92,7 +91,8 @@ static inline void GetDsoHandleFromDefaultPath(std::string& dso_path, ...@@ -92,7 +91,8 @@ static inline void GetDsoHandleFromDefaultPath(std::string& dso_path,
static inline void GetDsoHandleFromSearchPath(const std::string& search_root, static inline void GetDsoHandleFromSearchPath(const std::string& search_root,
const std::string& dso_name, const std::string& dso_name,
void** dso_handle) { void** dso_handle,
bool throw_on_error = true) {
int dynload_flags = RTLD_LAZY | RTLD_LOCAL; int dynload_flags = RTLD_LAZY | RTLD_LOCAL;
*dso_handle = nullptr; *dso_handle = nullptr;
...@@ -111,15 +111,19 @@ static inline void GetDsoHandleFromSearchPath(const std::string& search_root, ...@@ -111,15 +111,19 @@ static inline void GetDsoHandleFromSearchPath(const std::string& search_root,
GetDsoHandleFromDefaultPath(dlPath, dso_handle, dynload_flags); GetDsoHandleFromDefaultPath(dlPath, dso_handle, dynload_flags);
} }
} }
PADDLE_ENFORCE(nullptr != *dso_handle, auto error_msg =
"Failed to find dynamic library: %s ( %s ) \n Please specify " "Failed to find dynamic library: %s ( %s ) \n Please specify "
"its path correctly using following ways: \n Method. set " "its path correctly using following ways: \n Method. set "
"environment variable LD_LIBRARY_PATH on Linux or " "environment variable LD_LIBRARY_PATH on Linux or "
"DYLD_LIBRARY_PATH on Mac OS. \n For instance, issue command: " "DYLD_LIBRARY_PATH on Mac OS. \n For instance, issue command: "
"export LD_LIBRARY_PATH=... \n Note: After Mac OS 10.11, " "export LD_LIBRARY_PATH=... \n Note: After Mac OS 10.11, "
"using the DYLD_LIBRARY_PATH is impossible unless System " "using the DYLD_LIBRARY_PATH is impossible unless System "
"Integrity Protection (SIP) is disabled.", "Integrity Protection (SIP) is disabled.";
dlPath, dlerror()); if (throw_on_error) {
PADDLE_ENFORCE(nullptr != *dso_handle, error_msg, dlPath, dlerror());
} else if (nullptr == *dso_handle) {
LOG(WARNING) << string::Sprintf(error_msg, dlPath, dlerror());
}
} }
void GetCublasDsoHandle(void** dso_handle) { void GetCublasDsoHandle(void** dso_handle) {
...@@ -132,9 +136,10 @@ void GetCublasDsoHandle(void** dso_handle) { ...@@ -132,9 +136,10 @@ void GetCublasDsoHandle(void** dso_handle) {
void GetCudnnDsoHandle(void** dso_handle) { void GetCudnnDsoHandle(void** dso_handle) {
#if defined(__APPLE__) || defined(__OSX__) #if defined(__APPLE__) || defined(__OSX__)
GetDsoHandleFromSearchPath(FLAGS_cudnn_dir, "libcudnn.dylib", dso_handle); GetDsoHandleFromSearchPath(FLAGS_cudnn_dir, "libcudnn.dylib", dso_handle,
false);
#else #else
GetDsoHandleFromSearchPath(FLAGS_cudnn_dir, "libcudnn.so", dso_handle); GetDsoHandleFromSearchPath(FLAGS_cudnn_dir, "libcudnn.so", dso_handle, false);
#endif #endif
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册