1 | // This file is part of OpenCV project. |
---|---|
2 | // It is subject to the license terms in the LICENSE file found in the top-level directory |
3 | // of this distribution and at http://opencv.org/license.html. |
4 | // |
5 | // Copyright (C) 2023 Intel Corporation |
6 | |
7 | #include "backends/onnx/coreml_ep.hpp" |
8 | #include "logger.hpp" |
9 | |
10 | #ifdef HAVE_ONNX |
11 | #include <onnxruntime_cxx_api.h> |
12 | |
13 | #ifdef HAVE_ONNX_COREML |
14 | #include "../providers/coreml/coreml_provider_factory.h" |
15 | |
16 | void cv::gimpl::onnx::addCoreMLExecutionProvider(Ort::SessionOptions *session_options, |
17 | const cv::gapi::onnx::ep::CoreML &coreml_ep) { |
18 | uint32_t flags = 0u; |
19 | if (coreml_ep.use_cpu_only) { |
20 | flags |= COREML_FLAG_USE_CPU_ONLY; |
21 | } |
22 | |
23 | if (coreml_ep.enable_on_subgraph) { |
24 | flags |= COREML_FLAG_ENABLE_ON_SUBGRAPH; |
25 | } |
26 | |
27 | if (coreml_ep.enable_only_ane) { |
28 | flags |= COREML_FLAG_ONLY_ENABLE_DEVICE_WITH_ANE; |
29 | } |
30 | |
31 | try { |
32 | OrtSessionOptionsAppendExecutionProvider_CoreML(*session_options, flags); |
33 | } catch (const std::exception &e) { |
34 | std::stringstream ss; |
35 | ss << "ONNX Backend: Failed to enable CoreML" |
36 | << " Execution Provider: "<< e.what(); |
37 | cv::util::throw_error(std::runtime_error(ss.str())); |
38 | } |
39 | } |
40 | |
41 | #else // HAVE_ONNX_COREML |
42 | |
43 | void cv::gimpl::onnx::addCoreMLExecutionProvider(Ort::SessionOptions*, |
44 | const cv::gapi::onnx::ep::CoreML&) { |
45 | util::throw_error(std::runtime_error("G-API has been compiled with ONNXRT" |
46 | " without CoreML support")); |
47 | } |
48 | |
49 | #endif // HAVE_ONNX_COREML |
50 | #endif // HAVE_ONNX |
51 |