Skip to content

Commit

Permalink
support session options "optimizedModelFilePath" and "extra"
Browse files Browse the repository at this point in the history
  • Loading branch information
fs-eire committed Oct 15, 2024
1 parent e76fe1f commit c7c1e82
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions js/node/src/session_options_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,22 @@ void ParseExecutionProviders(const Napi::Array epList, Ort::SessionOptions& sess
}
}

void IterateExtraOptions(const std::string& prefix, const Napi::Object& obj, Ort::SessionOptions& sessionOptions) {
for (const auto& kvp : obj) {
auto key = kvp.first.As<Napi::String>().Utf8Value();
Napi::Value value = kvp.second;
if (value.IsObject()) {
IterateExtraOptions(prefix + key + ".", value.As<Napi::Object>(), sessionOptions);
} else {
ORT_NAPI_THROW_TYPEERROR_IF(!value.IsString(), obj.Env(),
"Invalid argument: sessionOptions.extra value must be a string in Node.js binding.");
std::string entry = prefix + key;
auto val = value.As<Napi::String>().Utf8Value();
sessionOptions.AddConfigEntry(entry.c_str(), val.c_str());
}
}
}

void ParseSessionOptions(const Napi::Object options, Ort::SessionOptions& sessionOptions) {
// Execution provider
if (options.Has("executionProviders")) {
Expand Down Expand Up @@ -189,6 +205,28 @@ void ParseSessionOptions(const Napi::Object options, Ort::SessionOptions& sessio
}
}

// optimizedModelFilePath
if (options.Has("optimizedModelFilePath")) {
auto optimizedModelFilePathValue = options.Get("optimizedModelFilePath");
ORT_NAPI_THROW_TYPEERROR_IF(!optimizedModelFilePathValue.IsString(), options.Env(),
"Invalid argument: sessionOptions.optimizedModelFilePath must be a string.");
#ifdef _WIN32
auto str = optimizedModelFilePathValue.As<Napi::String>().Utf16Value();
std::basic_string<ORTCHAR_T> optimizedModelFilePath = std::wstring{str.begin(), str.end()};
#else
std::basic_string<ORTCHAR_T> optimizedModelFilePath = optimizedModelFilePathValue.As<Napi::String>().Utf8Value();
#endif
sessionOptions.SetOptimizedModelFilePath(optimizedModelFilePath.c_str());
}

// extra
if (options.Has("extra")) {
auto extraValue = options.Get("extra");
ORT_NAPI_THROW_TYPEERROR_IF(!extraValue.IsObject(), options.Env(),
"Invalid argument: sessionOptions.extra must be an object.");
IterateExtraOptions("", extraValue.As<Napi::Object>(), sessionOptions);
}

// execution mode
if (options.Has("executionMode")) {
auto executionModeValue = options.Get("executionMode");
Expand Down

0 comments on commit c7c1e82

Please sign in to comment.