// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. #pragma once #include "core/common/common.h" #include "core/graph/graph.h" #include "core/framework/fuse_nodes_funcs.h" #include "core/framework/resource_accountant.h" #include "core/framework/transform_layout_functions.h" #include "core/optimizer/graph_optimizer_registry.h" namespace onnxruntime { class ExecutionProviders; class KernelRegistryManager; class LayeringIndex; class Model; struct ConfigOptions; namespace epctx { struct ModelGenOptions; } // OnPartitionAssignmentFunction is called by GraphPartitioner when a subgraph is assigned to // an execution provider. Can be used to collect partitioning information. using OnPartitionAssignmentFunction = std::function; class GraphPartitioner { public: enum class Mode { kNormal = 0, kAssignOnly = 1, // assign nodes. no call to Compile. used to create ORT format model support for compiling EPs kOrtFormatLoad = 2 // loading ORT format model. Partition with compiling EPs, GraphViewer based Compile. }; // The order of providers represents the user preference. GraphPartitioner(KernelRegistryManager& kernel_registry_mgr, const ExecutionProviders& providers, std::unique_ptr graph_optimizer_registry) : kernel_registry_mgr_(kernel_registry_mgr), providers_(providers), graph_optimizer_registry_(std::move(graph_optimizer_registry)) { } GraphPartitioner(KernelRegistryManager& kernel_registry_mgr, const ExecutionProviders& providers, std::unique_ptr graph_optimizer_registry, CheckLoadCancellationFn check_load_cancellation_fn, OnPartitionAssignmentFunction on_partition_assignment_fn = {}) : kernel_registry_mgr_(kernel_registry_mgr), providers_(providers), graph_optimizer_registry_(std::move(graph_optimizer_registry)), check_load_cancellation_fn_(std::move(check_load_cancellation_fn)), on_partition_assignment_fn_(std::move(on_partition_assignment_fn)) { } // Run partitioning. // // Output-model serialization (only when ep_context_gen_options is enabled, e.g. via the Compile API): if // a compiling EP produced EPContext nodes, that model is serialized here at the end of partition. // Otherwise, for a compile-only session (the Compile API with no compiled nodes), the plain optimized // graph is emitted by InferenceSession (not here) at a point chosen by the optimization level - before // the Level2+ loop for < Level2, or after all transforms for >= Level2 (to capture the L2-L4 fusions). // Callers distinguish via AnyEpContextNodesProduced(). Status Partition(Graph& graph, FuncManager& func_mgr, const layout_transformation::TransformLayoutFunction& transform_layout_function, const ConfigOptions& config_options, const logging::Logger& logger, LayeringIndex* layering_index, Mode mode = Mode::kNormal, const epctx::ModelGenOptions& ep_context_gen_options = {}, bool ep_context_data_read_callback_registered = false, const layout_transformation::DebugGraphFn& debug_graph_fn = {}, WorkspaceReservationMap* workspace_reservations = nullptr) const; #ifndef ORT_MINIMAL_BUILD // Returns true if any execution provider produced EPContext (compiled) nodes during partitioning. bool AnyEpContextNodesProduced() const; #endif bool IsLoadCancellationFlagSet() const { return check_load_cancellation_fn_ && check_load_cancellation_fn_(); } #ifndef ORT_MINIMAL_BUILD /// // Ahead of Time Function inlining. The main purpose of the function is to inline as many // functions as possible and delete locally defined functions to reduce the size of the model. // This would make other optimizations to be more effective. // // This function performs GetCapability on the graph and its subgraphs bottom up // and inlines any functions that are not claimed by any of the execution providers. // This function does not attempt to run layout transformation, and it does not assign EPs. // The latter will be done by graph partitioning after Level1 optimizations are done. /// /// model instance /// execution providers considered /// registry manager /// session config options /// session logger /// READ/WRITE flags for the session's registered callbacks /// Status InlineFunctionsAOT(Model& model, const ExecutionProviders& execution_providers, const KernelRegistryManager& kernel_registry_manager, const ConfigOptions& config_options, const logging::Logger& logger, uint32_t registered_ep_context_data_callbacks) const; #endif private: ORT_DISALLOW_COPY_ASSIGNMENT_AND_MOVE(GraphPartitioner); KernelRegistryManager& kernel_registry_mgr_; const ExecutionProviders& providers_; std::unique_ptr graph_optimizer_registry_; CheckLoadCancellationFn check_load_cancellation_fn_; OnPartitionAssignmentFunction on_partition_assignment_fn_; #ifndef ORT_MINIMAL_BUILD // Shared by AOT and fallback inlining so neither path can bypass the cumulative limit. mutable bool function_expansion_limits_initialized_ = false; mutable size_t function_expansion_node_limit_ = 0; mutable size_t function_expansion_byte_limit_ = 0; mutable size_t expanded_function_node_count_ = 0; mutable size_t expanded_function_proto_bytes_ = 0; #endif }; } // namespace onnxruntime