Skip to content

Add pybindings for Model Packaging - #33640

Open
Preetha Veeramalai (preetha-intel) wants to merge 4 commits into
microsoft:mainfrom
intel:pybindings/model_package
Open

Preetha Veeramalai (preetha-intel) wants to merge 4 commits into
microsoft:mainfrom
intel:pybindings/model_package

Conversation

@preetha-intel

Copy link
Copy Markdown
Contributor

Description

This PR adds Python bindings for the Model Package APIs. Python applications can now create, inspect and edit model packages, and select a variant and create an inference session from one. It also exposes set_ep_context_binary_information on ModelCompiler.

Changes

  • Add pybind bindings for model package authoring (ModelPackage) and for loading and selecting a variant (ModelPackageContext, ModelPackageOptions, ModelPackageComponentContext).
  • Add ModelCompiler.set_ep_context_binary_information.
  • Link model_package into the Python module and export the new classes from onnxruntime.
  • The bindings are not built in minimal builds.

Motivation and Context

The python API's are helpful in creating sample applications for public adoptions.

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Preetha Veeramalai (@preetha-intel), this looks good. I checked the bindings against model_package.h and the model package C API: the string and info-tree lifetimes are handled correctly (everything is copied before the next mutation), the OrtSession* adoption matches what CreateSession returns, and the session-options snapshot is only used while ModelPackageOptions is being constructed. The tests cover authoring → reopen → select → create_session end to end, plus releasing the context before the component.

I left a few suggestions inline. The first one (the type create_session returns) is probably worth settling before this ships, since public Python API shapes are hard to change later. I'll approve once you've had a chance to respond.

Note: the Azure pipelines haven't run yet. A maintainer will need to trigger /azp run.

@MayureshV1

Copy link
Copy Markdown
Contributor

All review feedback has been implemented and verified locally (CPU build with pybind, model package tests passing), hence approving.

@preetha-intel

Copy link
Copy Markdown
Contributor Author

Xiaoyu Z (@xiaoyu-work) I have added the lint fixes. Can you help retriggering the pipelines

@preetha-intel

Copy link
Copy Markdown
Contributor Author

The wasm_debug build failure is not related to this PR code changes. Its failing because the wasm runtime couldnt access the mounted directory.

@chilo-ms

Copy link
Copy Markdown
Contributor

Please help merge current main to include the fix for Web CI failure.

@chilo-ms

Copy link
Copy Markdown
Contributor

Rechecked head 50320ae80016aa3d3104c324e766009bf4617e00: all four review threads are already resolved, including the accepted exception-mapping follow-up. No duplicate binding changes are needed.

The remaining failure is Web CI / wasm_Debug, Test (Node.js) (simd + threads), currently run attempt 6. Three CheckAndBindComponentTests fail while loading disk external constants: DiskExternalConstantsUseOrtLoadingAndPropagateFailures, DiskExternalScalarAndSingletonConstantsPreserveExactTypeAndRank, and EmptyExternalConstantsValidateFilesAndOffsetsWithoutReadingWholeFiles. The actual error is Module.MountedFiles is not available in onnxruntime/test/optimizer/pattern_rewriter_components_test.cc, which is outside this PR's changes.

This is a WASM external-file test/harness compatibility failure, not a transient directory-permission or network failure. It needs a harness/test adjustment or an upstream fix; another unchanged rerun is not appropriate. I have not queued a rerun.

Copilot AI balanced review requested due to automatic review settings November 3, 2026 18:46
@preetha-intel

Copy link
Copy Markdown
Contributor Author

I have rebased with main for latest upstream changes

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unresolved synchronization gaps allow concurrent operations to access partially initialized session caches or freed package handles, creating critical memory-safety risks.

4 open issues
What changed in this PR

Adds Python access to ONNX Runtime’s model package APIs for authoring packages, inspecting and selecting variants, and creating inference sessions.

Changes:

  • Exposes model package classes in non-minimal builds.
  • Wraps package-created sessions as Python InferenceSession objects, with unsupported recreation operations blocked.
  • Adds ModelCompiler.set_ep_context_binary_information and tests for the new APIs.
File Description
onnxruntime/​test/​python/​onnxruntime_test_python_model_package.py Adds authoring, selection, inference, and lifetime tests.
onnxruntime/​test/​python/​onnxruntime_test_python_compile_api.py Tests EPContext binary configuration and invalid arguments.
onnxruntime/​python/​onnxruntime_pybind_state.cc Registers package bindings and the compiler setter.
onnxruntime/​python/​onnxruntime_pybind_state_common.h Supports adopting initialized native sessions.
onnxruntime/​python/​onnxruntime_pybind_model_package.cc Implements package authoring and consumption bindings.
onnxruntime/​python/​onnxruntime_pybind_model_compiler.h Declares the EPContext binary configuration setter.
onnxruntime/​python/​onnxruntime_pybind_model_compiler.cc Implements the setter with compilation-state protection.
onnxruntime/​python/​onnxruntime_inference_collection.py Wraps package sessions and exposes the compiler setter.
onnxruntime/​__init__.py Exports the new package classes when available.
cmake/​onnxruntime_python.cmake Links the model package library into the Python module.

Review effort: Balanced

Comment on lines +137 to +138
explicit BusyScope(PyModelPackage& owner) : owner_(owner) { owner_.busy_ = true; }
~BusyScope() { owner_.busy_ = false; }
Comment on lines +261 to +263
py::gil_scoped_release release;
OrtPybindThrowIfError(ToStatusAndRelease(Ort::GetModelPackageApi().CreateSession(
GetOrtEnv(), self, options ? &snapshot.options : nullptr, &result)));
std::lock_guard<std::mutex> lock{compilation_mutex_};
ORT_RETURN_IF(compilation_in_progress_,
"Cannot change the EPContext binary information while compilation is in progress.");
return model_compile_options_.SetEpContextBinaryInformation(output_directory, model_name);
Comment on lines +159 to +160
if __name__ == "__main__":
unittest.main()

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants