Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[NPUW] Introduce weights bank (#25927)

Alexey Smirnov7dbceea2fce7
diff --git a/src/plugins/intel_npu/src/al/include/intel_npu/al/config/npuw.hpp b/src/plugins/intel_npu/src/al/include/intel_npu/al/config/npuw.hpp
index c09a17640f7..2ce67a71a5d 100644
--- a/src/plugins/intel_npu/src/al/include/intel_npu/al/config/npuw.hpp
+++ b/src/plugins/intel_npu/src/al/include/intel_npu/al/config/npuw.hpp
@@ -45,6 +45,7 @@ DEFINE_OPT(NPUW_DCOFF_TYPE, std::string, "", npuw::partitioning::dcoff_type, Com
DEFINE_OPT(NPUW_DCOFF_SCALE, bool, false, npuw::partitioning::dcoff_with_scale, CompileTime);
DEFINE_OPT(NPUW_FUNCALL_FOR_ALL, bool, false, npuw::partitioning::funcall_for_all, CompileTime);
DEFINE_OPT(NPUW_PARALLEL_COMPILE, bool, false, npuw::parallel_compilation, CompileTime);
+DEFINE_OPT(NPUW_WEIGHTS_BANK, std::string, "", npuw::weights_bank, CompileTime);
DEFINE_OPT(NPUW_FUNCALL_ASYNC, bool, false, npuw::funcall_async, RunTime);
DEFINE_OPT(NPUW_ACC_CHECK, bool, false, npuw::accuracy::check, RunTime);
DEFINE_OPT(NPUW_ACC_THRESH, double, 0.01, npuw::accuracy::threshold, RunTime);
diff --git a/src/plugins/intel_npu/src/al/include/npuw_private_properties.hpp b/src/plugins/intel_npu/src/al/include/npuw_private_properties.hpp
index 12a4fa40e67..627b6b957eb 100644
--- a/src/plugins/intel_npu/src/al/include/npuw_private_properties.hpp
+++ b/src/plugins/intel_npu/src/al/include/npuw_private_properties.hpp
@@ -36,6 +36,15 @@ static constexpr ov::Property<std::string> devices{"NPUW_DEVICES"};
*/
static constexpr ov::Property<std::string> submodel_device{"NPUW_SUBMODEL_DEVICE"};
+/**
+ * @brief
+ * Type: std::string.
+ * Specify bank name to utilize for a particular model.
+ * Possible values: any std::string as a name.
+ * Default value: empty.
+ */
+static constexpr ov::Property<std::string> weights_bank{"NPUW_WEIGHTS_BANK"};
+
namespace partitioning {
namespace online {
/**
diff --git a/src/plugins/intel_npu/src/al/src/config/npuw.cpp b/src/plugins/intel_npu/src/al/src/config/npuw.cpp
index 276d0c2de84..a43794e8833 100644
--- a/src/plugins/intel_npu/src/al/src/config/npuw.cpp
+++ b/src/plugins/intel_npu/src/al/src/config/npuw.cpp
@@ -31,6 +31,7 @@ void intel_npu::registerNPUWOptions(OptionsDesc& desc) {
desc.add<NPUW_FUNCALL_FOR_ALL>();
desc.add<NPUW_PARALLEL_COMPILE>();
desc.add<NPUW_FUNCALL_ASYNC>();
+ desc.add<NPUW_WEIGHTS_BANK>();
desc.add<NPUW_ACC_CHECK>();
desc.add<NPUW_ACC_THRESH>();
desc.add<NPUW_ACC_DEVICE>();
diff --git a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp
index da8641bd06e..721f03185a8 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp
@@ -112,6 +112,10 @@ ov::npuw::CompiledModel::CompiledModel(const std::shared_ptr<ov::Model>& model,
LOG_INFO("Accuracy check is enabled.");
}
+ // Initialize weights bank
+ const std::string weights_bank_opt = m_cfg.get<::intel_npu::NPUW_WEIGHTS_BANK>();
+ m_weights_bank = ov::npuw::weights::bank(weights_bank_opt);
+
LOG_VERB("*** Original model ***");
const auto& orig_parameters = model->get_parameters();
{
@@ -271,6 +275,8 @@ ov::npuw::CompiledModel::CompiledModel(const std::shared_ptr<ov::Model>& model,
m_compiled_submodels[id].closure = subgraph._closure;
m_compiled_submodels[id].scales = subgraph._scales;
m_compiled_submodels[id].zerops = subgraph._zerops;
+ m_compiled_submodels[id].update_required.resize(subgraph._closure.size(), false);
+ fill_weights_bank(id);
} // if(!funcall)
if (!m_compiled_submodels[id].model && !m_compiled_submodels[id].replaced_by) {
@@ -387,6 +393,26 @@ ov::npuw::CompiledModel::CompiledModel(const std::shared_ptr<ov::Model>& model,
reset_io();
}
+void ov::npuw::CompiledModel::fill_weights_bank(const std::size_t idx) {
+ LOG_VERB("Filling weights bank for Subgraph[" << idx << "]...");
+ LOG_BLOCK();
+
+ NPUW_ASSERT(m_compiled_submodels[idx].replaced_by);
+
+ auto& comp_model_desc = m_compiled_submodels[idx];
+
+ for (std::size_t cidx = 0u; cidx < comp_model_desc.closure.size(); cidx++) {
+ comp_model_desc.closure[cidx] = m_weights_bank->get(comp_model_desc.closure[cidx]);
+ if (m_cfg.get<::intel_npu::NPUW_FOLD>()) {
+ comp_model_desc.update_required[cidx] = true;
+ } else {
+ comp_model_desc.update_required[cidx] = false;
+ }
+ }
+
+ LOG_VERB("DONE");
+}
+
void ov::npuw::CompiledModel::remove_long_output_names(const std::shared_ptr<ov::Model>& model) {
NPUW_ASSERT(model.get() != nullptr);
for (auto& output : model->outputs()) {
@@ -772,10 +798,11 @@ void ov::npuw::CompiledModel::implement_properties() {
BIND(npuw::partitioning::fold, NPUW_FOLD),
BIND(npuw::partitioning::cwai, NPUW_CWAI),
BIND(npuw::partitioning::funcall_for_all, NPUW_FUNCALL_FOR_ALL),
- BIND(npuw::parallel_compilation, NPUW_PARALLEL_COMPILE),
BIND(npuw::partitioning::dcoff_type, NPUW_DCOFF_TYPE),
BIND(npuw::partitioning::dcoff_with_scale, NPUW_DCOFF_SCALE),
+ BIND(npuw::parallel_compilation, NPUW_PARALLEL_COMPILE),
BIND(npuw::funcall_async, NPUW_FUNCALL_ASYNC),
+ BIND(npuw::weights_bank, NPUW_WEIGHTS_BANK),
BIND(npuw::accuracy::check, NPUW_ACC_CHECK),
BIND(npuw::accuracy::threshold, NPUW_ACC_THRESH),
BIND(npuw::accuracy::reference_device, NPUW_ACC_DEVICE),
diff --git a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hpp b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hpp
index ef260c91419..c2e9a9a6235 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hpp
@@ -13,6 +13,7 @@
#include "openvino/runtime/icompiled_model.hpp"
#include "openvino/runtime/so_ptr.hpp"
#include "partitioning/partitioning.hpp"
+#include "weights_bank.hpp"
namespace intel_npu {
class Plugin;
@@ -71,6 +72,8 @@ private:
void implement_properties();
+ void fill_weights_bank(const std::size_t idx);
+
std::shared_ptr<::intel_npu::OptionsDesc> m_options_desc;
::intel_npu::Config m_cfg;
GetPropertiesMap m_prop_to_opt;
@@ -119,6 +122,7 @@ private:
std::vector<ov::Tensor> closure;
std::vector<ov::Tensor> scales;
std::vector<ov::Tensor> zerops;
+ std::vector<bool> update_required;
// FIXME: Take it out of structure
ov::SoPtr<ov::ICompiledModel> ref_compiled_model;
@@ -133,6 +137,8 @@ private:
std::string m_ref_device;
execution_stats m_total_stat;
+
+ std::shared_ptr<weights::Bank> m_weights_bank = nullptr;
};
} // namespace npuw
} // namespace ov
diff --git a/src/plugins/intel_npu/src/plugin/npuw/just_sync_infer_request.cpp b/src/plugins/intel_npu/src/plugin/npuw/just_sync_infer_request.cpp
index c47962dc475..e64597abffe 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/just_sync_infer_request.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/just_sync_infer_request.cpp
@@ -18,6 +18,7 @@
#include "openvino/runtime/make_tensor.hpp"
#include "plugin.hpp"
#include "util.hpp"
+#include "weights_bank.hpp"
ov::npuw::JustInferRequest::JustInferRequest(const std::shared_ptr<ov::npuw::CompiledModel>& compiled_model)
: IBaseInferRequest(compiled_model) {
@@ -168,6 +169,35 @@ ov::npuw::JustInferRequest::JustInferRequest(const std::shared_ptr<ov::npuw::Com
m_subrequests_gio.at(sub_idx).global_results[i] = out_idx;
}
// }}}
+
+ // Sort out how to handle weights bank and unpack
+ for (size_t i = 0; i < m_num_submodels; i++) {
+ LOG_VERB("Trying to preemptively set tensors for Subgraph[" << i << "]...");
+ LOG_BLOCK();
+ auto& comp_model_desc = m_npuw_model->m_compiled_submodels[i];
+
+ if (!comp_model_desc.compiled_model || !comp_model_desc.replaced_by) {
+ continue;
+ }
+
+ const auto real_idx = comp_model_desc.replaced_by.value();
+ auto& func_desc = m_npuw_model->m_compiled_submodels[real_idx];
+
+ auto& request = m_subrequests[real_idx];
+
+ for (std::size_t cidx = 0u; cidx < comp_model_desc.closure.size(); cidx++) {
+ const auto& closure = comp_model_desc.closure[cidx];
+
+ const auto closure_param_id = comp_model_desc.param_base + cidx;
+ const auto& iport = func_desc.compiled_model->inputs()[closure_param_id];
+
+ // No update required to this tensor in runtime - so it can be set only once
+ if (!comp_model_desc.update_required[cidx]) {
+ request->set_tensor(iport, ov::get_tensor_impl(closure));
+ }
+ } // for(closure)
+ LOG_VERB("DONE");
+ }
}
void ov::npuw::JustInferRequest::connect_subrequests() {
@@ -452,11 +482,11 @@ void ov::npuw::JustInferRequest::unpack_closure(std::size_t idx, RqPtr request)
const auto closure_param_id = comp_model_desc.param_base + cidx;
auto& iport = func_desc.compiled_model->inputs()[closure_param_id];
- auto clparam = request->get_tensor(iport);
+ const auto& clparam = request->get_tensor(iport);
if (closure.get_element_type() != clparam->get_element_type()) {
// Remember where the unpack is required
closure_unpack_required.push_back(cidx);
- } else {
+ } else if (comp_model_desc.update_required[cidx]) {
// Easy case, just set one to another. Copy_to is also possible
// and even may be preferrable for some devices, like this:
// ```ov::get_tensor_impl(closure)->copy_to(clparam._ptr);'''
diff --git a/src/plugins/intel_npu/src/plugin/npuw/weights_bank.cpp b/src/plugins/intel_npu/src/plugin/npuw/weights_bank.cpp
new file mode 100644
index 00000000000..38a2fdd42a4
--- /dev/null
+++ b/src/plugins/intel_npu/src/plugin/npuw/weights_bank.cpp
@@ -0,0 +1,66 @@
+// Copyright (C) 2024 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "weights_bank.hpp"
+
+using ov::npuw::weights::Bank;
+
+class BankManager {
+public:
+ static BankManager& getInstance() {
+ static BankManager instance;
+ return instance;
+ }
+
+private:
+ BankManager() {}
+ BankManager(BankManager const&) = delete;
+ void operator=(BankManager const&) = delete;
+
+public:
+ // Public API
+ std::shared_ptr<Bank> getBank(const std::string& bank_name);
+
+private:
+ // Data
+ std::unordered_map<std::string, std::weak_ptr<Bank>> m_bank_map;
+ std::mutex m_mutex;
+};
+
+ov::Tensor Bank::get(const ov::Tensor& tensor) {
+ if (!tensor) {
+ OPENVINO_THROW("Uninitialized tensor in weights bank allocation!");
+ }
+
+ std::lock_guard<std::mutex> guard(m_mutex);
+
+ if (m_bank.find(tensor.data()) == m_bank.end()) {
+ // need to allocate first
+ m_bank[tensor.data()] = tensor;
+ }
+
+ return tensor;
+}
+
+std::shared_ptr<Bank> BankManager::getBank(const std::string& bank_name) {
+ std::lock_guard<std::mutex> guard(m_mutex);
+
+ auto iter = m_bank_map.find(bank_name);
+ if (iter == m_bank_map.end()) {
+ auto bank = std::make_shared<Bank>();
+ m_bank_map[bank_name] = bank;
+ return bank;
+ }
+ return iter->second.lock();
+}
+
+std::shared_ptr<Bank> ov::npuw::weights::bank(const std::string& bank_name) {
+ if (bank_name.empty()) {
+ // Don't share this bank in manager
+ return std::make_shared<Bank>();
+ }
+
+ auto& instance = BankManager::getInstance();
+ return instance.getBank(bank_name);
+}
diff --git a/src/plugins/intel_npu/src/plugin/npuw/weights_bank.hpp b/src/plugins/intel_npu/src/plugin/npuw/weights_bank.hpp
new file mode 100644
index 00000000000..1ddd00750d3
--- /dev/null
+++ b/src/plugins/intel_npu/src/plugin/npuw/weights_bank.hpp
@@ -0,0 +1,36 @@
+// Copyright (C) 2023 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include <memory>
+#include <mutex>
+#include <tuple>
+#include <unordered_map>
+
+#include "openvino/openvino.hpp"
+#include "openvino/runtime/make_tensor.hpp"
+#include "openvino/runtime/tensor.hpp"
+
+namespace ov {
+namespace npuw {
+namespace weights {
+
+class Bank {
+public:
+ Bank() = default;
+
+ ov::Tensor get(const ov::Tensor& tensor);
+
+private:
+ // Note: suits both - remote and ordinary tensors
+ std::unordered_map<void*, ov::Tensor> m_bank;
+ std::mutex m_mutex;
+};
+
+std::shared_ptr<Bank> bank(const std::string& bank_name);
+
+} // namespace weights
+} // namespace npuw
+} // namespace ov