Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions include/onnxruntime/core/graph/graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -1214,6 +1214,18 @@ class Graph { // NOLINT(clang-analyzer-optin.performance.Padding): preserve exi
const std::filesystem::path& model_file_path,
const ModelSavingOptions& model_saving_options) const;

/// <summary>
/// Serialize the Graph to a onnx::GraphProto. Caller provides a function that determines where each initializer
/// is stored (i.e., either in an external file or within the model).
/// </summary>
/// <param name="handle_initializer_func">Function called for every initializer.</param>
/// <param name="state">Opaque user state passed to the handle_initializer_func.</param>
/// <param name="graph_proto">Output parameter set to the serialized onnx::GraphProto.</param>
/// <returns>A status indicating success or an error.</returns>
common::Status ToGraphProtoWithInitializerHandler(OrtHandleInitializerDataFunc handle_initializer_func,
void* state,
/*out*/ ONNX_NAMESPACE::GraphProto& graph_proto) const;

/** Gets the ISchemaRegistry instances being used with this Graph. */
IOnnxRuntimeOpSchemaCollectionPtr GetSchemaRegistry() const;

Expand Down Expand Up @@ -1586,6 +1598,8 @@ class Graph { // NOLINT(clang-analyzer-optin.performance.Padding): preserve exi
std::ostream& external_stream,
int64_t& external_offset) const;

Status ToGraphProtoWithInitializerHandlerImpl(OrtHandleInitializerDataFunc handle_initializer_func,
void* state, /*out*/ ONNX_NAMESPACE::GraphProto& output_graph_proto) const;
#endif

