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
ov::Core::import_model(ov::Tensor ...) (#31327)
Oleg Pipikin0943c9e5fa13
diff --git a/src/inference/dev_api/openvino/runtime/icore.hpp b/src/inference/dev_api/openvino/runtime/icore.hppindex 63328cc353f..ffc7c03b152 100644--- a/src/inference/dev_api/openvino/runtime/icore.hpp+++ b/src/inference/dev_api/openvino/runtime/icore.hpp@@ -47,7 +47,7 @@ public:bool frontend_mode = false) const = 0;/**- * @brief Reads IR xml and bin from buffer+ * @brief Reads IR xml and bin from buffer. This method is not exposed to public API.* @param model shared pointer to aligned buffer with IR* @param weights shared pointer to aligned buffer with weights* @return shared pointer to ov::Model@@ -158,6 +158,29 @@ public:virtual ov::SoPtr<ov::ICompiledModel> import_model(std::istream& modelStream,const ov::SoPtr<ov::IRemoteContext>& context,const ov::AnyMap& config = {}) const = 0;+ /**+ * @brief Creates a compiled model from a previously exported model+ * @param compiled_blob model blob+ * @param device_name Name of device load executable model on+ * @param config Optional map of pairs: (config parameter name, config parameter value) relevant only for this load+ * operation*+ * @return A pointer to compiled model+ */+ virtual ov::SoPtr<ov::ICompiledModel> import_model(const ov::Tensor& compiled_blob,+ const std::string& device_name,+ const ov::AnyMap& config = {}) const = 0;++ /**+ * @brief Creates a compiled model from a previously exported model+ * @param compiled_blob model blob+ * @param context Remote context+ * @param config Optional map of pairs: (config parameter name, config parameter value) relevant only for this load+ * operation*+ * @return A pointer to compiled model+ */+ virtual ov::SoPtr<ov::ICompiledModel> import_model(const ov::Tensor& compiled_blob,+ const ov::SoPtr<ov::IRemoteContext>& context,+ const ov::AnyMap& config = {}) const = 0;/*** @brief Query device if it supports specified network with specified configurationdiff --git a/src/inference/dev_api/openvino/runtime/iplugin.hpp b/src/inference/dev_api/openvino/runtime/iplugin.hppindex 7463e5313da..9547bc40763 100644--- a/src/inference/dev_api/openvino/runtime/iplugin.hpp+++ b/src/inference/dev_api/openvino/runtime/iplugin.hpp@@ -192,7 +192,7 @@ public:* @param properties A ov::AnyMap of properties* @return An Compiled model*/- virtual std::shared_ptr<ov::ICompiledModel> import_model(const ov::Tensor& model, const ov::AnyMap& properties) const;+ virtual std::shared_ptr<ov::ICompiledModel> import_model(const ov::Tensor& model, const ov::AnyMap& properties) const = 0;/*** @brief Creates an compiled model from an previously exported model using plugin implementation@@ -205,7 +205,7 @@ public:*/virtual std::shared_ptr<ov::ICompiledModel> import_model(const ov::Tensor& model,const ov::SoPtr<ov::IRemoteContext>& context,- const ov::AnyMap& properties) const;+ const ov::AnyMap& properties) const = 0;/*** @brief Queries a plugin about supported layers in modeldiff --git a/src/inference/include/openvino/runtime/core.hpp b/src/inference/include/openvino/runtime/core.hppindex 21b9553f950..c7bdff8634c 100644--- a/src/inference/include/openvino/runtime/core.hpp+++ b/src/inference/include/openvino/runtime/core.hpp@@ -543,7 +543,8 @@ public:/*** @brief Imports a compiled model from the previously exported one.* @tparam Properties Should be the pack of `std::pair<std::string, ov::Any>` types.- * @param model_stream Model stream.+ * @param model_stream std::istream input stream containing a model previously exported using the+ * ov::CompiledModel::export_model method.* @param device_name Name of a device to import a compiled model for. Note, if @p device_name device was not used* to compile the original mode, an exception is thrown.* @param properties Optional pack of pairs: (property name, property value) relevant only for this@@ -559,8 +560,8 @@ public:/*** @brief Imports a compiled model from the previously exported one with the specified remote context.- * @param model_stream std::istream input stream containing a model previously exported from- * ov::CompiledModel::export_model+ * @param model_stream std::istream input stream containing a model previously exported using the+ * ov::CompiledModel::export_model method.* @param context A reference to a RemoteContext object. Note, if the device from @p context was not used to compile* the original mode, an exception is thrown.* @param properties Optional map of pairs: (property name, property value) relevant only for this load@@ -572,10 +573,10 @@ public:/*** @brief Imports a compiled model from the previously exported one with the specified remote context.* @tparam Properties Should be the pack of `std::pair<std::string, ov::Any>` types.- * @param model_stream Model stream.+ * @param model_stream std::istream input stream containing a model previously exported using the+ * ov::CompiledModel::export_model method.* @param context Pointer to a RemoteContext object.- * @param properties Optional pack of pairs: (property name, property value) relevant only for this- * load operation.+ * @param properties Optional pack of pairs: (property name, property value) relevant only for this load operation.* @return A compiled model.*/template <typename... Properties>@@ -585,6 +586,68 @@ public:return import_model(model_stream, context, AnyMap{std::forward<Properties>(properties)...});}+ /**+ * @brief Imports a compiled model from the previously exported one.+ * @param compiled_blob ov::Tensor input blob containing a model previously exported using the+ * ov::CompiledModel::export_model method.+ * @param device_name Name of a device to import a compiled model for. Note, if @p device_name device was not used+ * to compile the original mode, an exception is thrown.+ * @param properties Optional map of pairs: (property name, property value) relevant only for this load+ * operation.+ * @return A compiled model.+ */+ CompiledModel import_model(const ov::Tensor& compiled_blob,+ const std::string& device_name,+ const AnyMap& properties = {});++ /**+ * @brief Imports a compiled model from the previously exported one.+ * @tparam Properties Should be the pack of `std::pair<std::string, ov::Any>` types.+ * @param compiled_blob ov::Tensor input blob containing a model previously exported using the+ * ov::CompiledModel::export_model method.+ * @param device_name Name of a device to import a compiled model for. Note, if @p device_name device was not used+ * to compile the original mode, an exception is thrown.+ * @param properties Optional pack of pairs: (property name, property value) relevant only for this+ * load operation.+ * @return A compiled model.+ */+ template <typename... Properties>+ util::EnableIfAllStringAny<CompiledModel, Properties...> import_model(const ov::Tensor& compiled_blob,+ const std::string& device_name,+ Properties&&... properties) {+ return import_model(compiled_blob, device_name, AnyMap{std::forward<Properties>(properties)...});+ }++ /**+ * @brief Imports a compiled model from the previously exported one with the specified remote context.+ * @param compiled_blob ov::Tensor input blob containing a model previously exported using the+ * ov::CompiledModel::export_model method.+ * @param context A reference to a RemoteContext object. Note, if the device from @p context was not used to compile+ * the original mode, an exception is thrown.+ * @param properties Optional map of pairs: (property name, property value) relevant only for this load+ * operation.+ * @return A compiled model.+ */+ CompiledModel import_model(const ov::Tensor& compiled_blob,+ const RemoteContext& context,+ const AnyMap& properties = {});++ /**+ * @brief Imports a compiled model from the previously exported one with the specified remote context.+ * @tparam Properties Should be the pack of `std::pair<std::string, ov::Any>` types.+ * @param compiled_blob ov::Tensor input blob containing a model previously exported using the+ * ov::CompiledModel::export_model method.+ * @param context Pointer to a RemoteContext object.+ * @param properties Optional pack of pairs: (property name, property value) relevant only for this load operation.+ * @return A compiled model.+ */+ template <typename... Properties>+ util::EnableIfAllStringAny<CompiledModel, Properties...> import_model(const ov::Tensor& compiled_blob,+ const RemoteContext& context,+ Properties&&... properties) {+ return import_model(compiled_blob, context, AnyMap{std::forward<Properties>(properties)...});+ }+/*** @brief Query device if it supports the specified model with specified properties.*diff --git a/src/inference/src/cpp/core.cpp b/src/inference/src/cpp/core.cppindex 60264362ce5..dfdad3e569e 100644--- a/src/inference/src/cpp/core.cpp+++ b/src/inference/src/cpp/core.cpp@@ -201,6 +201,23 @@ CompiledModel Core::import_model(std::istream& modelStream, const RemoteContext&});}+CompiledModel Core::import_model(const ov::Tensor& model, const std::string& device_name, const AnyMap& config) {+ OV_ITT_SCOPED_TASK(ov::itt::domains::OV, "Core::import_model");+ OV_CORE_CALL_STATEMENT({+ auto exec = _impl->import_model(model, device_name, config);+ return {exec._ptr, exec._so};+ });+}++CompiledModel Core::import_model(const ov::Tensor& model, const RemoteContext& context, const AnyMap& config) {+ OV_ITT_SCOPED_TASK(ov::itt::domains::OV, "Core::import_model");++ OV_CORE_CALL_STATEMENT({+ auto exec = _impl->import_model(model, ov::SoPtr<ov::IRemoteContext>{context._impl, context._so}, config);+ return {exec._ptr, exec._so};+ });+}+SupportedOpsMap Core::query_model(const std::shared_ptr<const ov::Model>& model,const std::string& device_name,const AnyMap& config) const {diff --git a/src/inference/src/dev/core_impl.cpp b/src/inference/src/dev/core_impl.cppindex 37e092f4cc5..cd9e6ed583d 100644--- a/src/inference/src/dev/core_impl.cpp+++ b/src/inference/src/dev/core_impl.cpp@@ -232,11 +232,8 @@ ov::SoPtr<ov::ICompiledModel> import_compiled_model(const ov::Plugin& plugin,if (auto blob_hint = config.find(ov::hint::compiled_blob.name()); blob_hint != config.end()) {try {auto compiled_blob = blob_hint->second.as<ov::Tensor>();- auto _config = config;- _config.erase(ov::hint::compiled_blob.name());-- compiled_model = context ? plugin.import_model(compiled_blob, context, _config)- : plugin.import_model(compiled_blob, _config);+ compiled_model = context ? plugin.import_model(compiled_blob, context, config)+ : plugin.import_model(compiled_blob, config);} catch (...) {}}@@ -966,15 +963,32 @@ ov::SoPtr<ov::ICompiledModel> ov::CoreImpl::import_model(std::istream& modelStreconst ov::AnyMap& config) const {OV_ITT_SCOPED_TASK(ov::itt::domains::OV, "Core::import_model");OPENVINO_ASSERT(context, "Remote context must not be empty.");- auto parsed = parseDeviceNameIntoConfig(context->get_device_name(), config);+ const auto parsed = parseDeviceNameIntoConfig(context->get_device_name(), config);return get_plugin(parsed._deviceName).import_model(modelStream, context, parsed._config);}+ov::SoPtr<ov::ICompiledModel> ov::CoreImpl::import_model(const ov::Tensor& compiled_blob,+ const std::string& device_name,+ const ov::AnyMap& config) const {+ OV_ITT_SCOPED_TASK(ov::itt::domains::OV, "Core::import_model");+ const auto parsed = parseDeviceNameIntoConfig(device_name, config);+ return get_plugin(parsed._deviceName).import_model(compiled_blob, parsed._config);+}++ov::SoPtr<ov::ICompiledModel> ov::CoreImpl::import_model(const ov::Tensor& compiled_blob,+ const ov::SoPtr<ov::IRemoteContext>& context,+ const ov::AnyMap& config) const {+ OV_ITT_SCOPED_TASK(ov::itt::domains::OV, "Core::import_model");+ OPENVINO_ASSERT(context, "Remote context must not be empty.");+ const auto parsed = parseDeviceNameIntoConfig(context->get_device_name(), config);+ return get_plugin(parsed._deviceName).import_model(compiled_blob, context, parsed._config);+}+ov::SupportedOpsMap ov::CoreImpl::query_model(const std::shared_ptr<const ov::Model>& model,const std::string& device_name,const ov::AnyMap& config) const {OV_ITT_SCOPED_TASK(ov::itt::domains::OV, "Core::query_model");- auto parsed = parseDeviceNameIntoConfig(device_name, config);+ const auto parsed = parseDeviceNameIntoConfig(device_name, config);return get_plugin(parsed._deviceName).query_model(model, parsed._config);}diff --git a/src/inference/src/dev/core_impl.hpp b/src/inference/src/dev/core_impl.hppindex 362124591ad..806d2a06455 100644--- a/src/inference/src/dev/core_impl.hpp+++ b/src/inference/src/dev/core_impl.hpp@@ -347,6 +347,14 @@ public:const ov::SoPtr<ov::IRemoteContext>& context,const ov::AnyMap& config) const override;+ ov::SoPtr<ov::ICompiledModel> import_model(const ov::Tensor& compiled_blob,+ const std::string& device_name = {},+ const ov::AnyMap& config = {}) const override;++ ov::SoPtr<ov::ICompiledModel> import_model(const ov::Tensor& compiled_blob,+ const ov::SoPtr<ov::IRemoteContext>& context,+ const ov::AnyMap& config) const override;+ov::SupportedOpsMap query_model(const std::shared_ptr<const ov::Model>& model,const std::string& device_name,const ov::AnyMap& config) const override;diff --git a/src/inference/src/dev/iplugin.cpp b/src/inference/src/dev/iplugin.cppindex 92664a1b056..d7dc6ae617e 100644--- a/src/inference/src/dev/iplugin.cpp+++ b/src/inference/src/dev/iplugin.cpp@@ -82,23 +82,6 @@ std::shared_ptr<ov::ICompiledModel> ov::IPlugin::compile_model(const std::stringreturn compile_model(model, local_properties);}-// Implementation for backward compatibility for Nvidia plugin-// TODO: Update Nvidia plugin in contrib repo and make ov::IPlugin::import_model abstract again-std::shared_ptr<ov::ICompiledModel> ov::IPlugin::import_model(const ov::Tensor& model,- const ov::AnyMap& properties) const {- ov::SharedStreamBuffer buffer{reinterpret_cast<char*>(model.data()), model.get_byte_size()};- std::istream stream{&buffer};- return import_model(stream, properties);-};--std::shared_ptr<ov::ICompiledModel> ov::IPlugin::import_model(const ov::Tensor& model,- const ov::SoPtr<ov::IRemoteContext>& context,- const ov::AnyMap& properties) const {- ov::SharedStreamBuffer buffer{reinterpret_cast<char*>(model.data()), model.get_byte_size()};- std::istream stream{&buffer};- return import_model(stream, context, properties);-};-std::unordered_set<std::string> ov::get_supported_nodes(const std::shared_ptr<const ov::Model>& model,std::function<void(std::shared_ptr<ov::Model>&)> transform,diff --git a/src/inference/tests/functional/caching_test.cpp b/src/inference/tests/functional/caching_test.cppindex ce219b6f8f3..54f0358cbe2 100644--- a/src/inference/tests/functional/caching_test.cpp+++ b/src/inference/tests/functional/caching_test.cpp@@ -2862,7 +2862,7 @@ TEST_P(CachingTest, import_from_compiled_blob_weights_path_property_is_supportedif (m_checkConfigCb) {m_checkConfigCb(config);}- EXPECT_EQ(config.count(ov::hint::compiled_blob.name()), 0);+ EXPECT_EQ(config.count(ov::hint::compiled_blob.name()), 1);EXPECT_EQ(config.count(ov::hint::model.name()), m_type != TestLoadType::EModelName ? 1 : 0);EXPECT_EQ(config.count(ov::weights_path.name()), m_type == TestLoadType::EModelName ? 1 : 0);return nullptr;diff --git a/src/plugins/auto/tests/functional/shared_tests_instances/skip_tests_config.cpp b/src/plugins/auto/tests/functional/shared_tests_instances/skip_tests_config.cppindex 6425d12f0df..3a7914f694a 100644--- a/src/plugins/auto/tests/functional/shared_tests_instances/skip_tests_config.cpp+++ b/src/plugins/auto/tests/functional/shared_tests_instances/skip_tests_config.cpp@@ -34,7 +34,9 @@ std::vector<std::string> disabledTestPatterns() {R"(.*Behavior.*OVInferRequestIOTensorTest.*canInferAfterIOBlobReallocation.*)",R"(.*Behavior.*OVInferRequestDynamicTests.*InferUpperBoundNetworkAfterIOTensorsReshaping.*)",// template plugin doesn't support this case- R"(.*OVInferRequestPerfCountersTest.*CheckOperationInProfilingInfo.*)"};+ R"(.*OVInferRequestPerfCountersTest.*CheckOperationInProfilingInfo.*)",+ // model import is not supported+ R"(.*OVCompiledModelBaseTest.import_from_.*)"};#if !defined(OPENVINO_ARCH_X86_64)// very time-consuming testdiff --git a/src/plugins/auto_batch/tests/functional/skip_tests_config.cpp b/src/plugins/auto_batch/tests/functional/skip_tests_config.cppindex d10439a4c80..9e069218e9a 100644--- a/src/plugins/auto_batch/tests/functional/skip_tests_config.cpp+++ b/src/plugins/auto_batch/tests/functional/skip_tests_config.cpp@@ -29,6 +29,8 @@ std::vector<std::string> disabledTestPatterns() {R"(.*Behavior.*OVCompiledModelBaseTest.*import_from_weightless_blob.*targetDevice=(BATCH).*)",R"(.*Behavior.*OVCompiledModelBaseTest.*compile_from.*_blob.*targetDevice=(BATCH).*)",R"(.*Behavior.*OVCompiledModelBaseTest.*use_blob_hint.*targetDevice=(BATCH).*)",+ // model import is not supported+ R"(.*OVCompiledModelBaseTest.import_from_.*)"};return disabled_items;diff --git a/src/plugins/hetero/tests/functional/skip_tests_config.cpp b/src/plugins/hetero/tests/functional/skip_tests_config.cppindex d718ad63b76..a04a767c6db 100644--- a/src/plugins/hetero/tests/functional/skip_tests_config.cpp+++ b/src/plugins/hetero/tests/functional/skip_tests_config.cpp@@ -20,6 +20,7 @@ std::vector<std::string> disabledTestPatterns() {R"(.*OVCompiledModelBaseTest.*compile_from_.*_blob.*targetDevice=(HETERO.NPU).*)",R"(.*OVCompiledModelBaseTest.*compile_from_cached_weightless_blob.*targetDevice=(HETERO.NPU).*)",R"(.*OVCompiledModelBaseTest.*use_blob_hint_.*targetDevice=CPU.*)",- };+ // model import is not supported+ R"(.*OVCompiledModelBaseTest.import_from_.*)"};return retVector;}diff --git a/src/plugins/template/tests/functional/skip_tests_config.cpp b/src/plugins/template/tests/functional/skip_tests_config.cppindex c00fbce1297..351c65e4ea9 100644--- a/src/plugins/template/tests/functional/skip_tests_config.cpp+++ b/src/plugins/template/tests/functional/skip_tests_config.cpp@@ -102,7 +102,8 @@ std::vector<std::string> disabledTestPatterns() {R"(.*smoke_CompareWithRefs_static/EltwiseLayerTest.Inference/IS=.*_TS=\(\(2.17.5.1\)_\(1.17.1.4\)_\)_eltwise_op_type=Mod_secondary_input_type=PARAMETER_opType=VECTOR_model_type=f16_InType=dynamic_OutType=dynamic_.*)",R"(.*smoke_CompareWithRefs_static/EltwiseLayerTest.Inference/IS=.*_TS=.*(2.200|10.200|1.10.100|4.4.16|1.2.4|1.4.4|1.4.4.1).*eltwise_op_type=Mod_secondary_input_type=PARAMETER_opType=VECTOR_model_type=f16_InType=dynamic_OutType=dynamic.*)",R"(.*smoke_CompareWithRefs_static/EltwiseLayerTest.Inference/IS=.*_TS=.*2.*eltwise_op_type=Pow_secondary_input_type=PARAMETER_opType=VECTOR_model_type=f32_InType=dynamic_OutType=dynamic.*)",- };+ // model import is not supported+ R"(.*OVCompiledModelBaseTest.import_from_.*)"};#ifdef _WIN32// CVS-63989retVector.emplace_back(R"(.*ReferenceSigmoidLayerTest.*u64.*)");diff --git a/src/tests/functional/base_func_tests/include/behavior/compiled_model/compiled_model_base.hpp b/src/tests/functional/base_func_tests/include/behavior/compiled_model/compiled_model_base.hppindex e4f4a30eff8..69db3e817a8 100644--- a/src/tests/functional/base_func_tests/include/behavior/compiled_model/compiled_model_base.hpp+++ b/src/tests/functional/base_func_tests/include/behavior/compiled_model/compiled_model_base.hpp@@ -764,6 +764,29 @@ TEST_P(CompiledModelSetType, canSetInputOutputTypeAndCompileModel) {OV_ASSERT_NO_THROW(core.compile_model(model, target_device, configuration));}+TEST_P(OVCompiledModelBaseTest, import_from_istream) {+ std::stringstream export_stream;+ {+ auto model = make_model_with_weights();+ auto compiled_model = core->compile_model(model, target_device);+ ASSERT_TRUE(compiled_model);+ compiled_model.export_model(export_stream);+ }+ EXPECT_NO_THROW(core->import_model(export_stream, target_device));+}++TEST_P(OVCompiledModelBaseTest, import_from_tensor) {+ std::stringstream export_stream;+ {+ auto model = make_model_with_weights();+ auto compiled_model = core->compile_model(model, target_device);+ ASSERT_TRUE(compiled_model);+ compiled_model.export_model(export_stream);+ }+ ov::Tensor exported_model = from_stream(export_stream, export_stream.str().size());+ EXPECT_NO_THROW(core->import_model(exported_model, target_device));+}+TEST_P(OVCompiledModelBaseTest, import_from_weightless_blob) {const auto w_file_path =ov::util::path_join({utils::getCurrentWorkingDir(), utils::generateTestFilePrefix() + "_weights.bin"});diff --git a/src/tests/test_utils/unit_test_utils/mocks/openvino/runtime/mock_icore.hpp b/src/tests/test_utils/unit_test_utils/mocks/openvino/runtime/mock_icore.hppindex 91c8cfba5c0..2aef91a1ae8 100644--- a/src/tests/test_utils/unit_test_utils/mocks/openvino/runtime/mock_icore.hpp+++ b/src/tests/test_utils/unit_test_utils/mocks/openvino/runtime/mock_icore.hpp@@ -30,6 +30,10 @@ public:import_model,(std::istream&, const std::string&, const ov::AnyMap&),(const));+ MOCK_METHOD(ov::SoPtr<ov::ICompiledModel>,+ import_model,+ (const ov::Tensor&, const std::string&, const ov::AnyMap&),+ (const));MOCK_METHOD(ov::SoPtr<ov::ICompiledModel>,compile_model,(const std::shared_ptr<const ov::Model>&, const std::string&, const ov::AnyMap&),@@ -60,6 +64,10 @@ public:import_model,(std::istream&, const ov::SoPtr<ov::IRemoteContext>&, const ov::AnyMap&),(const));+ MOCK_METHOD(ov::SoPtr<ov::ICompiledModel>,+ import_model,+ (const ov::Tensor&, const ov::SoPtr<ov::IRemoteContext>&, const ov::AnyMap&),+ (const));MOCK_METHOD(bool, device_supports_model_caching, (const std::string&), (const));MOCK_METHOD(void, set_property, (const std::string& device_name, const ov::AnyMap& properties));