Skip to content

Commit

Permalink
[TensorRT EP] Fix updating provider options from session options (#20246
Browse files Browse the repository at this point in the history
)

The issue comes from if user specifies a path for "ep.context_file_path"
in session options, due to `context_cache_path` is a local variable and
it will be destroyed when returning from
`UpdateOrtTensorRTProviderOptionsV2FromSessionOptionsConfigs()`.
Later in
`onnxruntime::TensorrtProviderFactoryCreator::Create(&new_tensorrt_options)`,
it will access the corrupted memory location because of the location is
saved via context_cache_path.c_str().

Inline the
`UpdateOrtTensorRTProviderOptionsV2FromSessionOptionsConfigs()` can fix
this issue.
  • Loading branch information
chilo-ms authored Apr 10, 2024
1 parent 6a9d8a9 commit 4707250
Showing 1 changed file with 26 additions and 27 deletions.
53 changes: 26 additions & 27 deletions onnxruntime/core/session/provider_bridge_ort.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1640,32 +1640,6 @@ OrtTensorRTProviderOptionsV2 OrtTensorRTProviderOptionsToOrtTensorRTProviderOpti
return trt_options_converted;
}

#if !defined(ORT_MINIMAL_BUILD) && defined(USE_TENSORRT)
// Apply configs from session options to TensorRT provider options V2 that are needed for TensorRT EP.
// For example, EP context configs.
void UpdateOrtTensorRTProviderOptionsV2FromSessionOptionsConfigs(OrtSessionOptions* session_options, OrtTensorRTProviderOptionsV2* tensorrt_options) {
if (session_options) {
auto context_cache_enabled = (session_options->value).config_options.GetConfigOrDefault(kOrtSessionOptionEpContextEnable, "0") != "0";
tensorrt_options->trt_dump_ep_context_model = context_cache_enabled;
LOGS_DEFAULT(VERBOSE) << "Context cache enable: " << context_cache_enabled;

auto context_cache_path = (session_options->value).config_options.GetConfigOrDefault(kOrtSessionOptionEpContextFilePath, "");
tensorrt_options->trt_ep_context_file_path = context_cache_path.c_str();
LOGS_DEFAULT(VERBOSE) << "User specified context cache path: " << tensorrt_options->trt_ep_context_file_path;

auto embed_mode = (session_options->value).config_options.GetConfigOrDefault(kOrtSessionOptionEpContextEmbedMode, "1");
if ("1" == embed_mode) {
tensorrt_options->trt_ep_context_embed_mode = 1;
} else if ("0" == embed_mode) {
tensorrt_options->trt_ep_context_embed_mode = 0;
} else {
LOGS_DEFAULT(VERBOSE) << "Invalid ep.context_embed_mode: " << embed_mode << " only 0 or 1 allowed. Set to 1.";
}
LOGS_DEFAULT(VERBOSE) << "User specified context cache embed mode: " << tensorrt_options->trt_ep_context_embed_mode;
}
}
#endif

std::shared_ptr<IExecutionProviderFactory> TensorrtProviderFactoryCreator::Create(int device_id) {
return s_library_tensorrt.Get().CreateExecutionProviderFactory(device_id);
}
Expand Down Expand Up @@ -2097,7 +2071,32 @@ ORT_API_STATUS_IMPL(OrtApis::SessionOptionsAppendExecutionProvider_TensorRT_V2,
// Note: No need to worry about new_tensorrt_options being a local variable, CreateExecutionProviderFactory() in TRT EP will
// create a factory object that copies any provider options from tensorrt_options including "const char*" provider options.
OrtTensorRTProviderOptionsV2 new_tensorrt_options = *tensorrt_options; // copy and assign from tensorrt_options
onnxruntime::UpdateOrtTensorRTProviderOptionsV2FromSessionOptionsConfigs(options, &new_tensorrt_options);

// Update provider options from session options. Curretnly only EPContext related session options are supported.
// Note: The string-based local variables will be kept accessible during the lifetime of this function,
// therefore the "const char*" provider options can still be accessible when calling CreateExecutionProviderFactory() in TRT EP.
bool context_cache_enabled = false;
std::string context_cache_path = "";
std::string embed_mode = "";
if (options) {
context_cache_enabled = (options->value).config_options.GetConfigOrDefault(kOrtSessionOptionEpContextEnable, "0") != "0";
new_tensorrt_options.trt_dump_ep_context_model = context_cache_enabled;
LOGS_DEFAULT(VERBOSE) << "Context cache enable: " << context_cache_enabled;

context_cache_path = (options->value).config_options.GetConfigOrDefault(kOrtSessionOptionEpContextFilePath, "");
new_tensorrt_options.trt_ep_context_file_path = (context_cache_path.size() == 0) ? nullptr : context_cache_path.c_str();
LOGS_DEFAULT(VERBOSE) << "User specified context cache path: " << context_cache_path;

embed_mode = (options->value).config_options.GetConfigOrDefault(kOrtSessionOptionEpContextEmbedMode, "1");
if ("1" == embed_mode) {
new_tensorrt_options.trt_ep_context_embed_mode = 1;
} else if ("0" == embed_mode) {
new_tensorrt_options.trt_ep_context_embed_mode = 0;
} else {
LOGS_DEFAULT(VERBOSE) << "Invalid ep.context_embed_mode: " << embed_mode << " only 0 or 1 allowed. Set to 1.";
}
LOGS_DEFAULT(VERBOSE) << "User specified context cache embed mode: " << embed_mode;
}
factory = onnxruntime::TensorrtProviderFactoryCreator::Create(&new_tensorrt_options);
} else {
factory = onnxruntime::TensorrtProviderFactoryCreator::Create(tensorrt_options);
Expand Down

0 comments on commit 4707250

Please sign in to comment.