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`] Fix broken NPUW blob compatibility (#39644)
Mircea-Aurelian Dane31618d01c26
diff --git a/src/plugins/intel_npu/src/plugin/npuw/weights_bank.cpp b/src/plugins/intel_npu/src/plugin/npuw/weights_bank.cppindex fb08c13c0e0..4d6ecd5b168 100644--- a/src/plugins/intel_npu/src/plugin/npuw/weights_bank.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/weights_bank.cpp@@ -4,6 +4,9 @@#include "weights_bank.hpp"+#include <algorithm>+#include <limits>+#include "logging.hpp"#include "openvino/core/parallel.hpp"#include "serialization.hpp"@@ -56,6 +59,7 @@ int64_t Bank::registerLT(const LazyTensor& tensor, const std::string& device) {auto iter_registered = device_bank.registered_tensors.find(tensor);if (iter_registered == device_bank.registered_tensors.end()) {+ NPUW_ASSERT(uid_count < std::numeric_limits<int64_t>::max() && "Weights bank uid space exhausted");auto uid = uid_count++;device_bank.registered_tensors[tensor] = uid;device_bank.storage[uid] = {tensor, ov::Tensor()};@@ -73,7 +77,24 @@ ov::Tensor Bank::get(int64_t uid, const std::string& device) {std::unique_lock guard(m_mutex);- auto& device_bank = m_device_banks.at(device_for_alloc);+ auto iter_bank =+ std::find_if(m_device_banks.begin(), m_device_banks.end(), [&device_for_alloc, uid](const auto& device_bank) {+ if (device_bank.second.storage.count(uid) != 1) {+ return false;+ }+ // Exact device key, or the un-indexed "NPU" key written by old blobs,+ // which serves any NPU.<n> request for the uids restored under it. uids are unique across+ // the bank, so at most one key can hold a given uid.+ return device_bank.first == device_for_alloc ||+ (device_bank.first == "NPU" && ov::npuw::util::starts_with(device_for_alloc, "NPU."));+ });+ OPENVINO_ASSERT(iter_bank != m_device_banks.end(),+ "NPUW: weights bank '",+ m_bank_name,+ "' has no storage for device '",+ device_for_alloc,+ "'");+ auto& device_bank = iter_bank->second;auto iter_device = device_bank.storage.find(uid);NPUW_ASSERT(iter_device != device_bank.storage.end() && iter_device->second.tensor &&@@ -142,7 +163,7 @@ void Bank::evaluate_and_allocate_on_device(Bank::DeviceBank& device_bank,// Note: not locking here. This is a private function, so Bank should handle the locks around it// as we lock in evaluate_and_allocate() now.std::vector<TensorToAllocate> uids_to_allocated;- uids_to_allocated.reserve(uid_count);+ uids_to_allocated.reserve(to_process.size());for (const auto& lt : to_process) {auto iter_device_registered = device_bank.registered_tensors.find(lt);@@ -230,15 +251,15 @@ void Bank::serialize(ov::npuw::orc::Stream& stream) {void Bank::read_and_add_tensor(ov::npuw::orc::Stream& stream, int64_t uid, const std::string& device) {// This method is supposed to be used only during deserializationstd::unique_lock guard(m_mutex);+ NPUW_ASSERT(uid >= 0 && "Malformed blob: negative weights bank uid");+ NPUW_ASSERT(static_cast<uint64_t>(uid) < static_cast<uint64_t>(std::numeric_limits<int64_t>::max()) &&+ "Malformed blob: weights bank uid exceeds max int value");- auto& device_bank = m_device_banks[device];- auto iter_device = device_bank.storage.find(uid);-- if (iter_device != device_bank.storage.end()) {- // Shouldn't be possible- NPUW_ASSERT(false);- return;+ for (const auto& bank : m_device_banks) {+ NPUW_ASSERT(bank.second.storage.count(uid) == 0 && "Malformed blob: duplicate weights bank uid");}+ auto& device_bank = m_device_banks[device];+ uid_count = std::max(uid_count, uid + 1);if (device == "CPU") {// Just read deserialized tensor into the bankdiff --git a/src/plugins/intel_npu/tests/unit/npuw/serialization.cpp b/src/plugins/intel_npu/tests/unit/npuw/serialization.cppindex 645a4757dd9..55f69a7d916 100644--- a/src/plugins/intel_npu/tests/unit/npuw/serialization.cpp+++ b/src/plugins/intel_npu/tests/unit/npuw/serialization.cpp@@ -37,6 +37,7 @@#include "openvino/util/mmap_object.hpp"#include "pyramid_attention.hpp"#include "spatial.hpp"+#include "unit_test_utils/mocks/openvino/runtime/mock_icore.hpp"#include "weights_bank.hpp"using ov::test::npuw::ModelBuilder;@@ -106,6 +107,25 @@ void expect_serialize_throws(const Gather& hg,writer.serialize(stream, {});}+class MockRemoteContext : public ov::IRemoteContext {+ std::string m_name;++public:+ MockRemoteContext(std::string name) : m_name(std::move(name)) {}+ const std::string& get_device_name() const override {+ return m_name;+ }+ MOCK_METHOD(ov::SoPtr<ov::ITensor>,+ create_host_tensor,+ (const ov::element::Type type, const ov::Shape& shape),+ (override));+ MOCK_METHOD(const ov::AnyMap&, get_property, (), (const, override));+ MOCK_METHOD(ov::SoPtr<ov::IRemoteTensor>,+ create_tensor,+ (const ov::element::Type&, const ov::Shape&, const ov::AnyMap&),+ (override));+};+class SerializationNullPlugin final : public ov::IPlugin {public:std::shared_ptr<ov::ICompiledModel> compile_model(const std::shared_ptr<const ov::Model>&,@@ -640,6 +660,19 @@ void expect_lazy_tensor_transform_types_equal(const ov::npuw::weights::LazyTenso}}+std::shared_ptr<ov::MockICore> make_host_tensor_core() {+ auto core = std::make_shared<testing::NiceMock<ov::MockICore>>();+ ON_CALL(*core, get_default_context(testing::_)).WillByDefault([](const std::string& device) {+ auto ctx = std::make_shared<testing::NiceMock<MockRemoteContext>>(device);+ ON_CALL(*ctx, create_host_tensor(testing::_, testing::_))+ .WillByDefault([](const ov::element::Type type, const ov::Shape& shape) {+ return ov::make_tensor(type, shape);+ });+ return ctx;+ });+ return core;+}+} // namespacenamespace ov::npuw {@@ -1774,8 +1807,7 @@ TEST(SerializationTest, OVTypes_LazyTensor_concat_permute_convert_roundtrip) {TEST(SerializationTest, OVTypes_LazyTensor_subtract128_roundtrip) {using namespace ov::npuw::s11n;- auto constant = make_weightless_constant<uint8_t>(ov::element::u8, ov::Shape{2, 3},- {0, 1, 127, 128, 254, 255}, 0);+ auto constant = make_weightless_constant<uint8_t>(ov::element::u8, ov::Shape{2, 3}, {0, 1, 127, 128, 254, 255}, 0);auto var = ov::npuw::weights::LazyTensor(constant).subtract_128();ov::npuw::weights::LazyTensor res;const auto expected = var.eval();@@ -1879,6 +1911,97 @@ TEST(SerializationTest, OVTypes_WeightsBank_cpu_roundtrip) {expect_tensors_equal(var.get(uid1, "CPU"), res.get(uid1, "CPU"));}+TEST(SerializationTest, WeightsBank_RejectsOutOfRangeUid) {+ for (const int64_t bad_uid : {int64_t{-1}, std::numeric_limits<int64_t>::max()}) {+ std::stringstream ss;+ auto w = ov::npuw::orc::Stream::writer(ss);+ std::size_t bank_size = 1, storage_size = 1;+ std::string device = "CPU";+ int64_t uid = bad_uid;+ w & bank_size & device & storage_size & uid;+ ov::npuw::weights::Bank res(nullptr, "", "bad");+ OV_EXPECT_THROW(ov::npuw::s11n::read(ss, res), ov::Exception, testing::HasSubstr("weights bank uid"));+ }+}++TEST(SerializationTest, WeightsBank_RejectsDuplicatedUid) {+ using namespace ov::npuw::s11n;++ auto first = make_weightless_constant<float>(ov::element::f32, ov::Shape{2}, {1.0f, 2.0f}, 0);+ auto second = make_weightless_constant<float>(ov::element::f32, ov::Shape{2}, {3.0f, 4.0f}, first->get_byte_size());++ auto mock_core = make_host_tensor_core();++ ov::npuw::weights::Bank var(nullptr, "CPU", "test-bank");+ std::ignore = var.registerLT(ov::npuw::weights::LazyTensor(first), "CPU");+ std::ignore = var.registerLT(ov::npuw::weights::LazyTensor(second), "CPU");+ var.evaluate_and_allocate();++ std::stringstream tmp;+ write(tmp, var);++ ov::npuw::weights::Bank res(mock_core, "", "restored-bank");+ read(tmp, res);++ auto legacy_blob = tmp.str();+ const auto pos = legacy_blob.find("CPU");+ ASSERT_NE(pos, std::string::npos);+ legacy_blob.replace(pos, 3, "NPU");++ std::stringstream ss(legacy_blob);+ OV_EXPECT_THROW(read(ss, res), ov::Exception, testing::HasSubstr("Malformed blob: duplicate weights bank uid"));+}++TEST(SerializationTest, WeightsBank_LegacyNpuKey_Restore) {+ using namespace ov::npuw::s11n;++ auto first = make_weightless_constant<float>(ov::element::f32, ov::Shape{2}, {1.0f, 2.0f}, 0);+ auto second = make_weightless_constant<float>(ov::element::f32, ov::Shape{2}, {3.0f, 4.0f}, first->get_byte_size());+ auto third = make_weightless_constant<float>(ov::element::f32,+ ov::Shape{2},+ {5.0f, 6.0f},+ first->get_byte_size() + second->get_byte_size());++ auto mock_core = make_host_tensor_core();++ ov::npuw::weights::Bank old_bank(nullptr, "CPU", "old");+ const auto uid0 = old_bank.registerLT(ov::npuw::weights::LazyTensor(first), "CPU");+ const auto uid1 = old_bank.registerLT(ov::npuw::weights::LazyTensor(second), "CPU");+ old_bank.evaluate_and_allocate();++ std::stringstream tmp;+ write(tmp, old_bank);+ auto legacy_blob = tmp.str();+ const auto pos = legacy_blob.find("CPU");+ ASSERT_NE(pos, std::string::npos);+ legacy_blob.replace(pos, 3, "NPU");++ for (const std::string alloc : {"", "NPU"}) {+ ov::npuw::weights::Bank res(mock_core, alloc, "restored-bank");+ std::stringstream ss(legacy_blob);+ read(ss, res);++ for (const std::string dev : {"NPU", "NPU.0", "NPU.1"}) {+ expect_tensors_equal(old_bank.get(uid0, "CPU"), res.get(uid0, dev));+ expect_tensors_equal(old_bank.get(uid1, "CPU"), res.get(uid1, dev));+ }+ if (alloc.empty()) {+ EXPECT_THROW(res.get(uid0, "GPU"), ov::Exception);+ const auto uid_other = res.registerLT(ov::npuw::weights::LazyTensor(third), "NPU.0");+ res.evaluate_and_allocate();+ OV_EXPECT_THROW(res.get(uid_other, "NPU.1"), ov::Exception, testing::HasSubstr("has no storage"));+ }++ const auto uid_new = res.registerLT(ov::npuw::weights::LazyTensor(third), "NPU.1");+ EXPECT_NE(uid_new, uid0);+ EXPECT_NE(uid_new, uid1);+ res.evaluate_and_allocate();+ expect_tensors_equal(old_bank.get(uid0, "CPU"), res.get(uid0, "NPU.1"));+ expect_tensors_equal(old_bank.get(uid1, "CPU"), res.get(uid1, "NPU.1"));+ expect_tensors_equal(ov::npuw::weights::LazyTensor(third).eval(), res.get(uid_new, "NPU.1"));+ }+}+TEST(SerializationTest, AllSentinelsPass) {EXPECT_NO_THROW(expect_serialize_valid({-1, -1, -1}, {-1, -1, -1, -1, -1}, 0, 0, 0));}