1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-31 08:13:04 +04:00

Merge pull request #24045 from TolyaTalamanov:at/add-onnx-directml-execution-provider

G-API: Support DirectML Execution Provider for ONNXRT Backend #24045

### Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [ ] I agree to contribute to the project under Apache 2 License.
- [ ] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [ ] The PR is proposed to the proper branch
- [ ] There is a reference to the original bug report and related work
- [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
This commit is contained in:
Anatoliy Talamanov
2023-07-26 14:00:20 +01:00
committed by GitHub
parent a25e809da1
commit a817813b50
9 changed files with 198 additions and 63 deletions
@@ -22,8 +22,14 @@ cv::gapi::onnx::PyParams& cv::gapi::onnx::PyParams::cfgNormalize(const std::stri
}
cv::gapi::onnx::PyParams&
cv::gapi::onnx::PyParams::cfgExecutionProvider(cv::gapi::onnx::ep::OpenVINO ov_ep) {
m_priv->cfgExecutionProvider(std::move(ov_ep));
cv::gapi::onnx::PyParams::cfgAddExecutionProvider(cv::gapi::onnx::ep::OpenVINO ep) {
m_priv->cfgAddExecutionProvider(std::move(ep));
return *this;
}
cv::gapi::onnx::PyParams&
cv::gapi::onnx::PyParams::cfgAddExecutionProvider(cv::gapi::onnx::ep::DirectML ep) {
m_priv->cfgAddExecutionProvider(std::move(ep));
return *this;
}
+40
View File
@@ -0,0 +1,40 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
//
// Copyright (C) 2023 Intel Corporation
#include "backends/onnx/dml_ep.hpp"
#include "logger.hpp"
#ifdef HAVE_ONNX
#include <onnxruntime_cxx_api.h>
#ifdef HAVE_ONNX_DML
#include "../providers/dml/dml_provider_factory.h"
void cv::gimpl::onnx::addDMLExecutionProvider(Ort::SessionOptions *session_options,
const cv::gapi::onnx::ep::DirectML &dml_ep) {
namespace ep = cv::gapi::onnx::ep;
GAPI_Assert(cv::util::holds_alternative<int>(dml_ep.ddesc));
const int device_id = cv::util::get<int>(dml_ep.ddesc);
try {
OrtSessionOptionsAppendExecutionProvider_DML(*session_options, device_id);
} catch (const std::exception &e) {
std::stringstream ss;
ss << "ONNX Backend: Failed to enable DirectML"
<< " Execution Provider: " << e.what();
cv::util::throw_error(std::runtime_error(ss.str()));
}
}
#else // HAVE_ONNX_DML
void cv::gimpl::onnx::addDMLExecutionProvider(Ort::SessionOptions*,
const cv::gapi::onnx::ep::DirectML&) {
util::throw_error(std::runtime_error("G-API has been compiled with ONNXRT"
" without DirectML support"));
}
#endif // HAVE_ONNX_DML
#endif // HAVE_ONNX
+23
View File
@@ -0,0 +1,23 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
//
// Copyright (C) 2023 Intel Corporation
#ifndef OPENCV_GAPI_DML_EP_HPP
#define OPENCV_GAPI_DML_EP_HPP
#include "opencv2/gapi/infer/onnx.hpp"
#ifdef HAVE_ONNX
#include <onnxruntime_cxx_api.h>
namespace cv {
namespace gimpl {
namespace onnx {
void addDMLExecutionProvider(Ort::SessionOptions *session_options,
const cv::gapi::onnx::ep::DirectML &dml_ep);
}}}
#endif // HAVE_ONNX
#endif // OPENCV_GAPI_DML_EP_HPP
+38 -26
View File
@@ -9,6 +9,8 @@
#ifdef HAVE_ONNX
#include "backends/onnx/dml_ep.hpp"
#include <ade/util/algorithm.hpp> // any_of
#include <ade/util/zip_range.hpp>
#include <opencv2/gapi/infer.hpp>
@@ -143,37 +145,44 @@ public:
void run();
};
static void appendExecutionProvider(Ort::SessionOptions *session_options,
const cv::gapi::onnx::ep::EP &execution_provider) {
static void addOpenVINOExecutionProvider(Ort::SessionOptions *session_options,
const cv::gapi::onnx::ep::OpenVINO &ov_ep) {
OrtOpenVINOProviderOptions options;
options.device_type = ov_ep.device_type.c_str();
options.cache_dir = ov_ep.cache_dir.c_str();
options.num_of_threads = ov_ep.num_of_threads;
options.enable_opencl_throttling = ov_ep.enable_opencl_throttling;
options.enable_dynamic_shapes = ov_ep.enable_dynamic_shapes;
options.context = nullptr;
try {
session_options->AppendExecutionProvider_OpenVINO(options);
} catch (const std::exception &e) {
std::stringstream ss;
ss << "ONNX Backend: Failed to enable OpenVINO"
<< " Execution Provider: " << e.what();
cv::util::throw_error(std::runtime_error(ss.str()));
}
}
static void addExecutionProvider(Ort::SessionOptions *session_options,
const cv::gapi::onnx::ep::EP &execution_provider) {
namespace ep = cv::gapi::onnx::ep;
switch (execution_provider.index()) {
case ep::EP::index_of<ep::OpenVINO>(): {
GAPI_LOG_INFO(NULL, "OpenVINO Execution Provider is selected.");
const auto &ovep = cv::util::get<ep::OpenVINO>(execution_provider);
OrtOpenVINOProviderOptions options;
options.device_id = ovep.device_id.c_str();
options.cache_dir = ovep.cache_dir.c_str();
options.enable_opencl_throttling = ovep.enable_opencl_throttling;
options.enable_dynamic_shapes = ovep.enable_dynamic_shapes;
// NB: If are not specified, will be taken from onnxruntime build.
if (ovep.device_type) {
options.device_type = ovep.device_type->c_str();
}
if (ovep.num_of_threads) {
options.num_of_threads = *ovep.num_of_threads;
}
try {
session_options->AppendExecutionProvider_OpenVINO(options);
} catch (const std::exception &e) {
std::stringstream ss;
ss << "ONNX Backend: Failed to enable OpenVINO Execution Provider: "
<< e.what() << "\nMake sure that onnxruntime has"
" been compiled with OpenVINO support.";
cv::util::throw_error(std::runtime_error(ss.str()));
}
GAPI_LOG_INFO(NULL, "OpenVINO Execution Provider is added.");
const auto &ov_ep = cv::util::get<ep::OpenVINO>(execution_provider);
addOpenVINOExecutionProvider(session_options, ov_ep);
break;
}
case ep::EP::index_of<ep::DirectML>(): {
GAPI_LOG_INFO(NULL, "DirectML Execution Provider is added.");
const auto &dml_ep = cv::util::get<ep::DirectML>(execution_provider);
addDMLExecutionProvider(session_options, dml_ep);
break;
}
default:
GAPI_LOG_INFO(NULL, "CPU Execution Provider is added.");
break;
}
}
@@ -629,7 +638,10 @@ ONNXCompiled::ONNXCompiled(const gapi::onnx::detail::ParamDesc &pp)
}
// Create and initialize the ONNX session
Ort::SessionOptions session_options;
cv::gimpl::onnx::appendExecutionProvider(&session_options, pp.execution_provider);
GAPI_LOG_INFO(NULL, "Adding Execution Providers for \"" << pp.model_path << "\"");
for (const auto &ep : pp.execution_providers) {
cv::gimpl::onnx::addExecutionProvider(&session_options, ep);
}
if (pp.disable_mem_pattern) {
session_options.DisableMemPattern();