Skip to content

Model package: fold external_data into session options and remove legacy directory load path - #29501

Merged
jambayk merged 3 commits into
mainfrom
jambayk/model-package-ext-data
Jul 7, 2026
Merged

Model package: fold external_data into session options and remove legacy directory load path#29501
jambayk merged 3 commits into
mainfrom
jambayk/model-package-ext-data

Conversation

@jambayk

@jambayk jambayk commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Description

Two changes to the model package flow, enabled by the file-path external initializers support:

1. Fold external_data into session_options with path resolution.

  • Path-valued session options (an allowlist: session.model_external_initializers_file_folder_path and ep.context_file_path) are resolved against the package (sha256:<hex>, relative, or absolute) at variant-parse time, using the same rules as model_file.
  • The dedicated external_data variant field and its special session injection are removed; the model is always loaded from the selected variant path.
  • On the advanced path (caller supplies their own OrtSessionOptions), path-valued options are carried over from the variant for keys the caller did not set, so a model that needs its external-initializers folder still loads.

2. Remove the legacy directory-based CreateSession(package_dir) path.

  • Removes the is_directory(package_root) branch in CreateSessionAndLoadModelImpl. Model packages are loaded through the experimental OrtModelPackageApi (CreateModelPackageContext -> SelectComponent -> CreateSession).
  • The three end-to-end tests that exercised the directory path are migrated to the experimental API via a CreateSessionFromModelPackage test helper, preserving coverage for factory-based selection, PREFER_CPU policy selection, and compiled-model compatibility scoring.

Motivation and Context

