Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[core] Use std::filesystem::path in ov::pass::Serialize (#30034)

Pawel Raaszde894a64c5b7
diff --git a/src/bindings/python/src/pyopenvino/graph/passes/transformations.cpp b/src/bindings/python/src/pyopenvino/graph/passes/transformations.cpp
index 574ab29ebd3..c307db341a5 100644
--- a/src/bindings/python/src/pyopenvino/graph/passes/transformations.cpp
+++ b/src/bindings/python/src/pyopenvino/graph/passes/transformations.cpp
@@ -33,26 +33,24 @@ void regclass_transformations(py::module m) {
serialize(m, "Serialize");
serialize.doc() = "openvino.passes.Serialize transformation";
- serialize.def(
- py::init([](const py::object& path_to_xml, const py::object& path_to_bin, const py::object& version) {
- if (py::isinstance<py::str>(version)) {
- return std::make_shared<ov::pass::Serialize>(
- Common::utils::convert_path_to_string(path_to_xml),
- Common::utils::convert_path_to_string(path_to_bin),
- Common::utils::convert_to_version(version.cast<std::string>()));
- } else if (py::isinstance<Version>(version)) {
- return std::make_shared<ov::pass::Serialize>(Common::utils::convert_path_to_string(path_to_xml),
- Common::utils::convert_path_to_string(path_to_bin),
- version.cast<Version>());
- } else {
- return std::make_shared<ov::pass::Serialize>(Common::utils::convert_path_to_string(path_to_xml),
- Common::utils::convert_path_to_string(path_to_bin));
- }
- }),
- py::arg("path_to_xml"),
- py::arg("path_to_bin"),
- py::arg("version") = py::none(),
- R"(
+ serialize.def(py::init([](const py::object& path_to_xml, const py::object& path_to_bin, const py::object& version) {
+ const auto xml_path = Common::utils::to_fs_path(path_to_xml);
+ const auto bin_path = Common::utils::to_fs_path(path_to_bin);
+ if (py::isinstance<py::str>(version)) {
+ return std::make_shared<ov::pass::Serialize>(
+ xml_path,
+ bin_path,
+ Common::utils::convert_to_version(version.cast<std::string>()));
+ } else if (py::isinstance<Version>(version)) {
+ return std::make_shared<ov::pass::Serialize>(xml_path, bin_path, version.cast<Version>());
+ } else {
+ return std::make_shared<ov::pass::Serialize>(xml_path, bin_path);
+ }
+ }),
+ py::arg("path_to_xml"),
+ py::arg("path_to_bin"),
+ py::arg("version") = py::none(),
+ R"(
Create Serialize pass which is used for Model to IR serialization.
:param path_to_xml: Path where *.xml file will be saved.
diff --git a/src/bindings/python/src/pyopenvino/pyopenvino.cpp b/src/bindings/python/src/pyopenvino/pyopenvino.cpp
index d42b406ea68..140eabfee16 100644
--- a/src/bindings/python/src/pyopenvino/pyopenvino.cpp
+++ b/src/bindings/python/src/pyopenvino/pyopenvino.cpp
@@ -106,8 +106,8 @@ PYBIND11_MODULE(_pyopenvino, m) {
const std::string& version) {
const auto model = Common::utils::convert_to_model(ie_api_model);
ov::serialize(model,
- Common::utils::convert_path_to_string(xml_path),
- Common::utils::convert_path_to_string(bin_path),
+ Common::utils::to_fs_path(xml_path),
+ Common::utils::to_fs_path(bin_path),
Common::convert_to_version(version));
},
py::arg("model"),
@@ -167,7 +167,7 @@ PYBIND11_MODULE(_pyopenvino, m) {
"save_model",
[](py::object& ie_api_model, const py::object& xml_path, bool compress_to_fp16) {
const auto model = Common::utils::convert_to_model(ie_api_model);
- ov::save_model(model, Common::utils::convert_path_to_string(xml_path), compress_to_fp16);
+ ov::save_model(model, Common::utils::to_fs_path(xml_path), compress_to_fp16);
},
py::arg("model"),
py::arg("output_model"),
diff --git a/src/bindings/python/src/pyopenvino/utils/utils.cpp b/src/bindings/python/src/pyopenvino/utils/utils.cpp
index aa45ffd2511..c27609e167a 100644
--- a/src/bindings/python/src/pyopenvino/utils/utils.cpp
+++ b/src/bindings/python/src/pyopenvino/utils/utils.cpp
@@ -5,6 +5,7 @@
#include "utils.hpp"
#include <pybind11/stl.h>
+#include <pybind11/stl/filesystem.h>
#include <map>
#include <set>
@@ -589,6 +590,21 @@ std::shared_ptr<py::function> wrap_pyfunction(py::function f_callback) {
});
return callback_sp;
}
+
+std::filesystem::path to_fs_path(const py::object& path) {
+ // import pathlib.Path
+ py::object Path = py::module_::import("pathlib").attr("Path");
+
+ if (py::isinstance(path, Path) || py::isinstance<py::str>(path) || py::isinstance<py::bytes>(path)) {
+ return path.cast<std::filesystem::path>();
+ }
+
+ std::stringstream str;
+ str << "Path: '" << path << "'"
+ << " does not exist. Please provide valid model's path either as a string, bytes or pathlib.Path. "
+ "Examples:\n(1) '/home/user/models/model.onnx'\n(2) Path('/home/user/models/model/model.onnx')";
+ OPENVINO_THROW(str.str());
+}
}; // namespace utils
}; // namespace Common
diff --git a/src/bindings/python/src/pyopenvino/utils/utils.hpp b/src/bindings/python/src/pyopenvino/utils/utils.hpp
index a4673f679f5..8cf2c459e3c 100644
--- a/src/bindings/python/src/pyopenvino/utils/utils.hpp
+++ b/src/bindings/python/src/pyopenvino/utils/utils.hpp
@@ -6,11 +6,13 @@
#include <pybind11/pybind11.h>
+#include <filesystem>
+
#ifdef _MSC_VER
- // Warning occurred at the junction of pybind11
- // and the templates inside ov::Any.
- // Generated by operator `==` inside pybind::handle.
- #pragma warning( push )
+// Warning occurred at the junction of pybind11
+// and the templates inside ov::Any.
+// Generated by operator `==` inside pybind::handle.
+#pragma warning( push )
#pragma warning( disable: 4996 )
#elif defined(__GNUC__)
#pragma GCC diagnostic push
@@ -73,7 +75,7 @@ protected:
pos_type seekpos(pos_type pos, std::ios_base::openmode which) override {
return seekoff(pos, std::ios_base::beg, which);
}
-
+
private:
py::object m_py_stream;
};
@@ -148,6 +150,7 @@ protected:
std::shared_ptr<py::function> wrap_pyfunction(py::function f_callback);
+ std::filesystem::path to_fs_path(const py::object& path);
}; // namespace utils
}; // namespace Common
diff --git a/src/bindings/python/tests/test_runtime/test_model.py b/src/bindings/python/tests/test_runtime/test_model.py
index 569d59bbfe0..44bcc722941 100644
--- a/src/bindings/python/tests/test_runtime/test_model.py
+++ b/src/bindings/python/tests/test_runtime/test_model.py
@@ -165,7 +165,7 @@ def test_get_result_index_invalid():
([PartialShape([1]), PartialShape([4])], ["relu1", "relu2"], "TestModel1", 1, True, -1)
])
def test_result_index(shapes, relu_names, model_name, expected_outputs_length, is_invalid, expected_result_index):
- params = [ops.parameter(shape, dtype=np.float32, name=f"data{i+1}") for i, shape in enumerate(shapes)]
+ params = [ops.parameter(shape, dtype=np.float32, name=f"data{i + 1}") for i, shape in enumerate(shapes)]
relus = [ops.relu(param, name=relu_name) for param, relu_name in zip(params, relu_names)]
model = Model(relus[0], [params[0]], model_name)
diff --git a/src/bindings/python/tests/test_transformations/test_offline_api.py b/src/bindings/python/tests/test_transformations/test_offline_api.py
index e38d389a923..58d78206255 100644
--- a/src/bindings/python/tests/test_transformations/test_offline_api.py
+++ b/src/bindings/python/tests/test_transformations/test_offline_api.py
@@ -204,24 +204,37 @@ def compare_models_and_finalize_after_test(model, xml_path, bin_path):
(False, False),
],
)
-def test_serialize_pass_v2(request, tmp_path, is_path_xml, is_path_bin):
- model, xml_path, bin_path = prepare_test_model_for_serialize(request, tmp_path, is_path_xml, is_path_bin)
[email protected]("path_suffix", [r"test", r"晚安_пут"])
+def test_serialize_pass_v2(request, tmp_path, is_path_xml, is_path_bin, path_suffix):
+ model, xml_path, bin_path = prepare_test_model_for_serialize(request, tmp_path / path_suffix, is_path_xml, is_path_bin)
serialize(model, xml_path, bin_path)
compare_models_and_finalize_after_test(model, xml_path, bin_path)
[email protected]("path_suffix", [r"test", r"晚安_пут"])
+def test_serialize_pass_v2_path_as_bytes(request, tmp_path, path_suffix):
+ model, xml_path, bin_path = prepare_test_model_for_serialize(request, tmp_path / path_suffix, False, False)
+ serialize(model, xml_path.encode("utf-8"), bin_path.encode("utf-8"))
+ compare_models_and_finalize_after_test(model, xml_path, bin_path)
+
+
# request - https://docs.pytest.org/en/7.1.x/reference/reference.html#request
[email protected]("is_path_xml", [ # noqa: PT006
- (True),
- (False),
-],
-)
-def test_save_model(request, tmp_path, is_path_xml):
- model, xml_path, bin_path = prepare_test_model_for_serialize(request, tmp_path, is_path_xml, False)
[email protected]("is_path_xml", [True, False])
[email protected]("is_path_bin", [True, False])
[email protected]("path_suffix", [r"test", r"晚安_пут"])
+def test_save_model(request, tmp_path, is_path_xml, is_path_bin, path_suffix):
+ model, xml_path, bin_path = prepare_test_model_for_serialize(request, tmp_path / path_suffix, is_path_xml, is_path_bin)
save_model(model, xml_path, compress_to_fp16=False)
compare_models_and_finalize_after_test(model, xml_path, bin_path)
[email protected]("path_suffix", [r"test", r"晚安_пут"])
+def test_save_model_path_as_bytes(request, tmp_path, path_suffix):
+ model, xml_path, bin_path = prepare_test_model_for_serialize(request, tmp_path / path_suffix, False, False)
+ save_model(model, xml_path.encode("utf-8"), compress_to_fp16=False)
+ compare_models_and_finalize_after_test(model, xml_path, bin_path)
+
+
def test_save_model_fp16(request, tmp_path):
model, xml_path, bin_path = prepare_test_model_for_serialize(request, tmp_path, False, False)
save_model(model, xml_path)
diff --git a/src/bindings/python/tests/utils/helpers.py b/src/bindings/python/tests/utils/helpers.py
index aa67eb957c6..491bb2d6c5e 100644
--- a/src/bindings/python/tests/utils/helpers.py
+++ b/src/bindings/python/tests/utils/helpers.py
@@ -8,6 +8,7 @@ import os
import sys
import numpy as np
import base64
+import re
from sys import platform
from pathlib import Path
@@ -306,6 +307,11 @@ def generate_abs_compiled_model_with_data(device, ov_type, numpy_dtype):
def create_filename_for_test(test_name):
python_version = str(sys.version_info.major) + "_" + str(sys.version_info.minor)
filename = test_name.replace("test_", "").replace("[", "_").replace("]", "_")
+ # remove illegal characters
+ if os.name == "nt":
+ filename = re.sub(r"[\\/:*?\"<>|]", "", filename)
+ elif os.name == "posix":
+ filename = re.sub(r"[/]", "", filename)
filename = filename + "_" + python_version
return filename
diff --git a/src/cmake/openvino.cmake b/src/cmake/openvino.cmake
index a59243a2b99..2daae3ba293 100644
--- a/src/cmake/openvino.cmake
+++ b/src/cmake/openvino.cmake
@@ -49,11 +49,14 @@ target_include_directories(${TARGET_NAME} INTERFACE
$<BUILD_INTERFACE:${OpenVINO_SOURCE_DIR}/src/frontends/tensorflow/include>
$<BUILD_INTERFACE:${OpenVINO_SOURCE_DIR}/src/frontends/tensorflow_lite/include>)
-target_link_libraries(${TARGET_NAME} PRIVATE openvino::reference
- openvino::shape_inference
- openvino::pugixml
- ${CMAKE_DL_LIBS}
- Threads::Threads)
+target_link_libraries(${TARGET_NAME}
+ PRIVATE openvino::reference
+ openvino::shape_inference
+ openvino::pugixml
+ ${CMAKE_DL_LIBS}
+ Threads::Threads
+ PUBLIC $<$<AND:$<CXX_COMPILER_ID:GNU>,$<VERSION_LESS:$<CXX_COMPILER_VERSION>,9.1>>:stdc++fs>
+ $<$<AND:$<CXX_COMPILER_ID:Clang>,$<VERSION_LESS:$<CXX_COMPILER_VERSION>,9.0>>:c++fs>)
if (TBBBIND_2_5_FOUND)
target_link_libraries(${TARGET_NAME} PRIVATE ${TBBBIND_2_5_IMPORTED_TARGETS})
diff --git a/src/common/util/CMakeLists.txt b/src/common/util/CMakeLists.txt
index c539913968f..ce353d60887 100644
--- a/src/common/util/CMakeLists.txt
+++ b/src/common/util/CMakeLists.txt
@@ -37,36 +37,12 @@ add_library(${TARGET_NAME} STATIC ${LIBRARY_SRC} ${PUBLIC_HEADERS})
add_library(openvino::util ALIAS ${TARGET_NAME})
set_target_properties(${TARGET_NAME} PROPERTIES EXPORT_NAME util)
-file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/main.cpp
- "#include <experimental/filesystem>\nint main(int argc, char ** argv) {\n std::experimental::filesystem::path p(argv[0]);\n return p.string().length();\n}")
-
-try_compile(STD_FS_NO_LIB_NEEDED ${CMAKE_CURRENT_BINARY_DIR}
- SOURCES ${CMAKE_CURRENT_BINARY_DIR}/main.cpp
- COMPILE_DEFINITIONS -std=c++11)
-try_compile(STD_FS_NEEDS_STDCXXFS ${CMAKE_CURRENT_BINARY_DIR}
- SOURCES ${CMAKE_CURRENT_BINARY_DIR}/main.cpp
- COMPILE_DEFINITIONS -std=c++11
- LINK_LIBRARIES stdc++fs)
-try_compile(STD_FS_NEEDS_CXXFS ${CMAKE_CURRENT_BINARY_DIR}
- SOURCES ${CMAKE_CURRENT_BINARY_DIR}/main.cpp
- COMPILE_DEFINITIONS -std=c++11
- LINK_LIBRARIES c++fs)
-
-if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
- message(STATUS "MSVC - No explicit filesystem linker setting required.")
-elseif(STD_FS_NEEDS_STDCXXFS)
- message(STATUS "STD_FS_NEEDS_STDCXXFS - Add explicit filesystem linker setting: 'stdc++fs'.")
- set(STD_FS_LIB stdc++fs)
-elseif(STD_FS_NEEDS_CXXFS)
- message(STATUS "STD_FS_NEEDS_CXXFS - Add explicit filesystem linker setting: 'c++fs'.")
- set(STD_FS_LIB c++fs)
-elseif(STD_FS_NO_LIB_NEEDED)
- message(STATUS "STD_FS_NO_LIB_NEEDED - No explicit filesystem linker setting required.")
-else()
- message(WARNING "Unknown C++ build setup - No explicit filesystem linker setting set")
-endif()
+target_link_libraries(${TARGET_NAME}
+ PRIVATE ${CMAKE_DL_LIBS}
+ PUBLIC openvino::pugixml
+ $<$<AND:$<CXX_COMPILER_ID:GNU>,$<VERSION_LESS:$<CXX_COMPILER_VERSION>,9.1>>:stdc++fs>
+ $<$<AND:$<CXX_COMPILER_ID:Clang>,$<VERSION_LESS:$<CXX_COMPILER_VERSION>,9.0>>:c++fs>)
-target_link_libraries(${TARGET_NAME} PRIVATE ${CMAKE_DL_LIBS} PUBLIC openvino::pugixml ${STD_FS_LIB})
if (WIN32)
target_link_libraries(${TARGET_NAME} PRIVATE Shlwapi)
endif()
diff --git a/src/common/util/include/openvino/util/file_util.hpp b/src/common/util/include/openvino/util/file_util.hpp
index 0690d098d4e..506197cd986 100644
--- a/src/common/util/include/openvino/util/file_util.hpp
+++ b/src/common/util/include/openvino/util/file_util.hpp
@@ -131,11 +131,11 @@ std::string get_absolute_file_path(const std::string& path);
bool is_absolute_file_path(const std::string& path);
/**
- * @brief Interface function to create directorty recursively by given path
+ * @brief Interface function to create directories recursively by given path
* @param path - path to file, can be relative to current working directory
* @throw runtime_error if any error occurred
*/
-void create_directory_recursive(const std::string& path);
+void create_directory_recursive(const std::filesystem::path& path);
#ifdef OPENVINO_ENABLE_UNICODE_PATH_SUPPORT
/**
diff --git a/src/common/util/src/file_util.cpp b/src/common/util/src/file_util.cpp
index 3d3505d80bd..94cb311774b 100644
--- a/src/common/util/src/file_util.cpp
+++ b/src/common/util/src/file_util.cpp
@@ -287,41 +287,23 @@ bool ov::util::is_absolute_file_path(const std::string& path) {
#ifdef OPENVINO_ENABLE_UNICODE_PATH_SUPPORT
void ov::util::create_directory_recursive(const std::wstring& path) {
- if (path.empty() || directory_exists(path)) {
- return;
- }
-
- std::size_t pos = path.rfind(ov::util::FileTraits<wchar_t>::file_separator);
- if (pos != std::wstring::npos) {
- create_directory_recursive(path.substr(0, pos));
- }
-
- int err = wmakedir(path);
- if (err != 0 && errno != EEXIST) {
- std::stringstream ss;
- // TODO: in case of exception it may be needed to remove all created sub-directories
- ss << "Couldn't create directory [" << ov::util::wstring_to_string(path) << "], err=" << strerror(errno) << ")";
- throw std::runtime_error(ss.str());
- }
+ create_directory_recursive(std::filesystem::path{path});
}
#endif
-void ov::util::create_directory_recursive(const std::string& path) {
- if (path.empty() || directory_exists(path)) {
- return;
+void ov::util::create_directory_recursive(const std::filesystem::path& path) {
+ namespace fs = std::filesystem;
+ auto dir_path = fs::weakly_canonical(path);
+ if (!dir_path.has_filename() || dir_path.has_extension()) {
+ dir_path = get_directory(dir_path);
}
- std::size_t pos = path.rfind(ov::util::FileTraits<char>::file_separator);
- if (pos != std::string::npos) {
- create_directory_recursive(path.substr(0, pos));
- }
-
- int err = makedir(path);
- if (err != 0 && errno != EEXIST) {
- std::stringstream ss;
- // TODO: in case of exception it may be needed to remove all created sub-directories
- ss << "Couldn't create directory [" << path << "], err=" << strerror(errno) << ")";
- throw std::runtime_error(ss.str());
+ if (!dir_path.empty() && !directory_exists(dir_path)) {
+ if (std::error_code ec; !fs::create_directories(dir_path, ec)) {
+ std::stringstream ss;
+ ss << "Couldn't create directory [" << dir_path << "], err=" << ec.message() << ")";
+ throw std::runtime_error(ss.str());
+ }
}
}
diff --git a/src/core/include/openvino/core/graph_util.hpp b/src/core/include/openvino/core/graph_util.hpp
index d77a3329bb1..916d2287eca 100644
--- a/src/core/include/openvino/core/graph_util.hpp
+++ b/src/core/include/openvino/core/graph_util.hpp
@@ -292,29 +292,24 @@ bool replace_node_update_name(const std::shared_ptr<Node>& target, const std::sh
/// \{
OPENVINO_API
void serialize(const std::shared_ptr<const ov::Model>& m,
- const std::string& xml_path,
- const std::string& bin_path = "",
+ const std::filesystem::path& xml_path,
+ const std::filesystem::path& bin_path = {},
ov::pass::Serialize::Version version = ov::pass::Serialize::Version::UNSPECIFIED);
-
-template <class Path, std::enable_if_t<std::is_same_v<Path, std::filesystem::path>>* = nullptr>
-void serialize(const std::shared_ptr<const ov::Model>& m,
- const Path& xml_path,
- const Path& bin_path = {""},
- ov::pass::Serialize::Version version = ov::pass::Serialize::Version::UNSPECIFIED) {
- serialize(m, xml_path.string(), bin_path.string(), version);
-}
/// \}
/// \brief Save given model into IR. Floating point weights are compressed to FP16 by default.
/// This method saves a model to IR applying all necessary transformations that usually applied
-/// in model conversion flow provided by OVC tool. Particularly, floating point weights are compressed to FP16.
+/// in model conversion flow provided by OVC tool. Particularly, floating point weights are compressed to
+/// FP16.
/// \param model Model which will be converted to IR representation.
-/// \param output_model Path to the output model file, must have extension .xml
-/// \param compress_to_fp16 Whether to compress floating point weights to FP16 (true by default)
+/// \param output_model Path to the output model file, must have extension .xml.
+/// \param compress_to_fp16 Whether to compress floating point weights to FP16 (true by default).
+/// /// \{
OPENVINO_API
void save_model(const std::shared_ptr<const ov::Model>& model,
- const std::string& output_model,
+ const std::filesystem::path& output_model,
bool compress_to_fp16 = true);
+
#if defined(OPENVINO_ENABLE_UNICODE_PATH_SUPPORT)
OPENVINO_API
void save_model(const std::shared_ptr<const ov::Model>& model,
@@ -322,8 +317,6 @@ void save_model(const std::shared_ptr<const ov::Model>& model,
bool compress_to_fp16 = true);
#endif
-template <class Path, std::enable_if_t<std::is_same_v<Path, std::filesystem::path>>* = nullptr>
-void save_model(const std::shared_ptr<const ov::Model>& model, const Path& output_model, bool compress_to_fp16 = true) {
- save_model(model, output_model.string(), compress_to_fp16);
-}
+/// \}
+
} // namespace ov
diff --git a/src/core/include/openvino/pass/serialize.hpp b/src/core/include/openvino/pass/serialize.hpp
index 21a1a2ae263..5bf2721463f 100644
--- a/src/core/include/openvino/pass/serialize.hpp
+++ b/src/core/include/openvino/pass/serialize.hpp
@@ -34,18 +34,15 @@ public:
Serialize(std::ostream& xmlFile, std::ostream& binFile, Version version = Version::UNSPECIFIED);
- Serialize(const std::string& xmlPath, const std::string& binPath, Version version = Version::UNSPECIFIED);
-
Serialize(const std::filesystem::path& xmlPath,
const std::filesystem::path& binPath,
- Version version = Version::UNSPECIFIED)
- : Serialize(xmlPath.string(), binPath.string(), version) {}
+ Version version = Version::UNSPECIFIED);
private:
std::ostream* m_xmlFile;
std::ostream* m_binFile;
- const std::string m_xmlPath;
- const std::string m_binPath;
+ const std::filesystem::path m_xmlPath;
+ const std::filesystem::path m_binPath;
const Version m_version;
const std::map<std::string, ov::OpSet> m_custom_opsets;
};
diff --git a/src/core/src/graph_util.cpp b/src/core/src/graph_util.cpp
index d2944f28982..309ef3dc74d 100644
--- a/src/core/src/graph_util.cpp
+++ b/src/core/src/graph_util.cpp
@@ -304,15 +304,17 @@ bool replace_node_update_name(const std::shared_ptr<Node>& target, const std::sh
}
void serialize(const std::shared_ptr<const ov::Model>& m,
- const std::string& xml_path,
- const std::string& bin_path,
+ const std::filesystem::path& xml_path,
+ const std::filesystem::path& bin_path,
ov::pass::Serialize::Version version) {
ov::pass::Manager manager("Serialize");
manager.register_pass<ov::pass::Serialize>(xml_path, bin_path, version);
manager.run_passes(std::const_pointer_cast<ov::Model>(m));
}
-void save_model(const std::shared_ptr<const ov::Model>& m, const std::string& output_model, bool compress_to_fp16) {
+void save_model(const std::shared_ptr<const ov::Model>& m,
+ const std::filesystem::path& output_model,
+ bool compress_to_fp16) {
auto cloned = m->clone();
if (compress_to_fp16) {
// TODO: Implement on-the-fly compression in pass::Serialize, Ticket: 145380
diff --git a/src/core/src/pass/serialize.cpp b/src/core/src/pass/serialize.cpp
index a56f124cc57..825c0359335 100644
--- a/src/core/src/pass/serialize.cpp
+++ b/src/core/src/pass/serialize.cpp
@@ -1253,26 +1253,22 @@ void ngfunction_2_ir(pugi::xml_node& netXml,
}
}
-std::string valid_xml_path(const std::string& path) {
- OPENVINO_ASSERT(path.length() > 4, "Path for xml file is too short: \"" + path + "\"");
-
- const char* const extension = ".xml";
- const bool has_xml_extension = path.rfind(extension) == path.size() - std::strlen(extension);
- OPENVINO_ASSERT(has_xml_extension,
- "Path for xml file doesn't contains file name with 'xml' extension: \"" + path + "\"");
+const std::filesystem::path valid_xml_path(const std::filesystem::path& path) {
+ OPENVINO_ASSERT(path.extension() == ".xml",
+ "Path for xml file doesn't contains file name with 'xml' extension: \"",
+ path,
+ "\"");
return path;
}
-std::string provide_bin_path(const std::string& xmlPath, const std::string& binPath) {
- if (!binPath.empty()) {
- return binPath;
+std::filesystem::path provide_bin_path(const std::filesystem::path& xml_path, const std::filesystem::path& bin_path) {
+ if (bin_path.empty()) {
+ auto path = xml_path;
+ path.replace_extension(".bin");
+ return path;
+ } else {
+ return bin_path;
}
- assert(xmlPath.size() > 4); // should be check by valid_xml_path
- std::string bestPath = xmlPath;
- const char* const extension = "bin";
- const auto ext_size = std::strlen(extension);
- bestPath.replace(bestPath.size() - ext_size, ext_size, extension);
- return bestPath;
}
void serializeFunc(std::ostream& xml_file,
@@ -1327,27 +1323,14 @@ bool pass::Serialize::run_on_model(const std::shared_ptr<ov::Model>& model) {
if (m_xmlFile && m_binFile) {
serializeFunc(*m_xmlFile, *m_binFile, model, m_version);
} else {
-#if defined(OPENVINO_ENABLE_UNICODE_PATH_SUPPORT) && defined(_WIN32)
- const auto& xmlPath_ref = ov::util::string_to_wstring(m_xmlPath);
- const auto& binPath_ref = ov::util::string_to_wstring(m_binPath);
- std::string message_bin = "Can't open bin file.";
- std::string message_xml = "Can't open xml file.";
-#else
- const auto& xmlPath_ref = m_xmlPath;
- const auto& binPath_ref = m_binPath;
- std::string message_bin = "Can't open bin file: \"" + binPath_ref + "\"";
- std::string message_xml = "Can't open xml file: \"" + xmlPath_ref + "\"";
-#endif
- auto xmlDir = ov::util::get_directory(xmlPath_ref);
- if (xmlDir != xmlPath_ref)
- ov::util::create_directory_recursive(xmlDir);
-
- std::ofstream bin_file(binPath_ref, std::ios::out | std::ios::binary);
- OPENVINO_ASSERT(bin_file, message_bin);
+ ov::util::create_directory_recursive(m_xmlPath);
+
+ std::ofstream bin_file(m_binPath, std::ios::binary);
+ OPENVINO_ASSERT(bin_file, "Can't open bin file: \"", m_binPath, "\"");
// create xml file
- std::ofstream xml_file(xmlPath_ref, std::ios::out);
- OPENVINO_ASSERT(xml_file, message_xml);
+ std::ofstream xml_file(m_xmlPath);
+ OPENVINO_ASSERT(xml_file, "Can't open xml file: \"", m_xmlPath, "\"");
try {
serializeFunc(xml_file, bin_file, model, m_version);
@@ -1357,8 +1340,8 @@ bool pass::Serialize::run_on_model(const std::shared_ptr<ov::Model>& model) {
// hence we need to delete it here in case of failure
xml_file.close();
bin_file.close();
- std::ignore = std::remove(m_xmlPath.c_str());
- std::ignore = std::remove(m_binPath.c_str());
+ std::ignore = std::filesystem::remove(m_xmlPath);
+ std::ignore = std::filesystem::remove(m_binPath);
throw;
}
}
@@ -1374,7 +1357,7 @@ pass::Serialize::Serialize(std::ostream& xmlFile, std::ostream& binFile, pass::S
m_binPath{},
m_version{version} {}
-pass::Serialize::Serialize(const std::string& xmlPath, const std::string& binPath, pass::Serialize::Version version)
+pass::Serialize::Serialize(const std::filesystem::path& xmlPath, const std::filesystem::path& binPath, Version version)
: m_xmlFile{nullptr},
m_binFile{nullptr},
m_xmlPath{valid_xml_path(xmlPath)},
diff --git a/src/inference/src/dev/core_impl.cpp b/src/inference/src/dev/core_impl.cpp
index 9e20d75f8f8..25a9bc88c60 100644
--- a/src/inference/src/dev/core_impl.cpp
+++ b/src/inference/src/dev/core_impl.cpp
@@ -1724,18 +1724,19 @@ ov::CoreConfig::CacheConfig ov::CoreConfig::get_cache_config_for_device(const ov
}
ov::CoreConfig::CacheConfig ov::CoreConfig::CacheConfig::create(const std::string& dir) {
- std::shared_ptr<ov::ICacheManager> cache_manager = nullptr;
-
+ CacheConfig cache_config{dir, nullptr};
if (!dir.empty()) {
-#ifdef OPENVINO_ENABLE_UNICODE_PATH_SUPPORT
- ov::util::create_directory_recursive(ov::util::string_to_wstring(dir));
-#else
- ov::util::create_directory_recursive(dir);
-#endif
- cache_manager = std::make_shared<ov::FileStorageCacheManager>(dir);
+ if constexpr (std::is_same_v<std::filesystem::path::value_type, std::wstring::value_type>) {
+ // if path native type is same as wstring type (e.g. Windows) convert to wstring
+ // in case of string has unicode without conversion wrong created dir may have incorrect name
+ // should be removed if cache_dir will path instead of string
+ ov::util::create_directory_recursive(ov::util::string_to_wstring(dir));
+ } else {
+ ov::util::create_directory_recursive(dir);
+ }
+ cache_config._cacheManager = std::make_shared<ov::FileStorageCacheManager>(dir);
}
-
- return {dir, std::move(cache_manager)};
+ return cache_config;
}
std::mutex& ov::CoreImpl::get_mutex(const std::string& dev_name) const {
diff --git a/src/tests/functional/plugin/conformance/test_runner/op_conformance_runner/include/utils/models.hpp b/src/tests/functional/plugin/conformance/test_runner/op_conformance_runner/include/utils/models.hpp
index a86b128d602..7cd15808ff4 100644
--- a/src/tests/functional/plugin/conformance/test_runner/op_conformance_runner/include/utils/models.hpp
+++ b/src/tests/functional/plugin/conformance/test_runner/op_conformance_runner/include/utils/models.hpp
@@ -20,7 +20,7 @@ inline std::string get_ref_path(const std::string& model_path) {
return "";
}
if (!ov::util::directory_exists(conformance::refCachePath)) {
- ov::util::create_directory_recursive(conformance::refCachePath);
+ ov::util::create_directory_recursive(std::filesystem::path{conformance::refCachePath});
}
std::string path_to_cache = conformance::refCachePath + std::string(ov::test::utils::FileSeparator);
std::string ref_name = model_path.substr(model_path.rfind(ov::test::utils::FileSeparator) + 1);
@@ -88,4 +88,4 @@ get_model_paths(const std::vector<std::string>& conformance_ir_paths,
} // namespace op_conformance
} // namespace test
-} // namespace ov
\ No newline at end of file
+} // namespace ov
diff --git a/src/tests/test_utils/functional_test_utils/src/summary/api_summary.cpp b/src/tests/test_utils/functional_test_utils/src/summary/api_summary.cpp
index 26de08f4fd2..0fe68cdf2cf 100644
--- a/src/tests/test_utils/functional_test_utils/src/summary/api_summary.cpp
+++ b/src/tests/test_utils/functional_test_utils/src/summary/api_summary.cpp
@@ -152,7 +152,7 @@ void ApiSummary::saveReport() {
filename += ov::test::utils::REPORT_EXTENSION;
if (!ov::util::directory_exists(outputFolder)) {
- ov::util::create_directory_recursive(outputFolder);
+ ov::util::create_directory_recursive(std::filesystem::path{outputFolder});
}
std::string outputFilePath = outputFolder + std::string(ov::test::utils::FileSeparator) + filename;
diff --git a/src/tests/test_utils/functional_test_utils/src/summary/op_summary.cpp b/src/tests/test_utils/functional_test_utils/src/summary/op_summary.cpp
index 67d5ccf6e0c..43ee1d5b30c 100644
--- a/src/tests/test_utils/functional_test_utils/src/summary/op_summary.cpp
+++ b/src/tests/test_utils/functional_test_utils/src/summary/op_summary.cpp
@@ -237,7 +237,7 @@ void OpSummary::saveReport() {
filename += ov::test::utils::REPORT_EXTENSION;
if (!ov::util::directory_exists(outputFolder)) {
- ov::util::create_directory_recursive(outputFolder);
+ ov::util::create_directory_recursive(std::filesystem::path{outputFolder});
}
std::string outputFilePath = outputFolder + std::string(ov::test::utils::FileSeparator) + filename;