Skip to content

Commit 97cfedb

Browse files
author
Gopalakrishnan Nallasamy
committed
Add EPContext helper fallback coverage
1 parent 12eda44 commit 97cfedb

5 files changed

Lines changed: 416 additions & 4 deletions

File tree

onnxruntime/test/autoep/library/ep_context_data_utils.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,8 @@ inline OrtStatus* ReadEpContextDataWithFileFallback(const OrtApi& api, const Ort
203203

204204
inline OrtStatus* WriteEpContextDataWithFileFallback(const OrtApi& api, const OrtEpApi& ep_api,
205205
const OrtEpContextConfig* ep_context_config,
206-
const char* file_name, const OrtGraph* graph,
206+
const char* file_name, const char* fallback_file_name,
207+
const OrtGraph* graph,
207208
const void* buffer, size_t buffer_size) {
208209
if (file_name == nullptr || file_name[0] == '\0') {
209210
return api.CreateStatus(ORT_INVALID_ARGUMENT, "EPContext data file name must not be empty");
@@ -223,7 +224,19 @@ inline OrtStatus* WriteEpContextDataWithFileFallback(const OrtApi& api, const Or
223224
return write_func(write_state, file_name, buffer, buffer_size);
224225
}
225226

226-
return WriteEpContextDataToFile(api, file_name, graph, buffer, buffer_size);
227+
if (fallback_file_name == nullptr || fallback_file_name[0] == '\0') {
228+
return api.CreateStatus(ORT_INVALID_ARGUMENT, "EPContext data fallback file name must not be empty");
229+
}
230+
231+
return WriteEpContextDataToFile(api, fallback_file_name, graph, buffer, buffer_size);
232+
}
233+
234+
inline OrtStatus* WriteEpContextDataWithFileFallback(const OrtApi& api, const OrtEpApi& ep_api,
235+
const OrtEpContextConfig* ep_context_config,
236+
const char* file_name, const OrtGraph* graph,
237+
const void* buffer, size_t buffer_size) {
238+
return WriteEpContextDataWithFileFallback(api, ep_api, ep_context_config, file_name, file_name, graph, buffer,
239+
buffer_size);
227240
}
228241

229242
} // namespace ep_context_data_utils

onnxruntime/test/autoep/library/example_plugin_ep/ep.cc

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <atomic>
88
#include <cassert>
99
#include <cstring>
10+
#include <filesystem>
1011
#include <memory>
1112
#include <string>
1213
#include <unordered_map>
@@ -538,9 +539,21 @@ OrtStatus* ExampleEp::CreateEpContextNodes(const OrtGraph* graph,
538539
std::string ep_ctx = config_.embed_ep_context_in_model ? "binary_data" : fused_node_name + ".ctx";
539540
if (!config_.embed_ep_context_in_model) {
540541
const std::string ep_context_data = "binary_data";
542+
std::string fallback_ep_ctx = ep_ctx;
543+
const OrtGraph* fallback_graph = graph;
544+
if (!config_.ep_context_output_model_path.empty()) {
545+
const std::filesystem::path output_model_path =
546+
ep_context_data_utils::Utf8Path(config_.ep_context_output_model_path.c_str());
547+
const std::filesystem::path output_model_dir = output_model_path.parent_path();
548+
if (!output_model_dir.empty()) {
549+
fallback_ep_ctx = ep_context_data_utils::PathToUtf8String(
550+
output_model_dir / ep_context_data_utils::Utf8Path(ep_ctx.c_str()));
551+
}
552+
fallback_graph = nullptr;
553+
}
541554
RETURN_IF_ERROR(ep_context_data_utils::WriteEpContextDataWithFileFallback(
542-
ort_api, ep_api, ep_context_config_, ep_ctx.c_str(), graph, ep_context_data.data(),
543-
ep_context_data.size()));
555+
ort_api, ep_api, ep_context_config_, ep_ctx.c_str(), fallback_ep_ctx.c_str(), fallback_graph,
556+
ep_context_data.data(), ep_context_data.size()));
544557
}
545558
attributes[0] = Ort::OpAttr("ep_cache_context", ep_ctx.data(), static_cast<int>(ep_ctx.size()),
546559
ORT_OP_ATTR_STRING);

onnxruntime/test/autoep/library/example_plugin_ep/ep.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class ExampleEp : public OrtEp, public ApiPtrs {
6363
bool enable_ep_context = false;
6464
bool embed_ep_context_in_model = true;
6565
bool enable_weightless_ep_context_nodes = false;
66+
std::string ep_context_output_model_path;
6667
// Other EP configs (typically extracted from OrtSessionOptions or OrtHardwareDevice(s))
6768
};
6869

onnxruntime/test/autoep/library/example_plugin_ep/ep_factory.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,17 +213,21 @@ OrtStatus* ORT_API_CALL ExampleEpFactory::CreateEpImpl(OrtEpFactory* this_ptr,
213213
// Note: should not store a direct reference to the session options object as its lifespan is not guaranteed.
214214
std::string ep_context_enable;
215215
std::string ep_context_embed_mode;
216+
std::string ep_context_output_model_path;
216217
std::string weightless_ep_context_nodes_enable;
217218
RETURN_IF_ERROR(GetSessionConfigEntryOrDefault(*session_options, kOrtSessionOptionEpContextEnable, "0",
218219
ep_context_enable));
219220
RETURN_IF_ERROR(GetSessionConfigEntryOrDefault(*session_options, kOrtSessionOptionEpContextEmbedMode, "1",
220221
ep_context_embed_mode));
222+
RETURN_IF_ERROR(GetSessionConfigEntryOrDefault(*session_options, kOrtSessionOptionEpContextFilePath, "",
223+
ep_context_output_model_path));
221224
RETURN_IF_ERROR(GetSessionConfigEntryOrDefault(*session_options, kOrtSessionOptionEpEnableWeightlessEpContextNodes,
222225
"0", weightless_ep_context_nodes_enable));
223226

224227
ExampleEp::Config config = {};
225228
config.enable_ep_context = ep_context_enable == "1";
226229
config.embed_ep_context_in_model = ep_context_embed_mode != "0";
230+
config.ep_context_output_model_path = std::move(ep_context_output_model_path);
227231
config.enable_weightless_ep_context_nodes = weightless_ep_context_nodes_enable == "1";
228232

229233
OrtEpContextConfig* ep_context_config_raw = nullptr;

0 commit comments

Comments
 (0)