Skip to content

Commit

Permalink
[core] Extend Core API to accept std::filesystem::path when build wit…
Browse files Browse the repository at this point in the history
…h cpp17 (openvinotoolkit#27950)

### Details:
- The `ov::Core` accepts `std::filesytem::path` in functions where
string as path is used.

### Tickets:
 - CVS-157908

---------

Signed-off-by: Pawel Raasz <[email protected]>
  • Loading branch information
praasz authored Dec 6, 2024
1 parent bf62609 commit 94f647d
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src/core/shape_inference/include/ov_optional.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
#include <cstddef>

namespace ov {
#ifdef OPENVINO_CPP_17_VER
using optional = std::optional;
#else

/**
* @brief Store optional object of type T (basic version of std::optional).
Expand Down Expand Up @@ -132,4 +135,5 @@ class optional {
bool m_has_value = false;
Storage<T> m_opt{};
};
#endif
} // namespace ov
6 changes: 3 additions & 3 deletions src/core/tests/pattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -558,8 +558,8 @@ TEST(pattern, multiple_optionals_in_row) {

// Pattern:
auto in = wrap_type<v0::Parameter>();
auto pattern_convert = optional<v0::Convert>(in);
auto pattern_relu = optional<v0::Relu>(pattern_convert);
auto pattern_convert = pattern::optional<v0::Convert>(in);
auto pattern_relu = pattern::optional<v0::Relu>(pattern_convert);
auto pattern_sigmoid = wrap_type<v0::Sigmoid>({pattern_relu});

// Test:
Expand Down Expand Up @@ -1255,4 +1255,4 @@ TEST(pattern, pattern_optional_root) {

// Should perfectly match
ASSERT_TRUE(tm.match(pattern_relu, model_relu));
}
}
50 changes: 50 additions & 0 deletions src/inference/include/openvino/runtime/core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
#include "openvino/runtime/remote_context.hpp"
#include "openvino/runtime/tensor.hpp"

#ifdef OPENVINO_CPP_VER_17
# include <filesystem>
#endif

namespace ov {

/**
Expand Down Expand Up @@ -95,9 +99,18 @@ class OPENVINO_RUNTIME_API Core {
* * TF (*.pb)
* * TFLite (*.tflite)
* @return A model.
* @{
*/
std::shared_ptr<ov::Model> read_model(const std::string& model_path, const std::string& bin_path = {}) const;

#ifdef OPENVINO_CPP_VER_17
template <class Path, std::enable_if_t<std::is_same_v<Path, std::filesystem::path>>* = nullptr>
std::shared_ptr<ov::Model> read_model(const Path& model_path, const Path& bin_path = {}) const {
return read_model(model_path.string(), bin_path.string());
}
#endif
/// @}

/**
* @brief Reads models from IR / ONNX / PDPD / TF / TFLite formats.
* @param model String with a model in IR / ONNX / PDPD / TF / TFLite format.
Expand Down Expand Up @@ -197,6 +210,13 @@ class OPENVINO_RUNTIME_API Core {
*/
CompiledModel compile_model(const std::string& model_path, const AnyMap& properties = {});

#ifdef OPENVINO_CPP_VER_17
template <class Path, std::enable_if_t<std::is_same_v<Path, std::filesystem::path>>* = nullptr>
auto compile_model(const Path& model_path, const AnyMap& properties = {}) const {
return compile_model(model_path.string(), properties);
}
#endif

#ifdef OPENVINO_ENABLE_UNICODE_PATH_SUPPORT
CompiledModel compile_model(const std::wstring& model_path, const AnyMap& properties = {});
#endif
Expand All @@ -223,6 +243,13 @@ class OPENVINO_RUNTIME_API Core {
return compile_model(model_path, AnyMap{std::forward<Properties>(properties)...});
}

#ifdef OPENVINO_CPP_VER_17
template <class Path, class... Properties, std::enable_if_t<std::is_same_v<Path, std::filesystem::path>>* = nullptr>
auto compile_model(const Path& model_path, Properties&&... properties) {
return compile_model(model_path.string(), std::forward<Properties>(properties)...);
}
#endif

#ifdef OPENVINO_ENABLE_UNICODE_PATH_SUPPORT
template <typename... Properties>
util::EnableIfAllStringAny<CompiledModel, Properties...> compile_model(const std::wstring& model_path,
Expand Down Expand Up @@ -250,6 +277,13 @@ class OPENVINO_RUNTIME_API Core {
const std::string& device_name,
const AnyMap& properties = {});

#ifdef OPENVINO_CPP_VER_17
template <class Path, std::enable_if_t<std::is_same_v<Path, std::filesystem::path>>* = nullptr>
auto compile_model(const Path& model_path, const std::string& device_name, const AnyMap& properties = {}) {
return compile_model(model_path.string(), device_name, properties);
}
#endif

#ifdef OPENVINO_ENABLE_UNICODE_PATH_SUPPORT
CompiledModel compile_model(const std::wstring& model_path,
const std::string& device_name,
Expand Down Expand Up @@ -279,6 +313,13 @@ class OPENVINO_RUNTIME_API Core {
return compile_model(model_path, device_name, AnyMap{std::forward<Properties>(properties)...});
}

#ifdef OPENVINO_CPP_VER_17
template <class Path, class... Properties, std::enable_if_t<std::is_same_v<Path, std::filesystem::path>>* = nullptr>
auto compile_model(const Path& model_path, const std::string& device_name, Properties&&... properties) {
return compile_model(model_path.string(), device_name, std::forward<Properties>(properties)...);
}
#endif

#ifdef OPENVINO_ENABLE_UNICODE_PATH_SUPPORT
template <typename... Properties>
util::EnableIfAllStringAny<CompiledModel, Properties...> compile_model(const std::wstring& model_path,
Expand Down Expand Up @@ -359,9 +400,18 @@ class OPENVINO_RUNTIME_API Core {
/**
* @brief Registers an extension to a Core object.
* @param library_path Path to the library with ov::Extension.
* @{
*/
void add_extension(const std::string& library_path);

#ifdef OPENVINO_CPP_VER_17
template <class Path, std::enable_if_t<std::is_same_v<Path, std::filesystem::path>>* = nullptr>
void add_extension(const Path& model_path) {
add_extension(model_path.string());
}
#endif
/// @}

#ifdef OPENVINO_ENABLE_UNICODE_PATH_SUPPORT
/**
* @brief Registers an extension to a Core object.
Expand Down
69 changes: 63 additions & 6 deletions src/inference/tests/functional/ov_core_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,26 @@

#include "common_test_utils/common_utils.hpp"
#include "common_test_utils/file_utils.hpp"
#include "functional_test_utils/test_model/test_model.hpp"
#include "openvino/runtime/core.hpp"
#include "openvino/util/file_util.hpp"

class CoreBaseTest : public testing::Test {
protected:
void generate_test_model_files(const std::string& name) {
auto prefix = ov::test::utils::generateTestFilePrefix();
model_file_name = prefix + name + ".xml";
weight_file_name = prefix + name + ".bin";
ov::test::utils::generate_test_model(model_file_name, weight_file_name);
}

void TearDown() override {
ov::test::utils::removeIRFiles(model_file_name, weight_file_name);
}

std::string model_file_name, weight_file_name;
};

#ifndef OPENVINO_STATIC_LIBRARY

static void create_plugin_xml(const std::string& file_name, const std::string& plugin_name = "1") {
Expand All @@ -33,7 +50,7 @@ static void remove_plugin_xml(const std::string& file_name) {
ov::test::utils::removeFile(file_name);
}

TEST(CoreBaseTest, LoadPluginXML) {
TEST_F(CoreBaseTest, LoadPluginXML) {
std::string xml_file_name = "test_plugin.xml";
std::string xml_file_path =
ov::test::utils::getOpenvinoLibDirectory() + ov::util::FileTraits<char>::file_separator + xml_file_name;
Expand All @@ -42,7 +59,7 @@ TEST(CoreBaseTest, LoadPluginXML) {
remove_plugin_xml(xml_file_path);
}

TEST(CoreBaseTest, LoadPluginDifferentXMLExtension) {
TEST_F(CoreBaseTest, LoadPluginDifferentXMLExtension) {
std::string xml_file_name = "test_plugin.test";
std::string xml_file_path =
ov::test::utils::getOpenvinoLibDirectory() + ov::util::FileTraits<char>::file_separator + xml_file_name;
Expand All @@ -51,7 +68,7 @@ TEST(CoreBaseTest, LoadPluginDifferentXMLExtension) {
remove_plugin_xml(xml_file_path);
}

TEST(CoreBaseTest, LoadAbsoluteOVPathPluginXML) {
TEST_F(CoreBaseTest, LoadAbsoluteOVPathPluginXML) {
std::string xml_file_name = "test_plugin.xml";
std::string xml_file_path =
ov::test::utils::getOpenvinoLibDirectory() + ov::util::FileTraits<char>::file_separator + xml_file_name;
Expand All @@ -60,7 +77,7 @@ TEST(CoreBaseTest, LoadAbsoluteOVPathPluginXML) {
remove_plugin_xml(xml_file_path);
}

TEST(CoreBaseTest, LoadAbsoluteCWPathPluginXML) {
TEST_F(CoreBaseTest, LoadAbsoluteCWPathPluginXML) {
std::string xml_file_name = "test_plugin.xml";
std::string xml_file_path =
ov::test::utils::getCurrentWorkingDir() + ov::util::FileTraits<char>::file_separator + xml_file_name;
Expand All @@ -69,7 +86,7 @@ TEST(CoreBaseTest, LoadAbsoluteCWPathPluginXML) {
remove_plugin_xml(xml_file_path);
}

TEST(CoreBaseTest, LoadRelativeCWPathPluginXML) {
TEST_F(CoreBaseTest, LoadRelativeCWPathPluginXML) {
std::string xml_file_name = "test_plugin.xml";
std::string xml_file_path =
ov::test::utils::getCurrentWorkingDir() + ov::util::FileTraits<char>::file_separator + xml_file_name;
Expand All @@ -78,7 +95,7 @@ TEST(CoreBaseTest, LoadRelativeCWPathPluginXML) {
remove_plugin_xml(xml_file_path);
}

TEST(CoreBaseTest, LoadOVFolderOverCWPathPluginXML) {
TEST_F(CoreBaseTest, LoadOVFolderOverCWPathPluginXML) {
std::string xml_file_name = "test_plugin.xml";
std::string cwd_file_path =
ov::test::utils::getCurrentWorkingDir() + ov::util::FileTraits<char>::file_separator + xml_file_name;
Expand All @@ -96,3 +113,43 @@ TEST(CoreBaseTest, LoadOVFolderOverCWPathPluginXML) {
}

#endif

#if defined(OPENVINO_CPP_VER_17) && defined(ENABLE_OV_IR_FRONTEND)
namespace ov::test {
TEST_F(CoreBaseTest, read_model_with_std_fs_path) {
generate_test_model_files("test-model");

const auto model_path = std::filesystem::path(model_file_name);
const auto weight_path = std::filesystem::path(weight_file_name);

ov::Core core;
{
const auto model = core.read_model(model_path);
EXPECT_NE(model, nullptr);
}
{
const auto model = core.read_model(model_path, weight_path);
EXPECT_NE(model, nullptr);
}
}

TEST_F(CoreBaseTest, compile_model_with_std_fs_path) {
generate_test_model_files("model2");

const auto model_path = std::filesystem::path(model_file_name);
const auto weight_path = std::filesystem::path(weight_file_name);

ov::Core core;
{
const auto model = core.compile_model(model_path);
EXPECT_TRUE(model);
}
{
const auto devices = core.get_available_devices();

const auto model = core.compile_model(model_path, devices.at(0), ov::AnyMap{});
EXPECT_TRUE(model);
}
}
} // namespace ov::test
#endif
6 changes: 6 additions & 0 deletions src/inference/tests/functional/ov_extension_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ class CustomReLU : public ov::op::Op {
};

#if defined(ENABLE_OV_IR_FRONTEND)
# ifdef OPENVINO_CPP_VER_17
TEST_F(OVExtensionTests, ReshapeIRWithNewExtensionsPathLib) {
core.add_extension(std::filesystem::path(getOVExtensionPath()));
test();
}
# endif

TEST_F(OVExtensionTests, ReshapeIRWithNewExtensionsLib) {
core.add_extension(getOVExtensionPath());
Expand Down

0 comments on commit 94f647d

Please sign in to comment.