|
12 | 12 | #include "core/providers/openvino/onnx_ctx_model_helper.h"
|
13 | 13 | #include "core/providers/openvino/ov_versions/capability.h"
|
14 | 14 | #include "core/providers/openvino/qdq_transformations/qdq_stripping.h"
|
| 15 | +#include "core/providers/openvino/exceptions.h" |
15 | 16 | #include "core/session/onnxruntime_session_options_config_keys.h"
|
16 | 17 | #include "openvino/core/version.hpp"
|
17 | 18 | #ifdef USE_OVEP_NPU_MEMORY
|
@@ -94,101 +95,105 @@ common::Status OpenVINOExecutionProvider::Compile(
|
94 | 95 | auto& logger = *GetLogger();
|
95 | 96 | Status status = Status::OK();
|
96 | 97 |
|
97 |
| - if (!fused_nodes.empty()) { |
98 |
| - // Assume these properties are constant for all the model subgraphs, otherwise move to SubGraphContext |
99 |
| - const auto& graph_body_viewer_0 = fused_nodes[0].filtered_graph.get(); |
100 |
| - session_context_.onnx_model_path_name = graph_body_viewer_0.ModelPath().string(); |
101 |
| - session_context_.onnx_opset_version = |
102 |
| - graph_body_viewer_0.DomainToVersionMap().at(kOnnxDomain); |
103 |
| - } |
104 |
| - |
105 |
| - // Temporary code to read metadata before it moves to the .bin |
106 |
| - auto& metadata = shared_context_->shared_weights.metadata; |
107 |
| - if (session_context_.so_share_ep_contexts && metadata.empty()) { |
108 |
| - // Metadata is always read from model location, this could be a source or epctx model |
109 |
| - fs::path metadata_filename = session_context_.onnx_model_path_name.parent_path() / "metadata.bin"; |
110 |
| - std::ifstream file(metadata_filename, std::ios::binary); |
111 |
| - if (file) { |
112 |
| - file >> metadata; |
| 98 | + try { |
| 99 | + if (!fused_nodes.empty()) { |
| 100 | + // Assume these properties are constant for all the model subgraphs, otherwise move to SubGraphContext |
| 101 | + const auto& graph_body_viewer_0 = fused_nodes[0].filtered_graph.get(); |
| 102 | + session_context_.onnx_model_path_name = graph_body_viewer_0.ModelPath().string(); |
| 103 | + session_context_.onnx_opset_version = |
| 104 | + graph_body_viewer_0.DomainToVersionMap().at(kOnnxDomain); |
113 | 105 | }
|
114 |
| - } |
115 | 106 |
|
116 |
| - struct OpenVINOEPFunctionState { |
117 |
| - AllocateFunc allocate_func = nullptr; |
118 |
| - DestroyFunc destroy_func = nullptr; |
119 |
| - AllocatorHandle allocator_handle = nullptr; |
120 |
| - BackendManager& backend_manager; |
121 |
| - }; |
122 |
| - |
123 |
| - for (const FusedNodeAndGraph& fused_node_graph : fused_nodes) { |
124 |
| - const GraphViewer& graph_body_viewer = fused_node_graph.filtered_graph; |
125 |
| - const Node& fused_node = fused_node_graph.fused_node; |
126 |
| - |
127 |
| - NodeComputeInfo compute_info; |
128 |
| - |
129 |
| - // During backend creation, we check if user wants to use precompiled blob onnx model or the original model |
130 |
| - // For precompiled blob, directly load the model instead of compiling the model |
131 |
| - // For original model, check if the user wants to export a model with pre-compiled blob |
132 |
| - |
133 |
| - auto& backend_manager = backend_managers_.emplace_back(session_context_, |
134 |
| - *shared_context_, |
135 |
| - fused_node, |
136 |
| - graph_body_viewer, |
137 |
| - logger, |
138 |
| - ep_ctx_handle_); |
139 |
| - |
140 |
| - compute_info.create_state_func = |
141 |
| - [&backend_manager](ComputeContext* context, FunctionState* state) { |
142 |
| - OpenVINOEPFunctionState* p = new OpenVINOEPFunctionState{ |
143 |
| - .allocate_func = context->allocate_func, |
144 |
| - .destroy_func = context->release_func, |
145 |
| - .allocator_handle = context->allocator_handle, |
146 |
| - .backend_manager = backend_manager}; |
147 |
| - *state = static_cast<FunctionState>(p); |
148 |
| - return 0; |
149 |
| - }; |
150 |
| - |
151 |
| - compute_info.compute_func = [](FunctionState state, const OrtApi* /* api */, OrtKernelContext* context) { |
152 |
| - auto function_state = static_cast<OpenVINOEPFunctionState*>(state); |
153 |
| - try { |
154 |
| - function_state->backend_manager.Compute(context); |
155 |
| - } catch (const std::exception& ex) { |
156 |
| - return common::Status(common::ONNXRUNTIME, common::FAIL, ex.what()); |
| 107 | + // Temporary code to read metadata before it moves to the .bin |
| 108 | + auto& metadata = shared_context_->shared_weights.metadata; |
| 109 | + if (session_context_.so_share_ep_contexts && metadata.empty()) { |
| 110 | + // Metadata is always read from model location, this could be a source or epctx model |
| 111 | + fs::path metadata_filename = session_context_.onnx_model_path_name.parent_path() / "metadata.bin"; |
| 112 | + std::ifstream file(metadata_filename, std::ios::binary); |
| 113 | + if (file) { |
| 114 | + file >> metadata; |
157 | 115 | }
|
158 |
| - return Status::OK(); |
| 116 | + } |
| 117 | + |
| 118 | + struct OpenVINOEPFunctionState { |
| 119 | + AllocateFunc allocate_func = nullptr; |
| 120 | + DestroyFunc destroy_func = nullptr; |
| 121 | + AllocatorHandle allocator_handle = nullptr; |
| 122 | + BackendManager& backend_manager; |
159 | 123 | };
|
160 | 124 |
|
161 |
| - compute_info.release_state_func = |
162 |
| - [](FunctionState state) { |
163 |
| - if (state) { |
164 |
| - OpenVINOEPFunctionState* function_state = static_cast<OpenVINOEPFunctionState*>(state); |
165 |
| - delete function_state; |
166 |
| - } |
167 |
| - }; |
| 125 | + for (const FusedNodeAndGraph& fused_node_graph : fused_nodes) { |
| 126 | + const GraphViewer& graph_body_viewer = fused_node_graph.filtered_graph; |
| 127 | + const Node& fused_node = fused_node_graph.fused_node; |
| 128 | + |
| 129 | + NodeComputeInfo compute_info; |
| 130 | + |
| 131 | + // During backend creation, we check if user wants to use precompiled blob onnx model or the original model |
| 132 | + // For precompiled blob, directly load the model instead of compiling the model |
| 133 | + // For original model, check if the user wants to export a model with pre-compiled blob |
| 134 | + |
| 135 | + auto& backend_manager = backend_managers_.emplace_back(session_context_, |
| 136 | + *shared_context_, |
| 137 | + fused_node, |
| 138 | + graph_body_viewer, |
| 139 | + logger, |
| 140 | + ep_ctx_handle_); |
| 141 | + |
| 142 | + compute_info.create_state_func = |
| 143 | + [&backend_manager](ComputeContext* context, FunctionState* state) { |
| 144 | + OpenVINOEPFunctionState* p = new OpenVINOEPFunctionState{ |
| 145 | + .allocate_func = context->allocate_func, |
| 146 | + .destroy_func = context->release_func, |
| 147 | + .allocator_handle = context->allocator_handle, |
| 148 | + .backend_manager = backend_manager}; |
| 149 | + *state = static_cast<FunctionState>(p); |
| 150 | + return 0; |
| 151 | + }; |
| 152 | + |
| 153 | + compute_info.compute_func = [](FunctionState state, const OrtApi* /* api */, OrtKernelContext* context) { |
| 154 | + auto function_state = static_cast<OpenVINOEPFunctionState*>(state); |
| 155 | + try { |
| 156 | + function_state->backend_manager.Compute(context); |
| 157 | + } catch (const std::exception& ex) { |
| 158 | + return common::Status(common::ONNXRUNTIME, common::FAIL, ex.what()); |
| 159 | + } |
| 160 | + return Status::OK(); |
| 161 | + }; |
168 | 162 |
|
169 |
| - node_compute_funcs.push_back(std::move(compute_info)); |
| 163 | + compute_info.release_state_func = |
| 164 | + [](FunctionState state) { |
| 165 | + if (state) { |
| 166 | + OpenVINOEPFunctionState* function_state = static_cast<OpenVINOEPFunctionState*>(state); |
| 167 | + delete function_state; |
| 168 | + } |
| 169 | + }; |
170 | 170 |
|
171 |
| - if (!status.IsOK()) { |
172 |
| - break; |
173 |
| - } |
174 |
| - } |
| 171 | + node_compute_funcs.push_back(std::move(compute_info)); |
175 | 172 |
|
176 |
| - if (session_context_.so_share_ep_contexts) { |
177 |
| - fs::path metadata_filename; |
178 |
| - if (session_context_.so_context_file_path.empty()) { |
179 |
| - metadata_filename = session_context_.onnx_model_path_name.parent_path() / "metadata.bin"; |
180 |
| - } else { |
181 |
| - metadata_filename = session_context_.so_context_file_path.parent_path() / "metadata.bin"; |
| 173 | + if (!status.IsOK()) { |
| 174 | + break; |
| 175 | + } |
182 | 176 | }
|
183 | 177 |
|
184 |
| - // Metadata is generated only for shared contexts |
185 |
| - // If saving metadata then save it to the provided path or ose the original model path |
186 |
| - // Multiple calls to Compile() will update the metadata and for the last call |
187 |
| - // the resulting file will contain the aggregated content |
188 |
| - std::ofstream file(metadata_filename, std::ios::binary); |
189 |
| - if (file) { |
190 |
| - file << metadata; |
| 178 | + if (session_context_.so_share_ep_contexts) { |
| 179 | + fs::path metadata_filename; |
| 180 | + if (session_context_.so_context_file_path.empty()) { |
| 181 | + metadata_filename = session_context_.onnx_model_path_name.parent_path() / "metadata.bin"; |
| 182 | + } else { |
| 183 | + metadata_filename = session_context_.so_context_file_path.parent_path() / "metadata.bin"; |
| 184 | + } |
| 185 | + |
| 186 | + // Metadata is generated only for shared contexts |
| 187 | + // If saving metadata then save it to the provided path or ose the original model path |
| 188 | + // Multiple calls to Compile() will update the metadata and for the last call |
| 189 | + // the resulting file will contain the aggregated content |
| 190 | + std::ofstream file(metadata_filename, std::ios::binary); |
| 191 | + if (file) { |
| 192 | + file << metadata; |
| 193 | + } |
191 | 194 | }
|
| 195 | + } catch (ovep_exception ex) { |
| 196 | + status = ex; |
192 | 197 | }
|
193 | 198 |
|
194 | 199 | return status;
|
|
0 commit comments