Skip to content

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

[NPUW] Weights bank operate with UIDs (#28282)

Alexey Smirnov13a8d705b48d
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 de9588d01f3..a85277b636b 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp
@@ -344,10 +344,11 @@ ov::npuw::CompiledModel::CompiledModel(const std::shared_ptr<ov::Model>& model,
m_compiled_submodels[id].param_base = fcn_template._param_offset;
m_compiled_submodels[id].closure = subgraph._closure;
m_compiled_submodels[id].lazy_closure = subgraph._lazy_closure;
+ m_compiled_submodels[id].closure_uid.resize(m_compiled_submodels[id].closure.size(), -1);
m_compiled_submodels[id].scales = subgraph._scales;
m_compiled_submodels[id].zerops = subgraph._zerops;
m_compiled_submodels[id].forced_to_fcall = subgraph._forced_to_fcall;
- m_compiled_submodels[id].is_remote.resize(subgraph._lazy_closure.size(), false);
+ m_compiled_submodels[id].is_remote.resize(m_compiled_submodels[id].closure.size(), false);
} // if(!funcall)
if (!m_compiled_submodels[id].model && !m_compiled_submodels[id].replaced_by) {
@@ -503,7 +504,8 @@ void ov::npuw::CompiledModel::finalize_weights_bank() {
if (comp_model_desc.closure[tidx]) {
continue; // host-side closure
}
- m_weights_bank->registerLT(comp_model_desc.lazy_closure[tidx], *func_desc.device_it);
+ comp_model_desc.closure_uid[tidx] =
+ m_weights_bank->registerLT(comp_model_desc.lazy_closure[tidx], *func_desc.device_it);
}
}
@@ -522,16 +524,17 @@ void ov::npuw::CompiledModel::finalize_weights_bank() {
const auto real_idx = comp_model_desc.replaced_by.value_or(idx);
auto& func_desc = m_compiled_submodels[real_idx];
- for (std::size_t tidx = 0; tidx < comp_model_desc.lazy_closure.size(); ++tidx) {
+ for (std::size_t tidx = 0; tidx < comp_model_desc.closure.size(); ++tidx) {
if (comp_model_desc.closure[tidx]) {
// host-side closure - already set, do nothing
comp_model_desc.is_remote[tidx] = false;
continue;
}
- const auto& lt = comp_model_desc.lazy_closure[tidx];
- comp_model_desc.closure[tidx] = m_weights_bank->get(lt, *func_desc.device_it);
+ const auto& uid = comp_model_desc.closure_uid[tidx];
+ NPUW_ASSERT(uid != -1); // All tensors should be registered at this point
+ comp_model_desc.closure[tidx] = m_weights_bank->get(uid, *func_desc.device_it);
// FIXME: find a more reliable way to do so
- comp_model_desc.is_remote[tidx] = m_weights_bank->is_remote(lt);
+ comp_model_desc.is_remote[tidx] = m_weights_bank->is_remote(uid);
}
}
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 0e728570eda..f1de81f51d8 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hpp
@@ -141,6 +141,7 @@ private:
// lazy_closure is used for weights sharing and allocating device memory.
std::vector<ov::Tensor> closure;
std::vector<weights::LazyTensor> lazy_closure;
+ std::vector<int64_t> closure_uid;
std::vector<ov::Tensor> scales;
std::vector<ov::Tensor> zerops;
std::vector<bool> is_remote;
diff --git a/src/plugins/intel_npu/src/plugin/npuw/weights_bank.cpp b/src/plugins/intel_npu/src/plugin/npuw/weights_bank.cpp
index ddc8f10ccf0..7fded56c28c 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/weights_bank.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/weights_bank.cpp
@@ -35,38 +35,40 @@ private:
std::mutex m_mutex;
};
-ov::Tensor Bank::get(const LazyTensor& tensor, const std::string& device) {
+int64_t Bank::registerLT(const LazyTensor& tensor, const std::string& device) {
const std::string& device_for_alloc = m_alloc_device.empty() ? device : m_alloc_device;
std::lock_guard<std::mutex> guard(m_mutex);
auto& device_bank = m_device_banks[device_for_alloc];
-
- std::unique_lock<std::mutex> dev_guard(device_bank.mutex);
- auto iter_device = device_bank.storage.find(tensor);
-
- if (iter_device != device_bank.storage.end() && iter_device->second) {
- // Already allocated
- // tensor (the key) may be coming from a 2nd (3rd, ...) model
- // detach it here just in case
- const_cast<LazyTensor&>(tensor).detach();
- return iter_device->second;
+ auto iter_registered = device_bank.registered_tensors.find(tensor);
+ if (iter_registered == device_bank.registered_tensors.end()) {
+ auto uid = uid_count++;
+ device_bank.registered_tensors[tensor] = uid;
+ device_bank.storage[uid] = {tensor, ov::Tensor()};
+ return uid;
}
- dev_guard.unlock();
- // Allocation and evaluation needed
- return eval_and_alloc(tensor, device_bank, device_for_alloc);
+ return iter_registered->second;
}
-void Bank::registerLT(const LazyTensor& tensor, const std::string& device) {
+ov::Tensor Bank::get(int64_t uid, const std::string& device) {
const std::string& device_for_alloc = m_alloc_device.empty() ? device : m_alloc_device;
std::lock_guard<std::mutex> guard(m_mutex);
auto& device_bank = m_device_banks[device_for_alloc];
- if (device_bank.storage.find(tensor) == device_bank.storage.end()) {
- device_bank.storage[tensor] = ov::Tensor();
- }
+
+ std::unique_lock<std::mutex> dev_guard(device_bank.mutex);
+ auto iter_device = device_bank.storage.find(uid);
+
+ NPUW_ASSERT(iter_device != device_bank.storage.end() && iter_device->second.tensor &&
+ "Tensor should be registered and allocated first!");
+
+ // uid may be coming from a 2nd (3rd, ...) model
+ // detach the tensor here just in case
+ const_cast<LazyTensor&>(iter_device->second.lt).detach();
+ return iter_device->second.tensor;
}
void Bank::evaluate_and_allocate() {
@@ -79,13 +81,15 @@ void Bank::evaluate_and_allocate() {
std::vector<LazyTensor> vec;
vec.reserve(device_bank.storage.size());
for (const auto& el : device_bank.storage) {
- vec.push_back(el.first);
+ vec.push_back(el.second.lt);
}
ov::parallel_for(vec.size(), [&](std::size_t idx) {
const auto& lt = vec[idx];
std::unique_lock dev_guard(device_bank.mutex);
- auto iter_device = device_bank.storage.find(lt);
- if (iter_device != device_bank.storage.end() && iter_device->second) {
+ auto iter_device_registered = device_bank.registered_tensors.find(lt);
+ NPUW_ASSERT(iter_device_registered != device_bank.registered_tensors.end() &&
+ "Tensor should be registered first!");
+ if (device_bank.storage[iter_device_registered->second].tensor) {
// Already allocated
return;
}
@@ -106,7 +110,7 @@ ov::Tensor Bank::eval_and_alloc(const LazyTensor& tensor,
std::unique_lock<std::mutex> guard(dbank.mutex);
if (device_for_alloc == "CPU") {
- dbank.storage[tensor] = transformed_tensor;
+ dbank.storage[dbank.registered_tensors.at(tensor)].tensor = transformed_tensor;
return transformed_tensor;
}
@@ -117,7 +121,7 @@ ov::Tensor Bank::eval_and_alloc(const LazyTensor& tensor,
remote_tensor =
remote_ctx->create_host_tensor(transformed_tensor.get_element_type(), transformed_tensor.get_shape());
allocated_tensor = ov::make_tensor(remote_tensor);
- dbank.storage[tensor] = allocated_tensor;
+ dbank.storage[dbank.registered_tensors.at(tensor)].tensor = allocated_tensor;
guard.unlock(); // Unlock the guard, map update is done - copy can continue in parallel
transformed_tensor.copy_to(allocated_tensor);
@@ -130,14 +134,14 @@ ov::Tensor Bank::eval_and_alloc(const LazyTensor& tensor,
return allocated_tensor;
}
-bool Bank::is_remote(const LazyTensor& tensor) const {
+bool Bank::is_remote(int64_t uid) const {
// FIXME: make generic
std::lock_guard<std::mutex> guard(m_mutex);
auto npu_bank = m_device_banks.find("NPU");
if (npu_bank != m_device_banks.end()) {
std::lock_guard<std::mutex> dev_guard(npu_bank->second.mutex);
- if (npu_bank->second.storage.find(tensor) != npu_bank->second.storage.end()) {
+ if (npu_bank->second.storage.find(uid) != npu_bank->second.storage.end()) {
// Found in NPU bank so considered remote (utterly wrong for the generic case)
return true;
}
diff --git a/src/plugins/intel_npu/src/plugin/npuw/weights_bank.hpp b/src/plugins/intel_npu/src/plugin/npuw/weights_bank.hpp
index f2ca0436607..cc5489c6ae6 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/weights_bank.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/weights_bank.hpp
@@ -26,18 +26,26 @@ public:
: m_core(core),
m_alloc_device(alloc_device) {}
- // Allocate a new tensor (if needed) on a specified device. LazyTensor needs to be registered and evaluated first
- ov::Tensor get(const LazyTensor& tensor, const std::string& device);
- // Register LazyTensor in a bank if it's not there
- void registerLT(const LazyTensor& tensor, const std::string& device);
+ // Register LazyTensor in a bank if it's not there. Returns LazyTensor's unique id
+ int64_t registerLT(const LazyTensor& tensor, const std::string& device);
+
+ // Allocate and evaluate a registered tensor on a specified device (if needed) and return it from the bank
+ ov::Tensor get(int64_t uid, const std::string& device);
+
// Evaluate and allocate all LazyTensors in the bank
void evaluate_and_allocate();
- bool is_remote(const LazyTensor& tensor) const;
+
+ bool is_remote(int64_t uid) const;
private:
+ struct StoredTensor {
+ LazyTensor lt;
+ ov::Tensor tensor;
+ };
// Bank for specified device and their allocated memory
struct DeviceBank {
- std::unordered_map<LazyTensor, ov::Tensor, LazyTensor::Hash> storage;
+ std::unordered_map<int64_t, StoredTensor> storage;
+ std::unordered_map<LazyTensor, int64_t, LazyTensor::Hash> registered_tensors;
mutable std::mutex mutex;
};
std::unordered_map<std::string, DeviceBank> m_device_banks;
@@ -47,6 +55,7 @@ private:
mutable std::mutex m_mutex;
std::shared_ptr<const ov::ICore> m_core = nullptr;
std::string m_alloc_device;
+ int64_t uid_count = 0;
};
std::shared_ptr<Bank> bank(const std::string& bank_name,