Repository navigation
[Compile API] Add support for writing external initializers and EP context to buffer to enable saving models > 2GB - #31347
Conversation
|
mingmingtasd (@mingmingtasd) Please take a look to see if this will satisfy the WebNN requirements for compiling models > 2GB in the sandbox. |
|
Thanks for the plan! I think it satisfies the non-compiling EP case like WebGPU, but not the compiling EP case like OV EP. We need both in the sandbox. For the compiling EPs like OV EP, the main payload is the compiled blob stored in the EPContext node's |
The new experimental API was initially intended to enable encryption/decryption but could also be used to work around the sandbox limitations. You'd need to manage the buffers and track the associated filename but in theory it would work as a mechanism. Problem is that the EP implementation owns creating the EPContext node including whether it uses the write function pointer or not. ORT doesn't control how the write is done. We could try and invert that control (e.g. add a function to create an EPContext node that takes the bytes and other node properties/attributes to create the node and does the write to file or uses the provided function pointer), but that's a new API and also requires EP implementation changes for it to be used. Could also be that the EP implementation prefers a pointer to do the write via so it can be passed into their compilation implementation, and us adding a function for creating an EPContext node where they provide the buffer to write might not fit with that. Gopalakrishnan Nallasamy (@GopalakrishnanN) do you know if that's the case? |
|
Personally I would lean towards adding a CreateEPContextNode function and enforcing usage (reject call to CreateNode for EPContext node). That ensures any write function is consistently used. Needs feedback from all IHVs though to confirm that approach is valid. |
β¦iew and feedback.
β¦teInitializersToExternalBufferInCompileAPI
β¦teInitializersToExternalBufferInCompileAPI
β¦teInitializersToExternalBufferInCompileAPI
There was a problem hiding this comment.
Copilot review overview
π‘ Changes recommended
External-buffer outputs are committed before later model serialization can fail, violating the documented failure contract.
Review effort: Balanced
Findings: 3
Open (5)
What changed in this PR
Adds in-memory external initializer serialization and optional zero-copy loading, enabling compiled models whose weights exceed protobufβs 2 GB limit.
Changes:
- Adds C/C++ Compile API controls for external initializer buffers and alignment.
- Implements two-pass buffer serialization and recursive direct-buffer loading.
- Adds API documentation and integration tests.
| File | Description |
|---|---|
onnxruntime/βtest/βframework/βinference_session_test.cc |
Tests buffer serialization, loading, and file output. |
onnxruntime/βcore/βsession/βplugin_ep/βep_api.cc |
Uses the shared EP context configuration. |
onnxruntime/βcore/βsession/βmodel_compilation_options.h |
Declares new compilation options. |
onnxruntime/βcore/βsession/βmodel_compilation_options.cc |
Stores and validates buffer settings. |
onnxruntime/βcore/βsession/βinference_session.cc |
Enables optional direct-buffer loading. |
onnxruntime/βcore/βsession/βep_context_config.h |
Centralizes EP context callback state. |
onnxruntime/βcore/βsession/βcompile_api.h |
Declares new C API implementations. |
onnxruntime/βcore/βsession/βcompile_api.cc |
Implements and registers the APIs. |
onnxruntime/βcore/βsession/βabi_session_options.cc |
Strengthens external-buffer argument validation. |
onnxruntime/βcore/βgraph/βmodel.h |
Declares stream-based model serialization. |
onnxruntime/βcore/βgraph/βmodel.cc |
Implements stream-based model serialization. |
onnxruntime/βcore/βgraph/βgraph.cc |
Adds aligned serialization and direct recursive loading. |
onnxruntime/βcore/βframework/βtensor_external_data_info.cc |
Aligns threshold-boundary prepacked data. |
onnxruntime/βcore/βframework/βep_context_utils.cc |
Implements two-pass buffer generation. |
onnxruntime/βcore/βframework/βep_context_options.h |
Adds buffer destination and alignment state. |
onnxruntime/βcore/βframework/βep_context_options.cc |
Adds buffer-option lookup. |
include/βonnxruntime/βcore/βsession/βonnxruntime_session_options_config_keys.h |
Exposes direct-buffer loading configuration. |
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 |
Extends and documents the Compile API. |
include/βonnxruntime/βcore/βgraph/βgraph.h |
Declares serialization and loading interfaces. |
docs/βdesign/βCompile_API_In_Memory_External_Initializers.md |
Documents the design and validation plan. |
π‘ Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.


Description
Changes required to enable writing external initializers to a buffer when using
OrtCompileApiMotivation and Context
Enable WebNN usage in sandboxed scenario so models >2GB can be created.