Skip to content

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

[NPUW] Weightless ASYM vocabulary (#38049)

Eugene Smirnove17b9014a9b8
diff --git a/src/plugins/intel_npu/src/plugin/CMakeLists.txt b/src/plugins/intel_npu/src/plugin/CMakeLists.txt
index 1ca850a1b0e..224abea19db 100644
--- a/src/plugins/intel_npu/src/plugin/CMakeLists.txt
+++ b/src/plugins/intel_npu/src/plugin/CMakeLists.txt
@@ -57,7 +57,7 @@ cross_compiled_file(${TARGET_NAME}
ARCH AVX2 ANY
npuw/util_xarch.cpp
API npuw/util_xarch.hpp
- NAME unpack_i4i8 unpack_u4i8 unpack_i4f16 unpack_i4f16_scale unpack_i4f16_z unpack_u4f16 unpack_u4f16_scale_zp unpack_u4f16_asymm_zp unpack_u4f16_z unpack_u4f32 unpack_i8f16 unpack_i8f16_scale unpack_u8f16 to_f16 copy_row_as_column transpose_i4 transpose_f16 transpose_f32 unpack_f8f16_scale
+ NAME unpack_i4i8 unpack_u4i8 unpack_i4f16 unpack_i4f16_scale unpack_i4f16_z unpack_u4f16 unpack_u4f16_scale_zp unpack_u4f16_asymm_zp unpack_u4f16_z unpack_u4f32 unpack_i8f16 unpack_i8f16_scale unpack_i8f16_zp unpack_u8f16 to_f16 copy_row_as_column transpose_i4 transpose_f16 transpose_f32 unpack_f8f16_scale subtract_128
NAMESPACE ov::npuw::util::XARCH
)
diff --git a/src/plugins/intel_npu/src/plugin/npuw/base_sync_infer_request.cpp b/src/plugins/intel_npu/src/plugin/npuw/base_sync_infer_request.cpp
index 5bacf2144bf..b9fd0548eba 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/base_sync_infer_request.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/base_sync_infer_request.cpp
@@ -564,10 +564,11 @@ void ov::npuw::IBaseInferRequest::handle_quant_host_gather(std::size_t idx, RqPt
const auto& lport = comp_model_desc.compiled_model->inputs()[quant_unpack_gather.idx_idx];
const auto& lookup = request->get_tensor(lport);
+ const auto src_w_idx = static_cast<std::size_t>(quant_unpack_gather.src_w_idx);
const auto& gport = comp_model_desc.compiled_model->inputs()[quant_unpack_gather.dst_idx];
const auto& gather = request->get_tensor(gport);
- const auto& wport = comp_model_desc.compiled_model->inputs()[quant_unpack_gather.src_w_idx];
+ const auto& wport = comp_model_desc.compiled_model->inputs()[src_w_idx];
const auto& vocabw = request->get_tensor(wport);
// Gather weight
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 a93e4d0934d..d3b8a11fdee 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp
@@ -1835,7 +1835,7 @@ void ov::npuw::CompiledModel::set_weights_bank(std::shared_ptr<ov::npuw::weights
void ov::npuw::CompiledModel::finalize_weights_bank() {
LOG_INFO("Finalizing weights bank...");
- std::shared_future<void> weights_bank_evaluation = std::async(std::launch::async, [&]() {
+ auto finalize_weights = [&]() {
// Register lazy tensors
for (std::size_t idx = 0; idx < m_compiled_submodels.size(); ++idx) {
auto& comp_model_desc = m_compiled_submodels[idx];
@@ -1891,9 +1891,9 @@ void ov::npuw::CompiledModel::finalize_weights_bank() {
}
m_import_weights_ctx.reset();
- });
+ };
- m_eval_future = weights_bank_evaluation;
+ m_eval_future = std::async(std::launch::async, finalize_weights);
for (size_t idx = 0; idx < m_compiled_submodels.size(); ++idx) {
auto& comp_model_desc = m_compiled_submodels[idx];
@@ -1903,7 +1903,7 @@ void ov::npuw::CompiledModel::finalize_weights_bank() {
continue;
}
- comp_model_desc.closure.set_future(weights_bank_evaluation);
+ comp_model_desc.closure.set_future(m_eval_future);
}
LOG_INFO("Done.");
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 973e0a0da7b..aaaea322b31 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.cpp
@@ -325,6 +325,37 @@ void Convert::detach() {
tensor.detach();
}
+std::size_t Subtract128::hash() const {
+ return tensor.get_hash() ^ (ov::element::i8.hash() + 0x9e3779b9);
+}
+
+bool Subtract128::operator==(const Subtract128& other) const {
+ return tensor == other.tensor;
+}
+
+ov::Tensor Subtract128::eval() const {
+ const auto source = tensor.eval();
+ OPENVINO_ASSERT(source.get_element_type() == ov::element::u8, "Subtract128 requires u8 input");
+ ov::Tensor result(ov::element::i8, source.get_shape());
+ const auto& get_tensor_impl = ov::get_tensor_impl;
+ ov::npuw::util::subtract_128(get_tensor_impl(source), get_tensor_impl(result));
+ return result;
+}
+
+LazyTensor::Meta Subtract128::eval_meta() const {
+ const auto meta = tensor.eval_meta();
+ OPENVINO_ASSERT(meta.type == ov::element::u8, "Subtract128 requires u8 input");
+ return {meta.shape, ov::element::i8};
+}
+
+void Subtract128::read_weight(const ov::npuw::s11n::WeightsContext& ctx) {
+ tensor.read_weight(ctx);
+}
+
+void Subtract128::detach() {
+ tensor.detach();
+}
+
std::size_t Gather::hash() const {
std::size_t seed = w.get_hash() + 0x9e3779b9;
seed ^= t.get_element_type().hash() + 0x9e3779b9;
@@ -396,6 +427,7 @@ enum class TransformType : std::uint16_t {
PERMUTE = 4,
CONVERT = 5,
GATHER = 6,
+ SUBTRACT_128 = 7,
};
struct LazyTensorImpl {
@@ -458,6 +490,10 @@ ov::npuw::weights::TransformType get_transform_type(const ov::npuw::weights::op:
return ov::npuw::weights::TransformType::GATHER;
}
+ov::npuw::weights::TransformType get_transform_type(const ov::npuw::weights::op::Subtract128&) {
+ return ov::npuw::weights::TransformType::SUBTRACT_128;
+}
+
} // namespace
namespace ov {
@@ -517,6 +553,10 @@ void Convert::serialize(ov::npuw::orc::Stream& stream) {
}
}
+void Subtract128::serialize(ov::npuw::orc::Stream& stream) {
+ stream & tensor;
+}
+
void Gather::serialize(ov::npuw::orc::Stream& stream) {
std::string type_str;
if (stream.output()) {
@@ -565,6 +605,9 @@ void LazyTensorImpl::serialize(ov::npuw::orc::Stream& stream) {
case TransformType::GATHER:
m_transform.emplace<op::Gather>(ov::npuw::orc::load_versioned_payload<op::Gather>(section));
break;
+ case TransformType::SUBTRACT_128:
+ m_transform.emplace<op::Subtract128>(ov::npuw::orc::load_versioned_payload<op::Subtract128>(section));
+ break;
default:
OPENVINO_THROW("ORC LazyTensor: unknown op_type ", section.type, " — please upgrade NPUW");
break;
@@ -661,6 +704,10 @@ void LazyTensorImpl::get_transformations(std::vector<LazyTensor::Transform>& vec
auto next_tr = op.tensor.get_transformations();
vec.insert(vec.end(), next_tr.begin(), next_tr.end());
},
+ [&vec](const op::Subtract128& 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());
@@ -716,6 +763,12 @@ LazyTensor LazyTensor::convert(const ov::element::Type& type) {
return new_lt;
}
+LazyTensor LazyTensor::subtract_128() {
+ LazyTensor new_lt;
+ new_lt.m_impl = std::make_shared<LazyTensorImpl>(op::Subtract128(*this));
+ return new_lt;
+}
+
bool LazyTensor::operator==(const LazyTensor& other) const {
if (!m_impl && !other.m_impl) {
return true;
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 d1054a71964..64ef5d67fa1 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.hpp
@@ -27,6 +27,7 @@ class Unpack;
class Permute;
class Convert;
class Gather;
+class Subtract128;
} // namespace op
class LazyTensor {
@@ -41,7 +42,8 @@ public:
ov::npuw::weights::op::Unpack,
ov::npuw::weights::op::Permute,
ov::npuw::weights::op::Convert,
- ov::npuw::weights::op::Gather>;
+ ov::npuw::weights::op::Gather,
+ ov::npuw::weights::op::Subtract128>;
LazyTensor() = default;
LazyTensor(const std::shared_ptr<ov::op::v0::Constant>& const_ptr);
@@ -58,6 +60,7 @@ public:
LazyTensor permute(const std::vector<std::size_t>& axes);
LazyTensor convert(const ov::element::Type& type);
+ LazyTensor subtract_128();
bool operator==(const LazyTensor& other) const;
bool operator!=(const LazyTensor& other) const;
@@ -217,6 +220,27 @@ private:
ov::element::Type type;
};
+class Subtract128 {
+ friend struct ov::npuw::weights::LazyTensorImpl;
+
+public:
+ static constexpr std::uint16_t kVersion = 0u;
+
+ Subtract128() = default;
+ explicit Subtract128(const LazyTensor& source) : tensor(source) {}
+
+ std::size_t hash() const;
+ bool operator==(const Subtract128& other) const;
+ ov::Tensor eval() const;
+ LazyTensor::Meta eval_meta() const;
+ void read_weight(const ov::npuw::s11n::WeightsContext& ctx);
+ void detach();
+ void serialize(ov::npuw::orc::Stream& stream);
+
+private:
+ LazyTensor tensor;
+};
+
class Gather {
friend struct ov::npuw::weights::LazyTensorImpl;
diff --git a/src/plugins/intel_npu/src/plugin/npuw/llm_infer_request.cpp b/src/plugins/intel_npu/src/plugin/npuw/llm_infer_request.cpp
index a357aa2a85c..570f646c92f 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/llm_infer_request.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/llm_infer_request.cpp
@@ -259,6 +259,7 @@ void process_longrope_tables(const std::shared_ptr<ov::IAsyncInferRequest>& infe
infer_req->set_tensor(cos_port_it->second, ov::get_tensor_impl(tables.cos_rows(lut_len, is_long)));
infer_req->set_tensor(sin_port_it->second, ov::get_tensor_impl(tables.sin_rows(lut_len, is_long)));
}
+
} // anonymous namespace
void ov::npuw::LLMInferRequest::init_lora_states() {
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 f88ba628781..33d9f57b98a 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.cpp
@@ -2190,6 +2190,7 @@ void Partitioner::optimize(const std::string& func_name) {
// Run Head/Tail passes
ov::pass::GraphRewrite rewr;
if (cfg.get<::intel_npu::NPUW_HOST_GATHER>() && !part_ctx.use_host_gather_quant) {
+ rewr.add_matcher<ov::npuw::patterns::opt::ConvertDQVocab>(std::ref(ctx));
rewr.add_matcher<ov::npuw::patterns::opt::DQUnpackDictGatheru>(std::ref(ctx));
rewr.add_matcher<ov::npuw::patterns::opt::DQUnpackDictGatherGQi>(std::ref(ctx));
rewr.add_matcher<ov::npuw::patterns::opt::DQUnpackDictMatMulCWu>(std::ref(ctx));
@@ -2322,6 +2323,27 @@ void Partitioner::optimize(const std::string& func_name) {
// Convert parameters to f16 where required
do_cvtf16(ctx);
+ // Add lazy Sub128 parameters while retaining the original sources when they are still shared.
+ for (const auto& shifted_and_source : ctx.closures_to_subtract_128) {
+ const auto& shifted_param = shifted_and_source.first;
+ const auto& source_param = shifted_and_source.second;
+ const auto source_idx = f._model->get_parameter_index(source_param);
+ NPUW_ASSERT(source_idx >= static_cast<int64_t>(f._param_offset));
+
+ new_params.push_back(shifted_param);
+ const auto source_closure_idx = source_idx - f._param_offset;
+ ov::npuw::util::non_parallel_for(func_group.refs.size(), [&](std::size_t f_idx) {
+ auto& funcall = func_group.refs[f_idx].get();
+ funcall._lazy_closure.push_back(funcall._lazy_closure[source_closure_idx].subtract_128());
+ funcall._closure.emplace_back();
+ funcall._is_lazy_unpack.push_back(false);
+ });
+
+ if (source_param->output(0).get_target_inputs().empty() && to_remove_idx.insert(source_idx).second) {
+ to_remove.push_back(source_param);
+ }
+ }
+
// Host-side gather, pt 1. Add new parameters first
if (ctx.params_to_gather) {
auto& params_to_gather = *ctx.params_to_gather;
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 ea61ce79fbd..955bb4cc1be 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
@@ -47,6 +47,13 @@ void Context::to_f16(const PPtr& orig_param) {
orig_param->validate_and_infer_types();
}
+Context::PPtr Context::subtract_128(const PPtr& orig_param) {
+ auto shifted_param = std::make_shared<ov::op::v0::Parameter>(ov::element::i8, orig_param->get_partial_shape());
+ shifted_param->set_friendly_name(orig_param->get_friendly_name() + "_sub128");
+ closures_to_subtract_128.emplace(shifted_param, orig_param);
+ return shifted_param;
+}
+
void Context::register_parallel_matmul(const O& multiply, std::size_t axis, DQParMM&& mm) {
par_dq_mms[std::make_pair(multiply, axis)].push_back(std::move(mm));
}
@@ -1089,8 +1096,8 @@ DQLiftGatherAsymCW::DQLiftGatherAsymCW() {
auto qcoeff = opp::wrap_type<ov::op::v0::Constant>();
auto qcvtw = opp::wrap_type<ov::op::v0::Convert>({qweight});
auto qcvtz = opp::wrap_type<ov::op::v0::Convert>({qzerop});
- auto qshiftw = opp::optional<ov::op::v1::Subtract>({qcvtw->output(0), opp::any_input()});
- auto qshiftz = opp::optional<ov::op::v1::Subtract>({qcvtz->output(0), opp::any_input()});
+ auto qshiftw = opp::optional<ov::op::v1::Subtract>({qcvtw->output(0), opp::any_input()}, is_subtract_128);
+ auto qshiftz = opp::optional<ov::op::v1::Subtract>({qcvtz->output(0), opp::any_input()}, is_subtract_128);
auto qsubz = opp::wrap_type<ov::op::v1::Subtract>({qshiftw, qshiftz});
auto qmuls = opp::wrap_type<ov::op::v1::Multiply>({qsubz, qcoeff});
auto qcvtm = opp::wrap_type<ov::op::v0::Convert>({qmuls});
@@ -1110,14 +1117,11 @@ DQLiftGatherAsymCW::DQLiftGatherAsymCW() {
auto matched_out_ids = uat::_(node_to_output).at_or_at(cvtids, pids);
const auto& matched_out_gather = node_to_output.at(gather);
- if (node_to_output.count(qshiftw) != node_to_output.count(qshiftz)) {
- return false;
- }
- if (node_to_output.count(qshiftw) && (!is_subtract_128(node_to_output.at(qshiftw).get_node_shared_ptr()) ||
- !is_subtract_128(node_to_output.at(qshiftz).get_node_shared_ptr()))) {
+ const bool has_weight_shift = node_to_output.count(qshiftw) != 0;
+ const bool has_zeropoint_shift = node_to_output.count(qshiftz) != 0;
+ if (has_weight_shift != has_zeropoint_shift) {
return false;
}
-
// Replicate the compute part
auto gather_c = std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{}, 0);
auto new_g_w = std::make_shared<ov::op::v8::Gather>(matched_out_w, matched_out_ids, gather_c);
@@ -1126,7 +1130,20 @@ DQLiftGatherAsymCW::DQLiftGatherAsymCW() {
auto new_cvt_w = std::make_shared<ov::op::v0::Convert>(new_g_w, ov::element::f16);
auto new_cvt_z = std::make_shared<ov::op::v0::Convert>(new_g_z, ov::element::f16);
- auto new_sub = std::make_shared<ov::op::v1::Subtract>(new_cvt_w, new_cvt_z);
+ std::shared_ptr<ov::Node> dequantized_w = new_cvt_w;
+ std::shared_ptr<ov::Node> dequantized_z = new_cvt_z;
+ if (has_weight_shift && has_zeropoint_shift) {
+ auto shift = ov::op::v0::Constant::create(ov::element::f16, ov::Shape{}, {128.0f});
+ auto shifted_w = std::make_shared<ov::op::v1::Subtract>(new_cvt_w, shift);
+ auto shifted_z = std::make_shared<ov::op::v1::Subtract>(new_cvt_z, shift);
+ ov::copy_runtime_info(node_to_output.at(qshiftw).get_node_shared_ptr(), shifted_w);
+ ov::copy_runtime_info(node_to_output.at(qshiftz).get_node_shared_ptr(), shifted_z);
+ shifted_w->get_rt_info()[ov::npuw::NPUW_SUB128_SHIFT_RT_INFO] = true;
+ shifted_z->get_rt_info()[ov::npuw::NPUW_SUB128_SHIFT_RT_INFO] = true;
+ dequantized_w = shifted_w;
+ dequantized_z = shifted_z;
+ }
+ auto new_sub = std::make_shared<ov::op::v1::Subtract>(dequantized_w, dequantized_z);
auto new_mul = std::make_shared<ov::op::v1::Multiply>(new_sub, new_g_s);
auto new_out = std::make_shared<ov::op::v0::Convert>(new_mul, ov::element::f32);
@@ -1284,6 +1301,40 @@ DQLiftGatherSymGQ::DQLiftGatherSymGQ() {
register_matcher(std::make_shared<opp::Matcher>(gather, "DQGatherSymGQ"), std::move(callback));
}
+ConvertDQVocab::ConvertDQVocab(Context::Ref ctx) {
+ const auto weight = opp::wrap_type<ov::op::v0::Parameter>(opp::type_matches(ov::element::u8));
+ const auto zerop = opp::wrap_type<ov::op::v0::Parameter>(opp::type_matches(ov::element::u8));
+ const auto weight_convert = opp::wrap_type<ov::op::v0::Convert>({weight});
+ const auto zerop_convert = opp::wrap_type<ov::op::v0::Convert>({zerop});
+ const auto weight_shift = opp::wrap_type<ov::op::v1::Subtract>({weight_convert, opp::any_input()}, is_subtract_128);
+ const auto zerop_shift = opp::wrap_type<ov::op::v1::Subtract>({zerop_convert, opp::any_input()}, is_subtract_128);
+ const auto dequantized = opp::wrap_type<ov::op::v1::Subtract>({weight_shift, zerop_shift});
+
+ auto callback = [=](ov::pass::pattern::Matcher& matcher) {
+ const auto& values = matcher.get_pattern_value_map();
+ for (const auto& convert : {weight_convert, zerop_convert}) {
+ const auto type = values.at(convert).get_element_type();
+ if (type != ov::element::f16 && type != ov::element::f32) {
+ return false;
+ }
+ }
+
+ for (const auto& shift : {weight_shift, zerop_shift}) {
+ const auto matched_shift = values.at(shift).get_node_shared_ptr();
+ const auto convert = matched_shift->input_value(0).get_node_shared_ptr();
+ const auto source = ov::as_type_ptr<ov::op::v0::Parameter>(convert->input_value(0).get_node_shared_ptr());
+ OPENVINO_ASSERT(source, "ConvertDQVocab source must be a parameter");
+ auto shifted = ctx.get().subtract_128(source);
+ // after this we have: i8 derived Parameter -> Convert -> Subtract(128) -> outer Subtract
+ convert->input(0).replace_source_output(shifted);
+ // after this we have: i8 derived Parameter -> Convert -> outer Subtract
+ ov::replace_node(matched_shift, convert);
+ }
+ return true;
+ };
+ register_matcher(std::make_shared<opp::Matcher>(dequantized, "ConvertDQVocab"), std::move(callback));
+}
+
// This is a companion to DQLiftGatherAsymCW step. This pass runs if
// the respective block (mainly, a head) was turned a function
// (e.g. with FUNCALL_FOR_ALL) As in this case the DQUnpackDictMatMulCWu
@@ -1400,8 +1451,8 @@ HostGatherQuantAsymm<WType>::HostGatherQuantAsymm(Context::Ref ctx, bool verify_
auto qcvtw = opp::wrap_type<ov::op::v0::Convert>({qgthrw});
auto qcvtz = opp::wrap_type<ov::op::v0::Convert>({qgthrz});
- auto qshiftw = opp::optional<ov::op::v1::Subtract>({qcvtw->output(0), opp::any_input()});
- auto qshiftz = opp::optional<ov::op::v1::Subtract>({qcvtz->output(0), opp::any_input()});
+ auto qshiftw = opp::optional<ov::op::v1::Subtract>({qcvtw->output(0), opp::any_input()}, is_subtract_128);
+ auto qshiftz = opp::optional<ov::op::v1::Subtract>({qcvtz->output(0), opp::any_input()}, is_subtract_128);
auto qsubz = opp::wrap_type<ov::op::v1::Subtract>({qshiftw, qshiftz});
auto qmuls = opp::wrap_type<ov::op::v1::Multiply>({qsubz, qgthrs});
auto qcvtm = opp::wrap_type<ov::op::v0::Convert>({qmuls});
@@ -1412,17 +1463,11 @@ HostGatherQuantAsymm<WType>::HostGatherQuantAsymm(Context::Ref ctx, bool verify_
const auto& matched_out_mul = node_to_output.at(qmuls);
auto out_shape = matched_out_mul.get_shape();
- if (node_to_output.count(qshiftw) != node_to_output.count(qshiftz)) {
+ const bool has_weight_shift = node_to_output.count(qshiftw) != 0;
+ const bool has_zeropoint_shift = node_to_output.count(qshiftz) != 0;
+ if (has_weight_shift != has_zeropoint_shift) {
return false;
}
- // The paired shifts mark u8 tensors adapted for an i8 DQ graph. The current
- // host path replaces that graph with direct u8 dequantization, where the
- // equal shifts cancel: (W - 128) - (Z - 128) == W - Z.
- if (node_to_output.count(qshiftw) && (!is_subtract_128(node_to_output.at(qshiftw).get_node_shared_ptr()) ||
- !is_subtract_128(node_to_output.at(qshiftz).get_node_shared_ptr()))) {
- return false;
- }
-
if (out_shape.size() != 3 && out_shape.size() != 4) {
return false;
}
@@ -1456,11 +1501,24 @@ HostGatherQuantAsymm<WType>::HostGatherQuantAsymm(Context::Ref ctx, bool verify_
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);
+ const bool use_sub128 = has_weight_shift && has_zeropoint_shift;
+ const bool weight_is_sub128 = use_sub128 && matched_qweight->get_element_type() == ov::element::u8;
+ const bool zerop_is_sub128 = use_sub128 && matched_qzerop->get_element_type() == ov::element::u8;
+ if (weight_is_sub128 != zerop_is_sub128) {
+ return false;
+ }
+ auto host_weight = matched_qweight;
+ auto host_zerop = matched_qzerop;
+ if (weight_is_sub128) {
+ host_weight = ctx.get().subtract_128(matched_qweight);
+ host_zerop = ctx.get().subtract_128(matched_qzerop);
+ }
+
// Strip down the DQ subgraph, replace the original Q-ed closure tensor with future- unpacked and gathered
// fp16
auto new_wi = ctx.get().host_gather_unpack_quant(matched_ids,
- matched_qweight,
- matched_qzerop,
+ host_weight,
+ host_zerop,
matched_qcoeff,
ov::element::f16);
matched_node_cvt->input(0).replace_source_output(new_wi);
@@ -1966,9 +2024,7 @@ PreserveConstDictMatMulAsymm::PreserveConstDictMatMulAsymm(Context::Ref ctx,
auto qzerop = opp::wrap_type<ov::op::v0::Constant>();
auto qcvtw = opp::wrap_type<ov::op::v0::Convert>({qweight});
auto qcvtz = opp::wrap_type<ov::op::v0::Convert>({qzerop});
- auto qshiftw = opp::optional<ov::op::v1::Subtract>({qcvtw->output(0), opp::any_input()});
- auto qshiftz = opp::optional<ov::op::v1::Subtract>({qcvtz->output(0), opp::any_input()});
- auto qsub = opp::wrap_type<ov::op::v1::Subtract>({qshiftw, qshiftz});
+ auto qsub = opp::wrap_type<ov::op::v1::Subtract>({qcvtw, qcvtz});
auto qmuls = opp::wrap_type<ov::op::v1::Multiply>({qsub, qcoeff});
// The Convert between Multiply and MatMul is optional (some models omit it when Multiply is already f32)
auto qcvtm = opp::optional<ov::op::v0::Convert>({qmuls});
@@ -2006,14 +2062,6 @@ PreserveConstDictMatMulAsymm::PreserveConstDictMatMulAsymm(Context::Ref ctx,
auto qcoeff_shape = matched_qcoeff->output(0).get_shape();
- if (node_to_output.count(qshiftw) != node_to_output.count(qshiftz)) {
- return false;
- }
- if (node_to_output.count(qshiftw) && (!is_subtract_128(node_to_output.at(qshiftw).get_node_shared_ptr()) ||
- !is_subtract_128(node_to_output.at(qshiftz).get_node_shared_ptr()))) {
- return false;
- }
-
// Standard layout: weight [OC, IC], scale [OC, 1], transpose_b=true
const bool standard_layout = qcoeff_shape.size() == 2 && qcoeff_shape[1] == 1 &&
!matched_matmul->get_transpose_a() && matched_matmul->get_transpose_b();
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 c9463f249cb..163d62900d6 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
@@ -36,6 +36,9 @@ struct Context {
std::set<PPtr> closures_to_f16;
void to_f16(const PPtr& orig_param);
+ std::map<PPtr, PPtr> closures_to_subtract_128;
+ PPtr subtract_128(const PPtr& orig_param);
+
using O = ov::Output<ov::Node>;
struct DQParMM {
PPtr w, s;
@@ -198,6 +201,12 @@ public:
// Tail vocab unpacks
+class ConvertDQVocab : public ov::pass::MatcherPass {
+public:
+ OPENVINO_MATCHER_PASS_RTTI("npuw::patterns::opt::ConvertDQVocab");
+ explicit ConvertDQVocab(Context::Ref ctx);
+};
+
class DQUnpackDictMatMulCWu : public ov::pass::MatcherPass {
public:
OPENVINO_MATCHER_PASS_RTTI("npuw::patterns::opt::DQUnpackDictMatMulCWu");
@@ -222,7 +231,6 @@ public:
CompressDictMatMulf32(Context::Ref ctx);
};
-// Tail vocab transformations
class PreserveConstDictMatMulAsymm : public ov::pass::MatcherPass {
public:
OPENVINO_MATCHER_PASS_RTTI("npuw::patterns::opt::PreserveConstDictMatMulAsymm");
diff --git a/src/plugins/intel_npu/src/plugin/npuw/util.cpp b/src/plugins/intel_npu/src/plugin/npuw/util.cpp
index 0dae485d7f5..9ac8da0e646 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/util.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/util.cpp
@@ -298,9 +298,13 @@ void ov::npuw::util::unpack(const ov::SoPtr<ov::ITensor>& from,
NPUW_ASSERT(type_scale == ov::element::f16 || type_scale == ov::element::f32);
NPUW_ASSERT(type_to == ov::element::f16);
} else if (type_from == ov::element::u8) {
- NPUW_ASSERT(type_zerop == ov::element::u8);
+ NPUW_ASSERT(type_zerop == type_from);
NPUW_ASSERT(type_scale == ov::element::f16);
NPUW_ASSERT(type_to == ov::element::f16);
+ } else if (type_from == ov::element::i8) {
+ NPUW_ASSERT(type_zerop == type_from);
+ NPUW_ASSERT(type_scale == ov::element::f16 || type_scale == ov::element::f32);
+ NPUW_ASSERT(type_to == ov::element::f16);
} else {
NPUW_ASSERT(false && "Unsupported combination");
}
@@ -344,7 +348,7 @@ void ov::npuw::util::unpack(const ov::SoPtr<ov::ITensor>& from,
} else {
NPUW_ASSERT(false);
}
- } else if (type_from == ov::element::u8) {
+ } else if (type_from == ov::element::u8 || type_from == ov::element::i8) {
if (scale_shape.size() == 3 && scale_shape[1] == 1 && scale_shape[2] == 1) {
// Special case for broadcasting vocab by 2 dimensions
// FIXME: all this logic probably should be in some specific unpack or another util function
@@ -364,11 +368,19 @@ void ov::npuw::util::unpack(const ov::SoPtr<ov::ITensor>& from,
scale->data(),
ov::Strides{scale_strides[0], scale_strides[2]});
- ov::npuw::util::XARCH::unpack_u8f16(ov::get_tensor_impl(wraped_from),
- ov::get_tensor_impl(wraped_zerop),
- ov::get_tensor_impl(wraped_scale),
- to,
- unpack_options);
+ if (type_from == ov::element::u8) {
+ ov::npuw::util::XARCH::unpack_u8f16(ov::get_tensor_impl(wraped_from),
+ ov::get_tensor_impl(wraped_zerop),
+ ov::get_tensor_impl(wraped_scale),
+ to,
+ unpack_options);
+ } else {
+ ov::npuw::util::XARCH::unpack_i8f16_zp(ov::get_tensor_impl(wraped_from),
+ ov::get_tensor_impl(wraped_zerop),
+ ov::get_tensor_impl(wraped_scale),
+ to,
+ unpack_options);
+ }
} else if (scale_shape.size() == 3 && scale_shape[0] == 1 && scale_shape[2] == 1) {
// Special case for broadcasting vocab by 2 dimensions
// FIXME: all this logic probably should be in some specific unpack or another util function
@@ -380,19 +392,35 @@ void ov::npuw::util::unpack(const ov::SoPtr<ov::ITensor>& from,
ov::Shape{scale_shape[1], scale_shape[2]},
scale->data());
- ov::npuw::util::XARCH::unpack_u8f16(ov::get_tensor_impl(wraped_from),
- ov::get_tensor_impl(wraped_zerop),
- ov::get_tensor_impl(wraped_scale),
- to,
- unpack_options);
+ if (type_from == ov::element::u8) {
+ ov::npuw::util::XARCH::unpack_u8f16(ov::get_tensor_impl(wraped_from),
+ ov::get_tensor_impl(wraped_zerop),
+ ov::get_tensor_impl(wraped_scale),
+ to,
+ unpack_options);
+ } else {
+ ov::npuw::util::XARCH::unpack_i8f16_zp(ov::get_tensor_impl(wraped_from),
+ ov::get_tensor_impl(wraped_zerop),
+ ov::get_tensor_impl(wraped_scale),
+ to,
+ unpack_options);
+ }
} else if (scale_shape.size() == 2 && scale_shape[0] == from_shape[0] && scale_shape[1] == 1) {
- ov::npuw::util::XARCH::unpack_u8f16(from, zerop, scale, to, unpack_options);
+ if (type_from == ov::element::u8) {
+ ov::npuw::util::XARCH::unpack_u8f16(from, zerop, scale, to, unpack_options);
+ } else {
+ ov::npuw::util::XARCH::unpack_i8f16_zp(from, zerop, scale, to, unpack_options);
+ }
} else {
NPUW_ASSERT(false);
}
}
}
+void ov::npuw::util::subtract_128(const ov::SoPtr<ov::ITensor>& from, const ov::SoPtr<ov::ITensor>& to) {
+ ov::npuw::util::XARCH::subtract_128(from, to);
+}
+
void ov::npuw::util::gather(const ov::SoPtr<ov::ITensor>& src,
const ov::SoPtr<ov::ITensor>& idx,
const ov::SoPtr<ov::ITensor>& dst) {
diff --git a/src/plugins/intel_npu/src/plugin/npuw/util.hpp b/src/plugins/intel_npu/src/plugin/npuw/util.hpp
index e75950bca08..3647e4d6c92 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/util.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/util.hpp
@@ -95,6 +95,8 @@ void unpack(const ov::SoPtr<ov::ITensor>& from,
const ov::SoPtr<ov::ITensor>& to,
const UnpackOptions& unpack_options = UnpackOptions{true, 16, false});
+void subtract_128(const ov::SoPtr<ov::ITensor>& from, const ov::SoPtr<ov::ITensor>& to);
+
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,
diff --git a/src/plugins/intel_npu/src/plugin/npuw/util_xarch.cpp b/src/plugins/intel_npu/src/plugin/npuw/util_xarch.cpp
index f1c85ad7db8..40450055758 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/util_xarch.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/util_xarch.cpp
@@ -204,6 +204,13 @@ inline __m128i avx2_i8tof16(__m128i vi8, __m256 s) {
return _mm256_cvtps_ph(f32scl, _MM_FROUND_TO_NEAREST_INT); // convert: 8 x f32 -> 8 x f16 [128b]
}
+inline __m128i avx2_i8f16_zp(__m128i vi8, __m128i vzp, __m256 s) {
+ __m256i i32vec = _mm256_cvtepi8_epi32(vi8);
+ __m256i i32zp = _mm256_cvtepi8_epi32(vzp);
+ __m256 f32vec = _mm256_cvtepi32_ps(_mm256_sub_epi32(i32vec, i32zp));
+ return _mm256_cvtps_ph(_mm256_mul_ps(f32vec, s), _MM_FROUND_TO_NEAREST_INT);
+}
+
inline __m128i avx2_u8tof16_hi(__m128i vu8, __m256 z, __m256 s) {
__m256i u32vec = _mm256_cvtepu8_epi32(vu8); // extend: 8 x u8 -> 8 x i32 [256b of 256b]
__m256 f32vec = _mm256_cvtepi32_ps(u32vec); // convert: 8 x i32 -> 8 x f32 [256b of 256b]
@@ -463,6 +470,36 @@ void ov::npuw::util::XARCH::unpack_u4i8(const ov::SoPtr<ov::ITensor>& from,
}
}
+void ov::npuw::util::XARCH::subtract_128(const ov::SoPtr<ov::ITensor>& from, const ov::SoPtr<ov::ITensor>& to) {
+ NPUW_ASSERT(from->is_continuous());
+ NPUW_ASSERT(to->is_continuous());
+ NPUW_ASSERT(from->get_size() == to->get_size());
+
+ const auto total = from->get_size();
+ const auto* source = static_cast<const uint8_t*>(from->data());
+ auto* result = static_cast<int8_t*>(to->data());
+
+#if defined(HAVE_AVX2)
+ constexpr std::size_t vector_size = 32;
+ const auto vector_count = total / vector_size;
+ const auto mask = _mm256_set1_epi8(static_cast<char>(0x80));
+
+ ov::parallel_for(vector_count, [source, result, mask](std::size_t index) {
+ const auto values = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(source + index * 32));
+ const auto shifted = _mm256_xor_si256(values, mask);
+ _mm256_storeu_si256(reinterpret_cast<__m256i*>(result + index * 32), shifted);
+ });
+
+ for (std::size_t index = vector_count * vector_size; index < total; ++index) {
+ result[index] = static_cast<int8_t>(source[index] ^ 0x80u);
+ }
+#else
+ for (std::size_t index = 0; index < total; ++index) {
+ result[index] = static_cast<int8_t>(source[index] ^ 0x80u);
+ }
+#endif
+}
+
void ov::npuw::util::XARCH::unpack_i4f16(const ov::SoPtr<ov::ITensor>& from,
const ov::SoPtr<ov::ITensor>& to,
const ov::npuw::util::UnpackOptions& unpack_options) {
@@ -1742,6 +1779,99 @@ void ov::npuw::util::XARCH::transpose_f32(const float* src, float* dst, size_t r
#endif
}
+void ov::npuw::util::XARCH::unpack_i8f16_zp(const ov::SoPtr<ov::ITensor>& from,
+ const ov::SoPtr<ov::ITensor>& zerop,
+ const ov::SoPtr<ov::ITensor>& scale,
+ const ov::SoPtr<ov::ITensor>& to,
+ const ov::npuw::util::UnpackOptions& unpack_options) {
+ NPUW_ASSERT(from->is_continuous());
+ NPUW_ASSERT(zerop->is_continuous());
+ NPUW_ASSERT(scale->is_continuous());
+ NPUW_ASSERT(to->is_continuous());
+ NPUW_ASSERT(zerop->get_shape().size() == 2);
+ NPUW_ASSERT(zerop->get_shape()[0] == from->get_shape()[0]);
+ NPUW_ASSERT(zerop->get_shape()[1] == 1 || zerop->get_shape()[1] == from->get_shape()[1]);
+ NPUW_ASSERT(from->get_size() == to->get_size());
+ NPUW_ASSERT(from->get_shape().size() == 2);
+ NPUW_ASSERT(scale->get_shape().size() == 2);
+ NPUW_ASSERT(scale->get_shape()[0] == from->get_shape()[0]);
+ NPUW_ASSERT(scale->get_shape()[1] == 1);
+ NPUW_ASSERT(scale->get_element_type() == ov::element::f16 || scale->get_element_type() == ov::element::f32);
+
+ const auto rows = from->get_shape()[0];
+ const auto columns = from->get_shape()[1];
+ const bool zerop_broadcast = zerop->get_shape()[1] == 1;
+ const auto* source = from->data<const int8_t>();
+ const auto* zerop_data = zerop->data<const int8_t>();
+ auto* result = to->data<ov::float16>();
+
+#if defined(HAVE_AVX2)
+ constexpr std::size_t vector_size = 8;
+ const auto scale_element_type = scale->get_element_type();
+ const auto* scale_data = static_cast<const int8_t*>(scale->data());
+
+ auto unpack_row = [=](std::size_t row) {
+ const auto row_offset = row * columns;
+ const float scale_value = avx2_load_f32(scale_data + row * scale_element_type.size(), scale_element_type);
+ if (columns < vector_size) {
+ for (std::size_t column = 0; column < columns; ++column) {
+ const auto index = row_offset + column;
+ result[index] = ov::float16((source[index] - zerop_data[index]) * scale_value);
+ }
+ return;
+ }
+
+ const auto scale_vector = avx2_load_scale(scale_data + row * scale_element_type.size(), scale_element_type);
+
+ std::size_t column = 0;
+ for (; column + vector_size <= columns; column += vector_size) {
+ const auto input_vector = _mm_loadl_epi64(reinterpret_cast<const __m128i*>(source + row_offset + column));
+ const auto zerop_vector =
+ zerop_broadcast ? _mm_set1_epi8(zerop_data[row])
+ : _mm_loadl_epi64(reinterpret_cast<const __m128i*>(zerop_data + row_offset + column));
+ const auto output_vector = avx2_i8f16_zp(input_vector, zerop_vector, scale_vector);
+ _mm_storeu_si128(reinterpret_cast<__m128i*>(result + row_offset + column), output_vector);
+ }
+
+ for (; column < columns; ++column) {
+ const auto index = row_offset + column;
+ const auto zerop_value = zerop_broadcast ? zerop_data[row] : zerop_data[index];
+ result[index] = ov::float16((source[index] - zerop_value) * scale_value);
+ }
+ };
+
+ if (unpack_options.bUseOvParallelFor) {
+ ov::parallel_for(rows, unpack_row);
+ } else {
+ for (std::size_t row = 0; row < rows; ++row) {
+ unpack_row(row);
+ }
+ }
+#else
+ if (scale->get_element_type() == ov::element::f16) {
+ const auto* scale_data = scale->data<const ov::float16>();
+ ov::parallel_for(rows, [=](std::size_t row) {
+ const float scale_value = static_cast<float>(scale_data[row]);
+ for (std::size_t column = 0; column < columns; ++column) {
+ const auto index = row * columns + column;
+ const auto zerop_value = zerop_broadcast ? zerop_data[row] : zerop_data[index];
+ result[index] = ov::float16((source[index] - zerop_value) * scale_value);
+ }
+ });
+ } else {
+ const auto* scale_data = scale->data<const float>();
+ ov::parallel_for(rows, [=](std::size_t row) {
+ const float scale_value = scale_data[row];
+ for (std::size_t column = 0; column < columns; ++column) {
+ const auto index = row * columns + column;
+ const auto zerop_value = zerop_broadcast ? zerop_data[row] : zerop_data[index];
+ result[index] = ov::float16((source[index] - zerop_value) * scale_value);
+ }
+ });
+ }
+#endif
+}
+
void ov::npuw::util::XARCH::unpack_f8f16_scale(const ov::SoPtr<ov::ITensor>& from,
const ov::SoPtr<ov::ITensor>& scale,
const ov::SoPtr<ov::ITensor>& to,
diff --git a/src/plugins/intel_npu/src/plugin/npuw/util_xarch.hpp b/src/plugins/intel_npu/src/plugin/npuw/util_xarch.hpp
index ab65a1c9bc5..c1c8d057d92 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/util_xarch.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/util_xarch.hpp
@@ -80,6 +80,14 @@ void unpack_u8f16(const ov::SoPtr<ov::ITensor>& from,
const ov::SoPtr<ov::ITensor>& to,
const ov::npuw::util::UnpackOptions& _options);
+void unpack_i8f16_zp(const ov::SoPtr<ov::ITensor>& from,
+ const ov::SoPtr<ov::ITensor>& zerop,
+ const ov::SoPtr<ov::ITensor>& scale,
+ const ov::SoPtr<ov::ITensor>& to,
+ const ov::npuw::util::UnpackOptions& _options);
+
+void subtract_128(const ov::SoPtr<ov::ITensor>& from, const ov::SoPtr<ov::ITensor>& to);
+
ov::Tensor to_f16(const ov::Tensor& t);
void copy_row_as_column(const ov::SoPtr<ov::ITensor>& from, const ov::SoPtr<ov::ITensor>& to);
diff --git a/src/plugins/intel_npu/src/plugin/sources.cmake b/src/plugins/intel_npu/src/plugin/sources.cmake
index 118c8e2f78c..7526bf18577 100644
--- a/src/plugins/intel_npu/src/plugin/sources.cmake
+++ b/src/plugins/intel_npu/src/plugin/sources.cmake
@@ -140,7 +140,7 @@ set(NPUW_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/npuw/npuw_transformations/optimize_value_tensors.cpp
${CMAKE_CURRENT_SOURCE_DIR}/npuw/npuw_transformations/optimize_value_tensors.hpp
${CMAKE_CURRENT_SOURCE_DIR}/npuw/npuw_transformations/shrink_sliding_window_kv_cache.cpp
- ${CMAKE_CURRENT_SOURCE_DIR}/npuw/npuw_transformations/shrink_sliding_window_kv_cache.hpp
+ ${CMAKE_CURRENT_SOURCE_DIR}/npuw/npuw_transformations/shrink_sliding_window_kv_cache.hpp
${CMAKE_CURRENT_SOURCE_DIR}/npuw/npuw_transformations/patch_sliding_window_mask.cpp
${CMAKE_CURRENT_SOURCE_DIR}/npuw/npuw_transformations/patch_sliding_window_mask.hpp
${CMAKE_CURRENT_SOURCE_DIR}/npuw/npuw_transformations/remove_token_type_ids.cpp
diff --git a/src/plugins/intel_npu/tests/unit/npuw/gather_sub128_matcher_test.cpp b/src/plugins/intel_npu/tests/unit/npuw/gather_sub128_matcher_test.cpp
index 39c8791bf62..1e141a62752 100644
--- a/src/plugins/intel_npu/tests/unit/npuw/gather_sub128_matcher_test.cpp
+++ b/src/plugins/intel_npu/tests/unit/npuw/gather_sub128_matcher_test.cpp
@@ -4,7 +4,9 @@
#include <gtest/gtest.h>
+#include <array>
#include <cstdint>
+#include <map>
#include <memory>
#include <algorithm>
#include <optional>
@@ -12,11 +14,15 @@
#include <tuple>
#include <vector>
+#include "lazy_tensor.hpp"
+#include "intel_npu/config/npuw.hpp"
#include "npuw_transformations/insert_vocab_sub128.hpp"
+#include "openvino/core/graph_util.hpp"
#include "openvino/core/preprocess/pre_post_process.hpp"
#include "openvino/opsets/opset10.hpp"
#include "openvino/pass/graph_rewrite.hpp"
#include "partitioning/patterns/opt.hpp"
+#include "partitioning/partitioning.hpp"
#include "transformations/rt_info/decompression.hpp"
namespace {
@@ -63,12 +69,15 @@ bool run_lift(const std::shared_ptr<ov::Model>& model) {
}
std::shared_ptr<ov::Model> make_parameter_gather_model(std::optional<float> weight_shift,
- std::optional<float> zero_point_shift) {
+ std::optional<float> zero_point_shift,
+ ov::element::Type weight_type = ov::element::u8,
+ ov::element::Type zero_point_type = ov::element::u8) {
constexpr std::size_t vocab_size = 4096;
constexpr std::size_t hidden_size = 2048;
auto ids = std::make_shared<ov::opset10::Parameter>(ov::element::i32, ov::Shape{1, 1});
- auto weights = std::make_shared<ov::opset10::Parameter>(ov::element::u8, ov::Shape{vocab_size, hidden_size});
- auto zero_point = std::make_shared<ov::opset10::Parameter>(ov::element::u8, ov::Shape{vocab_size, hidden_size});
+ auto weights = std::make_shared<ov::opset10::Parameter>(weight_type, ov::Shape{vocab_size, hidden_size});
+ auto zero_point =
+ std::make_shared<ov::opset10::Parameter>(zero_point_type, ov::Shape{vocab_size, hidden_size});
auto scale = std::make_shared<ov::opset10::Parameter>(ov::element::f16, ov::Shape{vocab_size, hidden_size});
auto axis = ov::opset10::Constant::create(ov::element::i32, ov::Shape{}, {0});
@@ -105,6 +114,25 @@ std::shared_ptr<ov::Model> make_parameter_gather_model(float shift_value) {
return make_parameter_gather_model(shift_value, shift_value);
}
+std::shared_ptr<ov::Model> make_parameter_vocab_dq_model() {
+ auto weights = std::make_shared<ov::opset10::Parameter>(ov::element::u8, ov::Shape{4, 2});
+ auto zero_point = std::make_shared<ov::opset10::Parameter>(ov::element::u8, ov::Shape{4, 1});
+ auto scale = std::make_shared<ov::opset10::Parameter>(ov::element::f16, ov::Shape{4, 1});
+
+ auto weight_convert = std::make_shared<ov::opset10::Convert>(weights, ov::element::f16);
+ auto zero_point_convert = std::make_shared<ov::opset10::Convert>(zero_point, ov::element::f16);
+ auto shift = ov::opset10::Constant::create(ov::element::f16, ov::Shape{}, {128.0f});
+ auto shifted_weight = std::make_shared<ov::opset10::Subtract>(weight_convert, shift);
+ auto shifted_zero_point = std::make_shared<ov::opset10::Subtract>(zero_point_convert, shift);
+ shifted_weight->get_rt_info()[ov::npuw::NPUW_SUB128_SHIFT_RT_INFO] = true;
+ shifted_zero_point->get_rt_info()[ov::npuw::NPUW_SUB128_SHIFT_RT_INFO] = true;
+
+ auto dequantized = std::make_shared<ov::opset10::Subtract>(shifted_weight, shifted_zero_point);
+ auto scaled = std::make_shared<ov::opset10::Multiply>(dequantized, scale);
+ auto result = std::make_shared<ov::opset10::Result>(scaled);
+ return std::make_shared<ov::Model>(ov::ResultVector{result}, ov::ParameterVector{weights, zero_point, scale});
+}
+
bool run_host_gather(const std::shared_ptr<ov::Model>& model, ov::npuw::patterns::opt::Context& context) {
ov::pass::GraphRewrite rewrite;
rewrite.add_matcher<ov::npuw::patterns::opt::HostGatherQuantAsymm<>>(std::ref(context));
@@ -114,13 +142,14 @@ bool run_host_gather(const std::shared_ptr<ov::Model>& model, ov::npuw::patterns
enum class VocabMatMulTerminal { Add, Transpose, Convert, Gated };
std::shared_ptr<ov::Model> make_vocab_matmul_model(bool convert_before_matmul,
- std::optional<VocabMatMulTerminal> terminal = std::nullopt) {
+ std::optional<VocabMatMulTerminal> terminal = std::nullopt,
+ std::size_t vocab_size = 4) {
auto hidden = std::make_shared<ov::opset10::Parameter>(ov::element::f32, ov::Shape{1, 2});
- auto weights = ov::opset10::Constant::create(ov::element::u8, ov::Shape{4, 2}, std::vector<uint8_t>(8, 200));
+ auto weights = ov::opset10::Constant::create(ov::element::u8, ov::Shape{vocab_size, 2}, std::vector<uint8_t>(vocab_size * 2, 200));
auto zero_point =
- ov::opset10::Constant::create(ov::element::u8, ov::Shape{4, 1}, std::vector<uint8_t>(4, 128));
+ ov::opset10::Constant::create(ov::element::u8, ov::Shape{vocab_size, 1}, std::vector<uint8_t>(vocab_size, 128));
const auto scale_type = convert_before_matmul ? ov::element::f16 : ov::element::f32;
- auto scale = ov::opset10::Constant::create(scale_type, ov::Shape{4, 1}, std::vector<float>(4, 1.0f));
+ auto scale = ov::opset10::Constant::create(scale_type, ov::Shape{vocab_size, 1}, std::vector<float>(vocab_size, 1.0f));
auto weight_convert = std::make_shared<ov::opset10::Convert>(weights, scale_type);
weight_convert->set_friendly_name("vocab_weight_convert");
auto zero_point_convert = std::make_shared<ov::opset10::Convert>(zero_point, scale_type);
@@ -134,7 +163,7 @@ std::shared_ptr<ov::Model> make_vocab_matmul_model(bool convert_before_matmul,
auto matmul = std::make_shared<ov::opset10::MatMul>(hidden, matmul_weights, false, true);
ov::Output<ov::Node> output = matmul;
if (terminal == VocabMatMulTerminal::Add) {
- auto bias = ov::opset10::Constant::create(ov::element::f32, ov::Shape{1, 4}, std::vector<float>(4, 1.0f));
+ auto bias = ov::opset10::Constant::create(ov::element::f32, ov::Shape{1, vocab_size}, std::vector<float>(vocab_size, 1.0f));
output = std::make_shared<ov::opset10::Add>(matmul, bias);
} else if (terminal == VocabMatMulTerminal::Transpose) {
auto order = ov::opset10::Constant::create(ov::element::i32, ov::Shape{2}, {1, 0});
@@ -151,13 +180,13 @@ std::shared_ptr<ov::Model> make_vocab_matmul_model(bool convert_before_matmul,
return std::make_shared<ov::Model>(ov::OutputVector{output}, ov::ParameterVector{hidden});
}
-std::shared_ptr<ov::Model> make_pretransposed_vocab_matmul_model(bool convert_before_matmul) {
+std::shared_ptr<ov::Model> make_pretransposed_vocab_matmul_model(bool convert_before_matmul, std::size_t vocab_size = 4) {
auto hidden = std::make_shared<ov::opset10::Parameter>(ov::element::f32, ov::Shape{1, 2});
- auto weights = ov::opset10::Constant::create(ov::element::u8, ov::Shape{2, 4}, std::vector<uint8_t>(8, 200));
+ auto weights = ov::opset10::Constant::create(ov::element::u8, ov::Shape{2, vocab_size}, std::vector<uint8_t>(vocab_size * 2, 200));
auto zero_point =
- ov::opset10::Constant::create(ov::element::u8, ov::Shape{1, 4}, std::vector<uint8_t>(4, 128));
+ ov::opset10::Constant::create(ov::element::u8, ov::Shape{1, vocab_size}, std::vector<uint8_t>(vocab_size, 128));
const auto scale_type = convert_before_matmul ? ov::element::f16 : ov::element::f32;
- auto scale = ov::opset10::Constant::create(scale_type, ov::Shape{1, 4}, std::vector<float>(4, 1.0f));
+ auto scale = ov::opset10::Constant::create(scale_type, ov::Shape{1, vocab_size}, std::vector<float>(vocab_size, 1.0f));
auto weight_convert = std::make_shared<ov::opset10::Convert>(weights, scale_type);
weight_convert->set_friendly_name("vocab_weight_convert");
auto zero_point_convert = std::make_shared<ov::opset10::Convert>(zero_point, scale_type);
@@ -239,6 +268,57 @@ TEST(HostGatherQuantAsymmTest, AcceptsPairedSub128Shifts) {
ASSERT_EQ(context.params_to_quant_gather_unpack->params_to_runtime_unpack_gather.size(), 1);
}
+TEST(HostGatherQuantAsymmTest, MarksPairedSub128Sources) {
+ ov::npuw::patterns::opt::Context context;
+ const auto model = make_parameter_gather_model(128.0f);
+
+ EXPECT_TRUE(run_host_gather(model, context));
+ ASSERT_TRUE(context.params_to_quant_gather_unpack.has_value());
+ const auto& gather = context.params_to_quant_gather_unpack->params_to_runtime_unpack_gather.begin()->second;
+ ASSERT_EQ(gather.w->get_element_type(), ov::element::i8);
+ ASSERT_EQ(gather.z->get_element_type(), ov::element::i8);
+ ASSERT_EQ(context.closures_to_subtract_128.size(), 2);
+ for (const auto& shifted_and_source : context.closures_to_subtract_128) {
+ EXPECT_EQ(shifted_and_source.first->get_element_type(), ov::element::i8);
+ EXPECT_TRUE(shifted_and_source.second == model->get_parameters().at(1) ||
+ shifted_and_source.second == model->get_parameters().at(2));
+ }
+ // Simulating partitioning behavior
+ for (const auto& shifted_and_source : context.closures_to_subtract_128) {
+ model->add_parameters({shifted_and_source.first});
+ if (shifted_and_source.second->output(0).get_target_inputs().empty()) {
+ model->remove_parameter(shifted_and_source.second);
+ }
+ }
+
+ std::size_t i8_parameter_count = 0;
+ std::size_t u8_parameter_count = 0;
+ for (const auto& parameter : model->get_parameters()) {
+ if (parameter->get_element_type() == ov::element::i8) {
+ ++i8_parameter_count;
+ } else if (parameter->get_element_type() == ov::element::u8) {
+ ++u8_parameter_count;
+ }
+ }
+ EXPECT_EQ(i8_parameter_count, 2u);
+ EXPECT_EQ(u8_parameter_count, 0u);
+}
+
+TEST(HostGatherQuantAsymmTest, KeepsRawSourcesWithoutMarkedPattern) {
+ ov::npuw::patterns::opt::Context context;
+ const auto model = make_parameter_gather_model(std::nullopt,
+ std::nullopt,
+ ov::element::u8,
+ ov::element::u8);
+
+ EXPECT_TRUE(run_host_gather(model, context));
+ ASSERT_TRUE(context.params_to_quant_gather_unpack.has_value());
+ const auto& gather = context.params_to_quant_gather_unpack->params_to_runtime_unpack_gather.begin()->second;
+ ASSERT_EQ(gather.w->get_element_type(), ov::element::u8);
+ ASSERT_EQ(gather.z->get_element_type(), ov::element::u8);
+ EXPECT_TRUE(context.closures_to_subtract_128.empty());
+}
+
TEST(HostGatherQuantAsymmTest, RejectsNon128Subtractions) {
ov::npuw::patterns::opt::Context context;
EXPECT_FALSE(run_host_gather(make_parameter_gather_model(127.0f), context));
@@ -254,6 +334,34 @@ TEST(HostGatherQuantAsymmTest, RejectsUnmarked128Subtractions) {
EXPECT_EQ(count_subtracts(model), 3);
}
+TEST(ConvertDQVocabTest, ReplacesMarkedShiftsWithLazySources) {
+ ov::npuw::patterns::opt::Context context;
+ const auto model = make_parameter_vocab_dq_model();
+
+ ov::pass::GraphRewrite rewrite;
+ rewrite.add_matcher<ov::npuw::patterns::opt::ConvertDQVocab>(std::ref(context));
+
+ EXPECT_TRUE(rewrite.run_on_model(model));
+ ASSERT_EQ(context.closures_to_subtract_128.size(), 2);
+ for (const auto& shifted_and_source : context.closures_to_subtract_128) {
+ EXPECT_EQ(shifted_and_source.first->get_element_type(), ov::element::i8);
+ EXPECT_TRUE(shifted_and_source.second == model->get_parameters().at(0) ||
+ shifted_and_source.second == model->get_parameters().at(1));
+ EXPECT_EQ(model->get_parameter_index(shifted_and_source.first), -1);
+ }
+
+ std::size_t reachable_i8_parameter_count = 0;
+ for (const auto& node : model->get_ordered_ops()) {
+ const auto parameter = ov::as_type_ptr<ov::op::v0::Parameter>(node);
+ if (parameter && parameter->get_element_type() == ov::element::i8) {
+ ++reachable_i8_parameter_count;
+ }
+ }
+ EXPECT_EQ(reachable_i8_parameter_count, 2u);
+
+ EXPECT_EQ(count_sub128_shifts(model), 0u);
+}
+
using VocabSub128TestParams = std::tuple<bool, bool>;
class InsertVocabSub128PrePostProcessingTest : public ::testing::TestWithParam<VocabSub128TestParams> {
@@ -342,3 +450,49 @@ TEST(InsertVocabSub128LmHeadTerminalTest, SkipsManuallyAddedOutput) {
EXPECT_EQ(count_sub128_shifts(model), 0u);
}
+TEST(LazySubtract128Test, CoversEveryU8ValueWithoutChangingSource) {
+ std::vector<uint8_t> values(256);
+ for (std::size_t index = 0; index < values.size(); ++index) {
+ values[index] = static_cast<uint8_t>(index);
+ }
+ const auto constant = ov::opset10::Constant::create(ov::element::u8, ov::Shape{16, 16}, values);
+ ov::npuw::weights::LazyTensor source(constant);
+ const auto shifted = source.subtract_128();
+ EXPECT_EQ(shifted.eval_meta().shape, constant->get_shape());
+ EXPECT_EQ(shifted.eval_meta().type, ov::element::i8);
+ EXPECT_EQ(shifted, source.subtract_128());
+ EXPECT_NE(shifted, source);
+ const auto result = shifted.eval();
+ for (std::size_t index = 0; index < values.size(); ++index) {
+ EXPECT_EQ(result.data<const int8_t>()[index], static_cast<int>(index) - 128);
+ }
+ EXPECT_EQ(constant->cast_vector<uint8_t>(), values);
+}
+
+TEST(LazySubtract128Test, HandlesVectorAndTailSizes) {
+ const std::array<std::size_t, 8> sizes{1, 31, 32, 33, 63, 64, 65, 257};
+ for (const auto size : sizes) {
+ std::vector<uint8_t> values(size);
+ for (std::size_t index = 0; index < size; ++index) {
+ values[index] = static_cast<uint8_t>(index);
+ }
+
+ const auto constant = ov::opset10::Constant::create(ov::element::u8, ov::Shape{size}, values);
+ const auto result = ov::npuw::weights::LazyTensor(constant).subtract_128().eval();
+
+ ASSERT_EQ(result.get_shape(), (ov::Shape{size}));
+ ASSERT_EQ(result.get_element_type(), ov::element::i8);
+ for (std::size_t index = 0; index < size; ++index) {
+ EXPECT_EQ(result.data<const int8_t>()[index], static_cast<int>(values[index]) - 128);
+ }
+ }
+}
+
+TEST(LazySubtract128Test, RejectsNonU8Input) {
+ const auto constant = ov::opset10::Constant::create(ov::element::i8, ov::Shape{1}, {0});
+ const auto shifted = ov::npuw::weights::LazyTensor(constant).subtract_128();
+ EXPECT_THROW(shifted.eval(), ov::Exception);
+ EXPECT_THROW(shifted.eval_meta(), ov::Exception);
+}
+
+
diff --git a/src/plugins/intel_npu/tests/unit/npuw/preserve_const_matmul_pattern_test.cpp b/src/plugins/intel_npu/tests/unit/npuw/preserve_const_matmul_pattern_test.cpp
index 80138f2d4d5..8156d622dfe 100644
--- a/src/plugins/intel_npu/tests/unit/npuw/preserve_const_matmul_pattern_test.cpp
+++ b/src/plugins/intel_npu/tests/unit/npuw/preserve_const_matmul_pattern_test.cpp
@@ -216,7 +216,7 @@ TEST(OptPatterns, StandardLayout_NoConvert_MatchesAndPreservesConsts) {
EXPECT_TRUE(std::find(to_keep.begin(), to_keep.end(), qcoeff) != to_keep.end());
}
-TEST(OptPatterns, PairedSub128_MatchesAndPreservesConsts) {
+TEST(OptPatterns, PairedSub128_DoesNotPreserveConsts) {
const ov::Shape weight_shape{32064, 3072};
const ov::Shape scale_shape{32064, 1};
auto [qweight, qzerop, qcoeff, model] = build_asymm_matmul_subgraph(weight_shape,
@@ -231,10 +231,7 @@ TEST(OptPatterns, PairedSub128_MatchesAndPreservesConsts) {
ResultNodes to_keep;
run_preserve_pattern(model, to_keep);
- ASSERT_EQ(to_keep.size(), 3u);
- EXPECT_TRUE(std::find(to_keep.begin(), to_keep.end(), qweight) != to_keep.end());
- EXPECT_TRUE(std::find(to_keep.begin(), to_keep.end(), qzerop) != to_keep.end());
- EXPECT_TRUE(std::find(to_keep.begin(), to_keep.end(), qcoeff) != to_keep.end());
+ EXPECT_TRUE(to_keep.empty());
}
TEST(OptPatterns, Non128Subtraction_DoesNotPreserveConsts) {
diff --git a/src/plugins/intel_npu/tests/unit/npuw/serialization.cpp b/src/plugins/intel_npu/tests/unit/npuw/serialization.cpp
index 295d9f07ee6..de1f3a6e066 100644
--- a/src/plugins/intel_npu/tests/unit/npuw/serialization.cpp
+++ b/src/plugins/intel_npu/tests/unit/npuw/serialization.cpp
@@ -1557,6 +1557,42 @@ TEST(SerializationTest, OVTypes_LazyTensor_concat_permute_convert_roundtrip) {
EXPECT_EQ(var.eval_meta().type, res.eval_meta().type);
}
+TEST(SerializationTest, OVTypes_LazyTensor_subtract128_roundtrip) {
+ using namespace ov::npuw::s11n;
+
+ auto constant = make_weightless_constant<uint8_t>(ov::element::u8, ov::Shape{2, 3},
+ {0, 1, 127, 128, 254, 255}, 0);
+ auto var = ov::npuw::weights::LazyTensor(constant).subtract_128();
+ ov::npuw::weights::LazyTensor res;
+ const auto expected = var.eval();
+
+ std::stringstream ss;
+ write(ss, var);
+ var.detach();
+ read(ss, res);
+ res.read_weight(WeightsContext(nullptr, "", make_consts_cache({constant}), {}));
+
+ expect_lazy_tensor_transform_types_equal(var, res);
+ EXPECT_EQ(var.get_hash(), res.get_hash());
+ EXPECT_EQ(res.eval_meta().shape, (ov::Shape{2, 3}));
+ EXPECT_EQ(res.eval_meta().type, ov::element::i8);
+ expect_tensors_equal(expected, res.eval());
+}
+
+TEST(SerializationTest, OVTypes_LazyTensor_subtract128_embedded_roundtrip) {
+ using namespace ov::npuw::s11n;
+
+ auto constant = ov::op::v0::Constant::create(ov::element::u8, ov::Shape{4}, {0, 127, 128, 255});
+ auto var = ov::npuw::weights::LazyTensor(constant).subtract_128();
+ const auto expected = var.eval();
+ ov::npuw::weights::LazyTensor res;
+ std::stringstream ss;
+ write(ss, var);
+ read(ss, res);
+
+ expect_tensors_equal(expected, res.eval());
+}
+
TEST(SerializationTest, OVTypes_LazyTensor_unpack_roundtrip) {
using namespace ov::npuw::s11n;
diff --git a/src/plugins/intel_npu/tests/unit/npuw/unpack.cpp b/src/plugins/intel_npu/tests/unit/npuw/unpack.cpp
index 29a028893fc..7178214e03b 100644
--- a/src/plugins/intel_npu/tests/unit/npuw/unpack.cpp
+++ b/src/plugins/intel_npu/tests/unit/npuw/unpack.cpp
@@ -186,6 +186,18 @@ INSTANTIATE_TEST_SUITE_P(UnpackTestsWithScaleAndZeroPointTest3, UnpackTestsWithS
TestCasesScaleAndZeroPoints3,
UnpackTestsWithScaleAndZeroPointTest3::getTestCaseName);
+const auto TestCasesI8Asymmetric = ::testing::Combine(
+ ::testing::ValuesIn({ov::Shape{2, 4}, ov::Shape{2, 8}, ov::Shape{3, 9}, ov::Shape{4, 16}}),
+ ::testing::ValuesIn({ov::element::Type_t::f16, ov::element::Type_t::f32}),
+ ::testing::ValuesIn({0lu, 2lu, 3lu}),
+ ::testing::ValuesIn({true, false}),
+ ::testing::ValuesIn({true, false}));
+
+INSTANTIATE_TEST_SUITE_P(I8AsymmetricUnpackTests,
+ I8AsymmetricUnpackTests,
+ TestCasesI8Asymmetric,
+ I8AsymmetricUnpackTests::getTestCaseName);
+
} // anonymous namespace
#endif // __AVX2__
diff --git a/src/plugins/intel_npu/tests/unit/npuw/unpack.hpp b/src/plugins/intel_npu/tests/unit/npuw/unpack.hpp
index 226c4cbfa35..dcabdb70870 100644
--- a/src/plugins/intel_npu/tests/unit/npuw/unpack.hpp
+++ b/src/plugins/intel_npu/tests/unit/npuw/unpack.hpp
@@ -736,6 +736,69 @@ TEST_P(UnpackTestsWithScaleAndZeroPointTest3, u4) {
}
}
+using I8AsymmetricUnpackParams = std::tuple<ov::Shape, ov::element::Type_t, unsigned long, bool, bool>;
+
+class I8AsymmetricUnpackTests : public ::testing::TestWithParam<I8AsymmetricUnpackParams> {
+public:
+ static std::string getTestCaseName(const ::testing::TestParamInfo<I8AsymmetricUnpackParams>& info) {
+ const auto& [shape, scale_type, n_partitions, use_parallel_for, strict_partitions] = info.param;
+ std::ostringstream result;
+ result << shape[0] << "x" << shape[1] << "_scale_" << scale_type << "_p" << n_partitions
+ << (strict_partitions ? "_SP" : "") << (use_parallel_for ? "_parallel" : "_serial");
+ return result.str();
+ }
+};
+
+TEST_P(I8AsymmetricUnpackTests, i8_asymmetric) {
+ const auto& [shape, scale_type, n_partitions, use_parallel_for, strict_partitions] = GetParam();
+ ov::element::Type scale_element_type = scale_type;
+ const auto rows = shape[0];
+ const auto columns = shape[1];
+ const auto element_count = shape_size(shape);
+
+ std::vector<int8_t> weights(element_count);
+ std::vector<int8_t> zero_points(element_count);
+ for (std::size_t index = 0; index < element_count; ++index) {
+ weights[index] = static_cast<int8_t>(index * 13 - 128);
+ zero_points[index] = static_cast<int8_t>(index * 7 - 128);
+ }
+
+ const std::vector<float> scale_values{1.0f, 0.5f, 2.0f, 0.25f};
+ std::vector<int8_t> scale_storage(scale_element_type.bitwidth() * rows / 8);
+ if (scale_element_type == ov::element::f16) {
+ auto* scale_data = reinterpret_cast<uint16_t*>(scale_storage.data());
+ for (std::size_t row = 0; row < rows; ++row) {
+ scale_data[row] = details::float_to_half(scale_values[row % scale_values.size()]);
+ }
+ } else {
+ auto* scale_data = reinterpret_cast<float*>(scale_storage.data());
+ for (std::size_t row = 0; row < rows; ++row) {
+ scale_data[row] = scale_values[row % scale_values.size()];
+ }
+ }
+
+ auto from = ov::make_tensor(ov::element::i8, shape, weights.data());
+ auto zerop = ov::make_tensor(ov::element::i8, shape, zero_points.data());
+ auto scale = ov::make_tensor(scale_element_type, ov::Shape{rows, 1}, scale_storage.data());
+ auto to = ov::make_tensor(ov::element::f16, shape);
+
+ ov::npuw::util::unpack(from,
+ zerop,
+ scale,
+ to,
+ ov::npuw::util::UnpackOptions{use_parallel_for,
+ static_cast<std::size_t>(n_partitions),
+ strict_partitions});
+
+ const auto* output = to->data<const ov::float16>();
+ for (std::size_t index = 0; index < element_count; ++index) {
+ const auto row = index / columns;
+ const auto expected = (static_cast<float>(weights[index]) - static_cast<float>(zero_points[index])) *
+ scale_values[row % scale_values.size()];
+ EXPECT_FLOAT_EQ(static_cast<float>(output[index]), expected);
+ }
+}
+
#define Tensors [](std::vector<int>& input, std::vector<int>&scale, std::vector<int>&zerop)