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 memory consumption with bank uids (#28382)
Alexey Smirnov7cf3e8c1feb2
diff --git a/src/plugins/intel_npu/src/plugin/npuw/weights_bank.cpp b/src/plugins/intel_npu/src/plugin/npuw/weights_bank.cppindex 21b575fe54a..fb7faabbd42 100644--- a/src/plugins/intel_npu/src/plugin/npuw/weights_bank.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/weights_bank.cpp@@ -50,6 +50,9 @@ int64_t Bank::registerLT(const LazyTensor& tensor, const std::string& device) {device_bank.registered_tensors[tensor] = uid;device_bank.storage[uid] = {tensor, ov::Tensor()};return uid;+ } else {+ // Already registered - can be safely detach the incoming tensor+ const_cast<LazyTensor&>(tensor).detach();}return iter_registered->second;@@ -85,9 +88,11 @@ void Bank::evaluate_and_allocate() {std::unique_lock storage_guard(device_bank.mutex);vec.reserve(device_bank.storage.size());- // FIXME: only add non-allocated tensors herefor (const auto& el : device_bank.storage) {- vec.push_back(el.second.lt);+ // Add non-allocated tensors for furter evaluation and allocation+ if (!el.second.tensor) {+ vec.push_back(el.second.lt);+ }}storage_guard.unlock();@@ -103,43 +108,36 @@ void Bank::evaluate_and_allocate() {}dev_guard.unlock();- // Allocation and evaluation needed- eval_and_alloc(lt, device_bank, device_for_alloc);- });- }-}+ // Allocation and/or evaluation needed+ // Evaluate concurrently, lock the device+ // mutex only to update the device bank (& allocate on-device memory, if needed)+ const auto& transformed_tensor = lt.eval();-ov::Tensor Bank::eval_and_alloc(const LazyTensor& tensor,- Bank::DeviceBank& dbank,- const std::string& device_for_alloc) {- // Evaluate concurrently (see evaluate_and_allocate), lock the device- // mutex only to update the device bank (& allocate on-device memory, if needed)- const auto& transformed_tensor = tensor.eval();-- std::unique_lock<std::mutex> guard(dbank.mutex);- if (device_for_alloc == "CPU") {- dbank.storage[dbank.registered_tensors.at(tensor)].tensor = transformed_tensor;- return transformed_tensor;- }-- ov::SoPtr<ov::ITensor> remote_tensor;- ov::Tensor allocated_tensor;+ std::unique_lock<std::mutex> guard(device_bank.mutex);+ if (device_for_alloc == "CPU") {+ // No allocation needed+ device_bank.storage[device_bank.registered_tensors.at(lt)].tensor = transformed_tensor;+ return;+ }- auto remote_ctx = m_core->get_default_context(device_for_alloc)._ptr;- 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[dbank.registered_tensors.at(tensor)].tensor = allocated_tensor;- guard.unlock(); // Unlock the guard, map update is done - copy can continue in parallel+ ov::SoPtr<ov::ITensor> remote_tensor;+ ov::Tensor allocated_tensor;- transformed_tensor.copy_to(allocated_tensor);+ auto remote_ctx = m_core->get_default_context(device_for_alloc)._ptr;+ remote_tensor =+ remote_ctx->create_host_tensor(transformed_tensor.get_element_type(), transformed_tensor.get_shape());+ allocated_tensor = ov::make_tensor(remote_tensor);+ device_bank.storage[device_bank.registered_tensors.at(lt)].tensor = allocated_tensor;+ guard.unlock(); // Unlock the guard, map update is done - copy can continue in parallel- // Detach the evaluated LazyTensor from its memory here - when it is 100%- // not needed anymore (transformations, if any, and copies are done)- // Note: this is the non-CPU path!- const_cast<LazyTensor&>(tensor).detach();+ transformed_tensor.copy_to(allocated_tensor);- return allocated_tensor;+ // Detach the evaluated LazyTensor from its memory here - when it is 100%+ // not needed anymore (transformations, if any, and copies are done)+ // Note: this is the non-CPU path!+ const_cast<LazyTensor&>(lt).detach();+ });+ }}bool Bank::is_remote(int64_t uid) const {diff --git a/src/plugins/intel_npu/src/plugin/npuw/weights_bank.hpp b/src/plugins/intel_npu/src/plugin/npuw/weights_bank.hppindex fd9f0e39841..c276a54e772 100644--- a/src/plugins/intel_npu/src/plugin/npuw/weights_bank.hpp+++ b/src/plugins/intel_npu/src/plugin/npuw/weights_bank.hpp@@ -61,8 +61,6 @@ private:};std::unordered_map<std::string, DeviceBank> m_device_banks;- ov::Tensor eval_and_alloc(const LazyTensor& tensor, DeviceBank& dbank, const std::string& device);-void serialize(std::ostream& stream) const;static std::shared_ptr<Bank> deserialize(std::istream& stream,const std::shared_ptr<const ov::ICore>& core,