0
0
mirror of https://github.com/opencv/opencv.git synced 2026-01-18 17:21:42 +01:00

Merge pull request #27460 from fcmiron:gapi_set_workloadtype_dynamically

G-API: Add support to set workload type dynamically in both OpenVINO and ONNX OVEP #27460

### Pull Request Readiness Checklist

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

- [x] I agree to contribute to the project under Apache 2 License.
- [x] 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:
fcmiron
2025-12-22 20:04:53 +02:00
committed by GitHub
parent 2bca09a191
commit ad9387340a
7 changed files with 147 additions and 7 deletions

View File

@@ -20,6 +20,7 @@
#include <opencv2/core/cvdef.h> // GAPI_EXPORTS
#include <opencv2/gapi/gkernel.hpp> // GKernelPackage
#include <opencv2/gapi/infer.hpp> // Generic
#include <opencv2/gapi/infer/workload_type.hpp>
namespace cv {
namespace gapi {
@@ -752,8 +753,16 @@ protected:
std::string m_tag;
};
class WorkloadTypeONNX : public WorkloadType {};
using WorkloadTypeONNXPtr = std::shared_ptr<cv::gapi::onnx::WorkloadTypeONNX>;
} // namespace onnx
} // namespace gapi
namespace detail {
template<> struct CompileArgTag<cv::gapi::onnx::WorkloadTypeONNXPtr> {
static const char* tag() { return "gapi.onnx.workload_type"; }
};
} // namespace detail
} // namespace cv
#endif // OPENCV_GAPI_INFER_HPP

View File

@@ -13,6 +13,7 @@
#include <opencv2/gapi/own/exports.hpp> // GAPI_EXPORTS
#include <opencv2/gapi/gkernel.hpp> // GKernelType[M], GBackend
#include <opencv2/gapi/infer.hpp> // Generic
#include <opencv2/gapi/infer/workload_type.hpp>
#include <map>
@@ -745,6 +746,9 @@ namespace wip { namespace ov {
*/
struct benchmark_mode { };
class WorkloadTypeOV : public WorkloadType {};
using WorkloadTypeOVPtr = std::shared_ptr<cv::gapi::wip::ov::WorkloadTypeOV>;
} // namespace ov
} // namespace wip
@@ -756,6 +760,10 @@ namespace detail
{
static const char* tag() { return "gapi.wip.ov.benchmark_mode"; }
};
template<> struct CompileArgTag<cv::gapi::wip::ov::WorkloadTypeOVPtr>
{
static const char* tag() { return "gapi.wip.ov.workload_type"; }
};
}
} // namespace cv

View File

@@ -0,0 +1,58 @@
// 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) 2025 Intel Corporation
#ifndef OPENCV_WORKLOADTYPE_HPP
#define OPENCV_WORKLOADTYPE_HPP
#include <string>
#include <functional>
#include <vector>
#include <algorithm>
using Callback = std::function<void(const std::string &type)>;
class WorkloadListener {
Callback callback;
public:
uint64_t id;
WorkloadListener(const Callback &cb, uint64_t listener_id) : callback(cb), id(listener_id) {}
void operator()(const std::string &type) const {
if (callback) {
callback(type);
}
}
bool operator==(const WorkloadListener& other) const {
return id == other.id;
}
};
class WorkloadType {
std::vector<WorkloadListener> listeners;
uint64_t nextId = 1;
public:
uint64_t addListener(const Callback &cb) {
uint64_t id = nextId++;
listeners.emplace_back(cb, id);
return id;
}
void removeListener(uint64_t id) {
auto it = std::remove_if(listeners.begin(), listeners.end(),
[id](const WorkloadListener& entry) { return entry.id == id; });
if (it != listeners.end()) {
listeners.erase(it, listeners.end());
}
}
void notify(const std::string &type) {
for (const auto &listener : listeners) {
listener(type);
}
}
};
#endif // OPENCV_WORKLOADTYPE_HPP

View File

