Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
033a887
Add func typedefs
adrianlizarraga Jul 9, 2025
fcdb5cf
Merge branch 'main' into adrianl/compile-api-output-stream
adrianlizarraga Jul 9, 2025
03eb5fa
stub apis
adrianlizarraga Jul 15, 2025
3310968
merge main
adrianlizarraga Jul 17, 2025
c3693de
new branch. add 2 streams first
adrianlizarraga Jul 19, 2025
a69d5f9
Move away from using Graph's graph_proto_ member
adrianlizarraga Jul 19, 2025
5743dcd
fix deref assignment
adrianlizarraga Jul 20, 2025
fd87e0c
Clean up
adrianlizarraga Jul 21, 2025
a40f463
Merge branch 'main' into adrianl/compile-api-output-stream
adrianlizarraga Jul 21, 2025
0dadf4d
Use std::filesystem::path in ModelCompilationOptions; fix memleak in …
adrianlizarraga Jul 21, 2025
d94cf44
fix unused variable warning (as error)
adrianlizarraga Jul 21, 2025
5bfbddb
Merge main and fix conflicts
adrianlizarraga Aug 28, 2025
69a4338
Update handler function signature to take in the ExternalDataInfo for…
adrianlizarraga Aug 28, 2025
90ade82
Add test that reuses external initializers from original model
adrianlizarraga Aug 29, 2025
c36afe5
Define new ExternalDataInfo constructor only for non-minimal builds
adrianlizarraga Aug 29, 2025
c07dc11
Merge branch 'main' into adrianl/compile-api-output-stream
adrianlizarraga Aug 29, 2025
4b83a2b
Fix unused variable warning (as error)
adrianlizarraga Aug 29, 2025
91acc8f
another unused variable
adrianlizarraga Aug 29, 2025
6e5629a
Merge branch 'main' into adrianl/compile-api-output-stream
adrianlizarraga Aug 29, 2025
9b092bf
clean up
adrianlizarraga Aug 29, 2025
049b9ad
Start adding csharp api funcs
adrianlizarraga Aug 29, 2025
8e00a06
Remove qnn_factory memleak fix (address in different PR)
adrianlizarraga Aug 29, 2025
11a6c74
Add ExternalInitializerInfo to C++ api
adrianlizarraga Aug 29, 2025
9ca882f
Add compile_to_stream py api
adrianlizarraga Aug 29, 2025
6d522d8
Python bindings and tests
adrianlizarraga Aug 30, 2025
af996bb
C# API for WriteBuffer delegate
adrianlizarraga Aug 31, 2025
9b27b31
c# api handle initializers
adrianlizarraga Aug 31, 2025
9607193
missing documentation in c#
adrianlizarraga Aug 31, 2025
e65710a
Add ExternalInitializerInfo C# class
adrianlizarraga Aug 31, 2025
c16b327
Full C# API for delegate that handles initializers
adrianlizarraga Sep 1, 2025
0b2f0e6
Update comment
adrianlizarraga Sep 2, 2025
83758d1
Merge branch 'main' into adrianl/compile-api-output-stream
adrianlizarraga Sep 2, 2025
c62ed23
Address review comments
adrianlizarraga Sep 2, 2025
a35e7b6
Address review comments
adrianlizarraga Sep 3, 2025
d906855
Remove unused variable
adrianlizarraga Sep 3, 2025
255c2df
Merge branch 'main' into adrianl/compile-api-output-stream
adrianlizarraga Sep 3, 2025
3db3117
Merge main conflicts
adrianlizarraga Sep 3, 2025
c7f98de
Merge main again
adrianlizarraga Sep 3, 2025
9031635
Address review comments for C#
adrianlizarraga Sep 3, 2025
abd0297
Rename functions in C and python
adrianlizarraga Sep 3, 2025
d5012fb
Merge branch 'main' into adrianl/compile-api-output-stream
adrianlizarraga Sep 3, 2025
0e0497a
Address comments
adrianlizarraga Sep 4, 2025
0a61f1f
Merge branch 'main' into adrianl/compile-api-output-stream
adrianlizarraga Sep 4, 2025
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
Prev Previous commit
Next Next commit
Add test that reuses external initializers from original model
  • Loading branch information
