This directory implements ONNX Runtime's consumer-side glue for the
standalone model_package library:
loading packages, selecting variants against the runtime's execution
providers, and creating an OrtSession for the chosen variant.
The package format, manifest schema, shared-asset rules, and the C
authoring/inspection API all live in model_package/. This directory
adds three things on top:
executor_info["ort"] payload schema (this is ORT's slot in theOrtModelPackageApi C API table that wraps the library andOrtApi::GetModelPackageApi.ORT links the model_package library as a static archive; the library
itself never links against ORT.
| File | Responsibility |
|---|---|
model_package_context.h/.cc |
Translates the model_package library's C info tree into ORT-internal C++ structs (ModelPackageInfo, ComponentInfo, VariantInfo, VariantModelInfo). Parses the executor_info["ort"] payload. Owns ModelPackageContext (package-level) and ModelPackageComponentContext (per-component, with selected variant and provider list). |
model_package_options.h/.cc |
ModelPackageOptions snapshots EP intent (factories, devices, EP-name list) from an OrtSessionOptions when OrtModelPackageApi::CreateModelPackageOptionsFromSessionOptions is called. Drives variant selection and provider construction. |
model_package_variant_selector.h/.cc |
VariantSelector::SelectVariant picks the best variant from a component given the EP list. Uses OrtEpFactory::ValidateCompiledModelCompatibilityInfo. |
The C entry points themselves live inonnxruntime/core/session/model_package_api.cc undernamespace OrtModelPackageAPI.
executor_info["ort"] schemaORT's slot in variant.executor_info is a JSON object. All fields are
optional, but in practice model_file is required to load a session.
{
"model_file": "model.onnx",
"session_options": {
"session.intra_op_thread_count": "4",
"session.model_external_initializers_file_folder_path": "weights"
},
"provider_options": { "device_id": "0" }
}
| Field | Type | Required | Notes |
|---|---|---|---|
model_file |
string | yes (for session) | Path to the model file inside the variant. Resolved via ModelPackage_ResolveStringRef, anchored at the variant directory. Accepts relative paths, absolute paths or .. segments (installed layout only), and sha256:<hex>[/sub/path] for shared-asset content. |
session_options |
object | no | Map of string -> string. Merged on top of a fresh OrtSessionOptions when the caller passes session_options == NULL to CreateSession. Values of path-valued keys (see IsModelPackagePathSessionOption, e.g. session.model_external_initializers_file_folder_path, ep.context_file_path) are resolved with the same rules as model_file at parse time. Those path-valued keys are also applied on the advanced path if the caller did not set them (see below). Output file options (session.debug_layout_transformation, session.collect_node_memory_stats_to_file, session.enable_profiling, and session.optimized_model_filepath) are forbidden in a package and cause package parsing to fail; only caller-supplied OrtSessionOptions may set those keys. |
provider_options |
object | no | Map of string -> string. Merged into the variant's EP provider options on the default path. Ignored when the caller supplies their own OrtSessionOptions. |
The slot follows the standard executor_info shape: the value may be either
ort_info.json next to model.onnx), orcomponent.json /manifest.json.Inline form keeps the package single-file. External form (the common case)
keeps the variant directory self-describing and survives executor_info
schema evolution without rewriting the manifest.
The key under executor_info is the executor namespace name ("ort"),
not the EP. Other consumers use their own namespace key, so a single
variant can carry per-consumer payloads side by side.
ModelPackageOptions(env, session_options) captures the EP intent: the
ordered list of execution providers registered on the session options, plus
their associated OrtEpDevice / OrtHardwareDevice / metadata.
VariantSelector::SelectVariant(component, ep_infos, &selected) then walks
the component's variants and picks the best match:
variant.ep == ep_info.ep_name.variant.device is set ("cpu" / "gpu" / "npu"), require it toOrtHardwareDevice entries.OrtEpFactory::ValidateCompiledModelCompatibilityInfovariant.compatibility_string. The EP returns anOrtCompiledModelCompatibility enum which maps to a score:| Enum | Score |
| -------------------------------------------- | ----- |
| EP_SUPPORTED_OPTIMAL | 100 |
| EP_SUPPORTED_PREFER_RECOMPILATION | 50 |
| EP_NOT_APPLICABLE (or EP too old / no ABI) | 0 |
| EP_UNSUPPORTED | rejected |
If no variant matches, SelectComponent fails with "No suitable model
variant found for the configured execution providers."
ORT does not parse compatibility_string. The EP owns the format and
may encode multiple sub-targets (SoC ids, ISA flags, etc.) into the single
string internally; ORT only round-trips it through the EP callback.
OrtModelPackageApi::CreateSession(env, component_ctx, session_options, &session).
The component_ctx already knows which variant won selection and which
provider list it should use. Two paths:
session_options == NULL (default). ORT starts from a freshOrtSessionOptions and merges the variant's session_options /provider_options from executor_info["ort"] on top. EPs declared in the
manifest are constructed and registered. This is what nearly all callers
want.
session_options != NULL (advanced). ORT uses the caller-suppliedOrtSessionOptions as-is. The manifest's session_options andprovider_options are not merged, with one exception: path-valued
session options (see IsModelPackagePathSessionOption) are carried over
from the variant for keys the caller did not set, so a model that needs its
external-initializers folder still loads. Use this path when you need custom
EP setup that does not round-trip through string options (shared CUDA
streams, shared QNN EP contexts, custom allocators, ...). TheOrtSessionOptions passed earlier toCreateModelPackageOptionsFromSessionOptions only drives variant
selection / EP discovery; it is never silently re-applied here.
A variant points ORT at external-initializer weights by settingsession.model_external_initializers_file_folder_path in itssession_options to a folder (relative, absolute, or sha256:<hex> shared
asset). The value is resolved at parse time and overrides the model's own
directory, so the model file can reference weights stored next to (or shared
by) the package.
The model package API is a stable companion table returned byOrtApi::GetModelPackageApi. Its opaque handles
(OrtModelPackageOptions, OrtModelPackageContext, andOrtModelPackageComponentContext) and function table are declared inonnxruntime_c_api.h. onnxruntime_cxx_api.h provides RAII wrappers.
API entries:
| Function | Notes |
|---|---|
CreateModelPackageOptionsFromSessionOptions |
Snapshots EP intent. |
ReleaseModelPackageOptions |
|
CreateModelPackageContext |
Parses the manifest. |
ReleaseModelPackageContext |
|
ModelPackage_GetSchemaVersion |
|
ModelPackage_GetComponentCount |
|
ModelPackage_GetComponentNames |
|
ModelPackage_GetVariantCount |
|
ModelPackage_GetVariantNames |
|
ModelPackage_GetVariantEpName |
|
ModelPackage_ResolveStringRef |
Resolves package-relative and content-addressed paths. |
SelectComponent |
Resolves the best-matching variant. |
ReleaseModelPackageComponentContext |
|
ModelPackageComponent_GetSelectedVariantName |
|
ModelPackageComponent_GetSelectedVariantFolderPath |
|
CreateSession |
Typical flow:
#include "onnxruntime_c_api.h"
const OrtApi* ort = OrtGetApiBase()->GetApi(ORT_API_VERSION);
const OrtModelPackageApi* package_api = ort->GetModelPackageApi();
OrtSessionOptions* so = nullptr;
ort->CreateSessionOptions(&so);
ort->SessionOptionsAppendExecutionProvider(so, "CUDAExecutionProvider", nullptr, nullptr, 0);
OrtModelPackageOptions* mp_opts = nullptr;
package_api->CreateModelPackageOptionsFromSessionOptions(env, so, &mp_opts);
OrtModelPackageContext* ctx = nullptr;
package_api->CreateModelPackageContext(ORT_TSTR("/path/to/pkg"), &ctx);
OrtModelPackageComponentContext* comp_ctx = nullptr;
package_api->SelectComponent(ctx, "decoder", mp_opts, &comp_ctx);
OrtSession* session = nullptr;
package_api->CreateSession(env, comp_ctx, nullptr, &session);
ort->ReleaseSession(session);
package_api->ReleaseModelPackageComponentContext(comp_ctx);
package_api->ReleaseModelPackageContext(ctx);
package_api->ReleaseModelPackageOptions(mp_opts);
ort->ReleaseSessionOptions(so);
All const char* / const ORTCHAR_T* / array pointers returned by the API
are owned by the context that produced them and remain valid until the
context is released.
model_package/README.md: packageexecutor_info extension point.include/onnxruntime/core/session/onnxruntime_c_api.h: the stableOrtModelPackageApi declaration.