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
[NPUW] Validate imported closure metadata sizes (#37540)
Alexis Menard480e7c53ad06
diff --git a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cppindex 9ff9dd9c572..7f5086026c1 100644--- a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp@@ -206,6 +206,16 @@ std::set<std::string> device_list_to_set(const std::string& device_list) {}return result;}++void validate_closure_metadata_sizes(std::size_t closure_size,+ std::size_t lazy_closure_size,+ std::size_t is_remote_size,+ std::size_t closure_uid_size) {+ NPUW_ASSERT(lazy_closure_size == closure_size &&+ "Malformed ORC blob: lazy_closure size does not match closure size");+ NPUW_ASSERT(is_remote_size == closure_size && "Malformed ORC blob: is_remote size does not match closure size");+ NPUW_ASSERT(closure_uid_size == closure_size && "Malformed ORC blob: closure_uid size does not match closure size");+}} // anonymous namespacenamespace ov {@@ -991,10 +1001,10 @@ void ov::npuw::CompiledModel::CompiledModelDesc::serialize(ov::npuw::s11n::Streastd::size_t closure_size = closure_desc.closure.size();stream & closure_size;if (stream.input()) {- NPUW_ASSERT(closure_desc.closure_uid.size() == closure_size &&- "Malformed ORC blob: closure_uid size does not match closure size");- NPUW_ASSERT(closure_desc.is_remote.size() == closure_size &&- "Malformed ORC blob: is_remote size does not match closure size");+ validate_closure_metadata_sizes(closure_size,+ closure_size,+ closure_desc.is_remote.size(),+ closure_desc.closure_uid.size());}std::vector<ov::Tensor> cpu_closures;std::vector<std::size_t> cpu_closure_ids;@@ -1045,10 +1055,10 @@ void ov::npuw::CompiledModel::CompiledModelDesc::serialize(ov::npuw::s11n::Streastd::size_t closure_size = closure_desc.closure.size();stream & closure_size;if (stream.input()) {- NPUW_ASSERT(closure_desc.closure_uid.size() == closure_size &&- "Malformed ORC blob: closure_uid size does not match closure size");- NPUW_ASSERT(closure_desc.is_remote.size() == closure_size &&- "Malformed ORC blob: is_remote size does not match closure size");+ validate_closure_metadata_sizes(closure_size,+ closure_size,+ closure_desc.is_remote.size(),+ closure_desc.closure_uid.size());}std::vector<std::size_t> cpu_closure_ids;if (stream.output()) {@@ -1583,6 +1593,11 @@ void ov::npuw::CompiledModel::reconstruct_closure() {const auto real_idx = comp_model_desc.replaced_by.value_or(idx);auto& desc_closure = comp_model_desc.closure.get();+ validate_closure_metadata_sizes(desc_closure.closure.size(),+ desc_closure.closure.size(),+ desc_closure.is_remote.size(),+ desc_closure.closure_uid.size());+for (std::size_t cidx = 0; cidx < desc_closure.closure.size(); ++cidx) {if (desc_closure.closure[cidx]) {// host-side closure - already set, do nothing@@ -1629,6 +1644,11 @@ void ov::npuw::CompiledModel::finalize_weights_bank() {const auto real_idx = comp_model_desc.replaced_by.value_or(idx);+ validate_closure_metadata_sizes(comp_model_desc.closure.unsafe_get().closure.size(),+ comp_model_desc.lazy_closure.size(),+ comp_model_desc.closure.unsafe_get().is_remote.size(),+ comp_model_desc.closure.unsafe_get().closure_uid.size());+for (std::size_t tidx = 0; tidx < comp_model_desc.lazy_closure.size(); ++tidx) {if (comp_model_desc.closure.unsafe_get().closure[tidx]) {continue; // host-side closurediff --git a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hpp b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hppindex 9812443e588..d7fe7bb4553 100644--- a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hpp+++ b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hpp@@ -80,10 +80,6 @@ namespace moe {class MoEExecutor;}-namespace tests {-struct CompiledModelTestAccess;-}-class CompiledModel : public ov::npuw::ICompiledModel_v0 {using DevList = std::vector<std::string>;using GetPropertiesMap =@@ -148,7 +144,7 @@ private:friend class LLMCompiledModel;friend class LLMInferRequest;friend class moe::MoEExecutor;- friend struct tests::CompiledModelTestAccess;+ friend class CompiledModelDescSerializationAccess;bool compile_for_success(std::size_t id, const std::vector<std::string>& devices);ov::SoPtr<ov::ICompiledModel> compile_submodel(const std::shared_ptr<ov::Model>& submodel,diff --git a/src/plugins/intel_npu/tests/unit/npuw/serialization.cpp b/src/plugins/intel_npu/tests/unit/npuw/serialization.cppindex d6d0cae5b3a..327216445d7 100644--- a/src/plugins/intel_npu/tests/unit/npuw/serialization.cpp+++ b/src/plugins/intel_npu/tests/unit/npuw/serialization.cpp@@ -35,29 +35,55 @@using ov::test::npuw::ModelBuilder;-namespace ov {-namespace npuw {-namespace tests {-struct CompiledModelTestAccess {- static void deserialize_compiled_model_desc(std::stringstream& input,- const ov::npuw::s11n::WeightsContext& ctx) {- auto reader = ov::npuw::s11n::Stream::reader(input);- ov::npuw::CompiledModel::CompiledModelDesc imported_desc;- imported_desc.serialize(reader, ctx);- }-};-} // namespace tests-} // namespace npuw-} // namespace ov-namespace {+class SerializationNullPlugin final : public ov::IPlugin {+public:+ std::shared_ptr<ov::ICompiledModel> compile_model(const std::shared_ptr<const ov::Model>&,+ const ov::AnyMap&) const override {+ return {};+ }+ std::shared_ptr<ov::ICompiledModel> compile_model(const std::shared_ptr<const ov::Model>&,+ const ov::AnyMap&,+ const ov::SoPtr<ov::IRemoteContext>&) const override {+ return {};+ }+ std::shared_ptr<ov::ICompiledModel> import_model(std::istream&, const ov::AnyMap&) const override {+ return {};+ }+ std::shared_ptr<ov::ICompiledModel> import_model(std::istream&,+ const ov::SoPtr<ov::IRemoteContext>&,+ const ov::AnyMap&) const override {+ return {};+ }+ std::shared_ptr<ov::ICompiledModel> import_model(const ov::Tensor&, const ov::AnyMap&) const override {+ return {};+ }+ std::shared_ptr<ov::ICompiledModel> import_model(const ov::Tensor&,+ const ov::SoPtr<ov::IRemoteContext>&,+ const ov::AnyMap&) const override {+ return {};+ }+ ov::SupportedOpsMap query_model(const std::shared_ptr<const ov::Model>&, const ov::AnyMap&) const override {+ return {};+ }+ void set_property(const ov::AnyMap&) override {}+ ov::Any get_property(const std::string&, const ov::AnyMap&) const override {+ return {};+ }+ ov::SoPtr<ov::IRemoteContext> create_context(const ov::AnyMap&) const override {+ return {};+ }+ ov::SoPtr<ov::IRemoteContext> get_default_context(const ov::AnyMap&) const override {+ return {};+ }+};constexpr char kExpectedOobIndexMessage[] = "CPU closure index is out of range";constexpr char kExpectedClosureUidSizeMessage[] = "closure_uid size does not match closure size";constexpr char kExpectedIsRemoteSizeMessage[] = "is_remote size does not match closure size";+constexpr char kExpectedLazyClosureSizeMessage[] = "lazy_closure size does not match closure size";constexpr char kExpectedCpuCountMismatchMessage[] = "CPU closure ids count does not match CPU closure tensor count";-constexpr char kExpectedNonCpuCountMismatchMessage[] =- "non-CPU closure ids count does not match non-CPU tensor count";+constexpr char kExpectedNonCpuCountMismatchMessage[] = "non-CPU closure ids count does not match non-CPU tensor count";constexpr char kExpectedNonCpuOobIndexMessage[] = "non-CPU closure index is out of range";// Writes the fixed CompiledModelDesc::serialize() prefix (funcall/spatial metadata) that@@ -545,6 +571,43 @@ void expect_lazy_tensor_transform_types_equal(const ov::npuw::weights::LazyTenso} // namespace+namespace ov::npuw {+class CompiledModelDescSerializationAccess {+public:+ static void deserialize_compiled_model_desc(std::stringstream& input, const ov::npuw::s11n::WeightsContext& ctx) {+ auto reader = ov::npuw::s11n::Stream::reader(input);+ CompiledModel::CompiledModelDesc imported_desc;+ imported_desc.serialize(reader, ctx);+ }++ static std::shared_ptr<CompiledModel> make_serialized_compiled_model() {+ auto input = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::Shape{1});+ auto output = std::make_shared<ov::op::v0::Result>(input);+ auto model = std::make_shared<ov::Model>(ov::ResultVector{output}, ov::ParameterVector{input}, "test_model");+ auto plugin = std::make_shared<SerializationNullPlugin>();+ return std::make_shared<CompiledModel>(model, plugin, true);+ }++ static CompiledModel::CompiledModelDesc& append_submodel(CompiledModel& model) {+ model.m_compiled_submodels.emplace_back();+ return model.m_compiled_submodels.back();+ }++ static void run_reconstruct_closure(CompiledModel& model) {+ model.reconstruct_closure();+ }++ static void set_weights_bank(CompiledModel& model, std::shared_ptr<ov::npuw::weights::Bank> bank) {+ model.m_weights_bank = std::move(bank);+ }++ static void run_finalize_and_wait(CompiledModel& model) {+ model.finalize_weights_bank();+ model.m_eval_future.get();+ }+};+} // namespace ov::npuw+// FIXME: parametrize all the tests belowTEST(SerializationTest, BasicTypes_string) {@@ -1498,6 +1561,66 @@ TEST(SerializationTest, OVTypes_WeightsBank_cpu_roundtrip) {expect_tensors_equal(var.get(uid1, "CPU"), res.get(uid1, "CPU"));}+TEST(SerializationTest, ReconstructClosureRejectsMismatchedClosureMetadata) {+ auto compiled = ov::npuw::CompiledModelDescSerializationAccess::make_serialized_compiled_model();+ auto& submodel = ov::npuw::CompiledModelDescSerializationAccess::append_submodel(*compiled);++ submodel.replaced_by = 0;+ auto& closure = submodel.closure.get();+ closure.closure.resize(1);+ closure.closure[0] = ov::Tensor(ov::element::f32, ov::Shape{1});+ closure.is_remote.resize(2, false);+ closure.closure_uid.resize(2, -1);++ OV_EXPECT_THROW_HAS_SUBSTRING(ov::npuw::CompiledModelDescSerializationAccess::run_reconstruct_closure(*compiled),+ ov::Exception,+ kExpectedIsRemoteSizeMessage);+}++TEST(SerializationTest, FinalizeWeightsBankRejectsMismatchedLazyClosureMetadata) {+ auto compiled = ov::npuw::CompiledModelDescSerializationAccess::make_serialized_compiled_model();+ auto& submodel = ov::npuw::CompiledModelDescSerializationAccess::append_submodel(*compiled);++ submodel.replaced_by = 0;+ auto& closure = submodel.closure.get();+ closure.closure.resize(1);+ closure.closure[0] = ov::Tensor(ov::element::f32, ov::Shape{1});+ closure.is_remote.resize(1, false);+ closure.closure_uid.resize(1, -1);+ submodel.lazy_closure.resize(2);++ ov::npuw::CompiledModelDescSerializationAccess::set_weights_bank(+ *compiled,+ std::make_shared<ov::npuw::weights::Bank>(nullptr, "CPU", "test-bank"));++ OV_EXPECT_THROW_HAS_SUBSTRING(ov::npuw::CompiledModelDescSerializationAccess::run_finalize_and_wait(*compiled),+ ov::Exception,+ kExpectedLazyClosureSizeMessage);+}++TEST(SerializationTest, FinalizeWeightsBankRejectsMismatchedClosureAndLazyClosureMetadata) {+ auto compiled = ov::npuw::CompiledModelDescSerializationAccess::make_serialized_compiled_model();+ auto& submodel = ov::npuw::CompiledModelDescSerializationAccess::append_submodel(*compiled);++ submodel.replaced_by = 0;+ submodel.lazy_closure.resize(1);++ auto& closure = submodel.closure.get();+ closure.closure.resize(2);+ closure.closure[0] = ov::Tensor(ov::element::f32, ov::Shape{1});+ closure.closure[1] = ov::Tensor(ov::element::f32, ov::Shape{1});+ closure.is_remote.resize(2, false);+ closure.closure_uid.resize(2, -1);++ ov::npuw::CompiledModelDescSerializationAccess::set_weights_bank(+ *compiled,+ std::make_shared<ov::npuw::weights::Bank>(nullptr, "CPU", "test-bank"));++ OV_EXPECT_THROW_HAS_SUBSTRING(ov::npuw::CompiledModelDescSerializationAccess::run_finalize_and_wait(*compiled),+ ov::Exception,+ kExpectedLazyClosureSizeMessage);+}+TEST(SerializationTest, CompiledModelDesc_rejects_oob_cpu_closure_index_weightful) {using namespace ov::npuw::s11n;@@ -1506,9 +1629,10 @@ TEST(SerializationTest, CompiledModelDesc_rejects_oob_cpu_closure_index_weightfuconst auto malformed_blob = make_blob_with_oob_cpu_closure_id(false);std::stringstream input(malformed_blob, std::ios::in | std::ios::out | std::ios::binary);- OV_EXPECT_THROW_HAS_SUBSTRING(ov::npuw::tests::CompiledModelTestAccess::deserialize_compiled_model_desc(input, ctx),- ov::Exception,- kExpectedOobIndexMessage);+ OV_EXPECT_THROW_HAS_SUBSTRING(+ ov::npuw::CompiledModelDescSerializationAccess::deserialize_compiled_model_desc(input, ctx),+ ov::Exception,+ kExpectedOobIndexMessage);}TEST(SerializationTest, CompiledModelDesc_rejects_oob_cpu_closure_index_weightless) {@@ -1519,61 +1643,74 @@ TEST(SerializationTest, CompiledModelDesc_rejects_oob_cpu_closure_index_weightleconst auto malformed_blob = make_blob_with_oob_cpu_closure_id(true);std::stringstream input(malformed_blob, std::ios::in | std::ios::out | std::ios::binary);- OV_EXPECT_THROW_HAS_SUBSTRING(ov::npuw::tests::CompiledModelTestAccess::deserialize_compiled_model_desc(input, ctx),- ov::Exception,- kExpectedOobIndexMessage);+ OV_EXPECT_THROW_HAS_SUBSTRING(+ ov::npuw::CompiledModelDescSerializationAccess::deserialize_compiled_model_desc(input, ctx),+ ov::Exception,+ kExpectedOobIndexMessage);}TEST(SerializationTest, CompiledModelDesc_rejects_closure_uid_size_mismatch_weightful) {using namespace ov::npuw::s11n;WeightsContext ctx(false, {});- const auto malformed_blob = make_blob_with_metadata_size(false, /*closure_size=*/1u, /*is_remote_size=*/1u,+ const auto malformed_blob = make_blob_with_metadata_size(false,+ /*closure_size=*/1u,+ /*is_remote_size=*/1u,/*closure_uid_size=*/2u);std::stringstream input(malformed_blob, std::ios::in | std::ios::out | std::ios::binary);- OV_EXPECT_THROW_HAS_SUBSTRING(ov::npuw::tests::CompiledModelTestAccess::deserialize_compiled_model_desc(input, ctx),- ov::Exception,- kExpectedClosureUidSizeMessage);+ OV_EXPECT_THROW_HAS_SUBSTRING(+ ov::npuw::CompiledModelDescSerializationAccess::deserialize_compiled_model_desc(input, ctx),+ ov::Exception,+ kExpectedClosureUidSizeMessage);}TEST(SerializationTest, CompiledModelDesc_rejects_closure_uid_size_mismatch_weightless) {using namespace ov::npuw::s11n;WeightsContext ctx(true, {});- const auto malformed_blob = make_blob_with_metadata_size(true, /*closure_size=*/1u, /*is_remote_size=*/1u,+ const auto malformed_blob = make_blob_with_metadata_size(true,+ /*closure_size=*/1u,+ /*is_remote_size=*/1u,/*closure_uid_size=*/2u);std::stringstream input(malformed_blob, std::ios::in | std::ios::out | std::ios::binary);- OV_EXPECT_THROW_HAS_SUBSTRING(ov::npuw::tests::CompiledModelTestAccess::deserialize_compiled_model_desc(input, ctx),- ov::Exception,- kExpectedClosureUidSizeMessage);+ OV_EXPECT_THROW_HAS_SUBSTRING(+ ov::npuw::CompiledModelDescSerializationAccess::deserialize_compiled_model_desc(input, ctx),+ ov::Exception,+ kExpectedClosureUidSizeMessage);}TEST(SerializationTest, CompiledModelDesc_rejects_is_remote_size_mismatch_weightful) {using namespace ov::npuw::s11n;WeightsContext ctx(false, {});- const auto malformed_blob = make_blob_with_metadata_size(false, /*closure_size=*/1u, /*is_remote_size=*/2u,+ const auto malformed_blob = make_blob_with_metadata_size(false,+ /*closure_size=*/1u,+ /*is_remote_size=*/2u,/*closure_uid_size=*/1u);std::stringstream input(malformed_blob, std::ios::in | std::ios::out | std::ios::binary);- OV_EXPECT_THROW_HAS_SUBSTRING(ov::npuw::tests::CompiledModelTestAccess::deserialize_compiled_model_desc(input, ctx),- ov::Exception,- kExpectedIsRemoteSizeMessage);+ OV_EXPECT_THROW_HAS_SUBSTRING(+ ov::npuw::CompiledModelDescSerializationAccess::deserialize_compiled_model_desc(input, ctx),+ ov::Exception,+ kExpectedIsRemoteSizeMessage);}TEST(SerializationTest, CompiledModelDesc_rejects_is_remote_size_mismatch_weightless) {using namespace ov::npuw::s11n;WeightsContext ctx(true, {});- const auto malformed_blob = make_blob_with_metadata_size(true, /*closure_size=*/1u, /*is_remote_size=*/2u,+ const auto malformed_blob = make_blob_with_metadata_size(true,+ /*closure_size=*/1u,+ /*is_remote_size=*/2u,/*closure_uid_size=*/1u);std::stringstream input(malformed_blob, std::ios::in | std::ios::out | std::ios::binary);- OV_EXPECT_THROW_HAS_SUBSTRING(ov::npuw::tests::CompiledModelTestAccess::deserialize_compiled_model_desc(input, ctx),- ov::Exception,- kExpectedIsRemoteSizeMessage);+ OV_EXPECT_THROW_HAS_SUBSTRING(+ ov::npuw::CompiledModelDescSerializationAccess::deserialize_compiled_model_desc(input, ctx),+ ov::Exception,+ kExpectedIsRemoteSizeMessage);}TEST(SerializationTest, CompiledModelDesc_rejects_weightless_cpu_closure_count_mismatch) {@@ -1588,9 +1725,10 @@ TEST(SerializationTest, CompiledModelDesc_rejects_weightless_cpu_closure_count_m/*non_cpu_index=*/0u);std::stringstream input(malformed_blob, std::ios::in | std::ios::out | std::ios::binary);- OV_EXPECT_THROW_HAS_SUBSTRING(ov::npuw::tests::CompiledModelTestAccess::deserialize_compiled_model_desc(input, ctx),- ov::Exception,- kExpectedCpuCountMismatchMessage);+ OV_EXPECT_THROW_HAS_SUBSTRING(+ ov::npuw::CompiledModelDescSerializationAccess::deserialize_compiled_model_desc(input, ctx),+ ov::Exception,+ kExpectedCpuCountMismatchMessage);}TEST(SerializationTest, CompiledModelDesc_rejects_weightless_non_cpu_closure_count_mismatch) {@@ -1606,9 +1744,10 @@ TEST(SerializationTest, CompiledModelDesc_rejects_weightless_non_cpu_closure_cou/*non_cpu_index=*/0u);std::stringstream input(malformed_blob, std::ios::in | std::ios::out | std::ios::binary);- OV_EXPECT_THROW_HAS_SUBSTRING(ov::npuw::tests::CompiledModelTestAccess::deserialize_compiled_model_desc(input, ctx),- ov::Exception,- kExpectedNonCpuCountMismatchMessage);+ OV_EXPECT_THROW_HAS_SUBSTRING(+ ov::npuw::CompiledModelDescSerializationAccess::deserialize_compiled_model_desc(input, ctx),+ ov::Exception,+ kExpectedNonCpuCountMismatchMessage);}TEST(SerializationTest, CompiledModelDesc_rejects_weightless_non_cpu_closure_index_out_of_range) {@@ -1624,9 +1763,10 @@ TEST(SerializationTest, CompiledModelDesc_rejects_weightless_non_cpu_closure_ind/*non_cpu_index=*/5u);std::stringstream input(malformed_blob, std::ios::in | std::ios::out | std::ios::binary);- OV_EXPECT_THROW_HAS_SUBSTRING(ov::npuw::tests::CompiledModelTestAccess::deserialize_compiled_model_desc(input, ctx),- ov::Exception,- kExpectedNonCpuOobIndexMessage);+ OV_EXPECT_THROW_HAS_SUBSTRING(+ ov::npuw::CompiledModelDescSerializationAccess::deserialize_compiled_model_desc(input, ctx),+ ov::Exception,+ kExpectedNonCpuOobIndexMessage);}// Sub-byte coverage: the byte size is the *packed* size (ov::util::get_memory_size), which is what