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] DCOFF + Transpose (#29782)
Alexey Smirnovb9b952bd24eb
diff --git a/src/plugins/intel_npu/src/al/include/intel_npu/config/options.hpp b/src/plugins/intel_npu/src/al/include/intel_npu/config/options.hppindex 22870d32f42..410fd8a5c54 100644--- a/src/plugins/intel_npu/src/al/include/intel_npu/config/options.hpp+++ b/src/plugins/intel_npu/src/al/include/intel_npu/config/options.hpp@@ -641,6 +641,14 @@ struct WEIGHTS_PATH final : OptionBase<WEIGHTS_PATH, std::string> {static OptionMode mode() {return OptionMode::RunTime;}++ static bool isPublic() {+ return true;+ }++ static ov::PropertyMutability mutability() {+ return ov::PropertyMutability::RW;+ }};//@@ -1368,7 +1376,7 @@ struct MODEL_PTR final : OptionBase<MODEL_PTR, std::shared_ptr<const ov::Model>>}static constexpr std::string_view getTypeName() {- return "std::shared_ptr<ov::Model>";+ return "std::shared_ptr<const ov::Model>";}static std::shared_ptr<const ov::Model> defaultValue() {@@ -1385,6 +1393,14 @@ struct MODEL_PTR final : OptionBase<MODEL_PTR, std::shared_ptr<const ov::Model>>static OptionMode mode() {return OptionMode::RunTime;}++ static bool isPublic() {+ return true;+ }++ static ov::PropertyMutability mutability() {+ return ov::PropertyMutability::RW;+ }};} // namespace intel_npudiff --git a/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.cpp b/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.cppindex 85fcda56236..1aee369a9a0 100644--- a/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.cpp@@ -20,294 +20,285 @@ namespace ov {namespace npuw {namespace weights {namespace op {-struct Const {- std::shared_ptr<ov::op::v0::Constant> m_node = nullptr;- ov::element::Type m_cached_type;- ov::Shape m_cached_shape;- const void* m_cached_ptr = nullptr;- std::size_t m_offset = 0;- std::size_t m_byte_size = 0;- ov::Tensor m_read_from_bin;-- Const() = default;-- explicit Const(std::shared_ptr<ov::op::v0::Constant> n) : m_node(n) {- m_cached_type = m_node->get_element_type();- m_cached_shape = m_node->get_shape();- m_cached_ptr = m_node->get_data_ptr();- m_byte_size = m_node->get_byte_size();-- auto rt_info = m_node->get_rt_info();- auto weightless_cache_attr = rt_info.find(ov::WeightlessCacheAttribute::get_type_info_static());- if (weightless_cache_attr != rt_info.end()) {- m_offset = weightless_cache_attr->second.as<ov::WeightlessCacheAttribute>().bin_offset;- }- }- std::size_t hash() const {- std::size_t seed = std::hash<const void*>()(m_cached_ptr) + 0x9e3779b9;- seed ^= m_cached_type.hash() + 0x9e3779b9;- for (const auto& dim : m_cached_shape) {- seed ^= std::hash<std::size_t>()(dim) + 0x9e3779b9;- }- return seed;- }- bool operator==(const Const& other) const {- return (m_cached_type == other.m_cached_type && m_cached_shape == other.m_cached_shape &&- m_cached_ptr == other.m_cached_ptr);- }- ov::Tensor eval() const {- if (m_node) {- return ov::npuw::util::tensor_from_const(m_node);- }+Const::Const(std::shared_ptr<ov::op::v0::Constant> n) : m_node(n) {+ m_cached_type = m_node->get_element_type();+ m_cached_shape = m_node->get_shape();+ m_cached_ptr = m_node->get_data_ptr();+ m_byte_size = m_node->get_byte_size();- NPUW_ASSERT(m_read_from_bin && "Underlying data should have been read first!");- return m_read_from_bin;+ auto rt_info = m_node->get_rt_info();+ auto weightless_cache_attr = rt_info.find(ov::WeightlessCacheAttribute::get_type_info_static());+ if (weightless_cache_attr != rt_info.end()) {+ m_offset = weightless_cache_attr->second.as<ov::WeightlessCacheAttribute>().bin_offset;}- void read_weight(const ov::npuw::s11n::WeightsContext& ctx) {- NPUW_ASSERT(!m_node &&- "LazyTensor can only read weight when it's being deserialized and not created from a Constant!");- m_read_from_bin = ov::Tensor(m_cached_type, m_cached_shape);+}- if (ctx.weights) {- std::memcpy(m_read_from_bin.data(), ctx.weights->get_ptr(m_offset), m_byte_size);- } else {- auto it = ctx.consts_cache.find({m_offset, m_byte_size});- NPUW_ASSERT(it != ctx.consts_cache.end() && "Couldn't find Constant in cache!");- auto tensor = ov::npuw::util::tensor_from_const(it->second);- NPUW_ASSERT(tensor.get_byte_size() == m_byte_size && tensor.get_shape() == m_cached_shape &&- tensor.get_element_type() == m_cached_type);- tensor.copy_to(m_read_from_bin);- }- }- void detach() {- m_node.reset();- m_read_from_bin = ov::Tensor();- }- void serialize(std::ostream& stream) const {- using namespace ov::npuw::s11n;- write(stream, m_cached_type.to_string());- write(stream, m_cached_shape);- write(stream, m_offset);- write(stream, m_byte_size);- }- static Const deserialize(std::istream& stream) {- using namespace ov::npuw::s11n;- Const c;- std::string type_str;- read(stream, type_str);- c.m_cached_type = ov::element::Type(type_str);- read(stream, c.m_cached_shape);- read(stream, c.m_offset);- read(stream, c.m_byte_size);- return c;- }-};-struct Concat {- std::vector<LazyTensor> tensors;- std::size_t axis;-- Concat() = default;-- std::size_t hash() const {- std::size_t seed = std::hash<std::size_t>()(axis) + 0x9e3779b9;- for (auto& lt : tensors) {- seed ^= lt.get_hash() + 0x9e3779b9;- }- return seed;- }- bool operator==(const Concat& other) const {- return (axis == other.axis && tensors == other.tensors);- }- ov::Tensor eval() const {- std::vector<ov::Tensor> to_concat;- for (const auto& lt : tensors) {- to_concat.push_back(lt.eval());- }- return ov::npuw::util::concat(to_concat, axis);- }- void read_weight(const ov::npuw::s11n::WeightsContext& ctx) {- for (auto& lt : tensors) {- lt.read_weight(ctx);- }- }- void detach() {- for (auto&& lt : tensors) {- lt.detach();- }- }- void serialize(std::ostream& stream) const {- using namespace ov::npuw::s11n;- write(stream, axis);- write(stream, tensors);- }- static Concat deserialize(std::istream& stream) {- using namespace ov::npuw::s11n;- Concat c;- read(stream, c.axis);- read(stream, c.tensors);- return c;+std::size_t Const::hash() const {+ std::size_t seed = std::hash<const void*>()(m_cached_ptr) + 0x9e3779b9;+ seed ^= m_cached_type.hash() + 0x9e3779b9;+ for (const auto& dim : m_cached_shape) {+ seed ^= std::hash<std::size_t>()(dim) + 0x9e3779b9;}-};-struct Unpack {- LazyTensor w, z, s;- ov::element::Type type;- ov::Shape shape;-- Unpack() = default;-- std::size_t hash() const {- std::size_t seed = w.get_hash() + 0x9e3779b9;- seed ^= z.get_hash() + 0x9e3779b9;- seed ^= s.get_hash() + 0x9e3779b9;- seed ^= type.hash() + 0x9e3779b9;- for (const auto& dim : shape) {- seed ^= std::hash<std::size_t>()(dim) + 0x9e3779b9;- }- return seed;- }- bool operator==(const Unpack& other) const {- return (type == other.type && shape == other.shape && w == other.w && z == other.z && s == other.s);- }- ov::Tensor eval() const {- const auto& gti = ov::get_tensor_impl;- const auto& tw = w.eval();- const auto& tz = z.eval();- const auto& ts = s.eval();- NPUW_ASSERT(tw);- ov::Tensor dst(type, shape);- if (tw && tz && ts) {- ov::npuw::util::unpack(gti(tw), gti(tz), gti(ts), gti(dst));- } else if (tw && ts) {- ov::npuw::util::unpack(gti(tw), gti(ts), gti(dst));- } else {- NPUW_ASSERT(false && "Unsupported combination");- }- return dst;- }- void read_weight(const ov::npuw::s11n::WeightsContext& ctx) {- w.read_weight(ctx);- if (z) { // could be empty- z.read_weight(ctx);- }- s.read_weight(ctx);- }- void detach() {- w.detach();- z.detach();- s.detach();- }- void serialize(std::ostream& stream) const {- using namespace ov::npuw::s11n;- write(stream, type.to_string());- write(stream, shape);- write(stream, w);- write(stream, z);- write(stream, s);- }- static Unpack deserialize(std::istream& stream) {- using namespace ov::npuw::s11n;- Unpack u;- std::string type_str;- read(stream, type_str);- u.type = ov::element::Type(type_str);- read(stream, u.shape);- read(stream, u.w);- read(stream, u.z);- read(stream, u.s);- return u;- }-};-struct Permute {- LazyTensor tensor;- std::vector<std::size_t> axes;-- Permute() = default;-- std::size_t hash() const {- std::size_t seed = tensor.get_hash() + 0x9e3779b9;- for (const auto& axis : axes) {- seed ^= std::hash<std::size_t>()(axis) + 0x9e3779b9;- }- return seed;- }- bool operator==(const Permute& other) const {- return (axes == other.axes && tensor == other.tensor);- }- ov::Tensor eval() const {- return ov::npuw::util::permute(tensor.eval(), axes);- }- void read_weight(const ov::npuw::s11n::WeightsContext& ctx) {- tensor.read_weight(ctx);- }- void detach() {- tensor.detach();- }- void serialize(std::ostream& stream) const {- using namespace ov::npuw::s11n;- write(stream, axes);- write(stream, tensor);+ return seed;+}++bool Const::operator==(const Const& other) const {+ return (m_cached_type == other.m_cached_type && m_cached_shape == other.m_cached_shape &&+ m_cached_ptr == other.m_cached_ptr);+}++ov::Tensor Const::eval() const {+ if (m_node) {+ return ov::npuw::util::copy_tensor_from_const(m_node);}- static Permute deserialize(std::istream& stream) {- using namespace ov::npuw::s11n;- Permute p;- read(stream, p.axes);- read(stream, p.tensor);- return p;++ NPUW_ASSERT(m_read_from_bin && "Underlying data should have been read first! Or the tensor is already detached.");+ return m_read_from_bin;+}++void Const::read_weight(const ov::npuw::s11n::WeightsContext& ctx) {+ NPUW_ASSERT(!m_node &&+ "LazyTensor can only read weight when it's being deserialized and not created from a Constant!");++ if (ctx.weights) {+ m_read_from_bin = ov::Tensor(m_cached_type, m_cached_shape);+ std::memcpy(m_read_from_bin.data(), ctx.weights->get_ptr(m_offset), m_byte_size);+ } else {+ auto it = ctx.consts_cache.find({m_offset, m_byte_size});+ NPUW_ASSERT(it != ctx.consts_cache.end() && "Couldn't find Constant in cache!");+ m_read_from_bin = ov::npuw::util::copy_tensor_from_const(it->second);+ NPUW_ASSERT(m_read_from_bin.get_byte_size() == m_byte_size && m_read_from_bin.get_shape() == m_cached_shape &&+ m_read_from_bin.get_element_type() == m_cached_type);}-};-struct Convert {- LazyTensor tensor;- ov::element::Type type;+}++void Const::detach() {+ m_node.reset();+ m_read_from_bin = ov::Tensor();+}++void Const::serialize(std::ostream& stream) const {+ using namespace ov::npuw::s11n;+ write(stream, m_cached_type.to_string());+ write(stream, m_cached_shape);+ write(stream, m_offset);+ write(stream, m_byte_size);+}- Convert() = default;+Const Const::deserialize(std::istream& stream) {+ using namespace ov::npuw::s11n;+ Const c;+ std::string type_str;+ read(stream, type_str);+ c.m_cached_type = ov::element::Type(type_str);+ read(stream, c.m_cached_shape);+ read(stream, c.m_offset);+ read(stream, c.m_byte_size);+ return c;+}- std::size_t hash() const {- std::size_t seed = type.hash() + 0x9e3779b9;- seed ^= tensor.get_hash() + 0x9e3779b9;- return seed;+std::size_t Concat::hash() const {+ std::size_t seed = std::hash<std::size_t>()(axis) + 0x9e3779b9;+ for (auto& lt : tensors) {+ seed ^= lt.get_hash() + 0x9e3779b9;}- bool operator==(const Convert& other) const {- return (type == other.type && tensor == other.tensor);+ return seed;+}++bool Concat::operator==(const Concat& other) const {+ return (axis == other.axis && tensors == other.tensors);+}++ov::Tensor Concat::eval() const {+ std::vector<ov::Tensor> to_concat;+ for (const auto& lt : tensors) {+ to_concat.push_back(lt.eval());}- ov::Tensor eval() const {- NPUW_ASSERT(ov::element::f16 == type);- return ov::npuw::util::to_f16(tensor.eval());+ return ov::npuw::util::concat(to_concat, axis);+}++void Concat::read_weight(const ov::npuw::s11n::WeightsContext& ctx) {+ for (auto& lt : tensors) {+ lt.read_weight(ctx);}- void read_weight(const ov::npuw::s11n::WeightsContext& ctx) {- tensor.read_weight(ctx);+}++void Concat::detach() {+ for (auto&& lt : tensors) {+ lt.detach();}- void detach() {- tensor.detach();+}++void Concat::serialize(std::ostream& stream) const {+ using namespace ov::npuw::s11n;+ write(stream, axis);+ write(stream, tensors);+}++Concat Concat::deserialize(std::istream& stream) {+ using namespace ov::npuw::s11n;+ Concat c;+ read(stream, c.axis);+ read(stream, c.tensors);+ return c;+}++std::size_t Unpack::hash() const {+ std::size_t seed = w.get_hash() + 0x9e3779b9;+ seed ^= z.get_hash() + 0x9e3779b9;+ seed ^= s.get_hash() + 0x9e3779b9;+ seed ^= type.hash() + 0x9e3779b9;+ for (const auto& dim : shape) {+ seed ^= std::hash<std::size_t>()(dim) + 0x9e3779b9;}- void serialize(std::ostream& stream) const {- using namespace ov::npuw::s11n;- write(stream, type.to_string());- write(stream, tensor);+ return seed;+}++bool Unpack::operator==(const Unpack& other) const {+ return (type == other.type && shape == other.shape && w == other.w && z == other.z && s == other.s);+}++ov::Tensor Unpack::eval() const {+ const auto& gti = ov::get_tensor_impl;+ const auto& tw = w.eval();+ const auto& tz = z.eval();+ const auto& ts = s.eval();+ NPUW_ASSERT(tw);+ ov::Tensor dst(type, shape);+ if (tw && tz && ts) {+ ov::npuw::util::unpack(gti(tw), gti(tz), gti(ts), gti(dst));+ } else if (tw && ts) {+ ov::npuw::util::unpack(gti(tw), gti(ts), gti(dst));+ } else {+ NPUW_ASSERT(false && "Unsupported combination");+ }+ return dst;+}++void Unpack::read_weight(const ov::npuw::s11n::WeightsContext& ctx) {+ w.read_weight(ctx);+ if (z) { // could be empty+ z.read_weight(ctx);}- static Convert deserialize(std::istream& stream) {- using namespace ov::npuw::s11n;- Convert c;- std::string type_str;- read(stream, type_str);- c.type = ov::element::Type(type_str);- read(stream, c.tensor);- return c;+ s.read_weight(ctx);+}++void Unpack::detach() {+ w.detach();+ z.detach();+ s.detach();+}++void Unpack::serialize(std::ostream& stream) const {+ using namespace ov::npuw::s11n;+ write(stream, type.to_string());+ write(stream, shape);+ write(stream, w);+ write(stream, z);+ write(stream, s);+}++Unpack Unpack::deserialize(std::istream& stream) {+ using namespace ov::npuw::s11n;+ Unpack u;+ std::string type_str;+ read(stream, type_str);+ u.type = ov::element::Type(type_str);+ read(stream, u.shape);+ read(stream, u.w);+ read(stream, u.z);+ read(stream, u.s);+ return u;+}++std::size_t Permute::hash() const {+ std::size_t seed = tensor.get_hash() + 0x9e3779b9;+ for (const auto& axis : axes) {+ seed ^= std::hash<std::size_t>()(axis) + 0x9e3779b9;}-};-} // namespace op+ return seed;+}++bool Permute::operator==(const Permute& other) const {+ return (axes == other.axes && tensor == other.tensor);+}++ov::Tensor Permute::eval() const {+ return ov::npuw::util::permute(tensor.eval(), axes);+}-using Transform = std::variant<op::Const, op::Concat, op::Unpack, op::Permute, op::Convert>;+void Permute::read_weight(const ov::npuw::s11n::WeightsContext& ctx) {+ tensor.read_weight(ctx);+}++void Permute::detach() {+ tensor.detach();+}++void Permute::serialize(std::ostream& stream) const {+ using namespace ov::npuw::s11n;+ write(stream, axes);+ write(stream, tensor);+}++Permute Permute::deserialize(std::istream& stream) {+ using namespace ov::npuw::s11n;+ Permute p;+ read(stream, p.axes);+ read(stream, p.tensor);+ return p;+}++std::size_t Convert::hash() const {+ std::size_t seed = type.hash() + 0x9e3779b9;+ seed ^= tensor.get_hash() + 0x9e3779b9;+ return seed;+}++bool Convert::operator==(const Convert& other) const {+ return (type == other.type && tensor == other.tensor);+}++ov::Tensor Convert::eval() const {+ NPUW_ASSERT(ov::element::f16 == type);+ return ov::npuw::util::to_f16(tensor.eval());+}++void Convert::read_weight(const ov::npuw::s11n::WeightsContext& ctx) {+ tensor.read_weight(ctx);+}++void Convert::detach() {+ tensor.detach();+}++void Convert::serialize(std::ostream& stream) const {+ using namespace ov::npuw::s11n;+ write(stream, type.to_string());+ write(stream, tensor);+}++Convert Convert::deserialize(std::istream& stream) {+ using namespace ov::npuw::s11n;+ Convert c;+ std::string type_str;+ read(stream, type_str);+ c.type = ov::element::Type(type_str);+ read(stream, c.tensor);+ return c;+}+} // namespace openum class TransformType : int { CONST = 0, CONCAT, UNPACK, PERMUTE, CONVERT };struct LazyTensorImpl {-public:LazyTensorImpl() = default;- explicit LazyTensorImpl(Transform&& t);+ explicit LazyTensorImpl(LazyTensor::Transform&& t);bool operator==(const LazyTensorImpl& other) const;ov::Tensor eval() const;std::size_t get_hash() const;+ void get_transformations(std::vector<LazyTensor::Transform>& vec) const;void detach();@@ -315,7 +306,7 @@ public:static std::shared_ptr<LazyTensorImpl> deserialize(std::istream& stream);void read_weight(const ov::npuw::s11n::WeightsContext& ctx);- Transform m_transform;+ LazyTensor::Transform m_transform;std::size_t m_hash = 0;};@@ -325,7 +316,6 @@ public:using namespace ov::npuw::weights::op;using ov::npuw::weights::LazyTensorImpl;-using ov::npuw::weights::Transform;// std::visit helpertemplate <class... Ts>@@ -335,7 +325,7 @@ struct overloaded : Ts... {template <class... Ts>overloaded(Ts...) -> overloaded<Ts...>;-LazyTensorImpl::LazyTensorImpl(Transform&& t)+LazyTensorImpl::LazyTensorImpl(LazyTensor::Transform&& t): m_transform(std::move(t)),m_hash(std::visit(overloaded{[](const auto& op) {return op.hash();@@ -373,6 +363,36 @@ std::size_t LazyTensorImpl::get_hash() const {return m_hash;}+void LazyTensorImpl::get_transformations(std::vector<LazyTensor::Transform>& vec) const {+ vec.push_back(m_transform);+ std::visit(overloaded{[&vec](const op::Concat& op) {+ for (const auto& lt : op.tensors) {+ auto next_tr = lt.get_transformations();+ vec.insert(vec.end(), next_tr.begin(), next_tr.end());+ }+ },+ [](const op::Const& op) {+ // do nothing+ },+ [&vec](const op::Convert& op) {+ auto next_tr = op.tensor.get_transformations();+ vec.insert(vec.end(), next_tr.begin(), next_tr.end());+ },+ [&vec](const op::Permute& op) {+ auto next_tr = op.tensor.get_transformations();+ vec.insert(vec.end(), next_tr.begin(), next_tr.end());+ },+ [&vec](const op::Unpack& op) {+ auto next_w_tr = op.w.get_transformations();+ vec.insert(vec.end(), next_w_tr.begin(), next_w_tr.end());+ auto next_z_tr = op.z.get_transformations();+ vec.insert(vec.end(), next_z_tr.begin(), next_z_tr.end());+ auto next_s_tr = op.s.get_transformations();+ vec.insert(vec.end(), next_s_tr.begin(), next_s_tr.end());+ }},+ m_transform);+}+void LazyTensorImpl::detach() {std::visit(overloaded{[](auto& op) {op.detach();@@ -439,23 +459,23 @@ std::shared_ptr<LazyTensorImpl> LazyTensorImpl::deserialize(std::istream& streamLazyTensor::LazyTensor(const std::shared_ptr<ov::op::v0::Constant>& const_ptr): m_impl(std::make_shared<LazyTensorImpl>(op::Const(const_ptr))) {}LazyTensor::LazyTensor(const std::vector<LazyTensor>& to_concat, const std::size_t axis)- : m_impl(std::make_shared<LazyTensorImpl>(op::Concat{to_concat, axis})) {}+ : m_impl(std::make_shared<LazyTensorImpl>(op::Concat(to_concat, axis))) {}LazyTensor::LazyTensor(const LazyTensor& cw,const LazyTensor& cz,const LazyTensor& cs,const ov::element::Type& type,const ov::Shape& shape)- : m_impl(std::make_shared<LazyTensorImpl>(op::Unpack{cw, cz, cs, type, shape})) {}+ : m_impl(std::make_shared<LazyTensorImpl>(op::Unpack(cw, cz, cs, type, shape))) {}LazyTensor LazyTensor::permute(const std::vector<std::size_t>& axes) {LazyTensor new_lt;- new_lt.m_impl = std::make_shared<LazyTensorImpl>(op::Permute{*this, axes});+ new_lt.m_impl = std::make_shared<LazyTensorImpl>(op::Permute(*this, axes));return new_lt;}LazyTensor LazyTensor::convert(const ov::element::Type& type) {LazyTensor new_lt;- new_lt.m_impl = std::make_shared<LazyTensorImpl>(op::Convert{*this, type});+ new_lt.m_impl = std::make_shared<LazyTensorImpl>(op::Convert(*this, type));return new_lt;}@@ -496,6 +516,16 @@ std::size_t LazyTensor::get_hash() const {return m_impl->get_hash();}+std::vector<LazyTensor::Transform> LazyTensor::get_transformations() const {+ if (!m_impl) {+ return {};+ }++ std::vector<LazyTensor::Transform> transformations;+ m_impl->get_transformations(transformations);+ return transformations;+}+void LazyTensor::detach() {if (m_impl) {m_impl->detach();diff --git a/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.hpp b/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.hppindex 4850c9c85e5..1ad3bbc7ad8 100644--- a/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.hpp+++ b/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.hpp@@ -5,6 +5,7 @@#pragma once#include <memory>+#include <variant>#include "openvino/op/constant.hpp"#include "openvino/runtime/shared_buffer.hpp"@@ -19,6 +20,14 @@ namespace weights {class LazyTensor;struct LazyTensorImpl;+namespace op {+class Const;+class Concat;+class Unpack;+class Permute;+class Convert;+} // namespace op+class LazyTensor {public:class Hash {@@ -26,6 +35,12 @@ public:std::size_t operator()(const LazyTensor& lt) const;};+ using Transform = std::variant<ov::npuw::weights::op::Const,+ ov::npuw::weights::op::Concat,+ ov::npuw::weights::op::Unpack,+ ov::npuw::weights::op::Permute,+ ov::npuw::weights::op::Convert>;+LazyTensor() = default;LazyTensor(const std::shared_ptr<ov::op::v0::Constant>& const_ptr);LazyTensor(const std::vector<LazyTensor>& to_concat, const std::size_t axis); // construct from concat@@ -43,6 +58,7 @@ public:ov::Tensor eval() const;std::size_t get_hash() const;+ std::vector<Transform> get_transformations() const;void detach();void serialize(std::ostream& stream) const;@@ -54,6 +70,119 @@ private:std::shared_ptr<LazyTensorImpl> m_impl = nullptr;};+namespace op {+class Const {+ friend struct ov::npuw::weights::LazyTensorImpl;++public:+ Const() = default;++ explicit Const(std::shared_ptr<ov::op::v0::Constant> n);+ std::size_t hash() const;+ bool operator==(const Const& other) const;+ ov::Tensor eval() const;+ void read_weight(const ov::npuw::s11n::WeightsContext& ctx);+ void detach();+ void serialize(std::ostream& stream) const;+ static Const deserialize(std::istream& stream);++private:+ std::shared_ptr<ov::op::v0::Constant> m_node = nullptr;+ ov::element::Type m_cached_type;+ ov::Shape m_cached_shape;+ const void* m_cached_ptr = nullptr;+ std::size_t m_offset = 0;+ std::size_t m_byte_size = 0;+ ov::Tensor m_read_from_bin;+};++class Concat {+ friend struct ov::npuw::weights::LazyTensorImpl;++public:+ Concat() = default;+ Concat(const std::vector<LazyTensor>& _tensors, std::size_t _axis) : tensors(_tensors), axis(_axis) {}++ std::size_t hash() const;+ bool operator==(const Concat& other) const;+ ov::Tensor eval() const;+ void read_weight(const ov::npuw::s11n::WeightsContext& ctx);+ void detach();+ void serialize(std::ostream& stream) const;+ static Concat deserialize(std::istream& stream);++private:+ std::vector<LazyTensor> tensors;+ std::size_t axis;+};++class Unpack {+ friend struct ov::npuw::weights::LazyTensorImpl;++public:+ Unpack() = default;+ Unpack(const LazyTensor& _w, const LazyTensor& _z, const LazyTensor& _s, ov::element::Type _type, ov::Shape _shape)+ : w(_w),+ z(_z),+ s(_s),+ type(_type),+ shape(_shape) {}++ std::size_t hash() const;+ bool operator==(const Unpack& other) const;+ ov::Tensor eval() const;+ void read_weight(const ov::npuw::s11n::WeightsContext& ctx);+ void detach();+ void serialize(std::ostream& stream) const;+ static Unpack deserialize(std::istream& stream);++private:+ LazyTensor w, z, s;+ ov::element::Type type;+ ov::Shape shape;+};++class Permute {+ friend struct ov::npuw::weights::LazyTensorImpl;++public:+ Permute() = default;+ Permute(const LazyTensor& _tensor, const std::vector<std::size_t>& _axes) : tensor(_tensor), axes(_axes) {}++ std::size_t hash() const;+ bool operator==(const Permute& other) const;+ ov::Tensor eval() const;+ void read_weight(const ov::npuw::s11n::WeightsContext& ctx);+ void detach();+ void serialize(std::ostream& stream) const;+ static Permute deserialize(std::istream& stream);++private:+ LazyTensor tensor;+ std::vector<std::size_t> axes;+};++class Convert {+ friend struct ov::npuw::weights::LazyTensorImpl;++public:+ Convert() = default;+ Convert(const LazyTensor& _tensor, ov::element::Type _type) : tensor(_tensor), type(_type) {}++ std::size_t hash() const;+ bool operator==(const Convert& other) const;+ ov::Tensor eval() const;+ void read_weight(const ov::npuw::s11n::WeightsContext& ctx);+ void detach();+ void serialize(std::ostream& stream) const;+ static Convert deserialize(std::istream& stream);++private:+ LazyTensor tensor;+ ov::element::Type type;+};+} // namespace op+} // namespace weights} // namespace npuw} // namespace ovdiff --git a/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/dcoff.cpp b/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/dcoff.cppindex 4f1f91948b4..e8bfdc0eb25 100644--- a/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/dcoff.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/dcoff.cpp@@ -8,12 +8,7 @@#include "../../logging.hpp"#include "../../util.hpp"#include "../partitioning.hpp" // Subgraph-#include "openvino/op/convert.hpp"-#include "openvino/op/gather.hpp"-#include "openvino/op/matmul.hpp"-#include "openvino/op/multiply.hpp"-#include "openvino/op/reshape.hpp"-#include "openvino/op/subtract.hpp"+#include "openvino/op/ops.hpp"#include "openvino/op/util/op_types.hpp"#include "openvino/pass/pattern/op/label.hpp" // any_input#include "openvino/pass/pattern/op/optional.hpp"@@ -26,6 +21,7 @@ namespace npuw {namespace patterns {namespace opp = ov::pass::pattern;+namespace uat = ov::npuw::util::at;// The update procedure is tricky: The closure vector needs to be// freed of the scale coefficient tensors which are no longer the@@ -112,7 +108,7 @@ ClosureRemap build_remap(const Function& fbody, const DCOFFParams& params_to) {auto zerop_iter = params_to.zerops.find(param);if (zerop_iter != params_to.zerops.end()) {LOG_DEBUG("This parameter requires zero point: " << zerop_iter->second);- m.zero_points.push_back(ov::npuw::util::tensor_from_const(zerop_iter->second));+ m.zero_points.push_back(ov::npuw::util::copy_tensor_from_const(zerop_iter->second));} else {m.zero_points.push_back(ov::Tensor());}@@ -139,7 +135,20 @@ void apply_remap(Subgraph& fcall, const ClosureRemap& m) {if (fcall._closure[i]) {new_closure.push_back(fcall._closure[i]);} else {- new_closure.push_back(m.weights_to_unpack.count(i) ? fcall._lazy_closure[i].eval() : fcall._closure[i]);+ // Only support permute transformations+ auto transforms = fcall._lazy_closure[i].get_transformations();+ if (transforms.size() == 2 && std::holds_alternative<ov::npuw::weights::op::Permute>(transforms[0])) {+ // Assuming we only match weights where DQ did set Transpose after - thus nothing extra to do here+ new_closure.push_back(fcall._closure[i]);+ } else {+ bool weight_to_unpack = m.weights_to_unpack.count(i) > 0;+ new_closure.push_back(weight_to_unpack ? fcall._lazy_closure[i].eval() : fcall._closure[i]);+ // Note: It's important here to manually detach LazyTensor since it's not going to be present in the+ // bank - thus left in memory+ if (weight_to_unpack) {+ fcall._lazy_closure[i].detach();+ }+ }}auto scale_iter = m.scale_remap.find(i);@@ -150,6 +159,14 @@ void apply_remap(Subgraph& fcall, const ClosureRemap& m) {// Check for asymmetric zero points and add them to new_zeropsnew_zerops.push_back(zerop_iter != m.zerop_remap.end() ? fcall._lazy_closure[zerop_iter->second].eval(): m.zero_points[i]);+ // Note: It's important here to manually detach LazyTensor since it's not going to be present in the bank - thus+ // left in memory+ if (scale_iter != m.scale_remap.end()) {+ fcall._lazy_closure[scale_iter->second].detach();+ }+ if (zerop_iter != m.zerop_remap.end()) {+ fcall._lazy_closure[zerop_iter->second].detach();+ }}fcall._scales = std::move(new_scales);fcall._zerops = std::move(new_zerops);@@ -249,6 +266,7 @@ void DCOFFPassBase::build() {paramB = opp::wrap_type<ov::op::v0::Parameter>();toFP32 = opp::wrap_type<ov::op::v0::Convert>({paramA});mulply = opp::wrap_type<ov::op::v1::Multiply>({toFP32, paramB});+ transposeopt = opp::optional<ov::op::v1::Transpose>({mulply->output(0), opp::any_input()});}bool DCOFFPassBase::matcher_callback(ov::pass::pattern::Matcher& m) {@@ -281,7 +299,7 @@ bool DCOFFPassBase::matcher_callback(ov::pass::pattern::Matcher& m) {m_params_to.get().scales[matched_paramB] = matched_paramA;// Disconnect Multiply and Convert from their outputs- auto matched_mulply = node_to_output.at(mulply).get_node_shared_ptr();+ auto matched_mulply = uat::_(node_to_output).at_or_at(transposeopt, mulply).get_node_shared_ptr();auto matched_convrt = node_to_output.at(toFP32).get_node_shared_ptr();auto drop_outputs = [](std::shared_ptr<ov::Node> node) {for (auto&& node_outputs : node->outputs()) {@@ -306,7 +324,7 @@ bool DCOFFPassBase::matcher_callback(ov::pass::pattern::Matcher& m) {void DCOFFPassMatMul::build() {DCOFFPassBase::build();auto _mmin1 = opp::any_input();- cvtopt = opp::optional<ov::op::v0::Convert>({mulply->output(0)});+ cvtopt = opp::optional<ov::op::v0::Convert>({transposeopt->output(0)});matmul = opp::wrap_type<ov::op::v0::MatMul>({_mmin1, cvtopt});register_matcher(std::make_shared<opp::Matcher>(matmul, "TagDCOFFMatMul"),std::bind(&DCOFFPassMatMul::matcher_callback, this, std::placeholders::_1));@@ -331,7 +349,7 @@ void DCOFFPassGather::build() {DCOFFPassBase::build();auto _gin2 = opp::any_input();auto _gin3 = opp::any_input();- gather = opp::wrap_type<ov::op::v8::Gather>({mulply, _gin2, _gin3});+ gather = opp::wrap_type<ov::op::v8::Gather>({transposeopt, _gin2, _gin3});register_matcher(std::make_shared<opp::Matcher>(gather, "TagDCOFFGather"),std::bind(&DCOFFPassGather::matcher_callback, this, std::placeholders::_1));}@@ -395,6 +413,7 @@ void DCOFFPassBase::build() {cvtB = opp::wrap_type<ov::op::v0::Convert>({constB});subtr = opp::wrap_type<ov::op::v1::Subtract>({cvtA, cvtB});mulply = opp::wrap_type<ov::op::v1::Multiply>({subtr, paramC});+ transposeopt = opp::optional<ov::op::v1::Transpose>({mulply->output(0), opp::any_input()});}bool DCOFFPassBase::matcher_callback(ov::pass::pattern::Matcher& m) {@@ -435,7 +454,7 @@ bool DCOFFPassBase::matcher_callback(ov::pass::pattern::Matcher& m) {m_params_to.get().scales[matched_paramC] = matched_paramA;// Disconnect Multiply and Convert from their outputs- auto matched_mulply = node_to_output.at(mulply).get_node_shared_ptr();+ auto matched_mulply = uat::_(node_to_output).at_or_at(transposeopt, mulply).get_node_shared_ptr();auto matched_convrt = node_to_output.at(cvtA).get_node_shared_ptr();auto drop_outputs = [](std::shared_ptr<ov::Node> node) {for (auto&& node_outputs : node->outputs()) {@@ -459,7 +478,7 @@ bool DCOFFPassBase::matcher_callback(ov::pass::pattern::Matcher& m) {void DCOFFPassReshape1::build() {DCOFFPassBase::build();auto scalar = opp::wrap_type<ov::op::v0::Constant>();- reshpe = opp::wrap_type<ov::op::v1::Reshape>({mulply, scalar});+ reshpe = opp::wrap_type<ov::op::v1::Reshape>({transposeopt, scalar});register_matcher(std::make_shared<opp::Matcher>(reshpe, "TagDCOFFReshape1"),std::bind(&DCOFFPassReshape1::matcher_callback, this, std::placeholders::_1));}@@ -473,7 +492,7 @@ void DCOFFPassReshape1::reconnect_root(ov::pass::pattern::Matcher& m) {void DCOFFPassConvert1::build() {DCOFFPassBase::build();- cvtEnd = opp::wrap_type<ov::op::v0::Convert>({mulply});+ cvtEnd = opp::wrap_type<ov::op::v0::Convert>({transposeopt});register_matcher(std::make_shared<opp::Matcher>(cvtEnd, "TagDCOFFConvert1"),std::bind(&DCOFFPassConvert1::matcher_callback, this, std::placeholders::_1));}@@ -526,9 +545,10 @@ DCOFFPassReshape2::DCOFFPassReshape2(DCOffMode dcoff_mode, ov::element::Type dcoauto cvtA = opp::wrap_type<ov::op::v0::Convert>({paramA});auto subtr = opp::wrap_type<ov::op::v1::Subtract>({cvtA, constB});auto mulply = opp::wrap_type<ov::op::v1::Multiply>({subtr, paramC});+ auto transposeopt = opp::optional<ov::op::v1::Transpose>({mulply->output(0), opp::any_input()});auto scalar = opp::wrap_type<ov::op::v0::Constant>();- auto reshpe = opp::wrap_type<ov::op::v1::Reshape>({mulply, scalar});+ auto reshpe = opp::wrap_type<ov::op::v1::Reshape>({transposeopt, scalar});auto callback = [=](ov::pass::pattern::Matcher& m) {auto& node_to_output = m.get_pattern_value_map();@@ -567,7 +587,7 @@ DCOFFPassReshape2::DCOFFPassReshape2(DCOffMode dcoff_mode, ov::element::Type dcopref.get().scales[matched_paramC] = matched_paramA;// Disconnect Multiply and Convert from their outputs- auto matched_mulply = node_to_output.at(mulply).get_node_shared_ptr();+ auto matched_mulply = uat::_(node_to_output).at_or_at(transposeopt, mulply).get_node_shared_ptr();auto matched_convrt = node_to_output.at(cvtA).get_node_shared_ptr();auto drop_outputs = [](std::shared_ptr<ov::Node> node) {for (auto&& node_outputs : node->outputs()) {@@ -616,6 +636,7 @@ DCOFFPassReshape3::DCOFFPassReshape3(DCOffMode dcoff_mode, ov::element::Type dcoauto cvtA = opp::wrap_type<ov::op::v0::Convert>({paramA});auto mulply = opp::wrap_type<ov::op::v1::Multiply>({cvtA, paramC});auto cvt = opp::wrap_type<ov::op::v0::Convert>({mulply});+ auto transposeopt = opp::optional<ov::op::v1::Transpose>({cvt->output(0), opp::any_input()});auto callback = [=](ov::pass::pattern::Matcher& m) {auto& node_to_output = m.get_pattern_value_map();@@ -663,7 +684,7 @@ DCOFFPassReshape3::DCOFFPassReshape3(DCOffMode dcoff_mode, ov::element::Type dcodrop_outputs(matched_convrt);LOG_DEBUG("Reconnecting the Root...");- auto matched_cvt = node_to_output.at(cvt).get_node_shared_ptr();+ auto matched_cvt = uat::_(node_to_output).at_or_at(transposeopt, cvt).get_node_shared_ptr();matched_cvt->input(0).replace_source_output(matched_paramA);}LOG_DEBUG("Done");@@ -671,7 +692,7 @@ DCOFFPassReshape3::DCOFFPassReshape3(DCOffMode dcoff_mode, ov::element::Type dcoreturn false; // root node hasn't changed};- register_matcher(std::make_shared<opp::Matcher>(cvt, "TagDCOFFPassReshape3"), std::move(callback));+ register_matcher(std::make_shared<opp::Matcher>(transposeopt, "TagDCOFFPassReshape3"), std::move(callback));}// Pattern: i4 group-quant@@ -698,8 +719,9 @@ DCOFFPassReshape4::DCOFFPassReshape4(DCOffMode dcoff_mode, ov::element::Type dcoauto paramC = opp::wrap_type<ov::op::v0::Parameter>();auto cvtA = opp::wrap_type<ov::op::v0::Convert>({paramA});auto mulply = opp::wrap_type<ov::op::v1::Multiply>({cvtA, paramC});+ auto transposeopt = opp::optional<ov::op::v1::Transpose>({mulply->output(0), opp::any_input()});auto scalar = opp::wrap_type<ov::op::v0::Constant>();- auto reshape = opp::wrap_type<ov::op::v1::Reshape>({mulply, scalar});+ auto reshape = opp::wrap_type<ov::op::v1::Reshape>({transposeopt, scalar});auto callback = [=](ov::pass::pattern::Matcher& m) {auto& node_to_output = m.get_pattern_value_map();@@ -735,7 +757,7 @@ DCOFFPassReshape4::DCOFFPassReshape4(DCOffMode dcoff_mode, ov::element::Type dco}LOG_DEBUG("Reconnecting the Root...");- auto matched_reshape = node_to_output.at(reshape).get_node_shared_ptr();+ auto matched_reshape = uat::_(node_to_output).at_or_at(transposeopt, reshape).get_node_shared_ptr();matched_reshape->input(0).replace_source_output(new_rshp_in);}LOG_DEBUG("Done");@@ -983,9 +1005,10 @@ DCOFFPassReshape::DCOFFPassReshape(DCOffMode dcoff_mode, ov::element::Type dcoffauto cvtB = opp::wrap_type<ov::op::v0::Convert>({paramB});auto subtr = opp::wrap_type<ov::op::v1::Subtract>({cvtA, cvtB});auto mulply = opp::wrap_type<ov::op::v1::Multiply>({subtr, paramC});+ auto transposeopt = opp::optional<ov::op::v1::Transpose>({mulply->output(0), opp::any_input()});auto scalar = opp::wrap_type<ov::op::v0::Constant>();- auto reshpe = opp::wrap_type<ov::op::v1::Reshape>({mulply, scalar});+ auto reshpe = opp::wrap_type<ov::op::v1::Reshape>({transposeopt, scalar});auto callback = [=](ov::pass::pattern::Matcher& m) {auto& node_to_output = m.get_pattern_value_map();@@ -1024,7 +1047,7 @@ DCOFFPassReshape::DCOFFPassReshape(DCOffMode dcoff_mode, ov::element::Type dcoffpref.get().scales[matched_paramC] = matched_paramA;// Disconnect Multiply and Convert from their outputs- auto matched_mulply = node_to_output.at(mulply).get_node_shared_ptr();+ auto matched_mulply = uat::_(node_to_output).at_or_at(transposeopt, mulply).get_node_shared_ptr();auto matched_convrt = node_to_output.at(cvtA).get_node_shared_ptr();auto drop_outputs = [](std::shared_ptr<ov::Node> node) {for (auto&& node_outputs : node->outputs()) {diff --git a/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/dcoff.hpp b/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/dcoff.hppindex 4b4cbdd823d..bd4cf8466af 100644--- a/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/dcoff.hpp+++ b/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/dcoff.hpp@@ -62,7 +62,7 @@ protected:ov::element::Type m_dcoff_type;DCOFFParamRef m_params_to;- std::shared_ptr<ov::Node> paramA, paramB, toFP32, mulply, cvtopt;+ std::shared_ptr<ov::Node> paramA, paramB, toFP32, mulply, cvtopt, transposeopt;bool matcher_callback(ov::pass::pattern::Matcher& m);public:@@ -105,7 +105,7 @@ protected:ov::element::Type m_dcoff_type;DCOFFParamRef m_params_to;- std::shared_ptr<ov::Node> paramA, constB, paramC, cvtA, cvtB, subtr, mulply;+ std::shared_ptr<ov::Node> paramA, constB, paramC, cvtA, cvtB, subtr, mulply, transposeopt;bool matcher_callback(ov::pass::pattern::Matcher& m);public:diff --git a/src/plugins/intel_npu/src/plugin/npuw/serialization.cpp b/src/plugins/intel_npu/src/plugin/npuw/serialization.cppindex efb3f867b2b..eabd919c342 100644--- a/src/plugins/intel_npu/src/plugin/npuw/serialization.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/serialization.cpp@@ -94,7 +94,8 @@ enum class AnyType : int {FLOAT,BOOL,CACHE_MODE,- ELEMENT_TYPE+ ELEMENT_TYPE,+ ANYMAP};void ov::npuw::s11n::write_any(std::ostream& stream, const ov::Any& var) {@@ -133,6 +134,9 @@ void ov::npuw::s11n::write_any(std::ostream& stream, const ov::Any& var) {} else if (var.is<ov::element::Type>()) {write(stream, static_cast<int>(AnyType::ELEMENT_TYPE));write(stream, var.as<ov::element::Type>());+ } else if (var.is<ov::AnyMap>()) {+ write(stream, static_cast<int>(AnyType::ANYMAP));+ write(stream, var.as<ov::AnyMap>());} else {NPUW_ASSERT(false && "Unsupported type");}@@ -150,6 +154,14 @@ void ov::npuw::s11n::write(std::ostream& stream, const ov::element::Type& var) {stream.write(reinterpret_cast<const char*>(&var), sizeof var);}+void ov::npuw::s11n::write(std::ostream& stream, const ov::AnyMap& var) {+ write(stream, var.size());+ for (const auto& el : var) {+ write(stream, el.first);+ write_any(stream, el.second);+ }+}+void ov::npuw::s11n::read(std::istream& stream, std::streampos& var) {stream.read(reinterpret_cast<char*>(&var), sizeof var);}@@ -299,6 +311,10 @@ void ov::npuw::s11n::read_any(std::istream& stream, ov::Any& var) {ov::element::Type val;read(stream, val);var = val;+ } else if (type == AnyType::ANYMAP) {+ ov::AnyMap val;+ read(stream, val);+ var = val;} else {NPUW_ASSERT(false && "Unsupported type");}@@ -316,6 +332,18 @@ void ov::npuw::s11n::read(std::istream& stream, ov::element::Type& var) {stream.read(reinterpret_cast<char*>(&var), sizeof var);}+void ov::npuw::s11n::read(std::istream& stream, ov::AnyMap& var) {+ std::size_t var_size = 0;+ read(stream, var_size);+ for (std::size_t i = 0; i < var_size; ++i) {+ std::string k;+ read(stream, k);+ ov::Any v;+ read_any(stream, v);+ var[k] = v;+ }+}+// Weightless// FIXME: all serialization needs a good rewritingvoid ov::npuw::s11n::write_weightless(std::ostream& stream,@@ -375,10 +403,8 @@ void ov::npuw::s11n::read_weightless(std::istream& stream,} else {auto it = ctx.consts_cache.find({offset, byte_size});NPUW_ASSERT(it != ctx.consts_cache.end() && "Couldn't find Constant in cache!");- auto tensor = ov::npuw::util::tensor_from_const(it->second);- NPUW_ASSERT(tensor.get_byte_size() == byte_size && tensor.get_shape() == shape &&- tensor.get_element_type() == type);- tensor.copy_to(t);+ t = ov::npuw::util::copy_tensor_from_const(it->second);+ NPUW_ASSERT(t.get_byte_size() == byte_size && t.get_shape() == shape && t.get_element_type() == type);}var.push_back(t);diff --git a/src/plugins/intel_npu/src/plugin/npuw/serialization.hpp b/src/plugins/intel_npu/src/plugin/npuw/serialization.hppindex 6cc1d2d6f9c..7fcf7b4e22b 100644--- a/src/plugins/intel_npu/src/plugin/npuw/serialization.hpp+++ b/src/plugins/intel_npu/src/plugin/npuw/serialization.hpp@@ -120,6 +120,7 @@ void write_any(std::ostream& stream, const ov::Any& var);void write(std::ostream& stream, const ov::npuw::weights::LazyTensor& var);void write(std::ostream& stream, const ov::CacheMode& var);void write(std::ostream& stream, const ov::element::Type& var);+void write(std::ostream& stream, const std::map<std::string, Any>& var);void read(std::istream& stream, std::streampos& var);void read(std::istream& stream, std::string& var);@@ -134,6 +135,7 @@ void read_any(std::istream& stream, ov::Any& var);void read(std::istream& stream, ov::npuw::weights::LazyTensor& var);void read(std::istream& stream, ov::CacheMode& var);void read(std::istream& stream, ov::element::Type& var);+void read(std::istream& stream, std::map<std::string, Any>& var);// Weightless utilsvoid write_weightless(std::ostream& stream, const std::vector<ov::Tensor>& var, const WeightsContext& ctx);diff --git a/src/plugins/intel_npu/src/plugin/npuw/util.cpp b/src/plugins/intel_npu/src/plugin/npuw/util.cppindex ccef2079adc..0ca6f4382cb 100644--- a/src/plugins/intel_npu/src/plugin/npuw/util.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/util.cpp@@ -163,6 +163,16 @@ ov::Tensor ov::npuw::util::tensor_from_const(const std::shared_ptr<ov::Node>& noreturn ov::Tensor(port.get_element_type(), port.get_shape(), const_cast<void*>(cnst_node->get_data_ptr()));}+ov::Tensor ov::npuw::util::copy_tensor_from_const(const std::shared_ptr<ov::Node>& node) {+ NPUW_ASSERT(ov::op::util::is_constant(node));+ NPUW_ASSERT(node->outputs().size() == 1);+ const auto port = node->output(0);+ auto cnst_node = ov::as_type_ptr<ov::op::v0::Constant>(node);+ auto tensor = ov::Tensor(port.get_element_type(), port.get_shape());+ std::memcpy(tensor.data(), cnst_node->get_data_ptr(), cnst_node->get_byte_size());+ return tensor;+}+bool ov::npuw::util::starts_with(const std::string& str, const std::string& prefix) {return str.substr(0, prefix.size()) == prefix;}diff --git a/src/plugins/intel_npu/src/plugin/npuw/util.hpp b/src/plugins/intel_npu/src/plugin/npuw/util.hppindex 501c97cdff4..729187fe702 100644--- a/src/plugins/intel_npu/src/plugin/npuw/util.hpp+++ b/src/plugins/intel_npu/src/plugin/npuw/util.hpp@@ -24,6 +24,10 @@ bool is_set(const std::size_t sub_idx,// NB: Newer C++ standards would allow to use string views or smtov::Tensor tensor_from_const(const std::shared_ptr<ov::Node>& node);+// In case of working with memory which will be detached later (Constant will be freed),+// we need to explicitly create a tensor which owns the memory during the execution.+ov::Tensor copy_tensor_from_const(const std::shared_ptr<ov::Node>& node);+bool starts_with(const std::string& str, const std::string& prefix);std::string fmt(std::size_t number, std::size_t total);diff --git a/src/plugins/intel_npu/src/plugin/npuw/weights_bank.cpp b/src/plugins/intel_npu/src/plugin/npuw/weights_bank.cppindex c48534af0a2..9e59053abc4 100644--- a/src/plugins/intel_npu/src/plugin/npuw/weights_bank.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/weights_bank.cpp@@ -114,6 +114,7 @@ void Bank::evaluate_and_allocate() {if (device_for_alloc == "CPU") {// No allocation neededdevice_bank.storage[device_bank.registered_tensors.at(lt)].tensor = transformed_tensor;+ const_cast<LazyTensor&>(lt).detach();return;}