Version IrVersion() const noexcept {
Expand Down
121 changes: 121 additions & 0 deletions include/onnxruntime/core/session/onnxruntime_c_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,74 @@ typedef OrtStatus*(ORT_API_CALL* EpSelectionDelegate)(_In_ const OrtEpDevice** e
_Out_ size_t* num_selected,
_In_ void* state);

/** \brief Function called by ORT to write a buffer to a custom destination (e.g., file, stream, etc.).
*
* \param state Opaque pointer holding the user's state.
* \param buffer The buffer to write.
* \param buffer_num_bytes The size of the buffer in bytes.
*
* \return OrtStatus* Write status. Return nullptr on success.
* Use CreateStatus to provide error info. Use ORT_FAIL as the error code.
* ORT will release the OrtStatus* if not null.
*/
typedef OrtStatus*(ORT_API_CALL* OrtWriteBufferFunc)(_In_ void* state,
_In_ const void* buffer,
_In_ size_t buffer_num_bytes);

/** \brief Function called by ORT to allow user to specify how an initializer should be saved, that is, either
* written to an external file or stored within the model. ORT calls this function for every initializer when
* generating a model.
*
* If the function sets the `is_external` output parameter to false, ORT stores initializer data within the model.
*
* Otherwise, if `is_external` is set to false, ORT assumes that this function stores the initializer data to a file.
* In this case, ORT configures the model's initializer to point to the location and offset returned from this function
* via the `location` and `offset` output parameters.
*
* \param[in] state Opaque pointer holding the user's state.
* \param[in] initializer_name The initializer's name as a null-terminated string.
* \param[in] initializer_data Pointer to the initializer's raw data (contiguous).
* \param[in] initializer_num_bytes The size in bytes of the initializer's data.
* \param[in] initializer_type The type and shape information for the initializer.
* \param[out] is_external Output parameter set to true if the initializer data is to be stored externally.
* The function implemented is responsible for writing the initializer data to file.
* If set to false, ORT stores the initializers within the model.
* \param[out] location Output parameter set to the location (i.e., file path) into which the initializer data is stored
* by the function implementer. Ignored if `is_external` is set to false.
* \param[out] offset Output parameter set to the location offset into which the initializer data is stored
* by the function implementer. Ignored if `is_external` is set to false.
*
* \return OrtStatus* Write status. Return nullptr on success.
* Use CreateStatus to provide error info. Use ORT_FAIL as the error code.
* ORT will release the OrtStatus* if not null.
*/
typedef OrtStatus*(ORT_API_CALL* OrtHandleInitializerDataFunc)(_In_ void* state,
_In_ const char* initializer_name,
_In_ const void* initializer_data,
_In_ size_t initializer_num_bytes,
_In_ const OrtTypeInfo* initializer_type,
_Out_ bool* is_external,
_Out_ const ORTCHAR_T** location, _Out_ int64_t* offset);

/** \brief Function called by ORT to write a EPContext binary data to a custom destination (e.g., file, stream, etc.).
*
* \param state Opaque pointer holding the user's state.
* \param buffer The buffer to write.
* \param buffer_num_bytes The size of the buffer in bytes.
* \param[out] location Output parameter set to the location (i.e., file path) into which the data is stored
* by the function implementer.
*
* \return OrtStatus* Write status. Return nullptr on success.
* Use CreateStatus to provide error info. Use ORT_FAIL as the error code.
* ORT will release the OrtStatus* if not null.
*/
typedef OrtStatus*(ORT_API_CALL* OrtWriteEpContextDataFunc)(_In_ void* state,
_In_ const char* ep_context_node_name,
_In_ const char* ep_name,
_In_ const void* buffer,
_In_ size_t buffer_num_bytes,
_Out_ const ORTCHAR_T** location);

/** \brief Algorithm to use for cuDNN Convolution Op
*/
typedef enum OrtCudnnConvAlgoSearch {
Expand Down Expand Up @@ -7075,6 +7143,59 @@ struct OrtCompileApi {
_In_ OrtModelCompilationOptions* model_compile_options,
_In_ const ORTCHAR_T* output_directory,
_In_ const ORTCHAR_T* model_name);

/** \brief Sets a OrtWriteBufferFunc function that is called by ORT to write out the output model's serialized
* ONNX bytes.
*
* The provided write function may be called repeatedly until then entire output model has been written out. Each call
* to the write function is expected to consume the entire input buffer.
*
* The output model's destination (e.g., file path, memory buffer, or stream) can be set with any of the functions
* that begin with ModelCompilationOptions_SetOutputModel____.
*
* \param[in] model_compile_options The OrtModelCompilationOptions instance.
* \param[in] write_func The OrtWriteBufferFunc function called by ORT when writing out the model.
* \param[in] state Opaque state passed as the first argument to OrtWriteBufferFunc. Can be NULL.
*
* \snippet{doc} snippets.dox OrtStatus Return Value
*
* \since Version 1.23.
*/
ORT_API2_STATUS(ModelCompilationOptions_SetOutputModelWriteFunc,
_In_ OrtModelCompilationOptions* model_compile_options,
_In_ OrtWriteBufferFunc write_func, _In_ void* state);

/** \brief Sets a OrtHandleInitializerDataFunc function that is called by ORT for every initializer in the generated
* model. Allows implementer to specify whether initializers should be stored within the model or externally.
*
* \param[in] model_compile_options The OrtModelCompilationOptions instance.
* \param[in] write_func The OrtHandleInitializerDataFunc function called by ORT when writing out an initializer.
* \param[in] state Opaque state passed as the first argument to OrtHandleInitializerDataFunc. Can be NULL.
*
* \snippet{doc} snippets.dox OrtStatus Return Value
*
* \since Version 1.23.
*/
ORT_API2_STATUS(ModelCompilationOptions_SetOutputModelHandleInitializerFunc,
_In_ OrtModelCompilationOptions* model_compile_options,
_In_ OrtHandleInitializerDataFunc handle_initializer_func, _In_ void* state);

/** \brief Sets a OrtWriteEpContextDataFunc function that is called by an execution provider to write out an
* an EPContext node's binary data, which is usually stored in the attribute named `ep_cache_context`.
*
* \note Not compatible with embed mode set to true via ModelCompilationOptions_SetEpContextEmbedMode.
*
* \param[in] model_compile_options The OrtModelCompilationOptions instance.
* \param[in] write_func The OrtWriteEpContextDataFunc function called by an EP to write out an EPContext node's data.
* \param[in] state Opaque state passed as the first argument to OrtWriteEpContextDataFunc. Can be NULL.
*
* \snippet{doc} snippets.dox OrtStatus Return Value
*
* \since Version 1.23.
*/
ORT_API2_STATUS(ModelCompilationOptions_SetEpContextDataWriteFunc,
_In_ OrtModelCompilationOptions* model_compile_options,
_In_ OrtWriteEpContextDataFunc write_func, _In_ void* state);
};

/*
Expand Down
12 changes: 8 additions & 4 deletions include/onnxruntime/core/session/onnxruntime_cxx_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -1154,13 +1154,17 @@ struct ModelCompilationOptions : detail::Base<OrtModelCompilationOptions> {

ModelCompilationOptions& SetInputModelPath(const ORTCHAR_T* input_model_path); ///< Wraps OrtApi::ModelCompilationOptions_SetInputModelPath
ModelCompilationOptions& SetInputModelFromBuffer(const void* input_model_data,
size_t input_model_data_size); ///< Wraps OrtApi::ModelCompilationOptions_SetInputModelFromBuffer
ModelCompilationOptions& SetEpContextEmbedMode(bool embed_ep_context_in_model); ///< Wraps OrtApi::ModelCompilationOptions_SetEpContextEmbedMode
ModelCompilationOptions& SetOutputModelPath(const ORTCHAR_T* output_model_path); ///< Wraps OrtApi::ModelCompilationOptions_SetOutputModelPath
size_t input_model_data_size); ///< Wraps OrtApi::ModelCompilationOptions_SetInputModelFromBuffer
ModelCompilationOptions& SetEpContextEmbedMode(bool embed_ep_context_in_model); ///< Wraps OrtApi::ModelCompilationOptions_SetEpContextEmbedMode
ModelCompilationOptions& SetEpContextDataWriteFunc(OrtWriteEpContextDataFunc write_func, void* state); ///< Wraps OrtApi::ModelCompilationOptions_SetEpContextDataWriteFunc
ModelCompilationOptions& SetOutputModelPath(const ORTCHAR_T* output_model_path); ///< Wraps OrtApi::ModelCompilationOptions_SetOutputModelPath
ModelCompilationOptions& SetOutputModelExternalInitializersFile(const ORTCHAR_T* file_path,
size_t initializer_size_threshold); ///< Wraps OrtApi::ModelCompilationOptions_SetOutputModelExternalInitializersFile
ModelCompilationOptions& SetOutputModelHandleInitializerFunc(OrtHandleInitializerDataFunc handle_initializer_func,
void* state); ///< Wraps OrtApi::ModelCompilationOptions_SetOutputModelHandleInitializerFunc
ModelCompilationOptions& SetOutputModelBuffer(OrtAllocator* allocator, void** output_model_buffer_ptr,
size_t* output_model_buffer_size_ptr); ///< Wraps OrtApi::ModelCompilationOptions_SetOutputModelBuffer
size_t* output_model_buffer_size_ptr); ///< Wraps OrtApi::ModelCompilationOptions_SetOutputModelBuffer
ModelCompilationOptions& SetOutputModelWriteFunc(OrtWriteBufferFunc write_func, void* state); ///< Wraps OrtApi::ModelCompilationOptions_SetOutputModelWriteFunc
ModelCompilationOptions& SetEpContextBinaryInformation(const ORTCHAR_T* output_directory,
const ORTCHAR_T* model_name); ///< Wraps OrtApi::ModelCompilationOptions_SetEpContextBinaryInformation
ModelCompilationOptions& SetFlags(size_t flags); ///< Wraps OrtApi::ModelCompilationOptions_SetFlags
Expand Down
21 changes: 21 additions & 0 deletions include/onnxruntime/core/session/onnxruntime_cxx_inline.h
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,15 @@ inline ModelCompilationOptions& ModelCompilationOptions::SetOutputModelExternalI
return *this;
}

inline ModelCompilationOptions&
ModelCompilationOptions::SetOutputModelHandleInitializerFunc(OrtHandleInitializerDataFunc handle_initializer_func,
void* state) {
Ort::ThrowOnError(GetCompileApi().ModelCompilationOptions_SetOutputModelHandleInitializerFunc(this->p_,
handle_initializer_func,
state));
return *this;
}

inline ModelCompilationOptions& ModelCompilationOptions::SetOutputModelBuffer(
OrtAllocator* allocator, void** output_model_buffer_ptr, size_t* output_model_buffer_size_ptr) {
Ort::ThrowOnError(GetCompileApi().ModelCompilationOptions_SetOutputModelBuffer(this->p_, allocator,
Expand All @@ -845,6 +854,12 @@ inline ModelCompilationOptions& ModelCompilationOptions::SetOutputModelBuffer(
return *this;
}

inline ModelCompilationOptions& ModelCompilationOptions::SetOutputModelWriteFunc(OrtWriteBufferFunc write_func,
void* state) {
Ort::ThrowOnError(GetCompileApi().ModelCompilationOptions_SetOutputModelWriteFunc(this->p_, write_func, state));
return *this;
}

inline ModelCompilationOptions& ModelCompilationOptions::SetEpContextEmbedMode(
bool embed_ep_context_in_model) {
Ort::ThrowOnError(GetCompileApi().ModelCompilationOptions_SetEpContextEmbedMode(
Expand All @@ -853,6 +868,12 @@ inline ModelCompilationOptions& ModelCompilationOptions::SetEpContextEmbedMode(
return *this;
}

inline ModelCompilationOptions& ModelCompilationOptions::SetEpContextDataWriteFunc(OrtWriteEpContextDataFunc write_func,
void* state) {
Ort::ThrowOnError(GetCompileApi().ModelCompilationOptions_SetEpContextDataWriteFunc(this->p_, write_func, state));
return *this;
}

inline ModelCompilationOptions& ModelCompilationOptions::SetFlags(size_t flags) {
Ort::ThrowOnError(GetCompileApi().ModelCompilationOptions_SetFlags(this->p_, flags));
return *this;
Expand Down
67 changes: 67 additions & 0 deletions include/onnxruntime/core/session/onnxruntime_ep_c_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ ORT_RUNTIME_CLASS(EpFactory);
ORT_RUNTIME_CLASS(EpGraphSupportInfo);
ORT_RUNTIME_CLASS(MemoryDevice); // opaque class to wrap onnxruntime::OrtDevice
ORT_RUNTIME_CLASS(NodeComputeContext);
ORT_RUNTIME_CLASS(EpContextModelOptions);

ORT_RUNTIME_CLASS(DataTransferImpl);
ORT_RUNTIME_CLASS(SyncNotificationImpl);
Expand Down Expand Up @@ -425,6 +426,72 @@ struct OrtEpApi {
* \since Version 1.23.
*/
ORT_API_T(uint32_t, MemoryDevice_GetDeviceId, _In_ const OrtMemoryDevice* memory_device);

/** \brief Get the OrtEpContextModelOptions for generating EPContext nodes.
*
* \param[in] session_options The OrtSessionOptions from which to get the OrtEpContextModelOptions.
* \param[out] ep_context_model_options The OrtEpContextModelOptions to retrieve. Must be released
* with ReleaseEpContextModelOptions.
* \snippet{doc} snippets.dox OrtStatus Return Value
*
* \since Version 1.23.
*/
ORT_API2_STATUS(SessionOptions_GetEpContextModelOptions, _In_ const OrtSessionOptions* session_options,
_Outptr_ OrtEpContextModelOptions** ep_context_model_options);

ORT_CLASS_RELEASE(EpContextModelOptions);

/** \brief Get whether the EP should generate EPContext nodes.
*
* \param[in] ep_context_model_options The OrtEpContextModelOptions instance.
* \return True if EPContext model/node generation is enabled in the session.
*
* \since Version 1.23.
*/
ORT_API_T(bool, EpContextModelOptions_IsGenerationEnabled,
_In_ const OrtEpContextModelOptions* ep_context_model_options);

/** \brief Return a boolean indicating if the binary data in EPContext nodes should be embedded
* within the 'ep_cache_context' node attribute.
*
* \param[in] ep_context_model_options The OrtEpContextModelOptions instance.
* \return True if EPContext node binary data should be embedded.
*
* \since Version 1.23.
*/
ORT_API_T(bool, EpContextModelOptions_IsEpContextDataEmbedded,
_In_ const OrtEpContextModelOptions* ep_context_model_options);

/** \brief Return the OrtWriteEpContextDataFunc function (and state) set by the application/user to write out
* EPContext node binary data to a custom destination.
*
* \param[in] ep_context_model_options The OrtEpContextModelOptions instance.
* \param[out] write_func Output parameter set to the OrtWriteEpContextDataFunc that should be called to write out
* EPContext node binary data. Set to NULL if not set.
* \param[out] state Output parameter set to the opaque state that should be passed to the OrtWriteEpContextDataFunc.
* Set to NULL if not set or if user/application set a NULL state.
*
* \since Version 1.23.
*/
ORT_API_T(void, EpContextModelOptions_GetEpContextDataWriteFunc,
_In_ const OrtEpContextModelOptions* ep_context_model_options,
_Outptr_result_maybenull_ OrtWriteEpContextDataFunc* write_func,
_Outptr_result_maybenull_ void** state);

/** \brief Get the output EPContext model path, if any.
*
* \note Can be used by the EP to generated the binary file that stores non-embedded EPContext node binary data.
*
* \param[in] ep_context_model_options The OrtEpContextModelOptions instance.
* \param[out] output_model_path Output parameter set to the output model's path. Set to NULL if the path is unknown.
*
* \snippet{doc} snippets.dox OrtStatus Return Value
*
* \since Version 1.23.
*/
ORT_API2_STATUS(EpContextModelOptions_GetOutputModelPath,
_In_ const OrtEpContextModelOptions* ep_context_model_options,
_Outptr_result_maybenull_ const ORTCHAR_T** output_model_path);
};

/**
Expand Down
Loading
Loading