-
Notifications
You must be signed in to change notification settings - Fork 548
Expand file tree
/
Copy pathFindcuDNN.cmake
More file actions
238 lines (226 loc) · 8.64 KB
/
FindcuDNN.cmake
File metadata and controls
238 lines (226 loc) · 8.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# Fetched the original content of this file from
# https://github.com/soumith/cudnn.torch
#
# Original Copyright:
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
# file Copyright.txt or https://cmake.org/licensing for details.
#
# Copyright (c) 2021, ArrayFire
# All rights reserved.
#
# This file is distributed under 3-clause BSD license.
# The complete license agreement can be obtained at:
# http://arrayfire.com/licenses/BSD-3-Clause
#
# FindcuDNN
# -------
#
# Find cuDNN library
#
# This module creates imported target cuDNN::cuDNN upon successfull
# lookup of cuDNN headers and libraries.
#
# Valiables that affect result:
# <VERSION>, <REQUIRED>, <QUIET>: as usual
#
# Usage
# -----
# add_exectuable(helloworld main.cpp)
# target_link_libraries(helloworld PRIVATE cuDNN::cuDNN)
#
# Note: It is recommended to avoid using variables set by the find module.
#
# Result variables
# ----------------
#
# This module will set the following variables in your project:
#
# ``cuDNN_INCLUDE_DIRS``
# where to find cudnn.h.
#
# ``cuDNN_LINK_LIBRARY``
# the libraries to link against to use cuDNN. Priot to cuDNN 8, this is a huge monolithic
# library. However, since cuDNN 8 it has been split into multiple shared libraries. If
# cuDNN version 8 if found, this variable contains the shared library that dlopens the
# other libraries: cuDNN_*_INFER_LINK_LIBRARY and cuDNN_*_TRAIN_LINK_LIBRARY as needed.
# For versions of cuDNN 7 or lower, cuDNN_*_INFER_LINK_LIBRARY and cuDNN_*_TRAIN_LINK_LIBRARY
# are not defined.
#
# ``cuDNN_ADV_INFER_LINK_LIBRARY``
# the libraries to link directly to use advanced inference API from cuDNN.
# ``cuDNN_ADV_INFER_DLL_LIBRARY``
# Corresponding advanced inference API Windows DLL. This is not set on non-Windows platforms.
# ``cuDNN_ADV_TRAIN_LINK_LIBRARY``
# the libraries to link directly to use advanced training API from cuDNN.
# ``cuDNN_ADV_TRAIN_DLL_LIBRARY``
# Corresponding advanced training API Windows DLL. This is not set on non-Windows platforms.
#
# ``cuDNN_CNN_INFER_LINK_LIBRARY``
# the libraries to link directly to use convolutional nueral networks inference API from cuDNN.
# ``cuDNN_CNN_INFER_DLL_LIBRARY``
# Corresponding CNN inference API Windows DLL. This is not set on non-Windows platforms.
# ``cuDNN_CNN_TRAIN_LINK_LIBRARY``
# the libraries to link directly to use convolutional nueral networks training API from cuDNN.
# ``cuDNN_CNN_TRAIN_DLL_LIBRARY``
# Corresponding CNN training API Windows DLL. This is not set on non-Windows platforms.
#
# ``cuDNN_OPS_INFER_LINK_LIBRARY``
# the libraries to link directly to use starndard ML operations API from cuDNN.
# ``cuDNN_OPS_INFER_DLL_LIBRARY``
# Corresponding OPS inference API Windows DLL. This is not set on non-Windows platforms.
# ``cuDNN_OPS_TRAIN_LINK_LIBRARY``
# the libraries to link directly to use starndard ML operations API from cuDNN.
# ``cuDNN_OPS_TRAIN_DLL_LIBRARY``
# Corresponding OPS inference API Windows DLL. This is not set on non-Windows platforms.
#
# ``cuDNN_FOUND``
# If false, do not try to use cuDNN.
# ``cuDNN_VERSION``
# Version of the cuDNN library found
# ``cuDNN_VERSION_MAJOR``
# Major Version of the cuDNN library found
# ``cuDNN_VERSION_MINOR``
# Minor Version of the cuDNN library found
find_package(PkgConfig)
pkg_check_modules(PC_CUDNN QUIET cuDNN)
find_package(CUDA QUIET)
find_path(cuDNN_INCLUDE_DIRS
NAMES cudnn.h
HINTS
${cuDNN_ROOT_DIR}
${PC_CUDNN_INCLUDE_DIRS}
${CUDA_TOOLKIT_INCLUDE}
PATH_SUFFIXES include
DOC "cuDNN include directory path." )
if(cuDNN_INCLUDE_DIRS)
file(READ ${cuDNN_INCLUDE_DIRS}/cudnn.h CUDNN_VERSION_FILE_CONTENTS)
string(REGEX MATCH "define CUDNN_MAJOR * +([0-9]+)"
CUDNN_MAJOR_VERSION "${CUDNN_VERSION_FILE_CONTENTS}")
list(LENGTH CUDNN_MAJOR_VERSION cudnn_ver_matches)
if(${cudnn_ver_matches} EQUAL 0)
file(READ ${cuDNN_INCLUDE_DIRS}/cudnn_version.h CUDNN_VERSION_FILE_CONTENTS)
string(REGEX MATCH "define CUDNN_MAJOR * +([0-9]+)"
CUDNN_MAJOR_VERSION "${CUDNN_VERSION_FILE_CONTENTS}")
endif()
string(REGEX REPLACE "define CUDNN_MAJOR * +([0-9]+)" "\\1"
CUDNN_MAJOR_VERSION "${CUDNN_MAJOR_VERSION}")
string(REGEX MATCH "define CUDNN_MINOR * +([0-9]+)"
CUDNN_MINOR_VERSION "${CUDNN_VERSION_FILE_CONTENTS}")
string(REGEX REPLACE "define CUDNN_MINOR * +([0-9]+)" "\\1"
CUDNN_MINOR_VERSION "${CUDNN_MINOR_VERSION}")
string(REGEX MATCH "define CUDNN_PATCHLEVEL * +([0-9]+)"
CUDNN_PATCH_VERSION "${CUDNN_VERSION_FILE_CONTENTS}")
string(REGEX REPLACE "define CUDNN_PATCHLEVEL * +([0-9]+)" "\\1"
CUDNN_PATCH_VERSION "${CUDNN_PATCH_VERSION}")
set(cuDNN_VERSION_MAJOR ${CUDNN_MAJOR_VERSION})
set(cuDNN_VERSION_MINOR ${CUDNN_MINOR_VERSION})
set(cuDNN_VERSION ${CUDNN_MAJOR_VERSION}.${CUDNN_MINOR_VERSION})
endif()
# Choose lib suffix to be exact major version if requested
# otherwise, just pick the one read from cudnn.h header
if(cuDNN_FIND_VERSION_EXACT)
set(cudnn_ver_suffix "${cuDNN_FIND_VERSION_MAJOR}")
else()
set(cudnn_ver_suffix "${CUDNN_MAJOR_VERSION}")
endif()
if(cuDNN_INCLUDE_DIRS)
get_filename_component(libpath_cudart "${CUDA_CUDART_LIBRARY}" PATH)
macro(af_find_cudnn_libs cudnn_lib_name_infix)
if("${cudnn_lib_name_infix}" STREQUAL "")
set(LIB_INFIX "")
else()
string(TOUPPER ${cudnn_lib_name_infix} LIB_INFIX)
endif()
find_library(cuDNN${LIB_INFIX}_LINK_LIBRARY
NAMES
libcudnn${cudnn_lib_name_infix}.so.${cudnn_ver_suffix}
libcudnn${cudnn_lib_name_infix}.${cudnn_ver_suffix}.dylib
cudnn${cudnn_lib_name_infix}
PATHS
${cuDNN_ROOT_DIR}
${PC_CUDNN_LIBRARY_DIRS}
$ENV{LD_LIBRARY_PATH}
${libpath_cudart}
${CMAKE_INSTALL_PREFIX}
PATH_SUFFIXES lib lib64 bin lib/x64 bin/x64
DOC "cudnn${cudnn_lib_name_infix} link library." )
mark_as_advanced(cuDNN${LIB_INFIX}_LINK_LIBRARY)
if(WIN32 AND cuDNN_LINK_LIBRARY)
find_file(cuDNN${LIB_INFIX}_DLL_LIBRARY
NAMES cudnn${cudnn_lib_name_infix}64_${cudnn_ver_suffix}${CMAKE_SHARED_LIBRARY_SUFFIX}
PATHS
${cuDNN_ROOT_DIR}
${PC_CUDNN_LIBRARY_DIRS}
$ENV{PATH}
${libpath_cudart}
${CMAKE_INSTALL_PREFIX}
PATH_SUFFIXES lib lib64 bin lib/x64 bin/x64
DOC "cudnn${cudnn_lib_name_infix} Windows DLL." )
mark_as_advanced(cuDNN${LIB_INFIX}_DLL_LIBRARY)
endif()
endmacro()
af_find_cudnn_libs("") # gets base cudnn shared library
if(cuDNN_VERSION_MAJOR VERSION_EQUAL 8)
af_find_cudnn_libs("_adv_infer")
af_find_cudnn_libs("_adv_train")
af_find_cudnn_libs("_cnn_infer")
af_find_cudnn_libs("_cnn_train")
af_find_cudnn_libs("_ops_infer")
af_find_cudnn_libs("_ops_train")
elseif(cuDNN_VERSION_MAJOR VERSION_GREATER_EQUAL 9)
af_find_cudnn_libs("_adv")
af_find_cudnn_libs("_cnn")
af_find_cudnn_libs("_ops")
endif()
endif()
find_package_handle_standard_args(cuDNN
REQUIRED_VARS cuDNN_LINK_LIBRARY cuDNN_INCLUDE_DIRS
VERSION_VAR cuDNN_VERSION)
mark_as_advanced(cuDNN_LINK_LIBRARY cuDNN_INCLUDE_DIRS cuDNN_DLL_LIBRARY)
if(cuDNN_FOUND)
add_library(cuDNN::cuDNN SHARED IMPORTED)
if(WIN32)
set_target_properties(cuDNN::cuDNN
PROPERTIES
IMPORTED_LINK_INTERFACE_LANGUAGE "C"
INTERFACE_INCLUDE_DIRECTORIES "${cuDNN_INCLUDE_DIRS}"
IMPORTED_LOCATION "${cuDNN_DLL_LIBRARY}"
IMPORTED_IMPLIB "${cuDNN_LINK_LIBRARY}"
)
else(WIN32)
set_target_properties(cuDNN::cuDNN
PROPERTIES
IMPORTED_LINK_INTERFACE_LANGUAGE "C"
INTERFACE_INCLUDE_DIRECTORIES "${cuDNN_INCLUDE_DIRS}"
IMPORTED_LOCATION "${cuDNN_LINK_LIBRARY}"
)
endif(WIN32)
if(cuDNN_VERSION_MAJOR VERSION_GREATER 8 OR cuDNN_VERSION_MAJOR VERSION_EQUAL 8)
macro(create_cudnn_target cudnn_target_name)
string(TOUPPER ${cudnn_target_name} target_infix)
add_library(cuDNN::${cudnn_target_name} SHARED IMPORTED)
if(WIN32)
set_target_properties(cuDNN::${cudnn_target_name}
PROPERTIES
IMPORTED_LINK_INTERFACE_LANGUAGE "C"
INTERFACE_INCLUDE_DIRECTORIES "${cuDNN_INCLUDE_DIRS}"
IMPORTED_LOCATION "${cuDNN_${target_infix}_DLL_LIBRARY}"
IMPORTED_IMPLIB "${cuDNN_${target_infix}_LINK_LIBRARY}"
)
else(WIN32)
set_target_properties(cuDNN::${cudnn_target_name}
PROPERTIES
IMPORTED_LINK_INTERFACE_LANGUAGE "C"
INTERFACE_INCLUDE_DIRECTORIES "${cuDNN_INCLUDE_DIRS}"
IMPORTED_LOCATION "${cuDNN_${target_infix}_LINK_LIBRARY}"
)
endif(WIN32)
endmacro()
create_cudnn_target(adv_infer)
create_cudnn_target(adv_train)
create_cudnn_target(cnn_infer)
create_cudnn_target(cnn_train)
create_cudnn_target(ops_infer)
create_cudnn_target(ops_train)
endif()
endif(cuDNN_FOUND)