Skip to content

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

[NPUW] Extend HOST_GATHER with an additional case (#31535)

Alexey Smirnov4dcd25720405
diff --git a/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.cpp b/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.cpp
index 05cf51b5bf9..90fcb382fcf 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.cpp
@@ -360,9 +360,88 @@ Convert Convert::deserialize(std::istream& stream) {
read(stream, c.tensor);
return c;
}
+
+std::size_t Gather::hash() const {
+ std::size_t seed = w.get_hash() + 0x9e3779b9;
+ seed ^= t.get_element_type().hash() + 0x9e3779b9;
+ for (const auto& dim : t.get_shape()) {
+ seed ^= std::hash<std::size_t>()(dim) + 0x9e3779b9;
+ }
+ auto ttype = t.get_element_type();
+ NPUW_ASSERT(ttype == ov::element::f8e4m3 || ttype == ov::element::f8e5m2 || ttype == ov::element::f8e8m0);
+ std::vector<uint8_t> t_data(t.get_size());
+ std::memcpy(t_data.data(), static_cast<uint8_t*>(t.data()), t.get_size());
+ seed ^= t_data.size();
+ for (const auto& el : t_data) {
+ seed ^= std::hash<uint8_t>()(el) + 0x9e3779b9;
+ }
+ seed ^= dst_type.hash() + 0x9e3779b9;
+ for (const auto& dim : dst_shape) {
+ seed ^= std::hash<std::size_t>()(dim) + 0x9e3779b9;
+ }
+ return seed;
+}
+
+bool Gather::operator==(const Gather& other) const {
+ auto ttype = t.get_element_type();
+ NPUW_ASSERT(ttype == ov::element::f8e4m3 || ttype == ov::element::f8e5m2 || ttype == ov::element::f8e8m0);
+ std::vector<uint8_t> t_data(t.get_size());
+ std::memcpy(t_data.data(), static_cast<uint8_t*>(t.data()), t.get_size());
+
+ auto ttype_other = other.t.get_element_type();
+ NPUW_ASSERT(ttype_other == ov::element::f8e4m3 || ttype_other == ov::element::f8e5m2 ||
+ ttype_other == ov::element::f8e8m0);
+ std::vector<uint8_t> t_other_data(other.t.get_size());
+ std::memcpy(t_other_data.data(), static_cast<uint8_t*>(other.t.data()), other.t.get_size());
+
+ return (w == other.w && t.get_element_type() == other.t.get_element_type() &&
+ t.get_shape() == other.t.get_shape() && t_data == t_other_data && dst_type == other.dst_type &&
+ dst_shape == other.dst_shape);
+}
+
+ov::Tensor Gather::eval() const {
+ auto ttype = t.get_element_type();
+ NPUW_ASSERT(ttype == ov::element::f8e4m3 || ttype == ov::element::f8e5m2 || ttype == ov::element::f8e8m0);
+ ov::Tensor dst(dst_type, dst_shape);
+ const auto& gti = ov::get_tensor_impl;
+ ov::npuw::util::gather_cb4(gti(t), gti(w.eval()), gti(dst));
+ return dst;
+}
+
+LazyTensor::Meta Gather::eval_meta() const {
+ return {dst_shape, dst_type};
+}
+
+void Gather::read_weight(const ov::npuw::s11n::WeightsContext& ctx) {
+ w.read_weight(ctx);
+}
+
+void Gather::detach() {
+ w.detach();
+}
+
+void Gather::serialize(std::ostream& stream) const {
+ using namespace ov::npuw::s11n;
+ write(stream, dst_type.to_string());
+ write(stream, dst_shape);
+ write(stream, w);
+ write(stream, t);
+}
+
+Gather Gather::deserialize(std::istream& stream) {
+ using namespace ov::npuw::s11n;
+ Gather g;
+ std::string type_str;
+ read(stream, type_str);
+ g.dst_type = ov::element::Type(type_str);
+ read(stream, g.dst_shape);
+ read(stream, g.w);
+ read(stream, g.t);
+ return g;
+}
} // namespace op
-enum class TransformType : int { CONST = 0, CONCAT, UNPACK, PERMUTE, CONVERT };
+enum class TransformType : int { CONST = 0, CONCAT, UNPACK, PERMUTE, CONVERT, GATHER };
struct LazyTensorImpl {
LazyTensorImpl() = default;
@@ -446,31 +525,37 @@ std::size_t LazyTensorImpl::get_hash() const {
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());
- }},
+ 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());
+ },
+ [&vec](const op::Gather& op) {
+ auto next_tr = op.w.get_transformations();
+ vec.insert(vec.end(), next_tr.begin(), next_tr.end());
+ },
+ },
m_transform);
}
@@ -485,26 +570,32 @@ void LazyTensorImpl::serialize(std::ostream& stream) const {
using namespace ov::npuw::s11n;
write(stream, m_hash);
// FIXME: create proper op identificators instead of int
- std::visit(overloaded{[&stream](const op::Concat& op) {
- write(stream, static_cast<int>(TransformType::CONCAT));
- op.serialize(stream);
- },
- [&stream](const op::Const& op) {
- write(stream, static_cast<int>(TransformType::CONST));
- op.serialize(stream);
- },
- [&stream](const op::Convert& op) {
- write(stream, static_cast<int>(TransformType::CONVERT));
- op.serialize(stream);
- },
- [&stream](const op::Permute& op) {
- write(stream, static_cast<int>(TransformType::PERMUTE));
- op.serialize(stream);
- },
- [&stream](const op::Unpack& op) {
- write(stream, static_cast<int>(TransformType::UNPACK));
- op.serialize(stream);
- }},
+ std::visit(overloaded{
+ [&stream](const op::Concat& op) {
+ write(stream, static_cast<int>(TransformType::CONCAT));
+ op.serialize(stream);
+ },
+ [&stream](const op::Const& op) {
+ write(stream, static_cast<int>(TransformType::CONST));
+ op.serialize(stream);
+ },
+ [&stream](const op::Convert& op) {
+ write(stream, static_cast<int>(TransformType::CONVERT));
+ op.serialize(stream);
+ },
+ [&stream](const op::Permute& op) {
+ write(stream, static_cast<int>(TransformType::PERMUTE));
+ op.serialize(stream);
+ },
+ [&stream](const op::Unpack& op) {
+ write(stream, static_cast<int>(TransformType::UNPACK));
+ op.serialize(stream);
+ },
+ [&stream](const op::Gather& op) {
+ write(stream, static_cast<int>(TransformType::GATHER));
+ op.serialize(stream);
+ },
+ },
m_transform);
}
@@ -530,6 +621,9 @@ std::shared_ptr<LazyTensorImpl> LazyTensorImpl::deserialize(std::istream& stream
case TransformType::UNPACK:
lt_impl->m_transform = op::Unpack::deserialize(stream);
break;
+ case TransformType::GATHER:
+ lt_impl->m_transform = op::Gather::deserialize(stream);
+ break;
default:
NPUW_ASSERT(false && "Unsupported type");
break;
@@ -547,6 +641,11 @@ LazyTensor::LazyTensor(const LazyTensor& cw,
const ov::element::Type& type,
const ov::Shape& shape)
: m_impl(std::make_shared<LazyTensorImpl>(op::Unpack(cw, cz, cs, type, shape))) {}
+LazyTensor::LazyTensor(const LazyTensor& cw,
+ const ov::Tensor& t,
+ const ov::element::Type& dst_type,
+ const ov::Shape& dst_shape)
+ : m_impl(std::make_shared<LazyTensorImpl>(op::Gather(cw, t, dst_type, dst_shape))) {}
LazyTensor LazyTensor::permute(const std::vector<std::size_t>& axes) {
LazyTensor new_lt;
diff --git a/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.hpp b/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.hpp
index 273a24c15ac..78a482f5f32 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.hpp
@@ -26,6 +26,7 @@ class Concat;
class Unpack;
class Permute;
class Convert;
+class Gather;
} // namespace op
class LazyTensor {
@@ -39,7 +40,8 @@ public:
ov::npuw::weights::op::Concat,
ov::npuw::weights::op::Unpack,
ov::npuw::weights::op::Permute,
- ov::npuw::weights::op::Convert>;
+ ov::npuw::weights::op::Convert,
+ ov::npuw::weights::op::Gather>;
LazyTensor() = default;
LazyTensor(const std::shared_ptr<ov::op::v0::Constant>& const_ptr);
@@ -49,6 +51,10 @@ public:
const LazyTensor& cs,
const ov::element::Type& type,
const ov::Shape& shape); // construct from unpack
+ LazyTensor(const LazyTensor& cw,
+ const ov::Tensor& t,
+ const ov::element::Type& dst_type,
+ const ov::Shape& dst_shape); // construct from nf4_gather
LazyTensor permute(const std::vector<std::size_t>& axes);
LazyTensor convert(const ov::element::Type& type);
@@ -194,6 +200,33 @@ private:
LazyTensor tensor;
ov::element::Type type;
};
+
+class Gather {
+ friend struct ov::npuw::weights::LazyTensorImpl;
+
+public:
+ Gather() = default;
+ Gather(const LazyTensor& _w, const ov::Tensor& _t, const ov::element::Type& _dst_type, const ov::Shape& _dst_shape)
+ : w(_w),
+ t(_t),
+ dst_type(_dst_type),
+ dst_shape(_dst_shape) {}
+
+ std::size_t hash() const;
+ bool operator==(const Gather& other) const;
+ ov::Tensor eval() const;
+ LazyTensor::Meta eval_meta() const;
+ void read_weight(const ov::npuw::s11n::WeightsContext& ctx);
+ void detach();
+ void serialize(std::ostream& stream) const;
+ static Gather deserialize(std::istream& stream);
+
+private:
+ LazyTensor w;
+ ov::Tensor t;
+ ov::element::Type dst_type;
+ ov::Shape dst_shape;
+};
} // namespace op
} // namespace weights
diff --git a/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.cpp b/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.cpp
index f7a0a82f873..37ac28f92e6 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.cpp
@@ -1961,6 +1961,7 @@ void Partitioner::optimize(const std::string& func_name) {
ov::pass::GraphRewrite rewr2;
rewr2.add_matcher<ov::npuw::patterns::opt::HostGather>(std::ref(ctx));
rewr2.add_matcher<ov::npuw::patterns::opt::HostGatherDQ>(std::ref(ctx));
+ rewr2.add_matcher<ov::npuw::patterns::opt::HostGatherCB4>(std::ref(ctx));
rewr2.run_on_model(f._model);
}
@@ -2002,6 +2003,31 @@ void Partitioner::optimize(const std::string& func_name) {
});
}
+ // Gather NF4 closures in compile time, where requested
+ for (auto&& p : ctx.params_to_nf4_gather) {
+ const auto& tensor_to_gather = p.second;
+ auto w_idx = f._model->get_parameter_index(tensor_to_gather.w);
+
+ // Need to add a new parameter right away, since it's going to be processed by the unpack below
+ f._model->add_parameters({p.first});
+ to_remove.push_back(tensor_to_gather.w);
+ to_remove_idx.insert(w_idx);
+
+ ov::npuw::util::non_parallel_for(func_group.refs.size(), [&](std::size_t f_idx) {
+ auto& funcall = func_group.refs[f_idx].get();
+ LazyTensor cw = funcall._lazy_closure[w_idx - f._param_offset];
+ funcall._lazy_closure.push_back(
+ LazyTensor(cw, tensor_to_gather.t, p.first->get_element_type(), p.first->get_shape()));
+ // Some of the tensors might be in closure - preserve it's 1:1 idx mapping with _lazy_closure
+ funcall._closure.push_back(ov::Tensor());
+ // FIXME: in some cases we might have DCOFF and DQ enabled together. This
+ // might lead to DQ passes finding patterns and running remap on closures unconditionally.
+ // It assigns some closures to be calculated instead of keeping the lazy ones.
+ // Here we remember lazy closure not to be unpacked in compile time in DCOFF.
+ funcall._is_lazy_unpack.push_back(false);
+ });
+ }
+
// Unpack closures in compile time, where requested
for (auto&& p : ctx.params_to_unpack) {
const auto& tensor_to_unpack = p.second;
diff --git a/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/opt.cpp b/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/opt.cpp
index 4a0c13f0565..82c15c6802f 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/opt.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/opt.cpp
@@ -109,6 +109,21 @@ Context::PPtr Context::unpack(const Context::PPtr& w, const Context::PPtr& s, ov
return new_param;
}
+Context::PPtr Context::gather_cb4(const Context::PPtr& w, const ov::Tensor& t, ov::element::Type type) {
+ const auto& w_shape = w->get_shape();
+
+ Context::PPtr new_param;
+ if (w_shape.size() == 2) {
+ new_param = std::make_shared<ov::op::v0::Parameter>(type, w_shape);
+ } else {
+ NPUW_ASSERT(false && "Yet unsupported combination");
+ }
+
+ NPUW_ASSERT(new_param);
+ params_to_nf4_gather[new_param] = {w, t};
+ return new_param;
+}
+
Context::PPtr Context::host_gather(const Context::PPtr& w, const Context::PPtr& ids) {
const auto& w_shape = w->get_shape();
const auto& ids_shape = ids->get_shape();
@@ -1618,7 +1633,11 @@ HostGatherDQ::HostGatherDQ(Context::Ref ctx) {
const auto& matched_out_mul = node_to_output.at(qmul);
auto out_shape = matched_out_mul.get_shape();
- if (out_shape.size() != 3 && out_shape.size() != 4) {
+ const auto& matched_out_qweight = node_to_output.at(qweight);
+ auto qweight_type = matched_out_qweight.get_element_type();
+
+ if ((out_shape.size() != 3 && out_shape.size() != 4) ||
+ (out_shape.size() == 2 && qweight_type != ov::element::nf4)) {
return false;
}
@@ -1627,12 +1646,9 @@ HostGatherDQ::HostGatherDQ(Context::Ref ctx) {
// were Hs = hidden size, G is # of groups, N is the prompt size.
auto out_len = out_shape.size() == 3 ? out_shape[2] : out_shape[2] * out_shape[3];
- const auto& matched_out_qweight = node_to_output.at(qweight);
- auto qweight_type = matched_out_qweight.get_element_type();
-
if (out_len >= 2048 && (qweight_type == ov::element::i4 || qweight_type == ov::element::i8 ||
- qweight_type == ov::element::f8e4m3 || qweight_type == ov::element::f8e5m2 ||
- qweight_type == ov::element::f8e8m0)) {
+ qweight_type == ov::element::nf4 || qweight_type == ov::element::f8e4m3 ||
+ qweight_type == ov::element::f8e5m2 || qweight_type == ov::element::f8e8m0)) {
auto matched_node_qweight = node_to_output.at(qweight).get_node_shared_ptr();
auto matched_node_qcoeff = node_to_output.at(qcoeff).get_node_shared_ptr();
auto matched_node_ids = node_to_output.at(pids).get_node_shared_ptr();
@@ -1653,6 +1669,62 @@ HostGatherDQ::HostGatherDQ(Context::Ref ctx) {
register_matcher(std::make_shared<opp::Matcher>(qmul, "HostGatherDQ"), std::move(callback));
}
+HostGatherCB4::HostGatherCB4(Context::Ref ctx) {
+ auto pids = opp::wrap_type<ov::op::v0::Parameter>();
+ auto cvtids = opp::wrap_type<ov::op::v0::Convert>({pids});
+
+ auto qweight = opp::wrap_type<ov::op::v0::Parameter>();
+ auto qcvtw = opp::wrap_type<ov::op::v0::Convert>({qweight});
+ auto qtable = opp::wrap_type<ov::op::v0::Constant>();
+ auto qcvtwt = opp::wrap_type<ov::op::v0::Convert>({qtable});
+ auto qweightg = opp::wrap_type<ov::op::v8::Gather>({qcvtwt, qcvtw, opp::any_input()});
+
+ auto qcoeff = opp::wrap_type<ov::op::v0::Parameter>();
+
+ auto qmul = opp::wrap_type<ov::op::v1::Multiply>({qweightg, qcoeff});
+ auto qcvtmul = opp::wrap_type<ov::op::v0::Convert>({qmul});
+
+ auto qgthr = opp::wrap_type<ov::op::v8::Gather>({qcvtmul, cvtids, opp::any_input()});
+
+ auto callback = [=](ov::pass::pattern::Matcher& m) {
+ auto& node_to_output = m.get_pattern_value_map();
+ const auto& matched_out_mul = node_to_output.at(qmul);
+ auto out_shape = matched_out_mul.get_shape();
+
+ if (out_shape.size() == 2 && out_shape.back() >= 2048 &&
+ node_to_output.at(qweight).get_element_type() == ov::element::u4) {
+ auto matched_node_qweight = node_to_output.at(qweight).get_node_shared_ptr();
+ auto matched_node_qcoeff = node_to_output.at(qcoeff).get_node_shared_ptr();
+ auto matched_node_ids = node_to_output.at(pids).get_node_shared_ptr();
+ auto matched_node_qgthr = node_to_output.at(qgthr).get_node_shared_ptr();
+ auto matched_node_qtable = node_to_output.at(qtable).get_node_shared_ptr();
+
+ auto matched_qweight = std::static_pointer_cast<ov::op::v0::Parameter>(matched_node_qweight);
+ auto matched_qcoeff = std::static_pointer_cast<ov::op::v0::Parameter>(matched_node_qcoeff);
+ auto matched_ids = std::static_pointer_cast<ov::op::v0::Parameter>(matched_node_ids);
+ auto matched_qtable = std::static_pointer_cast<ov::op::v0::Constant>(matched_node_qtable);
+
+ // Need to gather the weight into f16 first
+ auto fp16weight = ctx.get().gather_cb4(matched_qweight,
+ ov::npuw::util::copy_tensor_from_const(matched_qtable),
+ ov::element::f16);
+ auto fp16vocab = ctx.get().unpack(fp16weight, matched_qcoeff, ov::element::f16);
+ auto new_param = ctx.get().host_gather(fp16vocab, matched_ids);
+
+ NPUW_ASSERT(new_param);
+ NPUW_ASSERT(matched_node_qgthr->get_element_type() == ov::element::f32);
+ auto cvt_new_param = std::make_shared<ov::op::v0::Convert>(new_param, ov::element::f32);
+
+ for (auto&& r : node_to_output.at(qgthr).get_target_inputs()) {
+ r.replace_source_output(cvt_new_param);
+ }
+ return true; // Root has changed
+ }
+ return false; // Root hasn't changed (yet)
+ };
+ register_matcher(std::make_shared<opp::Matcher>(qgthr, "HostGatherCB4"), std::move(callback));
+}
+
// FROM:
// Param(W) -> to(f16) ->
// Param(Z) -> to(f16) -> Subtract
diff --git a/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/opt.hpp b/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/opt.hpp
index e434e153ec6..6f77cf0e433 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/opt.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/opt.hpp
@@ -54,6 +54,13 @@ struct Context {
PPtr unpack(const PPtr& w, const PPtr& z, const PPtr& s, ov::element::Type type);
PPtr unpack(const PPtr& w, const PPtr& s, ov::element::Type type);
+ struct DQNF4Gather {
+ PPtr w;
+ ov::Tensor t;
+ };
+ std::map<PPtr, DQNF4Gather> params_to_nf4_gather;
+ PPtr gather_cb4(const PPtr& w, const ov::Tensor& t, ov::element::Type type);
+
struct Gather {
PPtr pnew, pold, pids;
};
@@ -185,6 +192,12 @@ public:
HostGatherDQ(Context::Ref ctx);
};
+class HostGatherCB4 : public ov::pass::MatcherPass {
+public:
+ OPENVINO_MATCHER_PASS_RTTI("npuw::patterns::opt::HostGatherCB4");
+ HostGatherCB4(Context::Ref ctx);
+};
+
// Tail vocab unpacks
class DQUnpackDictMatMulCWu : public ov::pass::MatcherPass {
diff --git a/src/plugins/intel_npu/src/plugin/npuw/serialization.hpp b/src/plugins/intel_npu/src/plugin/npuw/serialization.hpp
index eca0e709b55..613ad079d80 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/serialization.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/serialization.hpp
@@ -34,7 +34,7 @@ const constexpr ov::npuw::s11n::IndicatorType NPUW_COMPILED_MODEL_INDICATOR =
const constexpr ov::npuw::s11n::IndicatorType NPUW_LLM_COMPILED_MODEL_INDICATOR =
{char{0x4c}, char{0x4c}, char{0x4d}, char{0x43}, char{0x4d}, char{0x4f}};
-const constexpr char* NPUW_SERIALIZATION_VERSION = "0.7";
+const constexpr char* NPUW_SERIALIZATION_VERSION = "0.8";
// Forward declaration
namespace intel_npu {
diff --git a/src/plugins/intel_npu/src/plugin/npuw/util.cpp b/src/plugins/intel_npu/src/plugin/npuw/util.cpp
index 16d2d0e32bc..fbb60fe6ec7 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/util.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/util.cpp
@@ -153,6 +153,33 @@ void unpack_f8f16(const ov::SoPtr<ov::ITensor>& from,
}
}
+void unpack_f16f16(const ov::SoPtr<ov::ITensor>& from,
+ const ov::SoPtr<ov::ITensor>& scale,
+ const ov::SoPtr<ov::ITensor>& to,
+ const ov::npuw::util::UnpackOptions& unpack_options) {
+ auto from_shape = from->get_shape();
+ auto scale_shape = scale->get_shape();
+
+ NPUW_ASSERT(from->is_continuous());
+ NPUW_ASSERT(to->is_continuous());
+ NPUW_ASSERT(scale->is_continuous());
+ NPUW_ASSERT(from->get_size() == to->get_size());
+ NPUW_ASSERT(from_shape[0] == scale_shape[0]);
+ NPUW_ASSERT(scale_shape[1] == 1);
+ NPUW_ASSERT(from->get_element_type() == ov::element::f16);
+ NPUW_ASSERT(scale->get_element_type() == ov::element::f16);
+ NPUW_ASSERT(to->get_element_type() == ov::element::f16);
+
+ const auto* from_ptr = from->data<ov::float16>();
+ const auto* scale_ptr = scale->data<ov::float16>();
+ auto* to_ptr = to->data<ov::float16>();
+ const auto size = to->get_size();
+
+ ov::parallel_for(size, [&](size_t idx) {
+ to_ptr[idx] = from_ptr[idx] * scale_ptr[idx / from_shape[1]];
+ });
+}
+
} // namespace
ov::Tensor ov::npuw::util::tensor_from_const(const std::shared_ptr<ov::Node>& node) {
@@ -253,6 +280,9 @@ void ov::npuw::util::unpack(const ov::SoPtr<ov::ITensor>& from,
type_from == ov::element::f8e8m0) {
// FIXME: Implement XARCH::unpack
unpack_f8f16(from, scale, to, unpack_options);
+ } else if (type_from == ov::element::f16) {
+ // FIXME: Implement XARCH::unpack
+ unpack_f16f16(from, scale, to, unpack_options);
} else {
NPUW_ASSERT(false && "Unsupported combination");
}
@@ -402,6 +432,49 @@ void ov::npuw::util::gather(const ov::SoPtr<ov::ITensor>& src,
}
}
+void ov::npuw::util::gather_cb4(const ov::SoPtr<ov::ITensor>& src,
+ const ov::SoPtr<ov::ITensor>& idx,
+ const ov::SoPtr<ov::ITensor>& dst) {
+ const auto src_type = src->get_element_type();
+ const auto dst_type = dst->get_element_type();
+ NPUW_ASSERT(idx->get_element_type() == ov::element::u4);
+ NPUW_ASSERT(src_type == ov::element::f8e4m3 || src_type == ov::element::f8e5m2 || src_type == ov::element::f8e8m0);
+ NPUW_ASSERT(dst_type == ov::element::f16);
+
+ NPUW_ASSERT(idx->get_shape().size() == 2);
+ NPUW_ASSERT(src->get_shape().size() == 1);
+ NPUW_ASSERT(dst->get_shape().size() == 2);
+
+ NPUW_ASSERT(dst->get_size() % 2 == 0);
+
+ const uint8_t* pIdx = static_cast<uint8_t*>(idx->data()); // u4
+ ov::float16* pDst = dst->data<ov::float16>();
+
+ // FIXME: copypaste with a different type
+ if (src_type == ov::element::f8e4m3) {
+ const auto* pSrc = src->data<ov::float8_e4m3>();
+ for (std::size_t i = 0; i < dst->get_size(); i += 2) {
+ *(pDst + i) = static_cast<float>(pSrc[lo4(*pIdx)]);
+ *(pDst + i + 1) = static_cast<float>(pSrc[hi4(*pIdx)]);
+ pIdx++;
+ }
+ } else if (src_type == ov::element::f8e5m2) {
+ const auto* pSrc = src->data<ov::float8_e5m2>();
+ for (std::size_t i = 0; i < dst->get_size(); i += 2) {
+ *(pDst + i) = static_cast<float>(pSrc[lo4(*pIdx)]);
+ *(pDst + i + 1) = static_cast<float>(pSrc[hi4(*pIdx)]);
+ pIdx++;
+ }
+ } else {
+ const auto* pSrc = src->data<ov::float8_e8m0>();
+ for (std::size_t i = 0; i < dst->get_size(); i += 2) {
+ *(pDst + i) = static_cast<float>(pSrc[lo4(*pIdx)]);
+ *(pDst + i + 1) = static_cast<float>(pSrc[hi4(*pIdx)]);
+ pIdx++;
+ }
+ }
+}
+
ov::SoPtr<ov::ITensor> ov::npuw::util::view(const ov::SoPtr<ov::ITensor>& src,
const ov::npuw::util::View& from,
const ov::npuw::util::View& to) {
diff --git a/src/plugins/intel_npu/src/plugin/npuw/util.hpp b/src/plugins/intel_npu/src/plugin/npuw/util.hpp
index a91ea46e430..6543dee02bf 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/util.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/util.hpp
@@ -60,6 +60,9 @@ void unpack(const ov::SoPtr<ov::ITensor>& from,
const UnpackOptions& unpack_options = UnpackOptions{true, 16, false});
void gather(const ov::SoPtr<ov::ITensor>& src, const ov::SoPtr<ov::ITensor>& idx, const ov::SoPtr<ov::ITensor>& dst);
+void gather_cb4(const ov::SoPtr<ov::ITensor>& src,
+ const ov::SoPtr<ov::ITensor>& idx,
+ const ov::SoPtr<ov::ITensor>& dst);
using View = std::vector<std::size_t>;
ov::SoPtr<ov::ITensor> view(const ov::SoPtr<ov::ITensor>& src, const View& from, const View& to);