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

Update CUDA build help and tweak build setup #52

Merged
merged 3 commits into from
Nov 29, 2018
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: 24 additions & 17 deletions BUILD.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://blogs.msdn.microsoft.com/vcblog/2017/11/15/side-by-side-minor-version-msvc-toolsets-in-visual-studio-2017/>
- 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 <cudnn home path> --cuda_home <cuda home path> (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 <https://blogs.msdn.microsoft.com/vcblog/2017/11/15/side-by-side-minor-version-msvc-toolsets-in-visual-studio-2017/>

_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 <cudnn home path> --cuda_home <cuda home path> (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`
Expand Down
4 changes: 2 additions & 2 deletions cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -436,11 +436,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)
Expand Down
29 changes: 21 additions & 8 deletions tools/ci_build/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -295,6 +297,12 @@ def setup_cuda_vars(args):
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

Expand All @@ -308,6 +316,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()
Expand All @@ -316,24 +326,23 @@ 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(".")
if len(vc_ver) != 3:
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
Expand Down Expand Up @@ -449,7 +458,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"]
Expand Down