Skip to content

Weightless support for all initializers - #29607

Merged
Chi Lo (chilo-ms) merged 32 commits into
mainfrom
chilo-ms/weightless-all-initializers
Aug 10, 2026
Merged

Weightless support for all initializers#29607
Chi Lo (chilo-ms) merged 32 commits into
mainfrom
chilo-ms/weightless-all-initializers

Conversation

@chilo-ms

@chilo-ms Chi Lo (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
  • Add  SessionOptions_GetWeightlessSourceModelBuffer : EP getter for the source model byte buffer from session options

OrtApi ( onnxruntime_c_api.h ):

  • Add  SessionOptionsSetWeightlessSourceModelFromBuffer : byte buffer source model

OrtCompileApi ( onnxruntime_c_api.h ):

  • Add  ModelCompilationOptions_SetWeightlessCache : enable weightless for AOT compilation

EpDevice metadata ( onnxruntime_ep_device_ep_metadata_keys.h ):

  • Add kOrtEpDevice_EpMetadataKey_WeightlessSupport : pre-compilation query via  EpDevice_EpMetadata() . EP sets  "weightless_support"  =  "none"  /  "external_only"  /  "all"  during  GetSupportedDevices()  so apps can check device-specific capability before calling  SetWeightlessCache() .

Source model plumbing:

  • App sets source model via  OrtApi  setters ( SessionOptionsSetWeightlessSourceModelPath  /  FromBuffer )
  • EP retrieves it via  OrtEpApi  getters ( SessionOptions_GetWeightlessSourceModelPath  /  GetWeightlessSourceModelBuffer )

Future work

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

Motivation and Context

Chi Lo (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
Comment thread onnxruntime/core/session/plugin_ep/ep_plugin_provider_interfaces.cc Outdated
Comment thread onnxruntime/core/session/onnxruntime_c_api.cc Outdated
Comment thread onnxruntime/core/session/ort_apis.h Outdated
@chilo-ms
Chi Lo (chilo-ms) marked this pull request as ready for review July 9, 2026 16:45
Chi Lo (chilo-ms) and others added 5 commits July 27, 2026 13:15
Add two getter APIs to OrtEpApi so plugin EPs can retrieve the weightless
source model during CreateEp/Compile:

- SessionOptions_GetWeightlessSourceModelPath: returns the file path set
  by SessionOptionsSetWeightlessSourceModelPath, or NULL if not set.
- SessionOptions_GetWeightlessSourceModelBuffer: returns the buffer and
  size set by SessionOptionsSetWeightlessSourceModelFromBuffer, or
  NULL/0 if not set.

This completes the weightless source model plumbing: app sets the source
model via OrtApi setters, EP retrieves it via OrtEpApi getters.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The weightless_enabled_ flag was set but never read after the
drop_constant_initializers override was removed. The EP handles
weightless behavior on its own via session options and
GetWeightlessSupport(). ORT Core doesn't need to track it separately.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The source model file path is set via the session config string
ep.context_source_model_path which the EP reads directly. No
dedicated C API or OrtEpApi getter is needed for the file path case.

Keep only the buffer-based APIs since byte buffers cannot be passed
via config strings.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Documentation:
- SessionOptionsSetWeightlessSourceModelFromBuffer: add note about
  kOrtSessionOptionEpContextSourceModelPath for file-based source models
- kOrtSessionOptionEpContextSourceModelPath: mention
  SessionOptionsSetWeightlessSourceModelFromBuffer for buffer-based source

Tests:
- PluginEp_WeightlessAllInitializers_CompileApi: end-to-end test using
  ep.enable_weightless + ModelCompilationOptions_SetWeightlessCache
- PluginEp_WeightlessSourceModelFromBuffer_Validation: C API validation
  tests for null pointer and zero-length buffer error cases

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

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This was referenced Aug 12, 2026
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.

5 participants