Skip to content

dnn: add plugin support for OpenVINO#21745

Merged
opencv-pushbot merged 2 commits intoopencv:4.xfrom
alalek:dnn_plugin_openvino
Oct 8, 2022
Merged

dnn: add plugin support for OpenVINO#21745
opencv-pushbot merged 2 commits intoopencv:4.xfrom
alalek:dnn_plugin_openvino

Conversation

@alalek
Copy link
Copy Markdown
Member

@alalek alalek commented Mar 18, 2022

TODO:

  • Fix Windows build scripts for plugin: disable PCH
  • Fix Windows build scripts for plugin: .simd.hpp support
  • (postpone) BUILD_PLUGIN -> OPENCV_BUILD_PLUGIN (backlog, to be done as separate PR - need to fix other modules too)
  • Myriad detection (NCS/NCS2) - 2021.4+ supports only NCS2
force_builders=Custom,Custom Win
Xforce_builders=linux,docs,Custom,Custom Win

Xbuild_image:Custom=ubuntu-openvino-2022.1.0:20.04
build_image:Custom=ubuntu-openvino-2021.4.2:20.04
Xbuild_image:Custom=ubuntu-openvino-2022.1.0.dev20220131:20.04
build_image:Custom Win=openvino-2021.4.2
build_image:Custom Mac=openvino-2021.4.2

test_modules:Custom=dnn,gapi,python2,python3,java
test_modules:Custom Win=dnn,gapi,python2,python3,java
test_modules:Custom Mac=dnn,gapi,python2,python3,java

buildworker:Custom=linux-1
# disabled due high memory usage: test_opencl:Custom=ON
test_opencl:Custom=ON
test_bigdata:Custom=1
test_filter:Custom=*

buildworker:Custom Win=windows-1
test_bigdata:Custom Win=1
test_filter:Custom Win=*
test_opencl:Custom Win=ON
build_contrib:Custom Win=OFF

allow_multiple_commits=1

@asmorkalov asmorkalov requested a review from rogday September 9, 2022 12:09
@alalek
Copy link
Copy Markdown
Member Author

alalek commented Sep 13, 2022

@rogday It makes sense to start review from "backend API" in this file: modules/dnn/src/backend.hpp

Copy link
Copy Markdown
Contributor

@mshabunin mshabunin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good to me, though I haven't reviewed some parts in details.

Some notes:

  • perhaps we need to extract common registry/factory/backend/cmake part from all plugin-compatible modules
  • we don't have world builder in CI, will we support this configuration?

endif()
list(APPEND dnn_runtime_libs ocv.3rdparty.openvino)
if("openvino" IN_LIST DNN_PLUGIN_LIST OR DNN_PLUGIN_LIST STREQUAL "all")
add_subdirectory(misc/plugin/openvino) # plugin doesn't support PCH, separate directory scope is necessary
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does not work with world build:

$ cmake -GNinja \
  -DWITH_OPENVINO=ON \
  -DBUILD_opencv_world=ON \
  ../opencv
CMake Error at modules/dnn/CMakeLists.txt:207 (add_subdirectory):
  add_subdirectory given source "misc/plugin/openvino" which is not an
  existing directory.
Call Stack (most recent call first):
  modules/world/CMakeLists.txt:13 (include)
  modules/world/CMakeLists.txt:32 (include_one_module)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

To properly use OV plugin need to disable GAPI module or OV-in-GAPI:

  • -DBUILD_opencv_gapi=OFF
  • or -DOPENCV_GAPI_WITH_OPENVINO=OFF

Otherwise gapi links OpenVINO to opencv_world directly, which affects DNN code compilation flags too.

//} // Net::Impl

#if 0
#define printf_(args) printf args
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it would be better to use logging facilities? Are they available in the plugin?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes sense in general.
Currently it is out of scope of this PR as mentioned code is a copy-paste.

Copy link
Copy Markdown
Member

@rogday rogday left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for contribution! Design looks good.
A few minor comments.

{
CV_Assert(instancePtr);
// TODO C++20 "aliasing constructor"
return std::shared_ptr<cv::dnn_backend::NetworkBackend>(instancePtr, [](cv::dnn_backend::NetworkBackend*){}); // empty deleter
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could capture shared_from_this() inside a deleter for now?
Seems to achieve the same goal.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. This requires changes in class inheritance (use std::enable_shared_from_this<T>). I prefer to keep interfaces clear as much as possible.
  2. I don't want to transfer object lifetime control from the plugin (it could not prevent unloading of plugin code anyway).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Line 22: class PluginDNNBackend CV_FINAL: public std::enable_shared_from_this<PluginDNNBackend>
  2. Then why shared_ptr is used in the first place?
  3. Does TODO makes sense then?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. PluginDNNBackend != NetworkBackend.
  2. To avoid uncontrolled usage of RAW pointers.
  3. AFAIK, to alias on plugins instance (the same is for highgui/parallel plugins - code borrowed from there)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. We are in a class that is derived from enable_shared_from_this. The idea behind capturing shared_from_this()(PluginDNNBackend) inside a deleter of NetworkBackend was that the former wouldn't run its destructor before the latter.
  2. In its current state they are equivalent.
  3. The idea seems to be similar to p.1 above.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we need a separate proposal which covers similar cases from other plugins.

// TODO: implemented accurate versions-based comparator
std::sort(candidates.begin(), candidates.end(), std::greater<std::string>());
CV_LOG_DEBUG(NULL, " - " << path << ": " << candidates.size());
copy(candidates.begin(), candidates.end(), back_inserter(results));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed, as ADL should work here well.
(also there are other places with the same code which works well)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not immediately obvious why sort is used with std, but copy - without. Just consistency nitpick.

@alalek
Copy link
Copy Markdown
Member Author

alalek commented Oct 7, 2022

Rebased and removed test commit.

@alalek
Copy link
Copy Markdown
Member Author

alalek commented Oct 8, 2022

👍

@opencv-pushbot opencv-pushbot merged commit 3472469 into opencv:4.x Oct 8, 2022
@fengyuentau
Copy link
Copy Markdown
Member

Great refactoring! I think I can have the same design as openvino (NetImplOpenVINO) for the CANN backend to have higher freedom to utilize CANN features.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants