From ade4fa108f2061734d7e9fd6f48f5e1991b46bc2 Mon Sep 17 00:00:00 2001 From: KeDengMS Date: Thu, 5 Mar 2020 14:40:22 -0800 Subject: [PATCH] Disable delayload for cuda dlls (#3147) This change fixes #3129. When running onnxruntime as dll on Windows, CUDA does some internal cleanups when process exits. After this, any call to CUDA would cause crash. Delayload makes thread_local destructor to happen after CUDA cleanup, thus the crash. --- cmake/CMakeLists.txt | 13 +++++++------ onnxruntime/test/shared_lib/test_model_loading.cc | 9 +++++++++ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt index cd586f22c6..c0ccab69ee 100644 --- a/cmake/CMakeLists.txt +++ b/cmake/CMakeLists.txt @@ -724,12 +724,13 @@ if (onnxruntime_USE_CUDA) if (WIN32) link_directories(${onnxruntime_CUDNN_HOME}/lib/x64) - file(GLOB cuda_dll_paths "${onnxruntime_CUDA_HOME}/bin/cublas64_*" "${onnxruntime_CUDA_HOME}/bin/cudart64_*" "${onnxruntime_CUDA_HOME}/bin/curand64_*") - set(onnxruntime_DELAYLOAD_FLAGS "${onnxruntime_DELAYLOAD_FLAGS} /DELAYLOAD:cudnn64_7.dll") - foreach(cuda_dll_path ${cuda_dll_paths}) - get_filename_component(cuda_dll_file_name ${cuda_dll_path} NAME) - set(onnxruntime_DELAYLOAD_FLAGS "${onnxruntime_DELAYLOAD_FLAGS} /DELAYLOAD:${cuda_dll_file_name}") - endforeach(cuda_dll_path) + # delayload causes crash on exit, so disable for now + #file(GLOB cuda_dll_paths "${onnxruntime_CUDA_HOME}/bin/cublas64_*" "${onnxruntime_CUDA_HOME}/bin/cudart64_*" "${onnxruntime_CUDA_HOME}/bin/curand64_*") + #set(onnxruntime_DELAYLOAD_FLAGS "${onnxruntime_DELAYLOAD_FLAGS} /DELAYLOAD:cudnn64_7.dll") + #foreach(cuda_dll_path ${cuda_dll_paths}) + # get_filename_component(cuda_dll_file_name ${cuda_dll_path} NAME) + # set(onnxruntime_DELAYLOAD_FLAGS "${onnxruntime_DELAYLOAD_FLAGS} /DELAYLOAD:${cuda_dll_file_name}") + #endforeach(cuda_dll_path) else() link_directories(${onnxruntime_CUDNN_HOME}/lib64) diff --git a/onnxruntime/test/shared_lib/test_model_loading.cc b/onnxruntime/test/shared_lib/test_model_loading.cc index ee51301676..b3f88fc2af 100644 --- a/onnxruntime/test/shared_lib/test_model_loading.cc +++ b/onnxruntime/test/shared_lib/test_model_loading.cc @@ -2,6 +2,9 @@ // Licensed under the MIT License. #include "core/session/onnxruntime_cxx_api.h" +#ifdef USE_CUDA +#include "core/providers/cuda/cuda_provider_factory.h" +#endif #include #include "test_fixture.h" #include "file_util.h" @@ -25,6 +28,12 @@ TEST(CApiTest, model_from_array) { Ort::SessionOptions so; Ort::Session session(*ort_env.get(), buffer.data(), buffer.size(), so); + +#ifdef USE_CUDA + // test with CUDA provider when using onnxruntime as dll + Ort::ThrowOnError(OrtSessionOptionsAppendExecutionProvider_CUDA(so, 0)); + Ort::Session session_cuda(*ort_env.get(), buffer.data(), buffer.size(), so); +#endif } } // namespace test } // namespace onnxruntime