Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 26 additions & 2 deletions src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ struct ProviderOptionsObject_Element : JSON::Element {
};

struct ProviderOptionsArray_Element : JSON::Element {
explicit ProviderOptionsArray_Element(std::vector<Config::ProviderOptions>& v) : v_{v} {}
explicit ProviderOptionsArray_Element(std::vector<Config::ProviderOptions>& v,
std::vector<Config::ProviderOptions>* v_backup = nullptr)
: v_{v}, v_backup_{v_backup} {}

JSON::Element& OnObject(std::string_view name) override { return object_; }

Expand All @@ -69,10 +71,18 @@ struct ProviderOptionsArray_Element : JSON::Element {
v.name = "DML";
}
}

// Copy the provider options to the backup vector.
// The backup is used to restore the original provider options when
// AppendProvider is called with the same provider name.
if (v_backup_) {
*v_backup_ = v_;
}
}

private:
std::vector<Config::ProviderOptions>& v_;
std::vector<Config::ProviderOptions>* v_backup_;
ProviderOptionsObject_Element object_{v_};
};

Expand Down Expand Up @@ -143,7 +153,7 @@ struct SessionOptions_Element : JSON::Element {

private:
Config::SessionOptions& v_;
ProviderOptionsArray_Element provider_options_{v_.provider_options};
ProviderOptionsArray_Element provider_options_{v_.provider_options, &v_.provider_options_backup};
NamedStrings_Element config_entries_{v_.config_entries};
};

Expand Down Expand Up @@ -715,6 +725,20 @@ void ClearProviders(Config& config) {
}

void SetProviderOption(Config& config, std::string_view provider_name, std::string_view option_name, std::string_view option_value) {
for (auto& provider_options : config.model.decoder.session_options.provider_options_backup) {
// If config.model.decoder.session_options.provider_options already has the provider name, add the requested option to it.
// Otherwise, if the provider name is found in the backup, query the provider options from the backup and add them to the current provider options.
// Otherwise, create a new provider options object and add it to the current provider options.
// This ensures that any __required__ options specified in the genai_config.json are not lost when ClearProviders -> AppendProvider is called.
// This is important for providers like Cuda, which require certain options to be set for some models.
if (provider_options.name == provider_name &&
std::none_of(config.model.decoder.session_options.provider_options.begin(),
config.model.decoder.session_options.provider_options.end(),
[&provider_name](const Config::ProviderOptions& po) { return po.name == provider_name; })) {
config.model.decoder.session_options.provider_options.emplace_back(provider_options);
return;
}
}
std::ostringstream json;
json << R"({")" << provider_name << R"(":{)";
if (!option_name.empty()) {
Expand Down
1 change: 1 addition & 0 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ struct Config {
std::vector<NamedString> config_entries; // Entries go into OrtSessionOptions::AddConfigEntry

std::vector<ProviderOptions> provider_options;
std::vector<ProviderOptions> provider_options_backup;
Comment thread
baijumeswani marked this conversation as resolved.
Outdated
Comment thread
baijumeswani marked this conversation as resolved.
Outdated
std::optional<GraphOptimizationLevel> graph_optimization_level;
};

Expand Down