|
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,6 +95,7 @@ common::Status OpenVINOExecutionProvider::Compile(
|
94 | 95 | auto& logger = *GetLogger();
|
95 | 96 | Status status = Status::OK();
|
96 | 97 |
|
| 98 | + try { |
97 | 99 | bool is_epctx_model = false;
|
98 | 100 | if (!fused_nodes.empty()) {
|
99 | 101 | // Assume these properties are constant for all the model subgraphs, otherwise move to SubGraphContext
|
@@ -127,65 +129,65 @@ common::Status OpenVINOExecutionProvider::Compile(
|
127 | 129 | file >> metadata;
|
128 | 130 | }
|
129 | 131 |
|
130 |
| - struct OpenVINOEPFunctionState { |
131 |
| - AllocateFunc allocate_func = nullptr; |
132 |
| - DestroyFunc destroy_func = nullptr; |
133 |
| - AllocatorHandle allocator_handle = nullptr; |
134 |
| - BackendManager& backend_manager; |
135 |
| - }; |
136 |
| - |
137 |
| - for (const FusedNodeAndGraph& fused_node_graph : fused_nodes) { |
138 |
| - const GraphViewer& graph_body_viewer = fused_node_graph.filtered_graph; |
139 |
| - const Node& fused_node = fused_node_graph.fused_node; |
140 |
| - |
141 |
| - NodeComputeInfo compute_info; |
142 |
| - |
143 |
| - // During backend creation, we check if user wants to use precompiled blob onnx model or the original model |
144 |
| - // For precompiled blob, directly load the model instead of compiling the model |
145 |
| - // For original model, check if the user wants to export a model with pre-compiled blob |
146 |
| - |
147 |
| - auto& backend_manager = backend_managers_.emplace_back(session_context_, |
148 |
| - *shared_context_, |
149 |
| - fused_node, |
150 |
| - graph_body_viewer, |
151 |
| - logger, |
152 |
| - ep_ctx_handle_); |
153 |
| - |
154 |
| - compute_info.create_state_func = |
155 |
| - [&backend_manager](ComputeContext* context, FunctionState* state) { |
156 |
| - OpenVINOEPFunctionState* p = new OpenVINOEPFunctionState{ |
157 |
| - .allocate_func = context->allocate_func, |
158 |
| - .destroy_func = context->release_func, |
159 |
| - .allocator_handle = context->allocator_handle, |
160 |
| - .backend_manager = backend_manager}; |
161 |
| - *state = static_cast<FunctionState>(p); |
162 |
| - return 0; |
163 |
| - }; |
164 |
| - |
165 |
| - compute_info.compute_func = [](FunctionState state, const OrtApi* /* api */, OrtKernelContext* context) { |
166 |
| - auto function_state = static_cast<OpenVINOEPFunctionState*>(state); |
167 |
| - try { |
168 |
| - function_state->backend_manager.Compute(context); |
169 |
| - } catch (const std::exception& ex) { |
170 |
| - return common::Status(common::ONNXRUNTIME, common::FAIL, ex.what()); |
171 |
| - } |
172 |
| - return Status::OK(); |
| 132 | + struct OpenVINOEPFunctionState { |
| 133 | + AllocateFunc allocate_func = nullptr; |
| 134 | + DestroyFunc destroy_func = nullptr; |
| 135 | + AllocatorHandle allocator_handle = nullptr; |
| 136 | + BackendManager& backend_manager; |
173 | 137 | };
|
174 | 138 |
|
175 |
| - compute_info.release_state_func = |
176 |
| - [](FunctionState state) { |
177 |
| - if (state) { |
178 |
| - OpenVINOEPFunctionState* function_state = static_cast<OpenVINOEPFunctionState*>(state); |
179 |
| - delete function_state; |
180 |
| - } |
181 |
| - }; |
| 139 | + for (const FusedNodeAndGraph& fused_node_graph : fused_nodes) { |
| 140 | + const GraphViewer& graph_body_viewer = fused_node_graph.filtered_graph; |
| 141 | + const Node& fused_node = fused_node_graph.fused_node; |
| 142 | + |
| 143 | + NodeComputeInfo compute_info; |
| 144 | + |
| 145 | + // During backend creation, we check if user wants to use precompiled blob onnx model or the original model |
| 146 | + // For precompiled blob, directly load the model instead of compiling the model |
| 147 | + // For original model, check if the user wants to export a model with pre-compiled blob |
| 148 | + |
| 149 | + auto& backend_manager = backend_managers_.emplace_back(session_context_, |
| 150 | + *shared_context_, |
| 151 | + fused_node, |
| 152 | + graph_body_viewer, |
| 153 | + logger, |
| 154 | + ep_ctx_handle_); |
| 155 | + |
| 156 | + compute_info.create_state_func = |
| 157 | + [&backend_manager](ComputeContext* context, FunctionState* state) { |
| 158 | + OpenVINOEPFunctionState* p = new OpenVINOEPFunctionState{ |
| 159 | + .allocate_func = context->allocate_func, |
| 160 | + .destroy_func = context->release_func, |
| 161 | + .allocator_handle = context->allocator_handle, |
| 162 | + .backend_manager = backend_manager}; |
| 163 | + *state = static_cast<FunctionState>(p); |
| 164 | + return 0; |
| 165 | + }; |
| 166 | + |
| 167 | + compute_info.compute_func = [](FunctionState state, const OrtApi* /* api */, OrtKernelContext* context) { |
| 168 | + auto function_state = static_cast<OpenVINOEPFunctionState*>(state); |
| 169 | + try { |
| 170 | + function_state->backend_manager.Compute(context); |
| 171 | + } catch (const std::exception& ex) { |
| 172 | + return common::Status(common::ONNXRUNTIME, common::FAIL, ex.what()); |
| 173 | + } |
| 174 | + return Status::OK(); |
| 175 | + }; |
182 | 176 |
|
183 |
| - node_compute_funcs.push_back(std::move(compute_info)); |
| 177 | + compute_info.release_state_func = |
| 178 | + [](FunctionState state) { |
| 179 | + if (state) { |
| 180 | + OpenVINOEPFunctionState* function_state = static_cast<OpenVINOEPFunctionState*>(state); |
| 181 | + delete function_state; |
| 182 | + } |
| 183 | + }; |
184 | 184 |
|
185 |
| - if (!status.IsOK()) { |
186 |
| - break; |
| 185 | + node_compute_funcs.push_back(std::move(compute_info)); |
| 186 | + |
| 187 | + if (!status.IsOK()) { |
| 188 | + break; |
| 189 | + } |
187 | 190 | }
|
188 |
| - } |
189 | 191 |
|
190 | 192 | // The block below is executed during EP context model generation
|
191 | 193 | if (session_context_.so_context_enable &&
|
@@ -213,6 +215,10 @@ common::Status OpenVINOExecutionProvider::Compile(
|
213 | 215 | ORT_RETURN_IF_NOT(file, "Metadata file could not be written: ", metadata_file_path);
|
214 | 216 | file << metadata;
|
215 | 217 | }
|
| 218 | + } |
| 219 | + catch (const ovep_exception& ex) { |
| 220 | + status = ex; |
| 221 | + } |
216 | 222 |
|
217 | 223 | return status;
|
218 | 224 | }
|
|
0 commit comments