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

Add possibility to add graph optimization level to session options #1268

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
15 changes: 15 additions & 0 deletions src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@ struct ProviderOptionsArray_Element : JSON::Element {
ProviderOptionsObject_Element object_{v_};
};

GraphOptimizationLevel getGraphOptimizationLevel(std::string_view name) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To keep in style with the casing used in other methods within this file:

Suggested change
GraphOptimizationLevel getGraphOptimizationLevel(std::string_view name) {
GraphOptimizationLevel GetGraphOptimizationLevel(std::string_view name) {

if (name =="ORT_DISABLE_ALL") {
return ORT_DISABLE_ALL;
} else if (name == "ORT_ENABLE_BASIC") {
return ORT_ENABLE_BASIC;
} else if (name == "ORT_ENABLE_EXTENDED") {
return ORT_ENABLE_EXTENDED;
} else if (name == "ORT_ENABLE_ALL") {
return ORT_ENABLE_ALL;
} else
throw JSON::unknown_value_error{};
Copy link
Member

@RyanUnderhill RyanUnderhill Feb 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
throw JSON::unknown_value_error{};
throw std::runtime_error("Unrecognized value:" + name);

This exception isn't what you want, it's to say that the value name is unknown (it'll say "graph_optimization_level" is not recognized). The value name is recognized, but it's being set to an unrecognized value. This should be more useful.

}

struct SessionOptions_Element : JSON::Element {
explicit SessionOptions_Element(Config::SessionOptions& v) : v_{v} {}

Expand Down Expand Up @@ -94,6 +107,8 @@ struct SessionOptions_Element : JSON::Element {
v_.ep_context_enable = JSON::Get<bool>(value);
else if (name == "use_env_allocators")
v_.use_env_allocators = JSON::Get<bool>(value);
else if (name == "graph_optimization_level")
v_.graph_optimization_level = getGraphOptimizationLevel(JSON::Get<std::string_view>(value));
else
throw JSON::unknown_value_error{};
}
Expand Down
1 change: 1 addition & 0 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ struct Config {
bool use_env_allocators{};

std::vector<ProviderOptions> provider_options;
std::optional<GraphOptimizationLevel> graph_optimization_level;
};

struct Model {
Expand Down
4 changes: 4 additions & 0 deletions src/models/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,10 @@ void Model::CreateSessionOptionsFromConfig(const Config::SessionOptions& config_
session_options.AddConfigEntry("session.use_env_allocators", "1");
}

if (config_session_options.graph_optimization_level.has_value()) {
session_options.SetGraphOptimizationLevel(config_session_options.graph_optimization_level.value());
}

for (auto& provider_options : config_session_options.provider_options) {
if (provider_options.name == "cuda") {
auto ort_provider_options = OrtCUDAProviderOptionsV2::Create();
Expand Down
Loading