From 2f619263900e488a51f8d833f9b01762d2a9c67e Mon Sep 17 00:00:00 2001 From: Scott McKay Date: Thu, 29 Nov 2018 10:37:21 +1000 Subject: [PATCH 1/2] Update build.md section on CUDA builds to clarify and add CUDA 10.0 info. Add --msvc_toolset param to build.py Tweak CMakeLists.txt to set the CUDA toolset path from onnxruntime_CUDA_HOME. --- BUILD.md | 41 ++++++++++++++++++++++++----------------- cmake/CMakeLists.txt | 4 ++-- tools/ci_build/build.py | 28 ++++++++++++++++++++-------- 3 files changed, 46 insertions(+), 27 deletions(-) diff --git a/BUILD.md b/BUILD.md index f21a98999840a..ba63fdfd7694a 100644 --- a/BUILD.md +++ b/BUILD.md @@ -58,32 +58,39 @@ The complete list of build flavors can be seen by running `./build.sh --help` or ONNX Runtime supports CUDA builds. You will need to download and install [CUDA](https://developer.nvidia.com/cuda-toolkit) and [CUDNN](https://developer.nvidia.com/cudnn). ONNX Runtime is built and tested with CUDA 9.1 and CUDNN 7.1 using the Visual Studio 2017 14.11 toolset (i.e. Visual Studio 2017 v15.3). -CUDA versions up to 9.2 and CUDNN version 7.1 should also work with versions of Visual Studio 2017 up to and including v15.7, however you may need to explicitly install and use the 14.11 toolset due to CUDA and CUDNN only being compatible with earlier versions of Visual Studio 2017. +CUDA versions from 9.1 up to 10.0, and CUDNN versions from 7.1 up to 7.4 should also work with Visual Studio 2017. -To install the Visual Studio 2017 14.11 toolset, see + - The path to the CUDA installation must be provided via the CUDA_PATH environment variable, or the --cuda_home parameter. + - The path to the CUDNN installation (include the 'cuda' folder in the path) must be provided via the CUDNN_PATH environment variable, or --cudnn_home parameter. The CUDNN path should contain 'bin', 'include' and 'lib' directories. + - The path to the CUDNN bin directory must be added to the PATH environment variable so that cudnn64_7.dll is found. -If using this toolset with a later version of Visual Studio 2017 you have two options: +You can build with: -1. Setup the Visual Studio environment variables to point to the 14.11 toolset by running vcvarsall.bat prior to running cmake - - e.g. if you have VS2017 Enterprise, an x64 build would use the following command -`"C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Auxiliary\Build\vcvarsall.bat" amd64 -vcvars_ver=14.11` +``` +./build.sh --use_cuda --cudnn_home /usr --cuda_home /usr/local/cuda (Linux) +./build.bat --use_cuda --cudnn_home --cuda_home (Windows) +``` -2. Alternatively if you have CMake 3.12 or later you can specify the toolset version in the "-T" parameter by adding "version=14.11" - - e.g. use the following with the below cmake command -`-T version=14.11,host=x64` +Depending on compatibility between the CUDA, CUDNN, and Visual Studio 2017 versions you are using, you may need to explicitly install an earlier version of the MSVC toolset. +CUDA 9.2 is known to work with the 14.11 MSVC toolset (Visual Studio 15.3 and 15.4) +CUDA 10.0 is known to work with toolsets from 14.11 up to 14.16 (Visual Studio 2017 15.9) -CMake should automatically find the CUDA installation. If it does not, or finds a different version to the one you wish to use, specify your root CUDA installation directory via the -DCUDA_TOOLKIT_ROOT_DIR CMake parameter. +To install the 14.11 MSVC toolset, see -_Side note: If you have multiple versions of CUDA installed on a Windows machine and are building with Visual Studio, CMake will use the build files for the highest version of CUDA it finds in the BuildCustomization folder. e.g. C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\VC\VCTargets\BuildCustomizations\. If you want to build with an earlier version, you must temporarily remove the 'CUDA x.y.*' files for later versions from this directory._ +To use the 14.11 toolset with a later version of Visual Studio 2017 you have two options: -The path to the 'cuda' folder in the CUDNN installation must be provided. The 'cuda' folder should contain 'bin', 'include' and 'lib' directories. +1. Setup the Visual Studio environment variables to point to the 14.11 toolset by running vcvarsall.bat, prior to running the build script + - e.g. if you have VS2017 Enterprise, an x64 build would use the following command +`"C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Auxiliary\Build\vcvarsall.bat" amd64 -vcvars_ver=14.11` + - For convenience, build.amd64.1411.bat will do this and can be used in the same way as build.bat. + - e.g.` .\build.amd64.1411.bat --use_cuda` -You can build with: +2. Alternatively if you have CMake 3.12 or later you can specify the toolset version via the "--msvc_toolset" build script parameter. + - e.g. `.\build.bat --msvc_toolset 14.11` -``` -./build.sh --use_cuda --cudnn_home /usr --cuda_home /usr/local/cuda (Linux) -./build.bat --use_cuda --cudnn_home --cuda_home (Windows) -``` +_Side note: If you have multiple versions of CUDA installed on a Windows machine and are building with Visual Studio, CMake will use the build files for the highest version of CUDA it finds in the BuildCustomization folder. +e.g. C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\VC\VCTargets\BuildCustomizations\. +If you want to build with an earlier version, you must temporarily remove the 'CUDA x.y.*' files for later versions from this directory._ ### MKL-DNN To build ONNX Runtime with MKL-DNN support, build it with `./build.sh --use_mkldnn --use_mklml` diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt index 158356b67fe60..777c9f3629e00 100644 --- a/cmake/CMakeLists.txt +++ b/cmake/CMakeLists.txt @@ -429,11 +429,11 @@ if (onnxruntime_USE_CUDA) add_definitions(-DUSE_CUDA=1) enable_language(CUDA) set(CMAKE_CUDA_STANDARD 14) - find_package(CUDA 9.0 REQUIRED) + file(TO_CMAKE_PATH ${onnxruntime_CUDA_HOME} CUDA_TOOLKIT_ROOT_DIR) file(TO_CMAKE_PATH ${onnxruntime_CUDNN_HOME} onnxruntime_CUDNN_HOME) + find_package(CUDA 9.0 REQUIRED) include(cub) set(CUDA_LINK_LIBRARIES_KEYWORD PRIVATE) - file(TO_CMAKE_PATH ${onnxruntime_CUDNN_HOME} onnxruntime_CUDNN_HOME) if (WIN32) link_directories(${onnxruntime_CUDNN_HOME}/lib/x64) set(ONNXRUNTIME_CUDA_LIBRARIES cudnn cublas) diff --git a/tools/ci_build/build.py b/tools/ci_build/build.py index f8c8a3a603d89..120b8076275da 100644 --- a/tools/ci_build/build.py +++ b/tools/ci_build/build.py @@ -72,6 +72,7 @@ def parse_arguments(): "These are just CMake -D options without the leading -D.") parser.add_argument("--x86", action='store_true', help="Create x86 makefiles. Requires --update and no existing cache CMake setup. Delete CMakeCache.txt if needed") + parser.add_argument("--msvc_toolset", help="MSVC toolset to use. e.g. 14.11") # Arguments needed by CI parser.add_argument("--cmake_path", default="cmake", help="Path to the CMake program.") @@ -182,6 +183,7 @@ def generate_build_tree(cmake_path, source_dir, build_dir, cuda_home, cudnn_home "-Donnxruntime_DEV_MODE=ON", "-DPYTHON_EXECUTABLE=" + sys.executable, "-Donnxruntime_USE_CUDA=" + ("ON" if args.use_cuda else "OFF"), + "-Donnxruntime_CUDA_HOME=" + (cuda_home if args.use_cuda else ""), "-Donnxruntime_CUDNN_HOME=" + (cudnn_home if args.use_cuda else ""), "-Donnxruntime_USE_JEMALLOC=" + ("ON" if args.use_jemalloc else "OFF"), "-Donnxruntime_ENABLE_PYTHON=" + ("ON" if args.enable_pybind else "OFF"), @@ -294,6 +296,11 @@ def setup_cuda_vars(args): .format(cuda_home, cuda_home_valid, cudnn_home, cudnn_home_valid)) sys.exit(-1) + if (not os.path.exists(os.path.join(cudnn_home, "bin"))): + log.error("cudnn_home path should include the 'cuda' folder, and must contain the CUDNN 'bin' directory.") + log.error("cudnn_home='{}'".format(cudnn_home)) + sys.exit(-1) + if (is_windows()): os.environ["CUDA_PATH"] = cuda_home os.environ["CUDA_TOOLKIT_ROOT_DIR"] = cuda_home @@ -308,6 +315,8 @@ def setup_cuda_vars(args): log.error("No version file found in CUDA install directory. Looked for " + version_file) sys.exit(-1) + cuda_major_version = "unknown" + with open(version_file) as f: # First line of version file should have something like 'CUDA Version 9.2.148' first_line = f.readline() @@ -316,9 +325,9 @@ def setup_cuda_vars(args): log.error("Couldn't read version from first line of " + version_file) sys.exit(-1) - major = m.group(1) + cuda_major_version = m.group(1) minor = m.group(2) - os.environ["CUDA_PATH_V{}_{}".format(major, minor)] = cuda_home + os.environ["CUDA_PATH_V{}_{}".format(cuda_major_version, minor)] = cuda_home vc_ver_str = os.getenv("VCToolsVersion") or "" vc_ver = vc_ver_str.split(".") @@ -326,14 +335,13 @@ def setup_cuda_vars(args): log.warning("Unable to automatically verify VS 2017 toolset is compatible with CUDA. Will attempt to use.") log.warning("Failed to get valid Visual C++ Tools version from VCToolsVersion environment variable value of '" + vc_ver_str + "'") log.warning("VCToolsVersion is set in a VS 2017 Developer Command shell, or by running \"%VS2017INSTALLDIR%\\VC\\Auxiliary\\Build\\vcvars64.bat\"") - log.warning("See ReadMe.md in the root ONNXRuntime directory for instructions on installing the Visual C++ 2017 14.11 toolset if needed.") + log.warning("See build.md in the root ONNXRuntime directory for instructions on installing the Visual C++ 2017 14.11 toolset if needed.") - elif vc_ver[0] == "14" and int(vc_ver[1]) > 11: - log.error("Visual C++ Tools version not supported by CUDA. You must setup the environment to use the 14.11 toolset.") + elif cuda_major_version == "9" and vc_ver[0] == "14" and int(vc_ver[1]) > 11: + log.error("Visual C++ Tools version not supported by CUDA v9. You must setup the environment to use the 14.11 toolset.") log.info("Current version is {}. CUDA 9.2 requires version 14.11.*".format(vc_ver_str)) log.info("If necessary manually install the 14.11 toolset using the Visual Studio 2017 updater.") - log.info("See https://blogs.msdn.microsoft.com/vcblog/2017/11/15/side-by-side-minor-version-msvc-toolsets-in-visual-studio-2017/") - log.info("Run 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Enterprise\\VC\\Auxiliary\\Build\\vcvarsall.bat amd64 -vcvars_ver=14.11' prior to this script, or use build.amd64.1411.bat.") + log.info("See 'Windows CUDA Build' in build.md in the root directory of this repository.") sys.exit(-1) return cuda_home, cudnn_home @@ -449,7 +457,11 @@ def main(): if (args.x86): cmake_extra_args = ['-A','Win32','-G', 'Visual Studio 15 2017'] else: - cmake_extra_args = ['-A','x64','-T', 'host=x64', '-G', 'Visual Studio 15 2017'] + toolset = 'host=x64' + if (args.msvc_toolset): + toolset += ',version=' + args.msvc_toolset + + cmake_extra_args = ['-A','x64','-T', toolset, '-G', 'Visual Studio 15 2017'] #Add python to PATH. Please remove this after https://github.com/onnx/onnx/issues/1080 is fixed () os.environ["PATH"] = os.path.dirname(sys.executable) + os.pathsep + os.environ["PATH"] From 91f1866b648b144a0d7c49e26f047c7b95493c1b Mon Sep 17 00:00:00 2001 From: Scott McKay Date: Thu, 29 Nov 2018 12:25:41 +1000 Subject: [PATCH 2/2] Only check for 'bin' folder in cudnn path on windows. --- tools/ci_build/build.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/tools/ci_build/build.py b/tools/ci_build/build.py index 120b8076275da..489955490f696 100644 --- a/tools/ci_build/build.py +++ b/tools/ci_build/build.py @@ -296,12 +296,13 @@ def setup_cuda_vars(args): .format(cuda_home, cuda_home_valid, cudnn_home, cudnn_home_valid)) sys.exit(-1) - if (not os.path.exists(os.path.join(cudnn_home, "bin"))): - log.error("cudnn_home path should include the 'cuda' folder, and must contain the CUDNN 'bin' directory.") - log.error("cudnn_home='{}'".format(cudnn_home)) - sys.exit(-1) - if (is_windows()): + # Validate that the cudnn_home is pointing at the right level + if (not os.path.exists(os.path.join(cudnn_home, "bin"))): + log.error("cudnn_home path should include the 'cuda' folder, and must contain the CUDNN 'bin' directory.") + log.error("cudnn_home='{}'".format(cudnn_home)) + sys.exit(-1) + os.environ["CUDA_PATH"] = cuda_home os.environ["CUDA_TOOLKIT_ROOT_DIR"] = cuda_home