Skip to content

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

[Paddle FE] Migrate the InputModel class to use std::filesystem::path (#35423)

Alicja Miloszewska124ef35fb5eb
diff --git a/src/frontends/paddle/src/frontend.cpp b/src/frontends/paddle/src/frontend.cpp
index 47bca7c0067..b9299f20e8a 100644
--- a/src/frontends/paddle/src/frontend.cpp
+++ b/src/frontends/paddle/src/frontend.cpp
@@ -151,7 +151,7 @@ std::istream* variant_to_stream_ptr(const ov::Any& variant, std::fstream& fs, st
FRONT_END_INITIALIZATION_CHECK(ss && ss.good(), "Cannot open ov::tensor.");
return &ss;
} else if (const auto path = ov::frontend::get_path_from_any(variant)) {
- fs.open(path.value(), std::ios::in | std::ifstream::binary);
+ fs.open(*path, std::ios::in | std::ifstream::binary);
FRONT_END_INITIALIZATION_CHECK(fs && fs.is_open(), "Cannot open model file.");
return &fs;
}
@@ -372,8 +372,8 @@ bool FrontEnd::supported_impl(const std::vector<ov::Any>& variants) const {
return false;
// Validating first path, it must contain a model
- if (const auto path = ov::frontend::get_path_from_any(variants[0])) {
- std::filesystem::path model_path = path.value();
+ if (auto path = ov::frontend::get_path_from_any(variants[0])) {
+ std::filesystem::path model_path = std::move(*path);
if (ov::util::directory_exists(model_path)) {
model_path /= "__model__";
FRONT_END_GENERAL_CHECK(util::file_exists(model_path), "Could not open the file: ", model_path);
@@ -411,7 +411,7 @@ InputModel::Ptr FrontEnd::load_impl(const std::vector<ov::Any>& variants) const
if (variants.size() == 1 + extra_variants_num) {
// The case when folder with __model__ and weight files is provided or .pdmodel file
if (const auto path = ov::frontend::get_path_from_any(variants[0])) {
- return std::make_shared<InputModel>(path.value().native(), m_telemetry);
+ return std::make_shared<InputModel>(*path, m_telemetry);
}
// The case with only model stream provided and no weights. This means model has
// no learnable weights
diff --git a/src/frontends/paddle/src/input_model.cpp b/src/frontends/paddle/src/input_model.cpp
index dddbbacc690..a0c33ecdd19 100644
--- a/src/frontends/paddle/src/input_model.cpp
+++ b/src/frontends/paddle/src/input_model.cpp
@@ -4,10 +4,8 @@
#include "input_model.hpp"
+#include <filesystem>
#include <fstream>
-#if defined(__MINGW32__) || defined(__MINGW64__)
-# include <filesystem>
-#endif
#include <limits>
#include <queue>
@@ -31,8 +29,7 @@ using namespace ::paddle::framework::proto;
class InputModel::InputModelImpl {
public:
- template <typename T>
- InputModelImpl(const std::basic_string<T>& path,
+ InputModelImpl(const std::filesystem::path& path,
const InputModel& input_model,
const std::shared_ptr<TelemetryExtension>& telemetry);
InputModelImpl(const std::vector<std::istream*>& streams,
@@ -63,8 +60,7 @@ public:
private:
void load_places();
- template <typename T>
- void load_consts(const std::basic_string<T>& folder_with_weights);
+ void load_consts(const std::filesystem::path& folder_with_weights);
void load_consts(std::istream* weight_stream);
void create_temp_consts();
std::vector<std::shared_ptr<OpPlace>> determine_cut_nodes() const;
@@ -187,67 +183,26 @@ ov::Shape make_shape_checked(const DimsT& dims) {
return shape;
}
-template <typename T>
-std::basic_string<T> get_const_path(const std::basic_string<T>& folder_with_weights, const std::string& name) {
- return folder_with_weights + paddle::get_path_sep<T>() + name;
+std::filesystem::path get_const_path(const std::filesystem::path& folder_with_weights, const std::string& name) {
+ return folder_with_weights / ov::util::make_path(name);
}
-#if defined(OPENVINO_ENABLE_UNICODE_PATH_SUPPORT) && defined(_WIN32)
-template <>
-std::basic_string<wchar_t> get_const_path(const std::basic_string<wchar_t>& folder, const std::string& name) {
- return folder + paddle::get_path_sep<wchar_t>() + ov::util::string_to_wstring(name);
+bool is_pdmodel(const std::filesystem::path& path) {
+ return path.extension() == ".pdmodel";
}
-#endif
-template <typename T>
-bool is_pdmodel(const std::basic_string<T>& path) {
- std::string ext = ".pdmodel";
- return ov::util::ends_with(path, ext);
-}
-
-#if defined(OPENVINO_ENABLE_UNICODE_PATH_SUPPORT) && defined(_WIN32)
-template <>
-bool is_pdmodel(const std::basic_string<wchar_t>& path) {
- std::wstring ext = L".pdmodel";
- return ov::util::ends_with(path, ext);
-}
-#endif
-
-template <typename T>
-std::basic_string<T> get_model_path(const std::basic_string<T>& path, std::ifstream* weights_stream) {
- std::string model_file{path};
- std::string ext = ".pdmodel";
- if (ov::util::ends_with(model_file, ext)) {
- std::string params_ext = ".pdiparams";
- std::string weights_file{path};
- weights_file.replace(weights_file.size() - ext.size(), ext.size(), params_ext);
+std::filesystem::path get_model_path(std::filesystem::path model_file, std::ifstream* weights_stream) {
+ if (is_pdmodel(model_file)) {
+ auto weights_file = model_file;
+ weights_file.replace_extension(".pdiparams");
weights_stream->open(weights_file, std::ios::binary);
// Don't throw error if file isn't opened
// It may mean that model don't have constants
} else {
- model_file += paddle::get_path_sep<T>() + "__model__";
+ model_file = model_file / "__model__";
}
return model_file;
}
-
-#if defined(OPENVINO_ENABLE_UNICODE_PATH_SUPPORT) && defined(_WIN32)
-template <>
-std::basic_string<wchar_t> get_model_path(const std::basic_string<wchar_t>& path, std::ifstream* weights_stream) {
- std::wstring model_file{path};
- std::wstring ext = L".pdmodel";
- if (ov::util::ends_with(model_file, ext)) {
- std::wstring params_ext = L".pdiparams";
- std::wstring weights_file{path};
- weights_file.replace(weights_file.size() - ext.size(), ext.size(), params_ext);
- weights_stream->open(weights_file.c_str(), std::ios::binary);
- // Don't throw error if file isn't opened
- // It may mean that model don't have constants
- } else {
- model_file += paddle::get_path_sep<wchar_t>() + L"__model__";
- }
- return model_file;
-}
-#endif
} // namespace
std::vector<std::shared_ptr<OpPlace>> InputModel::InputModelImpl::get_op_places(const int32_t blck_idx) const {
@@ -299,8 +254,7 @@ std::vector<std::shared_ptr<OpPlace>> InputModel::InputModelImpl::determine_cut_
}
// load_consts with folder is compatible with old PaddlePaddle API.
-template <typename T>
-void InputModel::InputModelImpl::load_consts(const std::basic_string<T>& folder_with_weights) {
+void InputModel::InputModelImpl::load_consts(const std::filesystem::path& folder_with_weights) {
for (const auto& item : m_var_places) {
const auto& var_desc = item.second->get_desc();
const auto& name = item.first;
@@ -319,12 +273,7 @@ void InputModel::InputModelImpl::load_consts(const std::basic_string<T>& folder_
bool read_succeed = false;
if (!folder_with_weights.empty()) {
-#if defined(__MINGW32__) || defined(__MINGW64__)
- std::ifstream is(std::filesystem::path(get_const_path(folder_with_weights, name)),
- std::ios::in | std::ifstream::binary);
-#else
std::ifstream is(get_const_path(folder_with_weights, name), std::ios::in | std::ifstream::binary);
-#endif
FRONT_END_GENERAL_CHECK(is && is.is_open(), "Cannot open file for constant value.");
const size_t header_size = 16;
std::vector<char> header(header_size);
@@ -438,20 +387,16 @@ void InputModel::InputModelImpl::load_consts(std::istream* weight_stream) {
read *.pdmodel as model stream.
read *.pdiparam as weight stream.
*/
-template <typename T>
-InputModel::InputModelImpl::InputModelImpl(const std::basic_string<T>& path,
+InputModel::InputModelImpl::InputModelImpl(const std::filesystem::path& path,
const InputModel& input_model,
const std::shared_ptr<TelemetryExtension>& telemetry)
: m_fw_ptr{std::make_shared<ProgramDesc>()},
m_input_model(input_model),
m_telemetry(telemetry) {
std::ifstream weights_stream;
- std::ifstream pb_stream(get_model_path<T>(path, &weights_stream).c_str(), std::ios::in | std::ifstream::binary);
+ std::ifstream pb_stream(get_model_path(path, &weights_stream), std::ios::in | std::ifstream::binary);
- FRONT_END_GENERAL_CHECK(pb_stream && pb_stream.is_open(),
- "Could not open the file: \"",
- util::path_to_string(path),
- '"');
+ FRONT_END_GENERAL_CHECK(pb_stream && pb_stream.is_open(), "Could not open the file: ", path);
FRONT_END_GENERAL_CHECK(m_fw_ptr->ParseFromIstream(&pb_stream), "Model can't be parsed");
// According to Paddle, the saved model has the framework version
// For example Paddle 2.1.0 is encoded as 2001000. 0 means the latest framework.
@@ -625,13 +570,8 @@ void InputModel::InputModelImpl::set_tensor_value(Place::Ptr place, const void*
m_tensor_values[name] = constant;
}
-InputModel::InputModel(const std::string& path, const std::shared_ptr<TelemetryExtension>& telemetry)
- : _impl{std::make_shared<InputModelImpl>(path, *this, telemetry)} {}
-
-#if defined(OPENVINO_ENABLE_UNICODE_PATH_SUPPORT) && defined(_WIN32)
-InputModel::InputModel(const std::wstring& path, const std::shared_ptr<TelemetryExtension>& telemetry)
+InputModel::InputModel(const std::filesystem::path& path, const std::shared_ptr<TelemetryExtension>& telemetry)
: _impl{std::make_shared<InputModelImpl>(path, *this, telemetry)} {}
-#endif
InputModel::InputModel(const std::vector<std::istream*>& streams, const std::shared_ptr<TelemetryExtension>& telemetry)
: _impl{std::make_shared<InputModelImpl>(streams, *this, telemetry)} {}
diff --git a/src/frontends/paddle/src/input_model.hpp b/src/frontends/paddle/src/input_model.hpp
index 4bdd6c58c68..b1fdacf36c3 100644
--- a/src/frontends/paddle/src/input_model.hpp
+++ b/src/frontends/paddle/src/input_model.hpp
@@ -4,6 +4,8 @@
#pragma once
+#include <filesystem>
+
#include "openvino/frontend/extension/telemetry.hpp"
#include "openvino/frontend/paddle/frontend.hpp"
@@ -16,10 +18,7 @@ class TensorPlace;
class InputModel : public ov::frontend::InputModel {
public:
- explicit InputModel(const std::string& path, const std::shared_ptr<TelemetryExtension>& telemetry = {});
-#if defined(OPENVINO_ENABLE_UNICODE_PATH_SUPPORT) && defined(_WIN32)
- explicit InputModel(const std::wstring& path, const std::shared_ptr<TelemetryExtension>& telemetry = {});
-#endif
+ explicit InputModel(const std::filesystem::path& path, const std::shared_ptr<TelemetryExtension>& telemetry = {});
explicit InputModel(const std::vector<std::istream*>& streams,
const std::shared_ptr<TelemetryExtension>& telemetry = {});
std::vector<Place::Ptr> get_inputs() const override;
diff --git a/src/frontends/paddle/src/paddle_utils.hpp b/src/frontends/paddle/src/paddle_utils.hpp
index 6ef7966668a..5c8c8120b7d 100644
--- a/src/frontends/paddle/src/paddle_utils.hpp
+++ b/src/frontends/paddle/src/paddle_utils.hpp
@@ -10,26 +10,6 @@
namespace ov {
namespace frontend {
namespace paddle {
-#ifdef _WIN32
-const char PATH_SEPARATOR = '\\';
-# if defined(OPENVINO_ENABLE_UNICODE_PATH_SUPPORT)
-const wchar_t WPATH_SEPARATOR = L'\\';
-# endif
-#else
-const char PATH_SEPARATOR = '/';
-#endif
-
-template <typename T>
-inline std::basic_string<T> get_path_sep() {
- return std::basic_string<T>{PATH_SEPARATOR};
-}
-
-#if defined(OPENVINO_ENABLE_UNICODE_PATH_SUPPORT) && defined(_WIN32)
-template <>
-inline std::basic_string<wchar_t> get_path_sep() {
- return std::basic_string<wchar_t>{WPATH_SEPARATOR};
-}
-#endif
std::shared_ptr<Node> reorder_axes(const Output<Node>& value, std::vector<size_t> axes_order);
} // namespace paddle