// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. #pragma once #include #include #include "core/framework/allocator.h" #include "core/framework/config_options.h" #include "core/session/onnxruntime_c_api.h" namespace onnxruntime { namespace epctx { /// /// Holds the buffer that will store the output model and the allocator used to allocate the memory. /// struct BufferHolder { void** buffer_ptr = nullptr; size_t* buffer_size_ptr = nullptr; AllocatorPtr buffer_allocator = nullptr; }; /// /// Holds the opaque stream state and the write function that ORT calls to write out the output model. /// struct BufferWriteFuncHolder { OrtWriteBufferFunc write_func = nullptr; void* stream_state = nullptr; // Opaque pointer to user's stream state. Passed as first argument to write_func. }; /// /// Holds the opaque state and write function that EPs use to write EPContext binary data. /// struct EpContextDataWriteFuncHolder { OrtWriteNamedBufferFunc write_func = nullptr; void* state = nullptr; }; /// /// Non-owning copy of the application callbacks and states used for external EPContext binary data. /// struct EpContextDataCallbacks { OrtReadNamedBufferFunc read_func = nullptr; void* read_state = nullptr; OrtWriteNamedBufferFunc write_func = nullptr; void* write_state = nullptr; }; /// /// Holds path and size threshold used to write out initializers to an external file. /// struct ExternalInitializerFileInfo { std::filesystem::path file_path; size_t size_threshold = 0; }; /// /// Holds the logical file name and caller-owned output parameters for external initializers saved to a buffer. /// struct ExternalInitializerBufferInfo { std::filesystem::path logical_file_name; size_t size_threshold = 0; AllocatorPtr buffer_allocator = nullptr; void** buffer_ptr = nullptr; size_t* buffer_size_ptr = nullptr; }; /// /// Holds function and state provided by user to handle initializer data (i.e., write to stream or embed in model). /// struct InitializerHandler { OrtGetInitializerLocationFunc handle_initializer_func = nullptr; void* state = nullptr; }; /// /// Stores EPContext model generation options. Used in SessionOptions. /// struct ModelGenOptions { // Action to take if the output model does not have compiled (EPContext) nodes. enum class ActionIfNoCompiledNodes { // Return OK() but don't generate an output model. Compiling via SessionOptions defaults to this behavior // to maintain compatibility. The explicit compile API does *not* use this action. kDontGenerateModel = 0, // Generate an output model even if it doesn't have compiled nodes. // The explicit Compile API defaults to this value. kGenerateModel, // Return an error if the model does not have compiled nodes. // The explicit Compile API can be configured to this value. kReturnError, }; ModelGenOptions(); // Initializes from string key/value pairs in session config options. explicit ModelGenOptions(const ConfigOptions& config_options); bool enable = false; bool error_if_output_file_exists = true; bool error_if_no_compiled_nodes = false; bool embed_ep_context_in_model = false; ActionIfNoCompiledNodes action_if_no_compiled_nodes = ActionIfNoCompiledNodes::kDontGenerateModel; std::variant // Function to write the output model to a user's stream. output_model_location = std::monostate{}; std::variant // Custom function called for every initializer to determine location. initializers_location = std::monostate{}; size_t external_initializers_alignment = 4096; size_t external_initializers_alignment_threshold = 1048576; EpContextDataWriteFuncHolder ep_context_data_write_func = {}; bool HasOutputModelLocation() const; const std::filesystem::path* TryGetOutputModelPath() const; const BufferHolder* TryGetOutputModelBuffer() const; const BufferWriteFuncHolder* TryGetOutputModelWriteFunc() const; const EpContextDataWriteFuncHolder* TryGetEpContextDataWriteFunc() const; bool AreInitializersEmbeddedInOutputModel() const; const ExternalInitializerFileInfo* TryGetExternalInitializerFileInfo() const; const ExternalInitializerBufferInfo* TryGetExternalInitializerBufferInfo() const; const InitializerHandler* TryGetInitializerHandler() const; }; } // namespace epctx } // namespace onnxruntime