// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. #pragma once #if !defined(ORT_MINIMAL_BUILD) #include #include #include #include "core/common/status.h" #include "core/framework/ep_context_options.h" #include "core/graph/model.h" namespace onnxruntime { namespace epctx { /// /// Serialize an EPContext model into a onnx::ModelProto based on the provided options. /// /// The EP Context model to serialize. /// The path into which to save the model. May be empty if serialized into a /// buffer or output stream. /// The model generation options. /// Output parameter set to the serialized onnx::ModelProto. /// A status indicating success or an error. Status EpContextModelToProto(const onnxruntime::Model& ep_context_model, const std::filesystem::path& validated_model_path, const epctx::ModelGenOptions& ep_context_gen_options, /*out*/ ONNX_NAMESPACE::ModelProto& model_proto); /// /// Validate an output-model path: ensure it points to a file (not a folder), derive a default /// "_ctx.onnx" name from the source model path when none is given, and optionally fail if the /// target file already exists. /// /// The requested output path. May be empty. /// The source model path, used to derive a default output name. /// Output parameter set to the validated path. /// Whether to fail if the target file already exists. /// A status indicating success or an error. Status GetValidatedEpContextPath(const std::filesystem::path& ep_context_path, const std::filesystem::path& model_path, std::filesystem::path& context_cache_path, bool error_if_output_file_exists = true); /// /// Write a serialized onnx::ModelProto to the location configured in the generation options: a buffer /// ORT allocates for the caller, the caller's output-stream write function, or a file. /// /// The model proto to serialize. /// The model generation options selecting the output location. /// The validated file path (used only for the file case). /// Session logger. /// A status indicating success or an error. Status SaveModelProtoToLocation(ONNX_NAMESPACE::ModelProto& model_proto, const epctx::ModelGenOptions& gen_options, const std::filesystem::path& valid_output_model_path, const logging::Logger& logger); /// /// Build and save a plain optimized (non-EPContext) output model from an already optimized, /// in-memory model. Used by the Compile API when no nodes were compiled (the kGenerateModel case, e.g. a /// non-compiling EP such as WebGPU), so the emitted model captures whatever optimizations the session's /// configured level produced. Unlike CreateEpContextModel this does no EPContext-node substitution - it /// serializes the model as-is. /// /// The in-memory model to serialize. /// The model generation options. /// Session logger. /// A status indicating success or an error. Status BuildAndSaveOptimizedModel(const onnxruntime::Model& model, const epctx::ModelGenOptions& gen_options, const logging::Logger& logger); // Class that wraps the user's OrtBufferWriteFunc function to enable use with // C++'s std::ostream. // Example: // BufferWriteFuncHolder write_func_holder{write_func, stream_state}; // std::unique_ptr out_stream_buf = std::make_unique(write_func_holder); // std::ostream out_stream(out_stream_buf.get()); class OutStreamBuf : public std::streambuf { public: explicit OutStreamBuf(BufferWriteFuncHolder write_func_holder); ~OutStreamBuf(); const Status& GetStatus() const { return last_status_; } protected: int_type overflow(int_type ch) override; int sync() override; private: BufferWriteFuncHolder write_func_holder_{}; std::vector buffer_; Status last_status_{}; }; } // namespace epctx } // namespace onnxruntime #endif // !defined(ORT_MINIMAL_BUILD)