Support external initializer data without filesystem access through two separate capabilities:
OrtCompileApi to write an external initializer file to a caller-owned buffer when saving a compiled orTogether, these capabilities support models whose initializer data makes the complete model exceed the protobuf 2 GB
limit. Compiling execution providers hit the same limit through EPContext node data; that path is covered separately
below.
Compilation produces two buffers:
The ONNX model records the caller-provided logical filename, offset, and length for every externalized initializer.
Multiple initializers share the same logical file and buffer.
Leading current-directory prefixes (./ or .\\, including repeated prefixes) are removed consistently when
recording logical filenames, registering in-memory files, and looking up external initializer data.
Add ModelCompilationOptions_SetOutputModelExternalInitializersBuffer with:
ORTCHAR_T, as in the existing file-output API) to store in each initializer'sTensorProto;OrtAllocator; andModelCompilationOptions_SetOutputModelExternalInitializersBuffer(
OrtModelCompilationOptions* options,
const ORTCHAR_T* logical_file_name,
size_t initializer_size_threshold,
OrtAllocator* allocator,
void** output_buffer,
size_t* output_buffer_size);
The setter records the allocator and output pointer locations; it does not take a preallocated buffer or allocate
immediately. CompileModel uses the allocator to allocate the buffer and fills both outputs on success. The allocator
and output pointer locations must remain valid until CompileModel returns.
When the model also uses an output buffer, the model and external-initializer buffer pointer locations must be distinct,
as must their size pointer locations. Compilation rejects aliasing before allocating either buffer.
Add the corresponding C++ wrapper and an ExternalInitializerBufferInfo alternative toepctx::ModelGenOptions::initializers_location.
The external-initializer file destination (ModelCompilationOptions_SetOutputModelExternalInitializersFile) and the
buffer destination (ModelCompilationOptions_SetOutputModelExternalInitializersBuffer) are mutually exclusive: the
caller provides one or the other, never both. Because both map to the single initializers_location variant, the
last setter called wins and silently replaces any prior external-initializer destination. The output model destination
is independent: it may be a file, buffer, or write callback. For file output, the caller is responsible for persisting
or otherwise supplying the returned initializer buffer under the logical filename recorded in the model.
Add ModelCompilationOptions_SetOutputModelExternalInitializersAlignment, which accepts a power-of-two alignment and a
minimum initializer size at which to apply it. It affects both file and buffer output; an alignment of zero disables
this additional policy. This allows a buffer to be persisted later with mmap-friendly offsets.
ModelCompilationOptions_SetOutputModelExternalInitializersAlignment(
OrtModelCompilationOptions* options,
size_t alignment,
size_t minimum_size);
Store the alignment settings separately from the initializer destination in ModelGenOptions, then apply them when
constructing ModelSavingOptions for either file or buffer output. Setter call order does not matter.
If CompileModel fails, ORT must free any temporary allocation and leave the caller's output pointer and size
unchanged. After CompileModel succeeds, the caller owns the buffer and releases it with the supplied allocator. If no data is
externalized, return a null buffer and size zero. Empty initializers remain embedded without external-file references.
Refactor the existing external-initializer save path so its physical destination can be either a file stream or an
allocated memory buffer. Preserve the existing initializer traversal, threshold, external-data metadata, endian
conversion, and prepacked-weight handling. File output retains its existing subgraph externalization behavior.
Buffer output externalizes only main-graph initializers and keeps subgraph initializer data embedded, regardless of the
size threshold, so the resulting model does not require subgraph file-buffer injection to load.
Use a two-pass implementation:
TensorProto.Sort prepacked-blob keys before writing so both passes have identical alignment padding and offsets. Verify that the
write pass fills the entire measured buffer before publishing it.
Write externalized initializers in load-ready tensor storage and align each tensor's offset to its natural alignment;
the writer controls the layout, so this alignment is guaranteed for ORT-produced buffers. Additionally apply the
alignment configured by ModelCompilationOptions_SetOutputModelExternalInitializersAlignment above its size threshold;
the default policy is mmap-friendly 4 KiB alignment for data of at least 1 MiB, even when the alignment setter is not
called. Passing zero disables this additional policy while preserving natural alignment. Prepacked blobs are opaque and
use only the configured alignment policy.
The serialized ONNX protobuf must still be smaller than 2 GB. Externalizing initializer bytes keeps the protobuf small;
this feature does not support graph metadata or embedded subgraph initializers that independently exceed protobuf's
limit.
The existing OrtApi::AddExternalInitializersFromFilesInMemory accepts whole logical files, and one supplied file may
contain multiple main-graph initializers. It copies initializer data during session creation by default.
Both AddExternalInitializers and AddExternalInitializersFromFilesInMemory apply only to the main graph; neither
replaces initializers in subgraphs. Consistent subgraph injection support is deferred beyond this release. The WebNN
scenario constructs models through the ModelEditor API, which does not currently support creating graph-valued node
attributes.
Add kOrtSessionOptionsConfigUseExternalInitializerFileBuffersDirectly with the config keysession.use_external_initializer_file_buffers_directly. Its default is "0". When set to "1", buffers supplied
through AddExternalInitializersFromFilesInMemory are borrowed and used directly for initializers. This follows the
existing ORT-format direct-buffer options and avoids adding another C/C++ API. Update the existing API documentation to
state that every session created from the options may outlive the options, and the application must keep each buffer
unchanged and alive until those sessions are released. If session creation fails, the buffers may be released after the
call returns.
During session initialization, inject individually supplied external initializers before file buffers, preserving their
precedence and the existing main-graph loading order. Match each main-graph external
initializer's logical filename, validate its declared and computed size, and use checked arithmetic to validate its offset and length against the file
buffer. When the slice is naturally aligned for the runtime storage type, create an initializer OrtValue over the
validated slice with non-owning storage and retain it in the graph/session instead of copying. The borrowed OrtValue
must be non-owning (wrap the slice in a Tensor with a plain CPU OrtMemoryInfo and no deleter, as the existing
native-endian inject branch already does) so the SessionOptions can be released while the underlying buffer persists.
Small values that must remain in the TensorProto for shape inference and values requiring endian conversion may be
copied.
Natural alignment is preferable but not required in direct-use mode. Even for an ORT-produced buffer whose tensor
offsets are naturally aligned, the supplied buffer's base address is caller/allocator-controlled, and the buffer may not
have been produced by ORT at all, so base + offset can fail to meet the runtime storage type's natural alignment. When
that happens, fall back to copying that individual initializer into owned storage instead of borrowing; the rest of the
initializers in the same buffer still borrow directly. Reject a null buffer, a filename mismatch, and out-of-range
slices. Valid overlapping slices are allowed.
Compiling execution providers wrap each fused subgraph in an EPContext node whose provider binary is carried in the
node's ep_cache_context attribute. This data is a node attribute, not an initializer, so the initializer-buffer path
above does not cover it, and the same 2 GB limit applies independently.
Embed mode cannot represent data at or above 2 GB. The blob lives inside the node's AttributeProto within theModelProto, and OrtApi::CreateOpAttr takes an int length, so a plugin EP cannot even create such an attribute. Whether to embed remains the caller's choice. Today the
EP creates the EPContext node itself (plugin EPs through OrtModelEditorApi::CreateNode, in-tree EPs by building theNode directly) and chooses both whether to embed and, when not embedding, where the data goes; the existing write
callback (OrtCompileApi_ModelCompilationOptions_SetEpContextDataWriteFunc retrieved via OrtEpContextConfig) is
opt-in, so ORT cannot force an arbitrary EP to route through it.
Use the stable READ and WRITE callback transport. The compile API accepts an OrtWriteNamedBufferFunc; plugin
EPs snapshot it from session options through an owned OrtEpContextConfig and retrieve the callback throughOrtEpApi. EPs advertise OrtEpContextDataCallbackSupportFlags_READ and OrtEpContextDataCallbackSupportFlags_WRITE
through OrtEp::GetEpContextDataCallbackSupport so ORT rejects a configured callback before Compile() when a plugin
EP cannot honor it.
CreateEpContextNode is intentionally deferred. EPs continue to create their own EPContext nodes and decide how to
encode ep_cache_context. This change provides stable callback transport and capability negotiation only; it does not
centralize node creation or alter the generic CreateNode behavior.
Reject an empty or absolute logical filename, null allocator or output pointers, a non-power-of-two alignment, and
offset or size overflow. Validate that generated offsets and lengths fit the ONNX signed 64-bit external-data fields.
A misaligned supplied buffer is valid; direct-use loading copies only slices that do not meet natural alignment.
OrtEpContextDataCallbackSupportFlags_WRITE.