1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-30 07:43:03 +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
@@ -34,7 +34,10 @@ public:
PyParams& cfgNormalize(const std::string &layer_name, bool flag);
GAPI_WRAP
PyParams& cfgExecutionProvider(ep::OpenVINO ov_ep);
PyParams& cfgAddExecutionProvider(ep::OpenVINO ep);
GAPI_WRAP
PyParams& cfgAddExecutionProvider(ep::DirectML ep);
GAPI_WRAP
PyParams& cfgDisableMemPattern();
@@ -45,28 +45,13 @@ struct GAPI_EXPORTS_W_SIMPLE OpenVINO {
/** @brief Class constructor.
Constructs OpenVINO parameters based on device information.
Constructs OpenVINO parameters based on device type information.
@param device Target device to use.
@param dev_type Target device type to use. ("CPU_FP32", "GPU_FP16", etc)
*/
GAPI_WRAP
OpenVINO(const std::string &device)
: device_id(device) {
}
/** @brief Specifies OpenVINO Execution Provider device type.
This function is used to override the accelerator hardware type
and precision at runtime. If this option is not explicitly configured, default
hardware and precision specified during onnxruntime build time is used.
@param type Device type ("CPU_FP32", "GPU_FP16", etc)
@return reference to this parameter structure.
*/
GAPI_WRAP
OpenVINO& cfgDeviceType(const std::string &type) {
device_type = cv::util::make_optional(type);
return *this;
explicit OpenVINO(const std::string &dev_type)
: device_type(dev_type) {
}
/** @brief Specifies OpenVINO Execution Provider cache dir.
@@ -86,15 +71,14 @@ struct GAPI_EXPORTS_W_SIMPLE OpenVINO {
/** @brief Specifies OpenVINO Execution Provider number of threads.
This function is used to override the accelerator default value
of number of threads with this value at runtime. If this option
is not explicitly set, default value of 8 is used during build time.
of number of threads with this value at runtime.
@param nthreads Number of threads.
@return reference to this parameter structure.
*/
GAPI_WRAP
OpenVINO& cfgNumThreads(size_t nthreads) {
num_of_threads = cv::util::make_optional(nthreads);
num_of_threads = nthreads;
return *this;
}
@@ -127,15 +111,39 @@ struct GAPI_EXPORTS_W_SIMPLE OpenVINO {
return *this;
}
std::string device_id;
std::string device_type;
std::string cache_dir;
cv::optional<std::string> device_type;
cv::optional<size_t> num_of_threads;
size_t num_of_threads = 0;
bool enable_opencl_throttling = false;
bool enable_dynamic_shapes = false;
};
using EP = cv::util::variant<cv::util::monostate, OpenVINO>;
/**
* @brief This structure provides functions
* that fill inference options for ONNX DirectML Execution Provider.
* Please follow https://onnxruntime.ai/docs/execution-providers/DirectML-ExecutionProvider.html#directml-execution-provider
*/
class GAPI_EXPORTS_W_SIMPLE DirectML {
public:
// NB: Used from python.
/// @private -- Exclude this constructor from OpenCV documentation
GAPI_WRAP
DirectML() = default;
/** @brief Class constructor.
Constructs DirectML parameters based on device id.
@param device_id Target device id to use. ("0", "1", etc)
*/
GAPI_WRAP
explicit DirectML(const int device_id) : ddesc(device_id) { };
using DeviceDesc = cv::util::variant<int>;
DeviceDesc ddesc;
};
using EP = cv::util::variant<cv::util::monostate, OpenVINO, DirectML>;
} // namespace ep
@@ -191,7 +199,7 @@ struct ParamDesc {
std::unordered_map<std::string, std::pair<cv::Scalar, cv::Scalar> > generic_mstd;
std::unordered_map<std::string, bool> generic_norm;
cv::gapi::onnx::ep::EP execution_provider;
std::vector<cv::gapi::onnx::ep::EP> execution_providers;
bool disable_mem_pattern;
};
} // namespace detail
@@ -395,17 +403,31 @@ public:
return *this;
}
/** @brief Specifies execution provider for runtime.
/** @brief Adds execution provider for runtime.
The function is used to set ONNX Runtime OpenVINO Execution Provider options.
The function is used to add ONNX Runtime OpenVINO Execution Provider options.
@param ovep OpenVINO Execution Provider options.
@param ep OpenVINO Execution Provider options.
@see cv::gapi::onnx::ep::OpenVINO.
@return the reference on modified object.
*/
Params<Net>& cfgExecutionProvider(ep::OpenVINO&& ovep) {
desc.execution_provider = std::move(ovep);
Params<Net>& cfgAddExecutionProvider(ep::OpenVINO&& ep) {
desc.execution_providers.emplace_back(std::move(ep));
return *this;
}
/** @brief Adds execution provider for runtime.
The function is used to add ONNX Runtime DirectML Execution Provider options.
@param ep DirectML Execution Provider options.
@see cv::gapi::onnx::ep::DirectML.
@return the reference on modified object.
*/
Params<Net>& cfgAddExecutionProvider(ep::DirectML&& ep) {
desc.execution_providers.emplace_back(std::move(ep));
return *this;
}
@@ -447,20 +469,29 @@ public:
Params(const std::string& tag, const std::string& model_path)
: desc{model_path, 0u, 0u, {}, {}, {}, {}, {}, {}, {}, {}, {}, true, {}, {}, {}, false }, m_tag(tag) {}
/** @see onnx::Params::cfgMeanStdDev. */
void cfgMeanStdDev(const std::string &layer,
const cv::Scalar &m,
const cv::Scalar &s) {
desc.generic_mstd[layer] = std::make_pair(m, s);
}
/** @see onnx::Params::cfgNormalize. */
void cfgNormalize(const std::string &layer, bool flag) {
desc.generic_norm[layer] = flag;
}
void cfgExecutionProvider(ep::OpenVINO&& ov_ep) {
desc.execution_provider = std::move(ov_ep);
/** @see onnx::Params::cfgAddExecutionProvider. */
void cfgAddExecutionProvider(ep::OpenVINO&& ep) {
desc.execution_providers.emplace_back(std::move(ep));
}
/** @see onnx::Params::cfgAddExecutionProvider. */
void cfgAddExecutionProvider(ep::DirectML&& ep) {
desc.execution_providers.emplace_back(std::move(ep));
}
/** @see onnx::Params::cfgDisableMemPattern. */
void cfgDisableMemPattern() {
desc.disable_mem_pattern = true;
}