From 6758735364c6c77e54a4698442853eb8291138f0 Mon Sep 17 00:00:00 2001 From: River Li Date: Mon, 19 Feb 2024 18:24:31 +0800 Subject: [PATCH] [Core] port PR-22525 and PR-22804 (#22554) ### Details: - Port: https://github.com/openvinotoolkit/openvino/pull/22525 - Port: https://github.com/openvinotoolkit/openvino/pull/22804 ### Tickets: - CVS-131048 --------- Co-authored-by: Ilya Lavrenov Co-authored-by: Vitaliy Urusovskij --- .../include/openvino/runtime/core.hpp | 5 +- src/inference/src/core.cpp | 49 +++++---- .../tests/functional/ov_core_test.cpp | 99 +++++++++++++++++++ 3 files changed, 132 insertions(+), 21 deletions(-) create mode 100644 src/inference/tests/functional/ov_core_test.cpp diff --git a/src/inference/include/openvino/runtime/core.hpp b/src/inference/include/openvino/runtime/core.hpp index 153559c17e0..0aab84afda0 100644 --- a/src/inference/include/openvino/runtime/core.hpp +++ b/src/inference/include/openvino/runtime/core.hpp @@ -50,8 +50,9 @@ public: * 1. (default) Use XML configuration file in case of dynamic libraries build; * 2. Use strictly defined configuration in case of static libraries build. * - * @param xml_config_file Path to the .xml file with plugins to load from. If the XML configuration file is not - * specified, default OpenVINO Runtime plugins are loaded from: + * @param xml_config_file Path to the .xml file with plugins to load from. If path contains only file name + * with extension, file will be searched in a folder with OpenVINO runtime shared library. + * If the XML configuration file is not specified, default OpenVINO Runtime plugins are loaded from: * 1. (dynamic build) default `plugins.xml` file located in the same folder as OpenVINO runtime shared library; * 2. (static build) statically defined configuration. In this case path to the .xml file is ignored. */ diff --git a/src/inference/src/core.cpp b/src/inference/src/core.cpp index 9ee07f6246a..69650f46a1d 100644 --- a/src/inference/src/core.cpp +++ b/src/inference/src/core.cpp @@ -16,26 +16,37 @@ namespace ov { std::string find_plugins_xml(const std::string& xml_file) { - if (xml_file.empty()) { - const auto ov_library_path = ov::util::get_ov_lib_path(); - - // plugins.xml can be found in either: - - // 1. openvino-X.Y.Z relative to libopenvino.so folder - std::ostringstream str; - str << "openvino-" << OPENVINO_VERSION_MAJOR << "." << OPENVINO_VERSION_MINOR << "." << OPENVINO_VERSION_PATCH; - const auto sub_folder = str.str(); - - // register plugins from default openvino-/plugins.xml config - auto xmlConfigFileDefault = ov::util::path_join({ov_library_path, sub_folder, "plugins.xml"}); - if (ov::util::file_exists(xmlConfigFileDefault)) - return xmlConfigFileDefault; - - // 2. in folder with libopenvino.so - xmlConfigFileDefault = ov::util::path_join({ov_library_path, "plugins.xml"}); - if (ov::util::file_exists(xmlConfigFileDefault)) - return xmlConfigFileDefault; + std::string xml_file_name = xml_file; + if (xml_file_name.empty()) { + // Default plugin xml file name, will search in OV folder. + xml_file_name = "plugins.xml"; + } else { + // If file path contains file separator, return file path; + // Otherwise search it in OV folder with no restriction on file name and extension. + if (xml_file_name.find(util::FileTraits().file_separator) != xml_file_name.npos) { + return xml_file_name; + } } + const auto ov_library_path = ov::util::get_ov_lib_path(); + + // plugins xml can be found in either: + // 1. openvino-X.Y.Z relative to libopenvino.so folder + std::ostringstream str; + str << "openvino-" << OPENVINO_VERSION_MAJOR << "." << OPENVINO_VERSION_MINOR << "." << OPENVINO_VERSION_PATCH; + const auto sub_folder = str.str(); + + // register plugins from default openvino-/plugins.xml config + auto xmlConfigFileDefault = ov::util::path_join({ov_library_path, sub_folder, xml_file_name}); + if (ov::util::file_exists(xmlConfigFileDefault)) { + return xmlConfigFileDefault; + } + + // 2. in folder with libopenvino.so + xmlConfigFileDefault = ov::util::path_join({ov_library_path, xml_file_name}); + if (ov::util::file_exists(xmlConfigFileDefault)) { + return xmlConfigFileDefault; + } + return xml_file; } diff --git a/src/inference/tests/functional/ov_core_test.cpp b/src/inference/tests/functional/ov_core_test.cpp new file mode 100644 index 00000000000..f166527a666 --- /dev/null +++ b/src/inference/tests/functional/ov_core_test.cpp @@ -0,0 +1,99 @@ +// Copyright (C) 2018-2024 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include + +#include + +#include "common_test_utils/common_utils.hpp" +#include "common_test_utils/file_utils.hpp" +#include "file_utils.h" +#include "openvino/runtime/core.hpp" +#include "openvino/util/file_util.hpp" + +#ifndef OPENVINO_STATIC_LIBRARY + +static void create_plugin_xml(const std::string& file_name, const std::string& plugin_name = "1") { + std::ofstream file(file_name); + + file << "::file_separator; + file << ov::util::FileTraits::library_prefix(); + file << "mock_engine"; + file << OV_BUILD_POSTFIX; + file << ov::util::FileTraits::dot_symbol; + file << ov::util::FileTraits::library_ext(); + file << "\" name=\"" << plugin_name << "\">"; + file.flush(); + file.close(); +} + +static void remove_plugin_xml(const std::string& file_name) { + ov::test::utils::removeFile(file_name); +} + +TEST(CoreBaseTest, LoadPluginXML) { + std::string xml_file_name = "test_plugin.xml"; + std::string xml_file_path = + InferenceEngine::getIELibraryPath() + ov::util::FileTraits::file_separator + xml_file_name; + create_plugin_xml(xml_file_path); + EXPECT_NO_THROW(ov::Core core(xml_file_name)); + remove_plugin_xml(xml_file_path); +} + +TEST(CoreBaseTest, LoadPluginDifferentXMLExtension) { + std::string xml_file_name = "test_plugin.test"; + std::string xml_file_path = + InferenceEngine::getIELibraryPath() + ov::util::FileTraits::file_separator + xml_file_name; + create_plugin_xml(xml_file_path); + EXPECT_NO_THROW(ov::Core core(xml_file_name)); + remove_plugin_xml(xml_file_path); +} + +TEST(CoreBaseTest, LoadAbsoluteOVPathPluginXML) { + std::string xml_file_name = "test_plugin.xml"; + std::string xml_file_path = + InferenceEngine::getIELibraryPath() + ov::util::FileTraits::file_separator + xml_file_name; + create_plugin_xml(xml_file_path); + EXPECT_NO_THROW(ov::Core core(xml_file_path)); + remove_plugin_xml(xml_file_path); +} + +TEST(CoreBaseTest, LoadAbsoluteCWPathPluginXML) { + std::string xml_file_name = "test_plugin.xml"; + std::string xml_file_path = + ov::test::utils::getCurrentWorkingDir() + ov::util::FileTraits::file_separator + xml_file_name; + create_plugin_xml(xml_file_path); + EXPECT_NO_THROW(ov::Core core(xml_file_path)); + remove_plugin_xml(xml_file_path); +} + +TEST(CoreBaseTest, LoadRelativeCWPathPluginXML) { + std::string xml_file_name = "test_plugin.xml"; + std::string xml_file_path = + ov::test::utils::getCurrentWorkingDir() + ov::util::FileTraits::file_separator + xml_file_name; + create_plugin_xml(xml_file_path); + EXPECT_NO_THROW(ov::Core core(xml_file_name)); + remove_plugin_xml(xml_file_path); +} + +TEST(CoreBaseTest, LoadOVFolderOverCWPathPluginXML) { + std::string xml_file_name = "test_plugin.xml"; + std::string cwd_file_path = + ov::test::utils::getCurrentWorkingDir() + ov::util::FileTraits::file_separator + xml_file_name; + std::string ov_file_path = + InferenceEngine::getIELibraryPath() + ov::util::FileTraits::file_separator + xml_file_name; + create_plugin_xml(cwd_file_path); + create_plugin_xml(ov_file_path, "2"); + ov::Core core(xml_file_name); + auto version = core.get_versions("2"); + EXPECT_EQ(1, version.size()); + version = core.get_versions("1"); + EXPECT_EQ(0, version.size()); + remove_plugin_xml(cwd_file_path); + remove_plugin_xml(ov_file_path); +} + +#endif \ No newline at end of file