adrianlizarraga committed Aug 29, 2025
commit 90ade82f696c75c9042b51e75b9a5989abc9b446
8 changes: 8 additions & 0 deletions onnxruntime/core/session/onnxruntime_c_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2541,9 +2541,17 @@ ORT_API(void, OrtApis::ReleaseExternalInitializerInfo, _Frees_ptr_opt_ OrtExtern
ORT_API_STATUS_IMPL(OrtApis::CreateExternalInitializerInfo, _In_ const ORTCHAR_T* filepath,
_In_ int64_t file_offset, _In_ size_t byte_size, _Outptr_ OrtExternalInitializerInfo** out) {
API_IMPL_BEGIN
#if !defined(ORT_MINIMAL_BUILD)
auto ext_data_info = std::make_unique<onnxruntime::ExternalDataInfo>(filepath, file_offset, byte_size);
*out = static_cast<OrtExternalInitializerInfo*>(ext_data_info.release());
return nullptr;
#else
*out = nullptr;
ORT_UNUSED_PARAMETER(filepath);
ORT_UNUSED_PARAMETER(file_offset);
ORT_UNUSED_PARAMETER(byte_size);
return OrtApis::CreateStatus(ORT_NOT_IMPLEMENTED, "CreateExternalInitializerInfo() is not supported in this build.");
#endif
API_IMPL_END
}

Expand Down
78 changes: 63 additions & 15 deletions onnxruntime/test/providers/qnn/qnn_ep_context_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ struct CustomInitializerHandlerState {
static OrtStatus* ORT_API_CALL TestHandleInitializerDataFunc(void* state,
const char* initializer_name,
const OrtValue* initializer_value,
const OrtExternalInitializerInfo* external_info,
const OrtExternalInitializerInfo* /*external_info*/,
OrtExternalInitializerInfo** new_external_info) {
const OrtApi& ort_api = Ort::GetApi();
CustomInitializerHandlerState* custom_state = reinterpret_cast<CustomInitializerHandlerState*>(state);
Expand All @@ -704,20 +704,6 @@ static OrtStatus* ORT_API_CALL TestHandleInitializerDataFunc(void* state,
// Store other initializers in an external file.
//

// If the original initializer was stored in an external file, keep it there (just for testing).
if (external_info != nullptr) {
const ORTCHAR_T* location = ort_api.ExternalInitializerInfo_GetFilePath(external_info);
int64_t offset = ort_api.ExternalInitializerInfo_GetFileOffset(external_info);
size_t byte_size = ort_api.ExternalInitializerInfo_GetByteSize(external_info);

if (OrtStatus* status = ort_api.CreateExternalInitializerInfo(location, offset, byte_size, new_external_info);
status != nullptr) {
return status;
}

return nullptr;
}

// Get initializer's byte size
size_t byte_size = 0;
if (OrtStatus* status = ort_api.GetTensorSizeInBytes(initializer_value, &byte_size); status != nullptr) {
Expand Down Expand Up @@ -794,6 +780,68 @@ TEST_F(QnnHTPBackendTests, CompileApi_InputFile_OutputFile_InitializerHandler) {
CheckEpContextNodeCounts(output_model_file, 2, 2);
}

static OrtStatus* ORT_API_CALL ReuseExternalInitializers(void* state,
const char* initializer_name,
const OrtValue* initializer_value,
const OrtExternalInitializerInfo* external_info,
OrtExternalInitializerInfo** new_external_info) {
const OrtApi& ort_api = Ort::GetApi();

// If the original initializer was stored in an external file, keep it there (just for testing).
if (external_info != nullptr) {
const ORTCHAR_T* location = ort_api.ExternalInitializerInfo_GetFilePath(external_info);
int64_t offset = ort_api.ExternalInitializerInfo_GetFileOffset(external_info);
size_t byte_size = ort_api.ExternalInitializerInfo_GetByteSize(external_info);

if (OrtStatus* status = ort_api.CreateExternalInitializerInfo(location, offset, byte_size, new_external_info);
status != nullptr) {
return status;
}

// Keep track of number of reused external initializers so that we can assert
// that we reused the expected number of initializers.
// THIS IS TEST CODE. An application would not do this.
size_t* num_reused_ext_initializers = reinterpret_cast<size_t*>(state);
*num_reused_ext_initializers += 1;

return nullptr;
}

// If not originally external, save it within the generated compiled model
*new_external_info = nullptr;
return nullptr;
}

// Test using the CompileModel() API with settings:
// - input model comes from a file
// - write output model to a file
// - Use callback to specify where each initializer is stored. We'll reuse external initializers
// from original model!
TEST_F(QnnHTPBackendTests, CompileApi_InitializerHandler_ReuseExternalInitializers) {
const ORTCHAR_T* input_model_file = ORT_TSTR("testdata/conv_qdq_external_ini.onnx");
const ORTCHAR_T* output_model_file = ORT_TSTR("testdata/conv_qdq_external_ini_reuse_ctx.onnx");
std::filesystem::remove(output_model_file);

size_t num_reused_ext_initializers = 0;

// Create model compilation options from the session options.
Ort::SessionOptions so;
Ort::ModelCompilationOptions compile_options(*ort_env, so);
compile_options.SetInputModelPath(input_model_file);
compile_options.SetOutputModelPath(output_model_file);
compile_options.SetOutputModelHandleInitializerFunc(ReuseExternalInitializers,
reinterpret_cast<void*>(&num_reused_ext_initializers));
compile_options.SetEpContextEmbedMode(true);

// Compile the model.
Ort::Status status = Ort::CompileModel(*ort_env, compile_options);
ASSERT_TRUE(status.IsOK()) << status.GetErrorMessage();
ASSERT_TRUE(std::filesystem::exists(output_model_file));
std::filesystem::remove(output_model_file);

ASSERT_EQ(num_reused_ext_initializers, 2); // Reused external conv weight and bias.
}

// Test that the explicit compile API can be configured to return an error if the output model does not
// have EPContext nodes.
TEST_F(QnnHTPBackendTests, CompileApi_SetFlags_ErrorIfNoCompiledNodes) {
Expand Down
Loading