Skip to content

Commit

Permalink
Static plugins: preprocessing and AUTO (openvinotoolkit#8265)
Browse files Browse the repository at this point in the history
* 1. Removed explicit SHARED from libraries
2. Fixed double definition for ie_layer_validators

* Fixed SEG in unit-test: order of initialization for global vars

* Added an ability to find plugins.xml from static IE

* Fixes in unit-test

* Migrated to new macro for import / export

* Minimized number of custom dllexport

* Don't use IR v7 for static libraries

* Revert for merge

* Don't enable tests with dlopen for static libraries

* Code style

* Added condition for export

* Revert format_reader

* Removed forward decalaration with external linkage

* Fixed IE linkage on Windows

* Reverted back 2 flags

* Minimal RRTI for cpuFuncTests

* Minimal RRTI for cpuFuncTests

* Still need IR v7 reader

* Fixed build

* Fixed compilation

* clang-format fix

* Removed BUILD_AS_IS and used USE_STATIC_IE

* Enable IR v7 reader as static library

* Fixed compilation for GPU plugin

* Trying to build plugins as static library

* Plugins are able provide their own name for CreatePluginEngine function

* Fixed CPU

* Fixed comments

* Fixed ENABLE_IR_V7_READER usage

* Fixed VPU

* clang-format

* Fixes

* Fix

* Load multiple plugins at once

* Fixed interpreter undefined symbols

* Trying to dynamically register static plugins

* Reverted some ngraph changes

* Fixed cpuUnitTests compilation

* Fixed compilation

* Fixed myriad

* Fixed custom_opset tests

* Reverted linker flags

* Support both static and dynamic plugins

* Fixed compilation of myriadFuncTests

* Removed duplication

* Preprocessing library

* Fixes after self-review

* Fixed linkage for preprocessing

* Fixed preprocessing plugin build

* Windows fix #2

* Fixed linkage for preprocessing

* Proprocessing linkage

* Fixes for Windows

* Added optimizing libker flags to executables as well

* Fixed creation of global ov::Core

* AUTO plugin is static

* Fixed case of build+_shared_libs

* Removed some global variables from ngraph

* Fixes

* Fixed link issue on Windows

* Fixed cmake options

* Fix

* Fix

* Fix 2
  • Loading branch information
ilya-lavrenov authored Oct 30, 2021
1 parent cc457e0 commit 661d436
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 63 deletions.
7 changes: 3 additions & 4 deletions cmake/developer_package/compile_flags/os_flags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -332,9 +332,11 @@ else()
if(APPLE)
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-dead_strip")
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,-dead_strip")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-dead_strip")
elseif(LINUX)
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--gc-sections -Wl,--exclude-libs,ALL")
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,--gc-sections -Wl,--exclude-libs,ALL")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections -Wl,--exclude-libs,ALL")
endif()
endif()

Expand All @@ -354,10 +356,7 @@ function(link_system_libraries TARGET_NAME)
)
endif()

target_link_libraries(${TARGET_NAME}
${MODE}
${arg}
)
target_link_libraries(${TARGET_NAME} ${MODE} ${arg})
endif()
endforeach()
endfunction()
34 changes: 28 additions & 6 deletions cmake/developer_package/plugins/create_plugins_hpp.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# SPDX-License-Identifier: Apache-2.0
#

foreach(var IE_DEVICE_NAMES IE_PLUGINS_HPP_HEADER IE_PLUGINS_HPP_HEADER_IN)
foreach(var IE_DEVICE_MAPPING IE_PLUGINS_HPP_HEADER IE_PLUGINS_HPP_HEADER_IN)
if(NOT DEFINED ${var})
message(FATAL_ERROR "${var} is required, but not defined")
endif()
Expand All @@ -12,18 +12,40 @@ endforeach()

set(IE_PLUGINS_DECLARATIONS "")
set(IE_PLUGINS_MAP_DEFINITION
"std::map<std::string, InferenceEngine::CreatePluginEngineFunc *> plugins_hpp = {")
" static const std::map<Key, Value> plugins_hpp = {")

