OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[IR FE] Weight share ir fe mark weights (#34454)
Pawel Raaszf3c663ee8529
diff --git a/src/frontends/ir/src/frontend.cpp b/src/frontends/ir/src/frontend.cppindex 75f99c87cb7..1b874185917 100644--- a/src/frontends/ir/src/frontend.cpp+++ b/src/frontends/ir/src/frontend.cpp@@ -5,6 +5,7 @@#include "openvino/frontend/ir/frontend.hpp"#include <array>+#include <optional>#include <pugixml.hpp>#include <vector>@@ -91,6 +92,20 @@ size_t get_ir_version(std::istream& model) {return ir_version;}+std::optional<std::filesystem::path> get_path_from_any(const ov::Any& param) {+ if (param.is<std::filesystem::path>()) {+ return std::make_optional(param.as<std::filesystem::path>());+ } else if (param.is<std::string>()) {+ return std::make_optional(ov::util::make_path(param.as<std::string>()));+#if defined(OPENVINO_ENABLE_UNICODE_PATH_SUPPORT)+ } else if (param.is<std::wstring>()) {+ return std::make_optional(ov::util::make_path(param.as<std::wstring>()));+#endif+ } else {+ return std::nullopt;+ }+}+} // namespacebool FrontEnd::supported_impl(const std::vector<ov::Any>& variants) const {@@ -104,23 +119,15 @@ bool FrontEnd::supported_impl(const std::vector<ov::Any>& variants) const {return false;}- const auto& model_variant = variants[0];- if (model_variant.is<std::string>()) {- const auto& path = model_variant.as<std::string>();- validate_path(path);- local_model_stream.open(path, std::ios::in | std::ifstream::binary);-#if defined(OPENVINO_ENABLE_UNICODE_PATH_SUPPORT) && defined(_WIN32)- } else if (model_variant.is<std::wstring>()) {- const auto& path = model_variant.as<std::wstring>();- validate_path(path);- local_model_stream.open(path.c_str(), std::ios::in | std::ifstream::binary);-#endif- } else if (model_variant.is<std::istream*>()) {+ if (const auto& model_variant = variants[0]; model_variant.is<std::istream*>()) {provided_model_stream = model_variant.as<std::istream*>();} else if (model_variant.is<std::istringstream*>()) {provided_model_stream = model_variant.as<std::istringstream*>();} else if (model_variant.is<std::shared_ptr<AlignedBuffer>>()) {model_buffer = model_variant.as<std::shared_ptr<AlignedBuffer>>();+ } else if (const auto path = get_path_from_any(model_variant); path) {+ validate_path(*path);+ local_model_stream.open(*path, std::ios::in | std::ifstream::binary);}if (provided_model_stream && local_model_stream.is_open()) {@@ -188,100 +195,65 @@ InputModel::Ptr FrontEnd::load_impl(const std::vector<ov::Any>& variants) constreturn nullptr;};-#if defined(OPENVINO_ENABLE_UNICODE_PATH_SUPPORT) && defined(_WIN32)- std::wstring weights_path, model_path;-#else- std::string weights_path, model_path;-#endif-- const auto& model_variant = variants.at(0);+ std::filesystem::path weights_path, model_path;- if (model_variant.is<std::string>()) {- const auto& tmp_path = model_variant.as<std::string>();- validate_path(tmp_path);-#if defined(OPENVINO_ENABLE_UNICODE_PATH_SUPPORT) && defined(_WIN32)- model_path = ov::util::string_to_wstring(tmp_path.c_str());-#else- model_path = tmp_path;-#endif- local_model_stream.open(model_path.c_str(), std::ios::in | std::ifstream::binary);-#if defined(OPENVINO_ENABLE_UNICODE_PATH_SUPPORT) && defined(_WIN32)- } else if (model_variant.is<std::wstring>()) {- model_path = model_variant.as<std::wstring>();- validate_path(model_path);- local_model_stream.open(model_path.c_str(), std::ios::in | std::ifstream::binary);-#endif- } else if (model_variant.is<std::istream*>()) {+ if (const auto& model_variant = variants[0]; model_variant.is<std::istream*>()) {provided_model_stream = model_variant.as<std::istream*>();} else if (model_variant.is<std::istringstream*>()) {provided_model_stream = model_variant.as<std::istringstream*>();} else if (model_variant.is<std::shared_ptr<AlignedBuffer>>()) {model_buf = model_variant.as<std::shared_ptr<AlignedBuffer>>();+ } else if (auto path = get_path_from_any(model_variant); path) {+ model_path = std::move(*path);+ validate_path(model_path);+ local_model_stream.open(model_path, std::ios::in | std::ifstream::binary);}// Check weights and extensionsfor (size_t variant_id = 1; variant_id < variants.size(); ++variant_id) {- const auto& variant = variants.at(variant_id);- if (variant.is<std::string>()) {- const auto& tmp_path = variant.as<std::string>();-#if defined(OPENVINO_ENABLE_UNICODE_PATH_SUPPORT) && defined(_WIN32)- weights_path = ov::util::string_to_wstring(tmp_path.c_str());-#else- weights_path = tmp_path;-#endif-#if defined(OPENVINO_ENABLE_UNICODE_PATH_SUPPORT) && defined(_WIN32)- } else if (variant.is<std::wstring>()) {- weights_path = variant.as<std::wstring>();-#endif- } else if (variant.is<std::shared_ptr<ov::AlignedBuffer>>()) {+ if (const auto& variant = variants.at(variant_id); variant.is<std::shared_ptr<ov::AlignedBuffer>>()) {weights = variant.as<std::shared_ptr<ov::AlignedBuffer>>();+ } else if (auto path = get_path_from_any(variant); path) {+ weights_path = std::move(*path);}}- bool enable_mmap = variants[variants.size() - 1].is<bool>() ? variants[variants.size() - 1].as<bool>() : false;// Find weights if only path to xml was providedif (weights_path.empty()) {- auto pos = model_path.rfind('.');- if (pos != model_path.npos)- weights_path = model_path.substr(0, pos);--#if defined(OPENVINO_ENABLE_UNICODE_PATH_SUPPORT) && defined(_WIN32)- weights_path += L".bin";-#else- weights_path += ".bin";-#endif+ weights_path = model_path;+ weights_path.replace_extension(".bin");+if (!ov::util::file_exists(weights_path)) {weights_path.clear();}}+if (!weights_path.empty()) {+ const auto enable_mmap = variants.back().is<bool>() ? variants.back().as<bool>() : false;if (enable_mmap) {- auto mapped_memory = ov::load_mmap_object(ov::util::make_path(weights_path));+ auto mapped_memory = ov::load_mmap_object(weights_path);weights = std::make_shared<ov::SharedBuffer<std::shared_ptr<MappedMemory>>>(mapped_memory->data(),mapped_memory->size(),mapped_memory);- } else {- std::ifstream bin_stream;- bin_stream.open(weights_path.c_str(), std::ios::binary);- if (!bin_stream.is_open())-#if defined(OPENVINO_ENABLE_UNICODE_PATH_SUPPORT) && defined(_WIN32)- OPENVINO_THROW("Weights file ", ov::util::wstring_to_string(weights_path), " cannot be opened!");-#else- OPENVINO_THROW("Weights file ", weights_path, " cannot be opened!");-#endif-+ } else if (std::ifstream bin_stream(weights_path, std::ios::binary); bin_stream.is_open()) {bin_stream.seekg(0, std::ios::end);size_t file_size = bin_stream.tellg();bin_stream.seekg(0, std::ios::beg);auto aligned_weights_buffer = std::make_shared<ov::AlignedBuffer>(file_size);bin_stream.read(aligned_weights_buffer->get_ptr<char>(), aligned_weights_buffer->size());- bin_stream.close();weights = std::make_shared<ov::SharedBuffer<std::shared_ptr<ov::AlignedBuffer>>>(aligned_weights_buffer->get_ptr<char>(),aligned_weights_buffer->size(),- aligned_weights_buffer);+ aligned_weights_buffer,+ ov::create_base_descriptor(+ std::hash<std::decay_t<decltype(weights_path.native())>>{}(weights_path.native()),+ 0,+ aligned_weights_buffer));++ } else {+ OPENVINO_THROW("Weights file ", weights_path, " cannot be opened!");}}diff --git a/src/frontends/ir/tests/frontend_test.hpp b/src/frontends/ir/tests/frontend_test.hppindex 4b988db2fc5..bb51e1683f7 100644--- a/src/frontends/ir/tests/frontend_test.hpp+++ b/src/frontends/ir/tests/frontend_test.hpp@@ -17,34 +17,30 @@ protected:ov::Core core;ov::frontend::FrontEndManager manager;- std::string xmlFileName{};- std::string binFileName{};+ std::filesystem::path prefix = ov::test::utils::generateTestFilePrefix();+ std::filesystem::path xmlFileName = std::filesystem::path(prefix).concat("_IrFrontendTestModel.xml");+ std::filesystem::path binFileName = std::filesystem::path(prefix).concat("_IrFrontendTestModel.bin");void createTemporalModelFile(std::string xmlFileContent,std::vector<unsigned char> binFileContent = std::vector<unsigned char>()) {ASSERT_TRUE(xmlFileContent.size() > 0);- auto filePrefix = ov::test::utils::generateTestFilePrefix();- xmlFileName = filePrefix + "_IrFrontendTestModel.xml";- binFileName = filePrefix + "_IrFrontendTestModel.bin";- {- std::ofstream xmlFile;- xmlFile.open(xmlFileName);- xmlFile << xmlFileContent;- xmlFile.close();+ if (std::ofstream xml_file(xmlFileName); xml_file.is_open()) {+ xml_file << xmlFileContent;}- if (binFileContent.size() > 0) {- std::ofstream binFile;- binFile.open(binFileName, std::ios::binary);- binFile.write((const char*)binFileContent.data(), binFileContent.size());- binFile.close();+ if (std::ofstream bin_file(binFileName, std::ios::binary); bin_file.is_open()) {+ bin_file.write(reinterpret_cast<const char*>(binFileContent.data()), binFileContent.size());}}void RemoveTemporalFiles() {- std::remove(xmlFileName.c_str());- std::remove(binFileName.c_str());+ if (std::filesystem::exists(xmlFileName)) {+ std::filesystem::remove(xmlFileName);+ }+ if (std::filesystem::exists(binFileName)) {+ std::filesystem::remove(binFileName);+ }}std::shared_ptr<ov::Model> getWithIRFrontend(const std::string& model) {diff --git a/src/frontends/ir/tests/frontend_test_basic.cpp b/src/frontends/ir/tests/frontend_test_basic.cppindex f96320e4326..44847915698 100644--- a/src/frontends/ir/tests/frontend_test_basic.cpp+++ b/src/frontends/ir/tests/frontend_test_basic.cpp@@ -4,6 +4,8 @@#include "common_test_utils/test_assertions.hpp"#include "frontend_test.hpp"+#include "openvino/core/graph_util.hpp"+#include "openvino/core/weight_sharing_util.hpp"#include "openvino/op/add.hpp"#include "openvino/op/proposal.hpp"#include "openvino/op/shape_of.hpp"@@ -568,6 +570,32 @@ TEST_P(IRFrontendMMapTests, model_with_lp_weights_reading_from_disk) {}}+TEST_P(IRFrontendMMapTests, read_model_get_weights_map) {+ {+ auto p1 = std::make_shared<ov::opset1::Parameter>(ov::element::f32, ov::Shape{1, 1});+ auto a1 = std::make_shared<ov::opset1::Add>(+ p1,+ std::make_shared<ov::opset1::Constant>(ov::element::f32, ov::Shape{1, 4}, 1.0f));+ auto a2 = std::make_shared<ov::opset1::Add>(+ a1,+ std::make_shared<ov::opset1::Constant>(ov::element::f32, ov::Shape{6, 1}, 2.0f));+ auto model = std::make_shared<ov::Model>(ov::OutputVector{std::make_shared<ov::opset1::Result>(a2)},+ ov::ParameterVector{p1});+ ov::save_model(model, xmlFileName);+ }++ const auto model = core.read_model(xmlFileName, binFileName, {ov::enable_mmap(GetParam())});++ const auto wt_sources = ov::wsh::Extension::get_weight_sources(*model);+ ASSERT_GE(wt_sources.size(), 1);++ const auto wt_map = ov::wsh::Extension::get_weight_registry(*model);+ ASSERT_GE(wt_map.size(), 1);++ const auto& wt_meta_map = wt_map.begin()->second;+ EXPECT_EQ(wt_meta_map.size(), 2);+}+INSTANTIATE_TEST_SUITE_P(EnableMMapPropery, IRFrontendMMapTests, ::testing::Bool());TEST_F(IRFrontendTests, model_without_weights_reading_from_disk) {@@ -1711,7 +1739,7 @@ TEST_F(IRFrontendTests, load_model_weights_not_exist_at_path) {OV_EXPECT_THROW(fe->load(model_file_path, weights_file_path),ov::Exception,- testing::HasSubstr(weights_file_path + error_msg));+ testing::HasSubstr('\"' + weights_file_path + '\"' + error_msg));std::remove(model_file_path.c_str());}diff --git a/src/frontends/ir/tests/frontend_test_mmap.cpp b/src/frontends/ir/tests/frontend_test_mmap.cppindex 28202c99771..e32b4f48c28 100644--- a/src/frontends/ir/tests/frontend_test_mmap.cpp+++ b/src/frontends/ir/tests/frontend_test_mmap.cpp@@ -28,7 +28,7 @@ protected:xmlFileName = filePrefix + "_IrFrontendTestModel.xml";binFileName = filePrefix + "_IrFrontendTestModel.bin";ov::serialize(model, xmlFileName);- binsize = ov::test::utils::fileSize(binFileName) / 1024;+ binsize = ov::test::utils::fileSize(binFileName.string()) / 1024;// In case of enabled `mmap` RAM should not increase more than 50% of .bin size// Otherwise RAM should increase on at least 50% of .bin size