From 3a85ade5116830792b9ad2497f309505e58a5e14 Mon Sep 17 00:00:00 2001 From: Chi Lo <54722500+chilo-ms@users.noreply.github.com> Date: Sat, 20 Nov 2021 07:52:50 -0800 Subject: [PATCH] Avoid segmentation fault when creating session with TRT EP or OpenVINO EP using python (#9814) * fix segmentation fault * fix typo * fix bug * make logic the same as CUDA ep * Modify for OpenVINO * Add env variable check for OpenVIO * refine the code * refine EP failed registration warning messages. * update OpenVINO exception message. Co-authored-by: George Wu --- .../python/onnxruntime_pybind_state.cc | 31 +++++++++++++------ 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/onnxruntime/python/onnxruntime_pybind_state.cc b/onnxruntime/python/onnxruntime_pybind_state.cc index ea198fb45d..090440459c 100644 --- a/onnxruntime/python/onnxruntime_pybind_state.cc +++ b/onnxruntime/python/onnxruntime_pybind_state.cc @@ -497,14 +497,15 @@ std::unique_ptr CreateExecutionProviderInstance( ORT_THROW("Invalid TensorRT EP option: ", option.first); } } - return onnxruntime::CreateExecutionProviderFactory_Tensorrt(¶ms)->CreateProvider(); + if (std::shared_ptr tensorrt_provider_factory = onnxruntime::CreateExecutionProviderFactory_Tensorrt(¶ms)) { + return tensorrt_provider_factory->CreateProvider(); + } } else { - return onnxruntime::CreateExecutionProviderFactory_Tensorrt(cuda_device_id)->CreateProvider(); + if (std::shared_ptr tensorrt_provider_factory = onnxruntime::CreateExecutionProviderFactory_Tensorrt(cuda_device_id)) { + return tensorrt_provider_factory->CreateProvider(); + } } - } else { - if (!Env::Default().GetEnvironmentVar("CUDA_PATH").empty()) { - ORT_THROW("CUDA_PATH is set but CUDA wasn't able to be loaded. Please install the correct version of CUDA and cuDNN as mentioned in the GPU requirements page (https://onnxruntime.ai/docs/reference/execution-providers/CUDA-ExecutionProvider.html#requirements) as well as TensorRT as mentioned in the TensorRT requirements page (https://onnxruntime.ai/docs/execution-providers/TensorRT-ExecutionProvider.html#requirements), make sure they're in the PATH, and that your GPU is supported."); - } + LOGS_DEFAULT(WARNING) << "Failed to register " << type << ". Please reference https://onnxruntime.ai/docs/execution-providers/TensorRT-ExecutionProvider.html#requirements to ensure all dependencies are met."; } #endif } else if (type == kMIGraphXExecutionProvider) { @@ -528,6 +529,8 @@ std::unique_ptr CreateExecutionProviderInstance( } else { if (!Env::Default().GetEnvironmentVar("CUDA_PATH").empty()) { ORT_THROW("CUDA_PATH is set but CUDA wasn't able to be loaded. Please install the correct version of CUDA and cuDNN as mentioned in the GPU requirements page (https://onnxruntime.ai/docs/reference/execution-providers/CUDA-ExecutionProvider.html#requirements), make sure they're in the PATH, and that your GPU is supported."); + } else { + LOGS_DEFAULT(WARNING) << "Failed to register " << type << ". Please reference https://onnxruntime.ai/docs/reference/execution-providers/CUDA-ExecutionProvider.html#requirements to ensure all dependencies are met."; } } } @@ -604,10 +607,18 @@ std::unique_ptr CreateExecutionProviderInstance( } - auto p = onnxruntime::CreateExecutionProviderFactory_OpenVINO(¶ms)->CreateProvider(); - // Reset global variables config to avoid it being accidentally passed on to the next session - openvino_device_type.clear(); - return p; + if (std::shared_ptr openvino_provider_factory = onnxruntime::CreateExecutionProviderFactory_OpenVINO(¶ms)) { + auto p = openvino_provider_factory->CreateProvider(); + // Reset global variables config to avoid it being accidentally passed on to the next session + openvino_device_type.clear(); + return p; + } else { + if (!Env::Default().GetEnvironmentVar("INTEL_OPENVINO_DIR").empty()) { + ORT_THROW("INTEL_OPENVINO_DIR is set but OpenVINO library wasn't able to be loaded. Please install a supported version of OpenVINO as mentioned in the requirements page (https://onnxruntime.ai/docs/execution-providers/OpenVINO-ExecutionProvider.html#requirements), ensure dependency libraries are in the PATH and your hardware is supported."); + } else { + LOGS_DEFAULT(WARNING) << "Failed to register " << type << ". Please reference https://onnxruntime.ai/docs/execution-providers/OpenVINO-ExecutionProvider.html#requirements to ensure all dependencies are met."; + } + } #endif } else if (type == kNupharExecutionProvider) { #if USE_NUPHAR