Skip to content

Add EP-agnostic callbacks for persisting EP caches - #34013

Open
Danny On The Air (dannyota) wants to merge 1 commit into
microsoft:mainfrom
dannyota:capi/ep-cache-callbacks
Open

Danny On The Air (dannyota) wants to merge 1 commit into
microsoft:mainfrom
dannyota:capi/ep-cache-callbacks

Conversation

@dannyota

@dannyota Danny On The Air (dannyota) commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Description

Implements the proposal in #33748: application-supplied callbacks for an EP's persistent caches (JIT kernel caches, compiled-engine caches, timing caches), as an alternative to filesystem paths.

  • OrtApi::SessionOptionsSetEpCacheDataFuncs(options, read_func, write_func, state) reuses OrtReadNamedBufferFunc / OrtWriteNamedBufferFunc. Each call replaces the tuple; NULL clears a direction.
  • EPs read them through OrtEpApi::SessionOptionsGetEpCacheDataFuncs and advertise READ/WRITE support through an optional OrtEp::GetEpCacheDataCallbackSupport (version-gated). Applications query support after session creation with OrtApi::SessionGetEpCacheDataCallbackSupport.
  • When a callback is set and the EP advertises support, the EP uses callback-only storage: a failed or invalid read is a cache miss, a failed write logs a bounded warning, and there is no filesystem fallback. Keys are EP-owned and filesystem-safe.
  • EPContext callbacks and their error semantics are unchanged. Model compilation inherits the callbacks through the copied session options.
  • C++ wrappers included. The example plugin EP persists a versioned Mul plan to show the flow end to end.

Follow-ups: preflight/strict capability enforcement, Python/C# bindings, and adoption by hardware EPs.

Testing (macOS arm64 and Linux x64, --build_shared_lib)

  • onnxruntime_autoep_test --gtest_filter='OrtEpLibrary.*': 79 pass, including new EpCacheOptions, EpCacheRoundTrip, EpCacheFailures and EpCacheCompatibility (cold store and warm reuse, corruption and size limits, one-sided callbacks, compilation inheritance, concurrent sessions, older-EP version gating).
  • Linux: the example plugin EP's .lds exports the new test hooks; same 79 pass.

Motivation and Context

Fixes #33748. Sandboxed hosts cannot give EPs a writable cache directory; this lets them keep EP caches in their own storage.

EPs keep persistent caches (JIT kernels, compiled engines, timing data)
under filesystem paths, which sandboxed hosts cannot use. The EPContext
read/write callbacks already let applications own model artifacts, but
not these optional, performance-only caches.

Add SessionOptionsSetEpCacheDataFuncs, reusing OrtReadNamedBufferFunc
and OrtWriteNamedBufferFunc. EPs read the callbacks through
OrtEpApi::SessionOptionsGetEpCacheDataFuncs and advertise READ/WRITE
support through an optional OrtEp::GetEpCacheDataCallbackSupport;
applications query it with SessionGetEpCacheDataCallbackSupport. When a
callback is set and the EP supports it, the EP uses callback-only
storage: read failures are cache misses and write failures are logged.
Compilation inherits the callbacks through the copied session options.

The example plugin EP persists a versioned Mul plan to show the flow.

Signed-off-by: Danny <dannyontheair@outlook.com>
Copilot AI balanced review requested due to automatic review settings November 9, 2026 15:26
@azure-pipelines

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

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.

🟢 Approval recommended

There are no active review issues for the application-managed execution-provider cache changes.

No open issues 🎉

What changed in this PR

Adds application-managed persistent caches for execution providers (EPs), addressing #33748. The APIs let hosts supply storage callbacks instead of filesystem paths while keeping EPContext behavior unchanged.

Changes:

  • Adds C APIs and C++ wrappers for cache callbacks and per-session capability queries.
  • Stores callbacks in session options and adds version-gated plugin support.
  • Demonstrates validated cache reuse in the example EP, with tests for failures, compatibility, compilation inheritance, and concurrent sessions.
File Description
onnxruntime/​test/​autoep/​test_execution.cc Adds cache lifecycle, failure, and compatibility tests.
onnxruntime/​test/​autoep/​test_autoep_utils.h Declares cache test-hook pointers.
onnxruntime/​test/​autoep/​test_autoep_utils.cc Loads cache test hooks on Windows and POSIX.
onnxruntime/​test/​autoep/​library/​example_plugin_ep/​example_plugin_ep_library.lds Exports cache test hooks.
onnxruntime/​test/​autoep/​library/​example_plugin_ep/​ep.h Declares cache configuration and plan helpers.
onnxruntime/​test/​autoep/​library/​example_plugin_ep/​ep.cc Implements bounded, validated Mul-plan persistence.
onnxruntime/​test/​autoep/​library/​example_plugin_ep/​ep_test_hooks.h Declares cache instrumentation hooks.
onnxruntime/​test/​autoep/​library/​example_plugin_ep/​ep_test_hooks.cc Implements atomic cache counters.
onnxruntime/​test/​autoep/​library/​example_plugin_ep/​ep_factory.cc Retrieves callbacks and configures compatibility fixtures.
onnxruntime/​core/​session/​plugin_ep/​ep_plugin_provider_interfaces.h Declares the plugin capability-query override.
onnxruntime/​core/​session/​plugin_ep/​ep_plugin_provider_interfaces.cc Gates capability queries by version and validates flags.
onnxruntime/​core/​session/​plugin_ep/​ep_api.h Declares the callback getter implementation.
onnxruntime/​core/​session/​plugin_ep/​ep_api.cc Implements and registers the callback getter.
onnxruntime/​core/​session/​ort_apis.h Declares application-facing API implementations.
onnxruntime/​core/​session/​onnxruntime_c_api.cc Implements session capability queries and registers APIs.
onnxruntime/​core/​session/​abi_session_options.cc Implements callback replacement and clearing.
onnxruntime/​core/​framework/​session_options.h Stores callbacks and shared application state.
include/​onnxruntime/​core/​session/​onnxruntime_ep_c_api.h Defines and documents EP-facing cache APIs.
include/​onnxruntime/​core/​session/​onnxruntime_cxx_inline.h Implements C++ wrappers.
include/​onnxruntime/​core/​session/​onnxruntime_cxx_api.h Declares C++ wrappers.
include/​onnxruntime/​core/​session/​onnxruntime_c_api.h Defines support flags and public cache contracts.
include/​onnxruntime/​core/​framework/​execution_provider.h Adds a default unsupported capability response.

Review effort: Balanced

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.

[Feature Request] EP-agnostic callbacks for persisting EP runtime/compilation caches

2 participants