From 4fd4b7414968fd1c0ca486cb63897236106f3fe3 Mon Sep 17 00:00:00 2001 From: Hariharan Seshadri Date: Wed, 2 Sep 2020 15:13:23 -0700 Subject: [PATCH] Change session option values if they don't work with EPs being registered for the session (#4991) --- onnxruntime/core/session/inference_session.cc | 37 ++++++++++++------- onnxruntime/core/session/onnxruntime_c_api.cc | 10 ----- .../test/framework/inference_session_test.cc | 8 +++- 3 files changed, 31 insertions(+), 24 deletions(-) diff --git a/onnxruntime/core/session/inference_session.cc b/onnxruntime/core/session/inference_session.cc index a822dcfb18..6456d27dcf 100644 --- a/onnxruntime/core/session/inference_session.cc +++ b/onnxruntime/core/session/inference_session.cc @@ -348,15 +348,34 @@ common::Status InferenceSession::RegisterExecutionProvider(std::unique_ptrType(); - // DML's memory is not byte addressable and hence mem pattern doesn't work. + // Some session option values (default or user provided) may not work with some EPs. + // Rather than put the onus on the user to know these, make the appropriate change while logging the change. if (provider_type == onnxruntime::kDmlExecutionProvider) { + // DML's memory is not byte addressable and hence mem pattern doesn't work. if (session_options_.enable_mem_pattern) { - return Status(ONNXRUNTIME, INVALID_ARGUMENT, - "Memory pattern must be disabled before registering DMLExecutionProvider"); + LOGS(*session_logger_, WARNING) + << "Having memory pattern enabled is not supported while using the DML Execution Provider. " + << "So disabling it for this session since it uses the DML Execution Provider."; + session_options_.enable_mem_pattern = false; } + + // Parallel execution mode does not support DML EP if (session_options_.execution_mode != ExecutionMode::ORT_SEQUENTIAL) { - return Status(ONNXRUNTIME, INVALID_ARGUMENT, - "Sequential execution must be enabled before registering DMLExecutionProvider"); + LOGS(*session_logger_, WARNING) + << "Parallel execution mode does not support the DML Execution Provider. " + << "So making the execution mode sequential for this session since it uses the DML Execution Provider."; + + session_options_.execution_mode = ExecutionMode::ORT_SEQUENTIAL; + } + } + + if (provider_type == onnxruntime::kCudaExecutionProvider) { + // Parallel execution mode does not support the CUDA EP + if (session_options_.execution_mode != ExecutionMode::ORT_SEQUENTIAL) { + LOGS(*session_logger_, WARNING) + << "Parallel execution mode does not support the CUDA Execution Provider. " + << "So making the execution mode sequential for this session since it uses the CUDA Execution Provider."; + session_options_.execution_mode = ExecutionMode::ORT_SEQUENTIAL; } } @@ -994,14 +1013,6 @@ common::Status InferenceSession::Initialize() { session_profiler_, session_options_.use_deterministic_compute); - if (session_options_.execution_mode == ExecutionMode::ORT_PARALLEL && - execution_providers_.Get(onnxruntime::kCudaExecutionProvider)) { - status = common::Status(common::ONNXRUNTIME, common::INVALID_ARGUMENT, - "Parallel execution mode doesn't support CUDA Execution Provider currently."); - LOGS(*session_logger_, ERROR) << status.ErrorMessage(); - return status; - } - onnxruntime::Graph& graph = model_->MainGraph(); // Collect the kernel registries from execution provider instances; diff --git a/onnxruntime/core/session/onnxruntime_c_api.cc b/onnxruntime/core/session/onnxruntime_c_api.cc index 8e63a22a96..7d7ba0790f 100644 --- a/onnxruntime/core/session/onnxruntime_c_api.cc +++ b/onnxruntime/core/session/onnxruntime_c_api.cc @@ -387,16 +387,6 @@ static ORT_STATUS_PTR LoadAndInitializeSession(_In_ const OrtEnv* /*env*/, _In_ if (options) { for (auto& factory : options->provider_factories) { auto provider = factory->CreateProvider(); - if (provider->Type() == kDmlExecutionProvider) { - if (options->value.enable_mem_pattern) { - // TODO Instead of returning an error, should we set mem pattern to false here and log a warning saying so? - // Doing so would be inconsistent with the Python API that doesn't go through this code path. - return OrtApis::CreateStatus(ORT_INVALID_ARGUMENT, "Mem pattern should be disabled when using DML execution provider."); - } - if (options->value.execution_mode != ExecutionMode::ORT_SEQUENTIAL) { - return OrtApis::CreateStatus(ORT_INVALID_ARGUMENT, "Sequential execution should be enabled when using DML execution provider."); - } - } provider_list.push_back(std::move(provider)); } } diff --git a/onnxruntime/test/framework/inference_session_test.cc b/onnxruntime/test/framework/inference_session_test.cc index bd68287015..1657d581a9 100644 --- a/onnxruntime/test/framework/inference_session_test.cc +++ b/onnxruntime/test/framework/inference_session_test.cc @@ -1867,7 +1867,13 @@ TEST(InferenceSessionTests, TestParallelExecutionWithCudaProvider) { auto status = session_object.Initialize(); - ASSERT_TRUE(!status.IsOK()); + ASSERT_TRUE(status.IsOK()); + + const auto& so_queried = session_object.GetSessionOptions(); + + // execution mode is sequential since we have registered the CUDA EP + // (which isn't supported by the parallel execution mode) + ASSERT_TRUE(so_queried.execution_mode == ExecutionMode::ORT_SEQUENTIAL); } #endif