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
Support u2 group-quantized weights with zero point in DQMatMulGQ2i (#39443)
Wenzeng Chen0dbc82e49d77
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.cppindex c26a0dc255c..3a082397b9b 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@@ -26,6 +26,32 @@ bool is_subtract_128(const std::shared_ptr<ov::Node>& node) {return subtract != nullptr && subtract->get_rt_info().count(ov::npuw::NPUW_SUB128_SHIFT_RT_INFO) > 0;}+std::shared_ptr<ov::op::v0::Parameter> find_matched_param(const ov::pass::pattern::PatternValueMap& matched,+ const std::shared_ptr<ov::Node>& pattern) {+ const auto it = matched.find(pattern);+ return it == matched.end() ? nullptr+ : std::static_pointer_cast<ov::op::v0::Parameter>(it->second.get_node_shared_ptr());+}++// Group-wise weights are i4 without a zero point, or u2 with a zero point of the same type and the scale+// shape. The split-by-groups (full) mode has no zero point support.+bool is_group_quantized_weight(const std::shared_ptr<ov::op::v0::Parameter>& weight,+ const std::shared_ptr<ov::op::v0::Parameter>& zerop,+ const ov::Shape& coeff_shape,+ const bool mm_dq_full) {+ if (!zerop) {+ return weight->get_element_type() == ov::element::i4;+ }+ return !mm_dq_full && weight->get_element_type() == ov::element::u2 &&+ zerop->get_element_type() == ov::element::u2 && zerop->get_shape() == coeff_shape;+}++// Only the split-by-groups (full) mode reshapes the activation and requires rank 3. The permute-only mode+// does not depend on the activation, so the rank-4 layout of GGUF frontend models is accepted there.+bool is_supported_act_rank(const ov::Shape& act_shape, const bool mm_dq_full) {+ return act_shape.size() == 3 || (act_shape.size() == 4 && !mm_dq_full);+}+} // namespaceContext::Context(std::shared_ptr<const ov::Model> model, std::size_t param_base_idx)@@ -541,9 +567,12 @@ DQMatMulGQi::DQMatMulGQi(Context::Ref ctx) {DQMatMulGQ2i::DQMatMulGQ2i(Context::Ref ctx) {auto qweight = opp::wrap_type<ov::op::v0::Parameter>();+ auto qzerop = opp::wrap_type<ov::op::v0::Parameter>();auto qcoeff = opp::wrap_type<ov::op::v0::Parameter>();auto qcvtw = opp::wrap_type<ov::op::v0::Convert>({qweight});- auto qmuls = opp::wrap_type<ov::op::v1::Multiply>({qcvtw, qcoeff});+ auto qcvtz = opp::wrap_type<ov::op::v0::Convert>({qzerop});+ auto qsubt = opp::optional<ov::op::v1::Subtract>({qcvtw->output(0), qcvtz->output(0)});+ auto qmuls = opp::wrap_type<ov::op::v1::Multiply>({qsubt, qcoeff});auto qcvtm = opp::optional<ov::op::v0::Convert>({qmuls->output(0)});auto qreshp = opp::wrap_type<ov::op::v1::Reshape>({qcvtm, opp::any_input()});auto qcvtr = opp::optional<ov::op::v0::Convert>({qreshp->output(0)});@@ -574,15 +603,20 @@ DQMatMulGQ2i::DQMatMulGQ2i(Context::Ref ctx) {auto act_shape = matched_out_mmi.get_shape();auto out_shape = matched_node_matmul->output(0).get_shape();- if (ov::element::i4 == matched_qweight->get_element_type() && qweight_shape.size() == 3 &&- ov::element::f16 == matched_qcoeff->get_element_type() && qcoeff_shape.size() == 3 &&- act_shape.size() == 3 && act_shape[0] == 1 && act_shape[1] == 1 && qcoeff_shape[0] == qweight_shape[0] &&- qcoeff_shape[2] == 1 && qcoeff_shape[1] == qweight_shape[1] && !matched_matmul->get_transpose_a() &&+ if (const auto matched_qzerop = find_matched_param(node_to_output, qzerop);+ is_group_quantized_weight(matched_qweight, matched_qzerop, qcoeff_shape, ctx.get().mm_dq_full) &&+ qweight_shape.size() == 3 && ov::element::f16 == matched_qcoeff->get_element_type() &&+ qcoeff_shape.size() == 3 && is_supported_act_rank(act_shape, ctx.get().mm_dq_full) && act_shape[0] == 1 &&+ act_shape[1] == 1 && qcoeff_shape[0] == qweight_shape[0] && qcoeff_shape[2] == 1 &&+ qcoeff_shape[1] == qweight_shape[1] && !matched_matmul->get_transpose_a() &&matched_matmul->get_transpose_b()) {if (!ctx.get().mm_dq_full) {// Transpose weight and coeffctx.get().permute(matched_qweight, {1, 0, 2});ctx.get().permute(matched_qcoeff, {1, 0, 2});+ if (matched_qzerop) {+ ctx.get().permute(matched_qzerop, {1, 0, 2});+ }// Add Transpose and insert itstd::vector<std::size_t> new_transpose_order = {1, 0, 2};@@ -826,9 +860,12 @@ DQMatMulGQiP::DQMatMulGQiP(Context::Ref ctx) {// S* : [32, 1,11008]DQMatMulGQ2iP::DQMatMulGQ2iP(Context::Ref ctx) {auto qweight = opp::wrap_type<ov::op::v0::Parameter>();+ auto qzerop = opp::wrap_type<ov::op::v0::Parameter>();auto qcoeff = opp::wrap_type<ov::op::v0::Parameter>();auto qcvtw = opp::wrap_type<ov::op::v0::Convert>({qweight});- auto qmuls = opp::wrap_type<ov::op::v1::Multiply>({qcvtw, qcoeff});+ auto qcvtz = opp::wrap_type<ov::op::v0::Convert>({qzerop});+ auto qsubt = opp::optional<ov::op::v1::Subtract>({qcvtw->output(0), qcvtz->output(0)});+ auto qmuls = opp::wrap_type<ov::op::v1::Multiply>({qsubt, qcoeff});auto qcvtm = opp::optional<ov::op::v0::Convert>({qmuls->output(0)});auto qreshp = opp::wrap_type<ov::op::v1::Reshape>({qcvtm, opp::any_input()});auto qcvtr = opp::optional<ov::op::v0::Convert>({qreshp->output(0)});@@ -862,15 +899,20 @@ DQMatMulGQ2iP::DQMatMulGQ2iP(Context::Ref ctx) {return (a == 1 && b > 1) || (a > 1 && b == 1);};- if (ov::element::i4 == matched_qweight->get_element_type() && qweight_shape.size() == 3 &&- ov::element::f16 == matched_qcoeff->get_element_type() && qcoeff_shape.size() == 3 &&- act_shape.size() == 3 && just_one(act_shape[0], act_shape[1]) && // multi-token case+ if (const auto matched_qzerop = find_matched_param(node_to_output, qzerop);+ is_group_quantized_weight(matched_qweight, matched_qzerop, qcoeff_shape, ctx.get().mm_dq_full) &&+ qweight_shape.size() == 3 && ov::element::f16 == matched_qcoeff->get_element_type() &&+ qcoeff_shape.size() == 3 && act_shape.size() == 3 &&+ just_one(act_shape[0], act_shape[1]) && // multi-token caseqcoeff_shape[0] == qweight_shape[0] && qcoeff_shape[1] == qweight_shape[1] && qcoeff_shape[2] == 1 &&!matched_matmul->get_transpose_a() && matched_matmul->get_transpose_b()) {if (!ctx.get().mm_dq_full) {// Transpose weight and coeffctx.get().permute(matched_qweight, {1, 0, 2});ctx.get().permute(matched_qcoeff, {1, 0, 2});+ if (matched_qzerop) {+ ctx.get().permute(matched_qzerop, {1, 0, 2});+ }// Add Transpose and insert itstd::vector<std::size_t> new_transpose_order = {1, 0, 2};diff --git a/src/plugins/intel_npu/src/plugin/npuw/util.cpp b/src/plugins/intel_npu/src/plugin/npuw/util.cppindex d7f227941d2..2c418dfe9bd 100644--- a/src/plugins/intel_npu/src/plugin/npuw/util.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/util.cpp@@ -911,10 +911,36 @@ ov::Tensor ov::npuw::util::permute(const ov::Tensor& t, const std::vector<std::s}return tnew;} else if (axes[0] == 1 && axes[1] == 0 && axes[2] == 2) {- NPUW_ASSERT(t.get_element_type() == ov::element::i4 || t.get_element_type() == ov::element::f16);+ NPUW_ASSERT(t.get_element_type() == ov::element::i4 || t.get_element_type() == ov::element::u2 ||+ t.get_element_type() == ov::element::f16);ov::Shape tshape = {shape[1], shape[0], shape[2]};ov::Tensor tnew(t.get_element_type(), tshape);switch (t.get_element_type()) {+ case ov::element::u2: {+ const uint8_t* src = static_cast<const uint8_t*>(t.data());+ uint8_t* dst = static_cast<uint8_t*>(tnew.data());+ if (shape[2] % 4 == 0) {+ ov::parallel_for2d(shape[0], shape[1], [&](size_t p, size_t r) {+ std::copy_n(&src[(p * shape[1] + r) * shape[2] / 4],+ shape[2] / 4,+ &dst[(r * shape[0] + p) * shape[2] / 4]);+ });+ } else {+ // Rows are not byte-aligned, so move the elements one by one (LSB-first packing)+ std::fill_n(dst, tnew.get_byte_size(), uint8_t{0});+ for (size_t p = 0; p < shape[0]; ++p) {+ for (size_t r = 0; r < shape[1]; ++r) {+ for (size_t k = 0; k < shape[2]; ++k) {+ const size_t src_idx = (p * shape[1] + r) * shape[2] + k;+ const size_t dst_idx = (r * shape[0] + p) * shape[2] + k;+ const uint8_t value = (src[src_idx / 4] >> (2 * (src_idx % 4))) & 0x3;+ dst[dst_idx / 4] |= static_cast<uint8_t>(value << (2 * (dst_idx % 4)));+ }+ }+ }+ }+ break;+ }case ov::element::i4: {NPUW_ASSERT(shape[2] % 2 == 0);const uint8_t* src = static_cast<const uint8_t*>(t.data());diff --git a/src/plugins/intel_npu/tests/unit/npuw/dq_matmul_gq2i_zero_point_test.cpp b/src/plugins/intel_npu/tests/unit/npuw/dq_matmul_gq2i_zero_point_test.cppnew file mode 100644index 00000000000..9479faccec0--- /dev/null+++ b/src/plugins/intel_npu/tests/unit/npuw/dq_matmul_gq2i_zero_point_test.cpp@@ -0,0 +1,178 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include <gtest/gtest.h>++#include <algorithm>+#include <vector>++#include "openvino/core/model.hpp"+#include "openvino/op/ops.hpp"+#include "openvino/pass/graph_rewrite.hpp"+#include "openvino/pass/validate.hpp"+#include "partitioning/patterns/opt.hpp"+#include "util.hpp"++namespace {++using namespace ov;++constexpr size_t kOutFeatures = 4;+constexpr size_t kGroups = 2;+constexpr size_t kGroupSize = 8;++struct GroupQuantModel {+ std::shared_ptr<ov::Model> model;+ std::shared_ptr<op::v0::Parameter> weight;+ std::shared_ptr<op::v0::Parameter> zero_point;+ std::shared_ptr<op::v0::Parameter> scale;+};++// Param(W)[OC,G,GS] -> Convert(f16) -> Subtract(Convert(Param(Z)[OC,G,1])) -> Multiply(Param(S)[OC,G,1])+// -> Reshape[OC,IC] -> Convert(f32) -> MatMul(Act[1,N,IC], transpose_b=true)+GroupQuantModel make_model(const element::Type zero_point_type, const size_t num_tokens, const bool rank4_act = false) {+ auto weight = std::make_shared<op::v0::Parameter>(element::u2, Shape{kOutFeatures, kGroups, kGroupSize});+ auto zero_point = std::make_shared<op::v0::Parameter>(zero_point_type, Shape{kOutFeatures, kGroups, 1});+ auto scale = std::make_shared<op::v0::Parameter>(element::f16, Shape{kOutFeatures, kGroups, 1});+ const Shape act_shape =+ rank4_act ? Shape{1, 1, num_tokens, kGroups * kGroupSize} : Shape{1, num_tokens, kGroups * kGroupSize};+ auto act = std::make_shared<op::v0::Parameter>(element::f32, act_shape);++ auto weight_cvt = std::make_shared<op::v0::Convert>(weight, element::f16);+ auto zero_point_cvt = std::make_shared<op::v0::Convert>(zero_point, element::f16);+ auto subtract = std::make_shared<op::v1::Subtract>(weight_cvt, zero_point_cvt);+ auto multiply = std::make_shared<op::v1::Multiply>(scale, subtract);+ auto reshape_shape = op::v0::Constant::create(element::i64, Shape{2}, {kOutFeatures, kGroups * kGroupSize});+ auto reshape = std::make_shared<op::v1::Reshape>(multiply, reshape_shape, false);+ auto out_cvt = std::make_shared<op::v0::Convert>(reshape, element::f32);+ auto matmul = std::make_shared<op::v0::MatMul>(act, out_cvt, false, true);++ return {std::make_shared<ov::Model>(ResultVector{std::make_shared<op::v0::Result>(matmul)},+ ParameterVector{weight, zero_point, scale, act}),+ weight,+ zero_point,+ scale};+}++template <class Pattern>+ov::npuw::patterns::opt::Context run_pattern(const GroupQuantModel& m, const bool mm_dq_full) {+ ov::npuw::patterns::opt::Context ctx(m.model, 0);+ ctx.mm_dq_full = mm_dq_full;+ ov::pass::GraphRewrite rewr;+ rewr.add_matcher<Pattern>(std::ref(ctx));+ rewr.run_on_model(m.model);+ ov::pass::Validate().run_on_model(m.model);+ return ctx;+}++size_t count_transposes(const std::shared_ptr<ov::Model>& model) {+ const auto ops = model->get_ops();+ return std::count_if(ops.begin(), ops.end(), [](const std::shared_ptr<ov::Node>& op) {+ return ov::is_type<op::v1::Transpose>(op);+ });+}++void expect_group_major_layout(const GroupQuantModel& m) {+ EXPECT_EQ(m.weight->get_shape(), (Shape{kGroups, kOutFeatures, kGroupSize}));+ EXPECT_EQ(m.zero_point->get_shape(), (Shape{kGroups, kOutFeatures, 1}));+ EXPECT_EQ(m.scale->get_shape(), (Shape{kGroups, kOutFeatures, 1}));+ EXPECT_EQ(count_transposes(m.model), 1u);+ EXPECT_EQ(m.model->get_results().front()->get_output_shape(0).back(), kOutFeatures);+}++} // namespace++TEST(DQMatMulGQ2iZeroPointTest, PermutesU2WeightsScaleAndZeroPointForGenerate) {+ const auto m = make_model(element::u2, 1);+ const auto ctx = run_pattern<ov::npuw::patterns::opt::DQMatMulGQ2i>(m, false);++ EXPECT_EQ(ctx.closures_to_permute.size(), 3u);+ expect_group_major_layout(m);+}++TEST(DQMatMulGQ2iZeroPointTest, PermutesU2WeightsForRank4Activation) {+ const auto m = make_model(element::u2, 1, true);+ const auto ctx = run_pattern<ov::npuw::patterns::opt::DQMatMulGQ2i>(m, false);++ EXPECT_EQ(ctx.closures_to_permute.size(), 3u);+ expect_group_major_layout(m);+}++TEST(DQMatMulGQ2iZeroPointTest, KeepsGraphForRank4ActivationInFullMode) {+ const auto m = make_model(element::u2, 1, true);+ const auto ctx = run_pattern<ov::npuw::patterns::opt::DQMatMulGQ2i>(m, true);++ EXPECT_TRUE(ctx.closures_to_permute.empty());+}++TEST(DQMatMulGQ2iZeroPointTest, PermutesU2WeightsScaleAndZeroPointForPrefill) {+ const auto m = make_model(element::u2, 5);+ const auto ctx = run_pattern<ov::npuw::patterns::opt::DQMatMulGQ2iP>(m, false);++ EXPECT_EQ(ctx.closures_to_permute.size(), 3u);+ EXPECT_EQ(m.weight->get_shape(), (Shape{kGroups, kOutFeatures, kGroupSize}));+ EXPECT_EQ(m.zero_point->get_shape(), (Shape{kGroups, kOutFeatures, 1}));+ EXPECT_EQ(count_transposes(m.model), 1u);+}++TEST(DQMatMulGQ2iZeroPointTest, KeepsGraphWhenZeroPointTypeDiffersFromWeights) {+ const auto m = make_model(element::u8, 1);+ const auto ctx = run_pattern<ov::npuw::patterns::opt::DQMatMulGQ2i>(m, false);++ EXPECT_TRUE(ctx.closures_to_permute.empty());+ EXPECT_EQ(m.weight->get_shape(), (Shape{kOutFeatures, kGroups, kGroupSize}));+ EXPECT_EQ(count_transposes(m.model), 0u);+}++TEST(DQMatMulGQ2iZeroPointTest, KeepsGraphWithZeroPointInFullMode) {+ const auto m = make_model(element::u2, 1);+ const auto ctx = run_pattern<ov::npuw::patterns::opt::DQMatMulGQ2i>(m, true);++ EXPECT_TRUE(ctx.closures_to_permute.empty());+ EXPECT_EQ(m.weight->get_shape(), (Shape{kOutFeatures, kGroups, kGroupSize}));+}++namespace {++// Permutes {1,0,2} of a u2 tensor of the given shape and returns the values as a flat vector.+std::vector<uint8_t> permute_u2(const Shape& shape, const std::vector<uint8_t>& values) {+ const auto input = op::v0::Constant::create(element::u2, shape, values);+ const ov::Tensor src(input->get_element_type(), input->get_shape(), const_cast<void*>(input->get_data_ptr()));+ const ov::Tensor dst = ov::npuw::util::permute(src, {1, 0, 2});+ return op::v0::Constant(dst).cast_vector<uint8_t>();+}++std::vector<uint8_t> expected_permute(const Shape& shape, const std::vector<uint8_t>& values) {+ std::vector<uint8_t> expected(values.size());+ for (size_t p = 0; p < shape[0]; ++p) {+ for (size_t r = 0; r < shape[1]; ++r) {+ for (size_t k = 0; k < shape[2]; ++k) {+ expected[(r * shape[0] + p) * shape[2] + k] = values[(p * shape[1] + r) * shape[2] + k];+ }+ }+ }+ return expected;+}++std::vector<uint8_t> make_values(const size_t count) {+ std::vector<uint8_t> values(count);+ for (size_t i = 0; i < count; ++i) {+ values[i] = static_cast<uint8_t>((i * 7 + i / 3) % 4);+ }+ return values;+}++} // namespace++TEST(NpuwPermuteU2Test, PermutesByteAlignedRows) {+ const Shape shape{3, 5, 8};+ const auto values = make_values(ov::shape_size(shape));+ EXPECT_EQ(permute_u2(shape, values), expected_permute(shape, values));+}++TEST(NpuwPermuteU2Test, PermutesSingleElementRows) {+ const Shape shape{7, 5, 1};+ const auto values = make_values(ov::shape_size(shape));+ EXPECT_EQ(permute_u2(shape, values), expected_permute(shape, values));+}