Skip to content

Commit

Permalink
Fix the issue that the new generated EP context model not able to fin…
Browse files Browse the repository at this point in the history
…d external data (#23537)

Fix the issue that the new generated EP context model not able to find external data

### Description
The new generated EP context model was not able to find the external data file because it lost track of the source model path which used to locate the external initializers.

Relate to issue: #23358
  • Loading branch information
HectorSVC authored Jan 30, 2025
1 parent fbae88f commit 5407c69
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
4 changes: 3 additions & 1 deletion onnxruntime/core/framework/graph_partitioner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,9 @@ static Status CreateEpContextModel(const ExecutionProviders& execution_providers
context_cache_path, "' exist already.");
}

Model ep_context_model(graph.Name(), false, graph.GetModel().MetaData(), PathString(), IOnnxRuntimeOpSchemaRegistryList{graph.GetSchemaRegistry()},
Model ep_context_model(graph.Name(), false, graph.GetModel().MetaData(),
graph.GetModel().ModelPath(), // use source model path so that external initializers can find the data file path
IOnnxRuntimeOpSchemaRegistryList{graph.GetSchemaRegistry()},
graph.DomainToVersionMap(), {}, logger);
auto& ep_graph = ep_context_model.MainGraph();
ep_graph.SetDescription(graph.Description());
Expand Down
8 changes: 5 additions & 3 deletions onnxruntime/test/providers/qnn/qnn_ep_context_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,15 @@ void EpCtxCpuNodeWithExternalIniFileTestBody(bool expect_external_ini_file) {
helper.SetGraphOutputs();
ASSERT_STATUS_OK(model.MainGraph().Resolve());
ModelSavingOptions model_saving_options{10};
const std::string model_with_ext = "model_external.onnx";
// dump the model in testdata folder in case it hides the bug that not able to find model not in current dir
const std::string model_with_ext = "./testdata/model_external.onnx";
const std::string model_ext_file = "model_external.bin";
ASSERT_STATUS_OK(Model::SaveWithExternalInitializers(model, model_with_ext,
model_ext_file, model_saving_options));

EXPECT_TRUE(std::filesystem::exists(model_with_ext.c_str()));
EXPECT_TRUE(std::filesystem::exists(model_ext_file.c_str()));
std::string model_ext_file_full_path = "./testdata/" + model_ext_file;
EXPECT_TRUE(std::filesystem::exists(model_ext_file_full_path.c_str()));

Ort::SessionOptions so;
so.AddConfigEntry(kOrtSessionOptionEpContextEnable, "1");
Expand All @@ -233,7 +235,7 @@ void EpCtxCpuNodeWithExternalIniFileTestBody(bool expect_external_ini_file) {

// clean up
ASSERT_EQ(std::remove(model_with_ext.c_str()), 0);
ASSERT_EQ(std::remove(model_ext_file.c_str()), 0);
ASSERT_EQ(std::remove(model_ext_file_full_path.c_str()), 0);
ASSERT_EQ(std::remove(ep_context_model_file.c_str()), 0);
}

Expand Down

0 comments on commit 5407c69

Please sign in to comment.