Skip to content

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

[NPUW] Support remote tensor in weights bank (#26034)

Alexey Smirnov08d8cbdfa68d
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 721f03185a8..6cfb7fd7ce1 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp
@@ -1,4 +1,4 @@
-// Copyright (C) 2023 Intel Corporation
+// Copyright (C) 2023-2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include "compiled_model.hpp"
@@ -114,7 +114,7 @@ ov::npuw::CompiledModel::CompiledModel(const std::shared_ptr<ov::Model>& model,
// 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);
+ m_weights_bank = ov::npuw::weights::bank(weights_bank_opt, plugin->get_core());
LOG_VERB("*** Original model ***");
const auto& orig_parameters = model->get_parameters();
@@ -402,7 +402,7 @@ void ov::npuw::CompiledModel::fill_weights_bank(const std::size_t idx) {
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]);
+ comp_model_desc.closure[cidx] = m_weights_bank->update(comp_model_desc.closure[cidx]);
if (m_cfg.get<::intel_npu::NPUW_FOLD>()) {
comp_model_desc.update_required[cidx] = true;
} else {
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 e64597abffe..6bd1b296c3a 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
@@ -1,4 +1,4 @@
-// Copyright (C) 2023 Intel Corporation
+// Copyright (C) 2023-2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
@@ -171,6 +171,7 @@ ov::npuw::JustInferRequest::JustInferRequest(const std::shared_ptr<ov::npuw::Com
// }}}
// Sort out how to handle weights bank and unpack
+ auto& wbank = m_npuw_model->m_weights_bank;
for (size_t i = 0; i < m_num_submodels; i++) {
LOG_VERB("Trying to preemptively set tensors for Subgraph[" << i << "]...");
LOG_BLOCK();
@@ -193,7 +194,7 @@ ov::npuw::JustInferRequest::JustInferRequest(const std::shared_ptr<ov::npuw::Com
// 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));
+ request->set_tensor(iport, ov::get_tensor_impl(wbank->get(closure, *func_desc.device_it)));
}
} // for(closure)
LOG_VERB("DONE");
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 38a2fdd42a4..08181f80e7f 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/weights_bank.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/weights_bank.cpp
@@ -20,7 +20,7 @@ private:
public:
// Public API
- std::shared_ptr<Bank> getBank(const std::string& bank_name);
+ std::shared_ptr<Bank> getBank(const std::string& bank_name, const std::shared_ptr<const ov::ICore>& core);
private:
// Data
@@ -28,7 +28,7 @@ private:
std::mutex m_mutex;
};
-ov::Tensor Bank::get(const ov::Tensor& tensor) {
+ov::Tensor Bank::update(const ov::Tensor& tensor) {
if (!tensor) {
OPENVINO_THROW("Uninitialized tensor in weights bank allocation!");
}
@@ -43,24 +43,63 @@ ov::Tensor Bank::get(const ov::Tensor& tensor) {
return tensor;
}
-std::shared_ptr<Bank> BankManager::getBank(const std::string& bank_name) {
+ov::Tensor Bank::get(const ov::Tensor& tensor, const std::string& device) {
+ if (!tensor) {
+ OPENVINO_THROW("Uninitialized tensor in weights bank allocation!");
+ }
+
+ if (device != "CPU" && device != "NPU") {
+ OPENVINO_THROW("Unsupported device in weights bank allocation: ", device);
+ }
+
+ std::lock_guard<std::mutex> guard(m_mutex);
+
+ auto iter_cpu = m_bank.find(tensor.data());
+ if (iter_cpu == m_bank.end()) {
+ OPENVINO_THROW("Unknown tensor in weights bank allocation!");
+ }
+
+ // If target device is CPU - just reuse the default bank
+ if (device == "CPU") {
+ return iter_cpu->second;
+ }
+
+ // Non-CPU - check if the tensor is already there
+ auto& device_bank = m_device_bank[device];
+ auto iter_device = device_bank.find(tensor.data());
+ if (iter_device != device_bank.end()) {
+ // Already allocated on the device - reuse
+ return iter_device->second;
+ }
+
+ // Allocation needed
+ m_remote_ctx = m_core->get_default_context(device)._ptr;
+ auto remote_tensor = m_remote_ctx->create_host_tensor(tensor.get_element_type(), tensor.get_shape());
+ auto allocated_tensor = ov::make_tensor(remote_tensor);
+ tensor.copy_to(allocated_tensor);
+ device_bank[tensor.data()] = allocated_tensor;
+ return allocated_tensor;
+}
+
+std::shared_ptr<Bank> BankManager::getBank(const std::string& bank_name, const std::shared_ptr<const ov::ICore>& core) {
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>();
+ auto bank = std::make_shared<Bank>(core);
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) {
+std::shared_ptr<Bank> ov::npuw::weights::bank(const std::string& bank_name,
+ const std::shared_ptr<const ov::ICore>& core) {
if (bank_name.empty()) {
// Don't share this bank in manager
- return std::make_shared<Bank>();
+ return std::make_shared<Bank>(core);
}
auto& instance = BankManager::getInstance();
- return instance.getBank(bank_name);
+ return instance.getBank(bank_name, core);
}
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 1ddd00750d3..c8efa46a301 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/weights_bank.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/weights_bank.hpp
@@ -1,4 +1,4 @@
-// Copyright (C) 2023 Intel Corporation
+// Copyright (C) 2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
@@ -9,7 +9,8 @@
#include <tuple>
#include <unordered_map>
-#include "openvino/openvino.hpp"
+#include "openvino/runtime/iplugin.hpp"
+#include "openvino/runtime/iremote_context.hpp"
#include "openvino/runtime/make_tensor.hpp"
#include "openvino/runtime/tensor.hpp"
@@ -19,17 +20,25 @@ namespace weights {
class Bank {
public:
- Bank() = default;
+ explicit Bank(const std::shared_ptr<const ov::ICore>& core) : m_core(core) {}
- ov::Tensor get(const ov::Tensor& tensor);
+ // Capture CPU version of the tensor
+ ov::Tensor update(const ov::Tensor& tensor);
+
+ // Based on previously captured tensor allocate a new tensor (if needed) on a specified device
+ ov::Tensor get(const ov::Tensor& tensor, const std::string& device);
private:
- // Note: suits both - remote and ordinary tensors
+ // Default CPU bank. Filled by update()
std::unordered_map<void*, ov::Tensor> m_bank;
+ // Bank for specified device and their allocated memory
+ std::unordered_map<std::string, std::unordered_map<void*, ov::Tensor>> m_device_bank;
std::mutex m_mutex;
+ std::shared_ptr<const ov::ICore> m_core = nullptr;
+ std::shared_ptr<ov::IRemoteContext> m_remote_ctx = nullptr;
};
-std::shared_ptr<Bank> bank(const std::string& bank_name);
+std::shared_ptr<Bank> bank(const std::string& bank_name, const std::shared_ptr<const ov::ICore>& core);
} // namespace weights
} // namespace npuw