@@ -126,8 +126,14 @@ class ONNXCompiled {
std::vector<cv::Mat>& outs);
std::vector<std::string> in_names_without_const;
cv::gapi::onnx::WorkloadTypeONNXPtr m_workload_type;
uint64_t m_workload_listener_id = 0;
void setWorkloadType(const std::string &type);
public:
explicit ONNXCompiled(const gapi::onnx::detail::ParamDesc &pp);
~ONNXCompiled();
void listenToWorkloadType(cv::gapi::onnx::WorkloadTypeONNXPtr workload);
// Extract the information about output layer #i
cv::GMatDesc outMeta(int i) const;
@@ -577,8 +583,10 @@ using GConstGONNXModel = ade::ConstTypedGraph
} // anonymous namespace
// GCPUExcecutable implementation //////////////////////////////////////////////
cv::gimpl::onnx::GONNXExecutable::GONNXExecutable(const ade::Graph &g,
const std::vector<ade::NodeHandle> &nodes)
cv::gimpl::onnx::GONNXExecutable::GONNXExecutable(const ade::Graph &g,
const std::vector<ade::NodeHandle> &nodes,
const cv::GCompileArgs &compileArgs)
: m_g(g), m_gm(m_g) {
// FIXME: Currently this backend is capable to run a single inference node only.
// Need to extend our island fusion with merge/not-to-merge decision making parametrization
@@ -589,6 +597,11 @@ cv::gimpl::onnx::GONNXExecutable::GONNXExecutable(const ade::Graph &g,
case NodeType::OP:
if (this_nh == nullptr) {
this_nh = nh;
auto workload_arg = cv::gapi::getCompileArg<cv::gapi::onnx::WorkloadTypeONNXPtr>(compileArgs);
if(workload_arg.has_value()) {
const auto &onnx_unit = iem.metadata(nh).get<ONNXUnit>();
onnx_unit.oc->listenToWorkloadType(workload_arg.value());
}
}
else {
util::throw_error(std::logic_error("Multi-node inference is not supported!"));
@@ -834,6 +847,23 @@ ONNXCompiled::ONNXCompiled(const gapi::onnx::detail::ParamDesc &pp)
out_data.resize(params.num_out);
}
ONNXCompiled::~ONNXCompiled() {
if (m_workload_type) {
m_workload_type->removeListener(m_workload_listener_id);
}
}
void ONNXCompiled::setWorkloadType(const std::string &type) {
const char* keys[] = {"ep.dynamic.workload_type"};
const char* values[] = {type.c_str()};
this_session.SetEpDynamicOptions(keys, values, 1);
}
void ONNXCompiled::listenToWorkloadType(cv::gapi::onnx::WorkloadTypeONNXPtr workload) {
m_workload_type = workload;
m_workload_listener_id = m_workload_type->addListener(std::bind(&ONNXCompiled::setWorkloadType, this, std::placeholders::_1));
}
std::vector<TensorInfo> ONNXCompiled::getTensorInfo(TensorPosition pos) {
GAPI_Assert(pos == INPUT || pos == OUTPUT);
@@ -1348,9 +1378,9 @@ namespace {
}
virtual EPtr compile(const ade::Graph &graph,
const cv::GCompileArgs &,
const cv::GCompileArgs &compileArgs,
const std::vector<ade::NodeHandle> &nodes) const override {
return EPtr{new cv::gimpl::onnx::GONNXExecutable(graph, nodes)};
return EPtr{new cv::gimpl::onnx::GONNXExecutable(graph, nodes, compileArgs)};
}
virtual cv::GKernelPackage auxiliaryKernels() const override {

View File

@@ -39,7 +39,8 @@ class GONNXExecutable final: public GIslandExecutable
public:
GONNXExecutable(const ade::Graph &graph,
const std::vector<ade::NodeHandle> &nodes);
const std::vector<ade::NodeHandle> &nodes,
const cv::GCompileArgs &compileArgs);
virtual inline bool canReshape() const override { return false; }
virtual inline void reshape(ade::Graph&, const GCompileArgs&) override {

View File

@@ -1599,7 +1599,16 @@ cv::gimpl::ov::GOVExecutable::GOVExecutable(const ade::Graph &g,
const cv::GCompileArgs &compileArgs,
const std::vector<ade::NodeHandle> &nodes)
: m_g(g), m_gm(m_g) {
auto workload_arg = cv::gapi::getCompileArg<cv::gapi::wip::ov::WorkloadTypeOVPtr>(compileArgs);
if(workload_arg.has_value()) {
#if INF_ENGINE_RELEASE >= 2024030000
m_workload_type = workload_arg.value();
m_workload_listener_id = m_workload_type->addListener(std::bind(&GOVExecutable::setWorkloadType, this, std::placeholders::_1));
#else
util::throw_error(std::logic_error("Workload type not supported in this version of OpenVINO, use >= 2024.3.0"));
#endif
}
m_options.inference_only =
cv::gapi::getCompileArg<cv::gapi::wip::ov::benchmark_mode>(compileArgs).has_value();
// FIXME: Currently this backend is capable to run a single inference node only.
@@ -1636,6 +1645,25 @@ cv::gimpl::ov::GOVExecutable::GOVExecutable(const ade::Graph &g,
}
}
#if INF_ENGINE_RELEASE >= 2024030000
cv::gimpl::ov::GOVExecutable::~GOVExecutable() {
if (m_workload_type)
m_workload_type->removeListener(m_workload_listener_id);
}
void cv::gimpl::ov::GOVExecutable::setWorkloadType(const std::string &type) {
if (type == "Default") {
compiled.compiled_model.set_property({{"WORKLOAD_TYPE", ::ov::WorkloadType::DEFAULT}});
}
else if (type == "Efficient") {
compiled.compiled_model.set_property({{"WORKLOAD_TYPE", ::ov::WorkloadType::EFFICIENT}});
}
else {
GAPI_LOG_WARNING(NULL, "Unknown value for WORKLOAD_TYPE");
}
}
#endif
void cv::gimpl::ov::GOVExecutable::run(cv::gimpl::GIslandExecutable::IInput &in,
cv::gimpl::GIslandExecutable::IOutput &out) {
std::vector<InObj> input_objs;

View File

@@ -50,12 +50,18 @@ class GOVExecutable final: public GIslandExecutable
// To manage additional execution options
Options m_options;
#if INF_ENGINE_RELEASE >= 2024030000
cv::gapi::wip::ov::WorkloadTypeOVPtr m_workload_type;
uint64_t m_workload_listener_id = 0;
void setWorkloadType(const std::string &type);
#endif
public:
GOVExecutable(const ade::Graph &graph,
const cv::GCompileArgs &compileArgs,
const std::vector<ade::NodeHandle> &nodes);
#if INF_ENGINE_RELEASE >= 2024030000
~GOVExecutable();
#endif
virtual inline bool canReshape() const override { return false; }
virtual inline void reshape(ade::Graph&, const GCompileArgs&) override {
GAPI_Error("InternalError"); // Not implemented yet