Skip to content

Commit

Permalink
Core read_model supports user configuration
Browse files Browse the repository at this point in the history
Signed-off-by: Raasz, Pawel <[email protected]>
  • Loading branch information
praasz committed Dec 13, 2024
1 parent abb5595 commit fb2861c
Show file tree
Hide file tree
Showing 10 changed files with 218 additions and 36 deletions.
17 changes: 13 additions & 4 deletions src/bindings/python/src/pyopenvino/core/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -393,10 +393,17 @@ void regclass_Core(py::module m) {

cls.def(
"read_model",
(std::shared_ptr<ov::Model>(ov::Core::*)(const std::string&, const std::string&) const) & ov::Core::read_model,
py::call_guard<py::gil_scoped_release>(),
[](ov::Core& self,
const std::string& model_path,
const std::string& weight_path,
const std::map<std::string, py::object>& properties) {
const auto any_map = Common::utils::properties_to_any_map(properties);
py::gil_scoped_release release;
return self.read_model(model_path, weight_path, any_map);
},
py::arg("model"),
py::arg("weights") = "",
py::arg("properties") = py::dict(),
R"(
Reads models from IR / ONNX / PDPD / TF and TFLite formats.
Expand All @@ -412,6 +419,8 @@ void regclass_Core(py::module m) {
For TF format (*.pb) weights parameter is not used.
For TFLite format (*.tflite) weights parameter is not used.
:type weights: str
:param properties: Optional map of pairs: (property name, property value) relevant only for this read operation.
:type properties: dict, optional
:return: A model.
:rtype: openvino.runtime.Model
)");
Expand Down Expand Up @@ -653,7 +662,7 @@ void regclass_Core(py::module m) {
:param properties: Optional dict of pairs: (property name, property value)
:type properties: dict
:return: Pairs a operation name -> a device name supporting this operation.
:rtype: dict
:rtype: dict
)");

cls.def("add_extension",
Expand All @@ -671,7 +680,7 @@ void regclass_Core(py::module m) {
py::arg("extension"),
R"(
Registers an extension to a Core object.
:param extension: Extension object.
:type extension: openvino.runtime.Extension
)");
Expand Down
36 changes: 36 additions & 0 deletions src/frontends/ir/tests/frontend_test_mmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,42 @@ TEST_F(IRFrontendMMapTestsAdvanced, core_enable_mmap_property) {
auto model = core.read_model(xmlFileName);
auto rss_read = ov::test::utils::getVmRSSInKB();

if (is_mmap != core.get_property("", ov::enable_mmap)) {
std::cout << "Test failed: core property is not set correctly" << std::endl;
exit(1);
}

bool is_weights_read = (rss_read - rss_init) > REF_RSS;
if (is_mmap == is_weights_read) {
std::cerr << "Test failed: mmap is " << (is_mmap ? "enabled" : "disabled") << ", but weights are "
<< (is_weights_read ? "read" : "not read") << " in RAM" << std::endl;
exit(1);
}
std::cerr << "Test passed" << std::endl;
exit(0);
};

for (const auto is_mmap : {true, false})
// Run test in a separate process to not affect RAM values by previous tests
EXPECT_EXIT(test(is_mmap), ::testing::ExitedWithCode(0), "Test passed");
}

TEST_F(IRFrontendMMapTestsAdvanced, core_enable_mmap_property_user_config) {
// Test checks that with enabled `mmap` .bin file
// isn't read into RAM on `read_model` stage.
// Otherwise, with disabled `mmap` .bin file should
// be in RAM

auto test = [&](const bool& is_mmap) {
auto rss_init = ov::test::utils::getVmRSSInKB();
auto model = core.read_model(xmlFileName, {}, {{ov::enable_mmap(is_mmap)}});
auto rss_read = ov::test::utils::getVmRSSInKB();

if (true != core.get_property("", ov::enable_mmap)) {
std::cout << "Test failed: core property changed by user configuration" << std::endl;
exit(1);
}

bool is_weights_read = (rss_read - rss_init) > REF_RSS;
if (is_mmap == is_weights_read) {
std::cerr << "Test failed: mmap is " << (is_mmap ? "enabled" : "disabled") << ", but weights are "
Expand Down
5 changes: 4 additions & 1 deletion src/inference/dev_api/openvino/runtime/icore.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,12 @@ class OPENVINO_RUNTIME_API ICore {
* @param model_path path to IR file
* @param bin_path path to bin file, if path is empty, will try to read bin file with the same name as xml and
* if bin file with the same name was not found, will load IR without weights.
* @param properties Optional map of pairs: (property name, property value) relevant only for this read operation.
* @return shared pointer to ov::Model
*/
virtual std::shared_ptr<ov::Model> read_model(const std::string& model_path, const std::string& bin_path) const = 0;
virtual std::shared_ptr<ov::Model> read_model(const std::string& model_path,
const std::string& bin_path,
const AnyMap& properties) const = 0;

virtual ov::AnyMap create_compile_config(const std::string& device_name, const ov::AnyMap& origConfig) const = 0;

Expand Down
43 changes: 40 additions & 3 deletions src/inference/include/openvino/runtime/core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,52 @@ class OPENVINO_RUNTIME_API Core {
* * PDPD (*.pdmodel)
* * TF (*.pb)
* * TFLite (*.tflite)
* @param properties Optional map of pairs: (property name, property value) relevant only for this read operation.
* @return A model.
* @{
*/
std::shared_ptr<ov::Model> read_model(const std::string& model_path, const std::string& bin_path = {}) const;
std::shared_ptr<ov::Model> read_model(const std::string& model_path,
const std::string& bin_path = {},
const ov::AnyMap& properties = {}) 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());
std::shared_ptr<ov::Model> read_model(const Path& model_path,
const Path& bin_path = {},
const ov::AnyMap& properties = {}) const {
return read_model(model_path.string(), bin_path.string(), properties);
}
#endif
/// @}

/**
* @brief Reads models from IR / ONNX / PDPD / TF / TFLite file formats.
*
* @param model_path Path to a model.
* @param bin_path Path to a data file.
* For IR format (*.bin):
* * if `bin_path` is empty, will try to read a bin file with the same name as xml and
* * if the bin file with the same name is not found, will load IR without weights.
* For the following file formats the `bin_path` parameter is not used:
* * ONNX format (*.onnx)
* * PDPD (*.pdmodel)
* * TF (*.pb)
* * TFLite (*.tflite)
* @param properties Optional pack of pairs: (property name, property value) relevant only for this read operation.
* @return A model.
* @{
*/
template <typename... Properties>
util::EnableIfAllStringAny<CompiledModel, Properties...> read_model(const std::string& model_path,
const std::string& bin_path,
Properties&&... properties) const {
return read_model(model_path, bin_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 read_model(const Path& model_path, const Path& bin_path = {}, Properties&&... properties) const {
return read_model(model_path.string(), bin_path.string(), std::forward<Properties>(properties)...);
}
#endif
/// @}
Expand Down
8 changes: 5 additions & 3 deletions src/inference/src/cpp/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,14 @@ std::map<std::string, Version> Core::get_versions(const std::string& device_name
#ifdef OPENVINO_ENABLE_UNICODE_PATH_SUPPORT
std::shared_ptr<ov::Model> Core::read_model(const std::wstring& model_path, const std::wstring& bin_path) const {
OV_CORE_CALL_STATEMENT(
return _impl->read_model(ov::util::wstring_to_string(model_path), ov::util::wstring_to_string(bin_path)););
return _impl->read_model(ov::util::wstring_to_string(model_path), ov::util::wstring_to_string(bin_path), {}););
}
#endif

std::shared_ptr<ov::Model> Core::read_model(const std::string& model_path, const std::string& bin_path) const {
OV_CORE_CALL_STATEMENT(return _impl->read_model(model_path, bin_path););
std::shared_ptr<ov::Model> Core::read_model(const std::string& model_path,
const std::string& bin_path,
const AnyMap& properties) const {
OV_CORE_CALL_STATEMENT(return _impl->read_model(model_path, bin_path, properties););
}

std::shared_ptr<ov::Model> Core::read_model(const std::string& model, const ov::Tensor& weights) const {
Expand Down
14 changes: 9 additions & 5 deletions src/inference/src/dev/core_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -856,12 +856,12 @@ ov::SoPtr<ov::ICompiledModel> ov::CoreImpl::compile_model(const std::string& mod
std::unique_ptr<CacheGuardEntry> lock = cacheGuard.get_hash_lock(cacheContent.blobId);
compiled_model =
load_model_from_cache(cacheContent, plugin, parsed._config, ov::SoPtr<ov::IRemoteContext>{}, [&]() {
auto model =
ov::util::read_model(model_path, std::string{}, extensions, parsed._core_config.get_enable_mmap());
const auto model = util::read_model(model_path, "", extensions, parsed._core_config.get_enable_mmap());
return compile_model_and_cache(plugin, model, parsed._config, {}, cacheContent);
});
} else {
compiled_model = plugin.compile_model(model_path, parsed._config);
const auto model = util::read_model(model_path, "", extensions, parsed._core_config.get_enable_mmap());
compiled_model = plugin.compile_model(model, parsed._config);
}
return compiled_model;
}
Expand Down Expand Up @@ -1664,9 +1664,13 @@ void ov::CoreImpl::add_mutex(const std::string& dev_name) {
dev_mutexes[dev_name];
}

std::shared_ptr<ov::Model> ov::CoreImpl::read_model(const std::string& modelPath, const std::string& binPath) const {
std::shared_ptr<ov::Model> ov::CoreImpl::read_model(const std::string& modelPath,
const std::string& binPath,
const AnyMap& properties) const {
OV_ITT_SCOPE(FIRST_INFERENCE, ov::itt::domains::ReadTime, "CoreImpl::read_model from file");
return ov::util::read_model(modelPath, binPath, extensions, coreConfig.get_enable_mmap());
auto local_core_config = coreConfig;
local_core_config.set(properties);
return ov::util::read_model(modelPath, binPath, extensions, local_core_config.get_enable_mmap());
}

std::shared_ptr<ov::Model> ov::CoreImpl::read_model(const std::string& model,
Expand Down
4 changes: 3 additions & 1 deletion src/inference/src/dev/core_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,9 @@ class CoreImpl : public ov::ICore, public std::enable_shared_from_this<ov::ICore
std::shared_ptr<ov::Model> read_model(const std::shared_ptr<AlignedBuffer>& model,
const std::shared_ptr<AlignedBuffer>& weights) const override;

std::shared_ptr<ov::Model> read_model(const std::string& model_path, const std::string& bin_path) const override;
std::shared_ptr<ov::Model> read_model(const std::string& model_path,
const std::string& bin_path,
const AnyMap& properties) const override;

ov::SoPtr<ov::ICompiledModel> compile_model(const std::shared_ptr<const ov::Model>& model,
const std::string& device_name,
Expand Down
2 changes: 1 addition & 1 deletion src/inference/src/dev/iplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ std::shared_ptr<ov::ICompiledModel> ov::IPlugin::compile_model(const std::string
const ov::AnyMap& properties) const {
auto core = get_core();
OPENVINO_ASSERT(core);
auto model = core->read_model(model_path, std::string());
auto model = core->read_model(model_path, std::string(), properties);
return compile_model(model, properties);
}

Expand Down
Loading

0 comments on commit fb2861c

Please sign in to comment.