Skip to content

Commit f291abf

Browse files
authored
Add HasCUDNN to detect if CUDNN is installed or not (#6349)
* Add HasCUDNN to detect if CUDNN is installed or not * Fix CI
1 parent a34fc8b commit f291abf

File tree

3 files changed

+43
-19
lines changed

3 files changed

+43
-19
lines changed

paddle/platform/dynload/cudnn.cc

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
See the License for the specific language governing permissions and
1313
limitations under the License. */
1414

15-
#include <paddle/platform/dynload/cudnn.h>
15+
#include "paddle/platform/dynload/cudnn.h"
16+
#include "paddle/platform/enforce.h"
1617

1718
namespace paddle {
1819
namespace platform {
@@ -41,6 +42,21 @@ CUDNN_DNN_ROUTINE_EACH_R5(DEFINE_WRAP);
4142
CUDNN_DNN_ROUTINE_EACH_R7(DEFINE_WRAP);
4243
#endif
4344

45+
#ifdef PADDLE_USE_DSO
46+
bool HasCUDNN() {
47+
std::call_once(cudnn_dso_flag, GetCudnnDsoHandle, &cudnn_dso_handle);
48+
return cudnn_dso_handle != nullptr;
49+
}
50+
51+
void EnforceCUDNNLoaded(const char* fn_name) {
52+
PADDLE_ENFORCE(cudnn_dso_handle != nullptr,
53+
"Cannot load cudnn shared library. Cannot invoke method %s",
54+
fn_name);
55+
}
56+
#else
57+
bool HasCUDNN() { return true; }
58+
#endif
59+
4460
} // namespace dynload
4561
} // namespace platform
4662
} // namespace paddle

paddle/platform/dynload/cudnn.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ namespace dynload {
2525

2626
extern std::once_flag cudnn_dso_flag;
2727
extern void* cudnn_dso_handle;
28+
extern bool HasCUDNN();
2829

2930
#ifdef PADDLE_USE_DSO
3031

32+
extern void EnforceCUDNNLoaded(const char* fn_name);
3133
#define DECLARE_DYNAMIC_LOAD_CUDNN_WRAP(__name) \
3234
struct DynLoad__##__name { \
3335
template <typename... Args> \
@@ -36,6 +38,7 @@ extern void* cudnn_dso_handle;
3638
std::call_once(cudnn_dso_flag, \
3739
paddle::platform::dynload::GetCudnnDsoHandle, \
3840
&cudnn_dso_handle); \
41+
EnforceCUDNNLoaded(#__name); \
3942
void* p_##__name = dlsym(cudnn_dso_handle, #__name); \
4043
return reinterpret_cast<cudnn_func>(p_##__name)(args...); \
4144
} \

paddle/platform/dynload/dynamic_loader.cc

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,11 @@ static inline void GetDsoHandleFromDefaultPath(std::string& dso_path,
7878
*dso_handle = dlopen(dso_path.c_str(), dynload_flags);
7979
if (nullptr == *dso_handle) {
8080
if (dso_path == "libcudnn.dylib") {
81-
PADDLE_ENFORCE(true,
82-
"Note: [Recommend] copy cudnn into /usr/local/cuda/ \n "
83-
"For instance, sudo tar -xzf "
84-
"cudnn-7.5-osx-x64-v5.0-ga.tgz -C /usr/local \n sudo "
85-
"chmod a+r /usr/local/cuda/include/cudnn.h "
86-
"/usr/local/cuda/lib/libcudnn*");
81+
LOG(WARNING) << "Note: [Recommend] copy cudnn into /usr/local/cuda/ \n "
82+
"For instance, sudo tar -xzf "
83+
"cudnn-7.5-osx-x64-v5.0-ga.tgz -C /usr/local \n sudo "
84+
"chmod a+r /usr/local/cuda/include/cudnn.h "
85+
"/usr/local/cuda/lib/libcudnn*";
8786
}
8887
}
8988
}
@@ -92,7 +91,8 @@ static inline void GetDsoHandleFromDefaultPath(std::string& dso_path,
9291

9392
static inline void GetDsoHandleFromSearchPath(const std::string& search_root,
9493
const std::string& dso_name,
95-
void** dso_handle) {
94+
void** dso_handle,
95+
bool throw_on_error = true) {
9696
int dynload_flags = RTLD_LAZY | RTLD_LOCAL;
9797
*dso_handle = nullptr;
9898

@@ -111,15 +111,19 @@ static inline void GetDsoHandleFromSearchPath(const std::string& search_root,
111111
GetDsoHandleFromDefaultPath(dlPath, dso_handle, dynload_flags);
112112
}
113113
}
114-
PADDLE_ENFORCE(nullptr != *dso_handle,
115-
"Failed to find dynamic library: %s ( %s ) \n Please specify "
116-
"its path correctly using following ways: \n Method. set "
117-
"environment variable LD_LIBRARY_PATH on Linux or "
118-
"DYLD_LIBRARY_PATH on Mac OS. \n For instance, issue command: "
119-
"export LD_LIBRARY_PATH=... \n Note: After Mac OS 10.11, "
120-
"using the DYLD_LIBRARY_PATH is impossible unless System "
121-
"Integrity Protection (SIP) is disabled.",
122-
dlPath, dlerror());
114+
auto error_msg =
115+
"Failed to find dynamic library: %s ( %s ) \n Please specify "
116+
"its path correctly using following ways: \n Method. set "
117+
"environment variable LD_LIBRARY_PATH on Linux or "
118+
"DYLD_LIBRARY_PATH on Mac OS. \n For instance, issue command: "
119+
"export LD_LIBRARY_PATH=... \n Note: After Mac OS 10.11, "
120+
"using the DYLD_LIBRARY_PATH is impossible unless System "
121+
"Integrity Protection (SIP) is disabled.";
122+
if (throw_on_error) {
123+
PADDLE_ENFORCE(nullptr != *dso_handle, error_msg, dlPath, dlerror());
124+
} else if (nullptr == *dso_handle) {
125+
LOG(WARNING) << string::Sprintf(error_msg, dlPath, dlerror());
126+
}
123127
}
124128

125129
void GetCublasDsoHandle(void** dso_handle) {
@@ -132,9 +136,10 @@ void GetCublasDsoHandle(void** dso_handle) {
132136

133137
void GetCudnnDsoHandle(void** dso_handle) {
134138
#if defined(__APPLE__) || defined(__OSX__)
135-
GetDsoHandleFromSearchPath(FLAGS_cudnn_dir, "libcudnn.dylib", dso_handle);
139+
GetDsoHandleFromSearchPath(FLAGS_cudnn_dir, "libcudnn.dylib", dso_handle,
140+
false);
136141
#else
137-
GetDsoHandleFromSearchPath(FLAGS_cudnn_dir, "libcudnn.so", dso_handle);
142+
GetDsoHandleFromSearchPath(FLAGS_cudnn_dir, "libcudnn.so", dso_handle, false);
138143
#endif
139144
}
140145

0 commit comments

Comments
 (0)