Skip to content

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

[NPUW] L0 allocation improvements (#27011)

Alexey Smirnov062762baf0b3
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 1c75b1cbdf3..2fe90eb82c4 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp
@@ -442,13 +442,14 @@ ov::npuw::CompiledModel::CompiledModel(const std::shared_ptr<ov::Model>& model,
}
void ov::npuw::CompiledModel::finalize_weights_bank() {
+ LOG_INFO("Finalizing weights bank...");
// Register lazy tensors
for (std::size_t idx = 0; idx < m_compiled_submodels.size(); ++idx) {
auto& comp_model_desc = m_compiled_submodels[idx];
// Skip optimized out and non-functions
if (!comp_model_desc.compiled_model && !comp_model_desc.replaced_by) {
- return;
+ continue;
}
const auto real_idx = comp_model_desc.replaced_by.value_or(idx);
@@ -489,6 +490,45 @@ void ov::npuw::CompiledModel::finalize_weights_bank() {
comp_model_desc.is_remote[tidx] = m_weights_bank->is_remote(lt);
}
}
+
+ LOG_INFO("Done.");
+}
+
+std::string ov::npuw::CompiledModel::global_mem_device() const {
+ // Force globally set device if set
+ const std::string device_alloc = m_cfg.get<::intel_npu::NPUW_WEIGHTS_BANK_ALLOC>();
+ if (!device_alloc.empty()) {
+ return device_alloc;
+ }
+
+ // Check if there is at least 1 NPU submodel
+ for (std::size_t idx = 0; idx < m_compiled_submodels.size(); ++idx) {
+ auto& comp_model_desc = m_compiled_submodels[idx];
+ if (!comp_model_desc.compiled_model) {
+ continue;
+ }
+ if (ov::npuw::util::starts_with(*comp_model_desc.device_it, "NPU")) {
+ return "NPU";
+ }
+ }
+
+ return "CPU";
+}
+
+std::string ov::npuw::CompiledModel::funcall_mem_device(const std::size_t idx) const {
+ // FIXME: currently we allocate intermediate tensors for EVERY submodel.
+ // It's not feasible to allocate them in L0 due to high memory consumption.
+ // Until we make such memory reusable, hard-coding those tensors to CPU.
+ return "CPU";
+
+ // Force globally set device if set
+ const std::string device_alloc = m_cfg.get<::intel_npu::NPUW_WEIGHTS_BANK_ALLOC>();
+ if (!device_alloc.empty()) {
+ return device_alloc;
+ }
+
+ auto& comp_model_desc = m_compiled_submodels[idx];
+ return *comp_model_desc.device_it;
}
void ov::npuw::CompiledModel::remove_long_output_names(const std::shared_ptr<ov::Model>& model) {
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 7f308d46094..4152d08275b 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hpp
@@ -74,6 +74,10 @@ private:
void finalize_weights_bank();
+ std::string global_mem_device() const;
+
+ std::string funcall_mem_device(const std::size_t idx) const;
+
std::shared_ptr<::intel_npu::OptionsDesc> m_options_desc;
::intel_npu::Config m_cfg;
GetPropertiesMap m_prop_to_opt;
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 93f9f12fe86..fbbabf083bc 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
@@ -16,7 +16,6 @@
#include "openvino/core/except.hpp"
#include "openvino/core/parallel.hpp"
#include "openvino/runtime/iasync_infer_request.hpp"
-#include "openvino/runtime/make_tensor.hpp"
#include "plugin.hpp"
#include "util.hpp"
#include "weights_bank.hpp"
@@ -49,7 +48,7 @@ ov::npuw::JustInferRequest::JustInferRequest(const std::shared_ptr<ov::npuw::Com
// FIXME: Shouldn't this be handled by the base class? (in create_tensor)
// A special case for function calls
if (comp_model_desc.replaced_by) {
- // Pre-allocate output tesnors for this function call
+ // Pre-allocate output tensors for this function call
const auto real_idx = comp_model_desc.replaced_by.value();
auto& proto_comp_model_desc = m_npuw_model->m_compiled_submodels[real_idx];
auto& proto_comp_model = proto_comp_model_desc.compiled_model;
@@ -68,13 +67,13 @@ ov::npuw::JustInferRequest::JustInferRequest(const std::shared_ptr<ov::npuw::Com
for (auto&& p : proto_comp_model_desc.spatial->params) {
const auto& iport = proto_comp_model_desc.compiled_model->inputs()[p.idx];
m_spatial_io[real_idx].input_tails[p.idx] =
- ov::get_tensor_impl(ov::Tensor(iport.get_element_type(), iport.get_shape()));
+ allocTensor(iport, m_npuw_model->funcall_mem_device(real_idx));
}
const auto num_outs = proto_comp_model_desc.compiled_model->outputs().size();
for (std::size_t out_idx = 0u; out_idx < num_outs; out_idx++) {
const auto& oport = proto_comp_model_desc.compiled_model->outputs()[out_idx];
m_spatial_io[real_idx].output_tails[out_idx] =
- ov::get_tensor_impl(ov::Tensor(oport.get_element_type(), oport.get_shape()));
+ allocTensor(oport, m_npuw_model->funcall_mem_device(real_idx));
}
}
} // if(spatial)
@@ -88,7 +87,7 @@ ov::npuw::JustInferRequest::JustInferRequest(const std::shared_ptr<ov::npuw::Com
shape[proto_comp_model_desc.spatial->out_dim] = proto_comp_model_desc.spatial->range;
}
m_funcall_result[LinkFrom{i, out_idx}] =
- ov::get_tensor_impl(ov::Tensor(port.get_element_type(), shape));
+ allocTensor(port.get_element_type(), shape, m_npuw_model->funcall_mem_device(real_idx));
}
if (real_idx != i) {
// If this function call is NOT the function body, do nothing here - the original
@@ -153,7 +152,9 @@ ov::npuw::JustInferRequest::JustInferRequest(const std::shared_ptr<ov::npuw::Com
LOG_INFO("Preallocating input tensors...");
for (size_t i = 0; i < m_npuw_model->inputs().size(); i++) {
const auto& port = m_npuw_model->inputs()[i];
- m_input_tensors.push_back(ov::get_tensor_impl(ov::Tensor(port.get_element_type(), port.get_shape())));
+ ov::SoPtr<ov::ITensor> allocated = allocTensor(port, m_npuw_model->global_mem_device());
+ m_input_tensors.push_back(allocated);
+ m_input_allocated.insert(allocated->data());
m_port_to_tensor[port] = TensorStorage{m_input_tensors.back(), true};
} // for(inputs)
@@ -173,7 +174,7 @@ ov::npuw::JustInferRequest::JustInferRequest(const std::shared_ptr<ov::npuw::Com
const auto& tensor =
funcall_result_iter != m_funcall_result.end()
? funcall_result_iter->second // Function calls have their tensors allocated, so just use one
- : ov::get_tensor_impl(ov::Tensor(port.get_element_type(), port.get_shape()));
+ : allocTensor(port, m_npuw_model->global_mem_device());
m_output_tensors.push_back(tensor);
m_port_to_tensor[port] = TensorStorage{tensor, true};
@@ -421,7 +422,7 @@ void ov::npuw::JustInferRequest::bind_global_parameters(std::size_t idx) {
LOG_BLOCK();
if (!is_spatial_param(sub_in_idx)) {
// Input parameter is non-spatial, do normal handling
- if (do_copy) {
+ if (do_copy || m_input_allocated.count(g_tnsr->data()) == 0) {
LOG_DEBUG("Will be copied");
copy_list.emplace_back(g_tnsr, s_port);
} else {
@@ -919,6 +920,29 @@ void ov::npuw::JustInferRequest::unsafe_run_this_prep_next(std::size_t idx, bool
} // if (replaced_by)
}
+ov::SoPtr<ov::ITensor> ov::npuw::JustInferRequest::allocTensor(const ov::element::Type type,
+ const ov::Shape& shape,
+ const std::string& device) {
+ if (device == "CPU" || ov::shape_size(shape) == 0) {
+ return ov::get_tensor_impl(ov::Tensor(type, shape));
+ }
+
+ ov::SoPtr<ov::ITensor> remote_tensor;
+ ov::Tensor allocated_tensor;
+ {
+ std::lock_guard<std::mutex> guard(m_alloc_mutex);
+ m_remote_ctx = m_npuw_model->get_plugin()->get_core()->get_default_context(device)._ptr;
+ remote_tensor = m_remote_ctx->create_host_tensor(type, shape);
+ allocated_tensor = ov::make_tensor(remote_tensor);
+ }
+ return ov::get_tensor_impl(allocated_tensor);
+}
+
+ov::SoPtr<ov::ITensor> ov::npuw::JustInferRequest::allocTensor(const ov::Output<const ov::Node>& node,
+ const std::string& device) {
+ return allocTensor(node.get_element_type(), node.get_shape(), device);
+}
+
void ov::npuw::JustInferRequest::subscribe_subrequest(std::size_t idx, Completed cb) {
get_real_subrequest(idx)->set_callback(std::move(cb));
}
diff --git a/src/plugins/intel_npu/src/plugin/npuw/just_sync_infer_request.hpp b/src/plugins/intel_npu/src/plugin/npuw/just_sync_infer_request.hpp
index 2544647dd00..7335b54c300 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/just_sync_infer_request.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/just_sync_infer_request.hpp
@@ -6,10 +6,15 @@
#include <limits>
#include <map>
+#include <mutex>
#include <optional>
#include <vector>
#include "base_sync_infer_request.hpp"
+#include "openvino/runtime/iplugin.hpp"
+#include "openvino/runtime/iremote_context.hpp"
+#include "openvino/runtime/make_tensor.hpp"
+#include "openvino/runtime/tensor.hpp"
namespace ov {
namespace npuw {
@@ -59,6 +64,9 @@ private:
void connect_subrequests();
void recreate_subrequests(std::size_t idx);
+ ov::SoPtr<ov::ITensor> allocTensor(const ov::element::Type type, const ov::Shape& shape, const std::string& device);
+ ov::SoPtr<ov::ITensor> allocTensor(const ov::Output<const ov::Node>& node, const std::string& device);
+
using LinkFrom = std::pair<std::size_t /* Subrequest index */
,
std::size_t /* Subrequest output index */
@@ -93,6 +101,11 @@ private:
map_t global_results; // result idx -> output idx
};
std::vector<GlobalIO> m_subrequests_gio;
+
+ std::mutex m_alloc_mutex;
+ std::shared_ptr<ov::IRemoteContext> m_remote_ctx = nullptr;
+
+ std::unordered_set<void*> m_input_allocated;
};
} // namespace npuw
diff --git a/src/plugins/intel_npu/src/plugin/npuw/partitioning/online/compiler.cpp b/src/plugins/intel_npu/src/plugin/npuw/partitioning/online/compiler.cpp
index 4b8973b5bb9..46b6cb7b126 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/partitioning/online/compiler.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/partitioning/online/compiler.cpp
@@ -74,8 +74,8 @@ std::vector<Avoid> getAvoids(::intel_npu::Config& cfg) {
std::string avoids_opt = cfg.getString<::intel_npu::NPUW_ONLINE_AVOID>();
if (avoids_opt.empty()) {
- LOG_WARN(::intel_npu::NPUW_ONLINE_AVOID().key()
- << " property is not set! NPU device will be prioritized for every subgraph.");
+ LOG_VERB(::intel_npu::NPUW_ONLINE_AVOID().key()
+ << " property is not set. NPU device will be prioritized for every subgraph.");
return {};
}
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 d142f72f9b7..2a79bf33ef9 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/weights_bank.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/weights_bank.cpp
@@ -95,13 +95,16 @@ ov::Tensor Bank::unsafe_eval_and_alloc(const LazyTensor& tensor, const std::stri
return transformed_tensor;
}
- // FIXME: L0 allocation may crash when run in parallel
- std::lock_guard<std::mutex> guard(m_alloc_mutex);
-
- m_remote_ctx = m_core->get_default_context(device_for_alloc)._ptr;
- auto remote_tensor =
- m_remote_ctx->create_host_tensor(transformed_tensor.get_element_type(), transformed_tensor.get_shape());
- auto allocated_tensor = ov::make_tensor(remote_tensor);
+ ov::SoPtr<ov::ITensor> remote_tensor;
+ ov::Tensor allocated_tensor;
+ {
+ // FIXME: L0 allocation may crash when run in parallel
+ std::lock_guard<std::mutex> guard(m_alloc_mutex);
+ m_remote_ctx = m_core->get_default_context(device_for_alloc)._ptr;
+ remote_tensor =
+ m_remote_ctx->create_host_tensor(transformed_tensor.get_element_type(), transformed_tensor.get_shape());
+ allocated_tensor = ov::make_tensor(remote_tensor);
+ }
transformed_tensor.copy_to(allocated_tensor);
m_device_bank[device_for_alloc][tensor] = allocated_tensor;
return allocated_tensor;