Skip to content

Weightless support for all initializers#29607

Draft
chilo-ms wants to merge 11 commits into
mainfrom
chilo-ms/weightless-all-initializers
Draft

Weightless support for all initializers#29607
chilo-ms wants to merge 11 commits into
mainfrom
chilo-ms/weightless-all-initializers

Conversation

@chilo-ms

@chilo-ms chilo-ms commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Description

Expand ORT's weightless mode to cover all initializers (internal and external), working in both JIT and AOT flows.

Problem

The existing  ep.enable_weightless_ep_context_nodes  session option is a string-based convention between the app and the EP with no ORT Core involvement. It only covers external initializers in the AOT flow, has no versioning or capability negotiation, and JIT flows cannot benefit.

Changes

Session options ( onnxruntime_session_options_config_keys.h ):

  • Add  ep.enable_weightless : unified weightless option for all initializers (JIT + AOT)
  • Add  ep.context_source_model_path : runtime override for source model location
  • Deprecate  ep.enable_weightless_ep_context_nodes 

OrtEpApi ( onnxruntime_ep_c_api.h ):

  • Add  OrtWeightlessSupport  enum (NONE / EXTERNAL_ONLY / ALL)
  • Add  GetWeightlessSupport : EP capability query for the handshake

OrtApi ( onnxruntime_c_api.h ):

  • Add  SessionOptionsSetWeightlessSourceModelPath : file path to source model
  • Add  SessionOptionsSetWeightlessSourceModelFromBuffer : byte buffer source model

OrtCompileApi ( onnxruntime_c_api.h ):

  • Add  ModelCompilationOptions_SetWeightlessCache : enable weightless for AOT compilation

ORT Core behavior ( ep_plugin_provider_interfaces.cc ):

  • When weightless is enabled, ORT auto-overrides  drop_constant_initializers  to false so constants remain as fused node inputs accessible via  KernelContext_GetInput() 

Future work

  • Extending OrtValue lifetime past Compile() for direct pointer caching

Motivation and Context

chilo-ms and others added 10 commits July 1, 2026 17:15
Add API surface for expanding weightless mode to cover all initializers
(internal and external), working in both JIT and AOT flows:

Session options:
- Add ep.enable_weightless: unified weightless option for all initializers
- Add ep.context_source_model_path: runtime override for source model location
- Deprecate ep.enable_weightless_ep_context_nodes (external-only, AOT-only)

OrtEpApi:
- Add GetWeightlessSupport: EP capability query for the handshake.
  App requests weightless (session option), EP confirms support (this API).

OrtCompileApi:
- Add ModelCompilationOptions_SetWeightlessCache: enable weightless for AOT
  compilation path.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Clarify that extending OrtValue lifetime past Compile() for direct pointer
caching is planned but not yet implemented. Currently, EPs should use
drop_constant_initializers=false and access initializers via
KernelContext_GetInput() at Compute() time.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
… time

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
… timing

- ep.enable_weightless: clarify JIT uses KernelContext path (direct pointer
  caching is TODO), remove premature lifetime extension claim
- ep.context_source_model_path: fix 'inference time' to 'session creation'

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Add two new OrtApi functions for providing the source model's initializer
data when creating a session from a weightless EPContext model:

- SessionOptionsSetWeightlessSourceModelPath: file path to source model,
  acts as runtime override for the onnx_model_filename EPContext attribute.
- SessionOptionsSetWeightlessSourceModelFromBuffer: in-memory byte buffer
  for scenarios where the source model is not on disk. Caller must keep
  the buffer alive for the session lifetime.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
SessionOptionsSetWeightlessSourceModelPath:
- Stores file path in OrtSessionOptions for runtime override of
  onnx_model_filename EPContext attribute
- Clears any previously set buffer source

SessionOptionsSetWeightlessSourceModelFromBuffer:
- Stores borrowed buffer pointer + size in OrtSessionOptions
- Caller must keep buffer alive for session lifetime
- Clears any previously set file path source

ModelCompilationOptions_SetWeightlessCache:
- Adds use_weightless_cache_ flag to ModelCompilationOptions
- Sets ep.enable_weightless=1 in session config options
- Registered in ort_compile_api table

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
When the app requests weightless mode and the EP confirms support via
GetWeightlessSupport(), ORT automatically overrides drop_constant_initializers
to false in GetCapability(). This ensures constant initializers remain as
fused node inputs so the EP can access them via KernelContext_GetInput()
at Compute() time.

The override is logged at INFO level for diagnostics.

Constructor queries the EP's weightless support at creation time and stores
the result in weightless_enabled_ for use during GetCapability().

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Add kOrtEpDevice_EpMetadataKey_WeightlessSupport to
onnxruntime_ep_device_ep_metadata_keys.h. EPs set this during
GetSupportedDevices() so apps can query device-specific weightless
capability before compilation via EpDevice_EpMetadata().

Valid values: 'none', 'external_only', 'all'.

Update all new weightless APIs from version 1.28 to 1.29:
- Session options: ep.enable_weightless, ep.context_source_model_path
- OrtEpApi: OrtWeightlessSupport enum, GetWeightlessSupport
- OrtApi: SessionOptionsSetWeightlessSourceModelPath,
  SessionOptionsSetWeightlessSourceModelFromBuffer
- OrtCompileApi: ModelCompilationOptions_SetWeightlessCache
- Version gate in PluginExecutionProvider constructor

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

@github-actions github-actions Bot 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.

You can commit the suggested changes from lintrunner.

Comment thread onnxruntime/core/session/onnxruntime_c_api.cc Outdated
// When weightless mode is enabled, override drop_constant_initializers to false so that ORT keeps
// constant initializers as fused node inputs. The EP can then access them via KernelContext_GetInput().
bool drop_constant_initializers = node_grouping.fusion_options.drop_constant_initializers;
if (weightless_enabled_ && drop_constant_initializers) {

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.

A couple of comments:

  • The override should only happen if the EP support level is all (2), otherwise if it's one this extends the initializers lifetime for no benefit; it's actually worse because EP would copy those to the compiled model. My suggestion is to make weightless_enabled_ of type OrtWeightlessSupport and check weightless_enabled_ >= OrtWeightlessSupport_ALL
  • Another thought is that EPs handle initializers in different ways, most use KernelContext_GetInput() but OV doesn't do that, instead it directly uses the initializer values. I guess that once phase 2 is done to reuse the initializers from Compile() in Compute() this code will not be necessary, is that correct?

ORT_API_STATUS_IMPL(OrtApis::SessionOptionsSetWeightlessSourceModelPath, _Inout_ OrtSessionOptions* options,
_In_ const ORTCHAR_T* source_model_path) {
API_IMPL_BEGIN
if (source_model_path == nullptr || source_model_path[0] == '\0') {

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.

Should it also validate that the path is valid? Say with std::filesystem::exists(std::filesystem::path(source_model_path))

// Experimental API
ORT_API(OrtExperimentalFnPtr, GetExperimentalFunction, _In_ const char* name);

ORT_API_STATUS_IMPL(KernelContext_GetSyncStream, _In_ const OrtKernelContext* context,

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.

Was this intentional?

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.

2 participants