The file-path external initializers folder support (#29459) lets the model package flow drop its workaround of memory-mapping the model and forcing a buffer load: it sets the folder option and loads from the selected variant path directly. Building on that, the dedicated external_data field folds into the general session-options mechanism. A variant declares session.model_external_initializers_file_folder_path (or other path-valued options) in its session_options, and ORT resolves those values against the package at parse time. This removes special-case code and lets other path-valued options such as the EPContext file path be resolved the same way. An allowlist is used rather than value-syntax sniffing because session-option values are arbitrary strings; only known path-valued keys are resolved, so ordinary values pass through untouched.

The legacy directory-based CreateSession(package_dir) path was meant to be removed when the experimental OrtModelPackageApi landed. It only did variant/EP selection and never merged the variant's session_options/provider_options, so a package loaded via Ort::Session(env, dir, so) silently ignored its manifest session options, inconsistent with the experimental API. Removing it leaves a single, consistent way to load a model package.

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.

Pull request overview

This PR simplifies the model package loading flow by (1) eliminating the dedicated external_data manifest field in favor of resolving path-valued session_options entries at variant-parse time, and (2) removing the legacy OrtCreateSession(package_dir) directory-based model package load path in favor of the experimental OrtModelPackageApi pipeline.

Changes:

  • Resolve allowlisted path-valued session options (e.g., session.model_external_initializers_file_folder_path, ep.context_file_path) against the package during variant parsing, and carry over those options on the “advanced” session-options path when unset by the caller.
  • Remove the legacy directory-based model package load path from CreateSessionAndLoadModelImpl.
  • Migrate existing end-to-end tests to create sessions via the experimental OrtModelPackageApi, and add a new test covering external-initializers folder resolution via variant session options.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
onnxruntime/test/autoep/test_model_package.cc Adds a helper to create sessions via OrtModelPackageApi, migrates directory-path tests, and adds a new test for resolved external initializers folder session option.
onnxruntime/core/session/utils.cc Removes directory-based model package loading from the standard session creation path.
onnxruntime/core/session/model_package/README.md Updates manifest docs to move external_data behavior into session_options and documents path-valued option resolution and carryover semantics.
onnxruntime/core/session/model_package/model_package_context.h Removes legacy external-data fields and introduces IsModelPackagePathSessionOption.
onnxruntime/core/session/model_package/model_package_context.cc Implements the allowlist and resolves path-valued session options during variant parsing.
onnxruntime/core/session/model_package_api.cc On advanced path, carries over only variant path-valued session options that the caller didn’t set.

Comment thread onnxruntime/core/session/utils.cc
Comment thread onnxruntime/test/autoep/test_model_package.cc
Comment thread onnxruntime/core/session/model_package/model_package_context.h
jambayk and others added 3 commits July 6, 2026 23:54
…ution

Resolve path-valued session options (an allowlist: the external initializers
folder and ep.context_file_path) against the model package at variant-parse
time, so variants reference shared assets by sha256: URI or relative path
directly in session_options.

Remove the dedicated external_data variant field and its special injection;
the model is now always loaded from the selected variant path. On the advanced
path, carry over path-valued session options from the variant for keys the
caller did not set.

Builds on the file-path external initializers folder support so the model
package flow no longer needs to mmap the model and force a buffer load.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Remove the is_directory(package_root) branch in CreateSessionAndLoadModelImpl
that loaded a model package directly from a directory passed to CreateSession.
Model packages are loaded through the experimental OrtModelPackageApi
(CreateModelPackageContext -> SelectComponent -> CreateSession); this legacy
inline path was meant to be removed when that API landed and it never merged
variant session/provider options.

Migrate the three end-to-end tests that exercised the directory path to the
experimental API via a CreateSessionFromModelPackage test helper, preserving
coverage for factory-based selection, PREFER_CPU policy selection, and
compiled-model compatibility scoring.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- CreateSessionAndLoadModelImpl: fail early with an explicit message when a
  directory path is passed, pointing callers at the model package API instead
  of falling through to a generic file-open error. Add a test for it.
- model_package_context.h: include <string_view> directly for
  IsModelPackagePathSessionOption instead of relying on transitive includes.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@jambayk
jambayk force-pushed the jambayk/model-package-ext-data branch from 96209f8 to 4410f86 Compare July 6, 2026 23:57
@jambayk
jambayk merged commit eca15c6 into main Jul 7, 2026
87 checks passed
@jambayk
jambayk deleted the jambayk/model-package-ext-data branch July 7, 2026 17:38
tianleiwu pushed a commit that referenced this pull request Jul 7, 2026
…acy directory load path (#29501)

### Description

Two changes to the model package flow, enabled by the file-path external
initializers support:

**1. Fold `external_data` into `session_options` with path resolution.**
- Path-valued session options (an allowlist:
`session.model_external_initializers_file_folder_path` and
`ep.context_file_path`) are resolved against the package
(`sha256:<hex>`, relative, or absolute) at variant-parse time, using the
same rules as `model_file`.
- The dedicated `external_data` variant field and its special session
injection are removed; the model is always loaded from the selected
variant path.
- On the advanced path (caller supplies their own `OrtSessionOptions`),
path-valued options are carried over from the variant for keys the
caller did not set, so a model that needs its external-initializers
folder still loads.

**2. Remove the legacy directory-based `CreateSession(package_dir)`
path.**
- Removes the `is_directory(package_root)` branch in
`CreateSessionAndLoadModelImpl`. Model packages are loaded through the
experimental `OrtModelPackageApi` (`CreateModelPackageContext` ->
`SelectComponent` -> `CreateSession`).
- The three end-to-end tests that exercised the directory path are
migrated to the experimental API via a `CreateSessionFromModelPackage`
test helper, preserving coverage for factory-based selection,
`PREFER_CPU` policy selection, and compiled-model compatibility scoring.

### Motivation and Context

The file-path external initializers folder support (#29459) lets the
model package flow drop its workaround of memory-mapping the model and
forcing a buffer load: it sets the folder option and loads from the
selected variant path directly. Building on that, the dedicated
`external_data` field folds into the general session-options mechanism.
A variant declares
`session.model_external_initializers_file_folder_path` (or other
path-valued options) in its `session_options`, and ORT resolves those
values against the package at parse time. This removes special-case code
and lets other path-valued options such as the EPContext file path be
resolved the same way. An allowlist is used rather than value-syntax
sniffing because session-option values are arbitrary strings; only known
path-valued keys are resolved, so ordinary values pass through
untouched.

The legacy directory-based `CreateSession(package_dir)` path was meant
to be removed when the experimental `OrtModelPackageApi` landed. It only
did variant/EP selection and never merged the variant's
`session_options`/`provider_options`, so a package loaded via
`Ort::Session(env, dir, so)` silently ignored its manifest session
options, inconsistent with the experimental API. Removing it leaves a
single, consistent way to load a model package.

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.

3 participants