foreach(dev_name IN LISTS IE_DEVICE_NAMES)
set(_IE_CREATE_PLUGIN_FUNC "CreatePluginEngine${dev_name}")
foreach(dev_map IN LISTS IE_DEVICE_MAPPING)
string(REPLACE ":" ";" dev_map "${dev_map}")
list(GET dev_map 0 mapped_dev_name)
list(GET dev_map 1 actual_dev_name)

# common
set(_IE_CREATE_PLUGIN_FUNC "CreatePluginEngine${actual_dev_name}")

# declarations
set(IE_PLUGINS_DECLARATIONS "${IE_PLUGINS_DECLARATIONS}
IE_DEFINE_PLUGIN_CREATE_FUNCTION_DECLARATION(${_IE_CREATE_PLUGIN_FUNC})")

# definitions
set(dev_config "{")
if(${mapped_dev_name}_CONFIG)
foreach(props IN LISTS ${mapped_dev_name}_CONFIG)
string(REPLACE ":" ";" props "${props}")

list(GET props 0 key)
list(GET props 1 value)

set(dev_config "${dev_config} { \"${key}\", \"${value}\" }, ")
endforeach()
endif()
set(dev_config "${dev_config}}")

set(IE_PLUGINS_MAP_DEFINITION "${IE_PLUGINS_MAP_DEFINITION}
{ \"${dev_name}\", ${_IE_CREATE_PLUGIN_FUNC} },")
{ \"${mapped_dev_name}\", Value { ${_IE_CREATE_PLUGIN_FUNC}, ${dev_config} } },")
endforeach()

set(IE_PLUGINS_MAP_DEFINITION "${IE_PLUGINS_MAP_DEFINITION}
};\n")
};\n")


message("${IE_PLUGINS_DECLARATIONS}")
Expand Down
28 changes: 21 additions & 7 deletions cmake/developer_package/plugins/plugins.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ endif()
#
# ie_add_plugin(NAME <targetName>
# DEVICE_NAME <deviceName>
# [PSEUDO]
# [PSEUDO_PLUGIN_FOR]
# [DEFAULT_CONFIG <key:value;...>]
# [SOURCES <sources>]
# [OBJECT_LIBRARIES <object_libs>]
Expand All @@ -29,8 +29,8 @@ endif()
# )
#
function(ie_add_plugin)
set(options SKIP_INSTALL ADD_CLANG_FORMAT PSEUDO_PLUGIN)
set(oneValueArgs NAME DEVICE_NAME VERSION_DEFINES_FOR)
set(options SKIP_INSTALL ADD_CLANG_FORMAT)
set(oneValueArgs NAME DEVICE_NAME VERSION_DEFINES_FOR PSEUDO_PLUGIN_FOR)
set(multiValueArgs DEFAULT_CONFIG SOURCES OBJECT_LIBRARIES CPPLINT_FILTERS)
cmake_parse_arguments(IE_PLUGIN "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

Expand All @@ -44,7 +44,7 @@ function(ie_add_plugin)

# create and configure target

if(NOT IE_PLUGIN_PSEUDO_PLUGIN)
if(NOT IE_PLUGIN_PSEUDO_PLUGIN_FOR)
if(IE_PLUGIN_VERSION_DEFINES_FOR)
addVersionDefines(${IE_PLUGIN_VERSION_DEFINES_FOR} CI_BUILD_NUMBER)
endif()
Expand Down Expand Up @@ -148,6 +148,7 @@ function(ie_add_plugin)
list(APPEND PLUGIN_FILES "${IE_PLUGIN_DEVICE_NAME}:${IE_PLUGIN_NAME}")
set(PLUGIN_FILES "${PLUGIN_FILES}" CACHE INTERNAL "" FORCE)
set(${IE_PLUGIN_DEVICE_NAME}_CONFIG "${IE_PLUGIN_DEFAULT_CONFIG}" CACHE INTERNAL "" FORCE)
set(${IE_PLUGIN_DEVICE_NAME}_PSEUDO_PLUGIN_FOR "${IE_PLUGIN_PSEUDO_PLUGIN_FOR}" CACHE INTERNAL "" FORCE)
endfunction()

#
Expand Down Expand Up @@ -230,17 +231,29 @@ macro(ie_register_plugins_static)
set(multiValueArgs POSSIBLE_PLUGINS)
cmake_parse_arguments(IE_REGISTER "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

set(device_names)
set(device_mapping)
set(device_configs)
foreach(name IN LISTS PLUGIN_FILES)
string(REPLACE ":" ";" name "${name}")
list(LENGTH name length)
if(NOT ${length} EQUAL 2)
message(FATAL_ERROR "Unexpected error, please, contact developer of this script")
endif()

# create device mapping: preudo device => actual device
list(GET name 0 device_name)
list(APPEND device_names ${device_name})
if(${device_name}_PSEUDO_PLUGIN_FOR)
list(APPEND device_mapping "${device_name}:${${device_name}_PSEUDO_PLUGIN_FOR}")
else()
list(APPEND device_mapping "${device_name}:${device_name}")
endif()

# add default plugin config options
if(${device_name}_CONFIG)
list(APPEND device_configs -D "${device_name}_CONFIG=${${device_name}_CONFIG}")
endif()

# link plugin to inference_engine static version
list(GET name 1 plugin_name)
target_link_libraries(${IE_REGISTER_MAIN_TARGET} PRIVATE ${plugin_name})
endforeach()
Expand All @@ -251,9 +264,10 @@ macro(ie_register_plugins_static)
add_custom_command(OUTPUT "${ie_plugins_hpp}"
COMMAND
"${CMAKE_COMMAND}"
-D "IE_DEVICE_NAMES=${device_names}"
-D "IE_DEVICE_MAPPING=${device_mapping}"
-D "IE_PLUGINS_HPP_HEADER_IN=${plugins_hpp_in}"
-D "IE_PLUGINS_HPP_HEADER=${ie_plugins_hpp}"
${device_configs}
-P "${IEDevScripts_DIR}/plugins/create_plugins_hpp.cmake"
DEPENDS
"${plugins_hpp_in}"
Expand Down
8 changes: 8 additions & 0 deletions cmake/developer_package/plugins/plugins.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
namespace {
@IE_PLUGINS_DECLARATIONS@

using Config = std::map<std::string, std::string>;
using Value = std::pair<InferenceEngine::CreatePluginEngineFunc *, Config>;
using Key = std::string;
using PluginsStaticRegistry = std::map<Key, Value>;

const std::map<Key, Value> getStaticPluginsRegistry() {
@IE_PLUGINS_MAP_DEFINITION@
return plugins_hpp;
}

} // namespace
16 changes: 8 additions & 8 deletions inference-engine/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@ if(CMAKE_COMPILER_IS_GNUCXX)
ie_add_compiler_flags(-Wmissing-declarations)
endif()

add_subdirectory(preprocessing)
add_subdirectory(transformations)

add_subdirectory(legacy_api)

add_subdirectory(low_precision_transformations)

add_subdirectory(offline_transformations)

add_subdirectory(snippets)

if(ENABLE_MKL_DNN)
add_subdirectory(mkldnn_plugin)
endif()
Expand All @@ -37,15 +43,9 @@ endif()

add_subdirectory(inference_engine)

add_subdirectory(transformations)

add_subdirectory(readers)

add_subdirectory(low_precision_transformations)

add_subdirectory(offline_transformations)

add_subdirectory(snippets)
add_subdirectory(preprocessing)

# add a custom target to build all Inference Engine Core libraries

Expand Down
16 changes: 10 additions & 6 deletions inference-engine/src/inference_engine/src/ie_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -445,26 +445,30 @@ class CoreImpl : public ie::ICore, public std::enable_shared_from_this<ie::ICore
}
}

#ifdef OPENVINO_STATIC_LIBRARY

/**
* @brief Register plugins for devices which are located in .xml configuration file.
* @note The function supports UNICODE path
* @param xmlConfigFile An .xml configuraion with device / plugin information
*/
void RegisterPluginsInRegistry(
const std::map<std::string, InferenceEngine::CreatePluginEngineFunc*>& static_registry) {
void RegisterPluginsInRegistry(const decltype(::getStaticPluginsRegistry())& static_registry) {
std::lock_guard<std::mutex> lock(pluginsMutex);

for (const auto& plugin : static_registry) {
const auto& deviceName = plugin.first;
if (deviceName.find('.') != std::string::npos) {
IE_THROW() << "Device name must not contain dot '.' symbol";
}
// TODO: add properties support to enable AUTO device
PluginDescriptor desc = {{}, {}, {}, plugin.second};
const auto& create_func = plugin.second.first;
const auto& config = plugin.second.second;
PluginDescriptor desc = {{}, config, {}, create_func};
pluginRegistry[deviceName] = desc;
}
}

#endif

//
// ICore public API
//
Expand Down Expand Up @@ -1135,7 +1139,7 @@ Core::Core(const std::string& xmlConfigFile) {
_impl = std::make_shared<Impl>();

#ifdef OPENVINO_STATIC_LIBRARY
_impl->RegisterPluginsInRegistry(::plugins_hpp);
_impl->RegisterPluginsInRegistry(::getStaticPluginsRegistry());
#else
RegisterPlugins(ov::runtime::parseXmlConfig(xmlConfigFile));
#endif
Expand Down Expand Up @@ -1398,7 +1402,7 @@ Core::Core(const std::string& xmlConfigFile) {
_impl = std::make_shared<Impl>();

#ifdef OPENVINO_STATIC_LIBRARY
_impl->RegisterPluginsInRegistry(::plugins_hpp);
_impl->RegisterPluginsInRegistry(::getStaticPluginsRegistry());
#else
register_plugins(parseXmlConfig(xmlConfigFile));
#endif
Expand Down
3 changes: 1 addition & 2 deletions inference-engine/src/multi_device/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ ie_add_plugin(NAME ${TARGET_NAME}
SOURCES ${SOURCES} ${HEADERS}
VERSION_DEFINES_FOR multi_device_plugin.cpp)

# TODO: add fix for this case since DEFAULT_CONFIG is not used now
ie_add_plugin(NAME ${TARGET_NAME}
DEVICE_NAME "AUTO"
PSEUDO_PLUGIN
PSEUDO_PLUGIN_FOR "MULTI"
DEFAULT_CONFIG "MULTI_WORK_MODE_AS_AUTO:YES")

target_link_libraries(${TARGET_NAME} PRIVATE inference_engine ngraph inference_engine_transformations)
Expand Down
5 changes: 4 additions & 1 deletion inference-engine/src/preprocessing/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ target_include_directories(${TARGET_NAME}_obj SYSTEM PRIVATE $<TARGET_PROPERTY:o
$<TARGET_PROPERTY:inference_engine_plugin_api,INTERFACE_INCLUDE_DIRECTORIES>)

target_include_directories(${TARGET_NAME}_obj PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}"
$<TARGET_PROPERTY:openvino::util,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:openvino::itt,INTERFACE_INCLUDE_DIRECTORIES>)

set_ie_threading_interface_for(${TARGET_NAME}_obj)
Expand All @@ -121,7 +122,7 @@ else()
set(library_type STATIC)
endif()

add_library(${TARGET_NAME} MODULE
add_library(${TARGET_NAME} ${library_type}
$<TARGET_OBJECTS:${TARGET_NAME}_obj>)

ie_add_vs_version_file(NAME ${TARGET_NAME}
Expand All @@ -134,6 +135,8 @@ target_link_libraries(${TARGET_NAME} PRIVATE fluid openvino::itt openvino::util)
if(BUILD_SHARED_LIBS)
# for static linkage the dependencies are in opposite order
target_link_libraries(${TARGET_NAME} PRIVATE inference_engine)
else()
target_link_libraries(inference_engine PRIVATE ${TARGET_NAME})
endif()

target_include_directories(${TARGET_NAME} INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}"
Expand Down
Loading

0 comments on commit 661d436

Please sign in to comment.