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
Broadcast the scalar zero point of u2 weights to per-group u2 (#39441)
Wenzeng Chen9c704f6eb817
diff --git a/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.cpp b/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.cppindex a0c71330585..d3c02a3acc9 100644--- a/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.cpp@@ -18,6 +18,7 @@#include "moe_transformations/apply_moe_device_routed_transforms.hpp"#include "moe_transformations/moe_topology.hpp"#include "npuw_transformations/add_position_ids_param.hpp"+#include "npuw_transformations/broadcast_u2_zero_point.hpp"#include "npuw_transformations/convert_kvcache_to_precision.hpp"#include "npuw_transformations/cut_lm_head.hpp"#include "npuw_transformations/detect_causal_mask.hpp"@@ -1116,6 +1117,11 @@ ov::npuw::LLMCompiledModel::LLMCompiledModel(const std::shared_ptr<ov::Model>& m}}+ ov::npuw::BroadcastU2ZeroPoint().run_on_model(kvcache_model);+ if (lm_head_model) {+ ov::npuw::BroadcastU2ZeroPoint().run_on_model(lm_head_model);+ }+LOG_DEBUG("Creating prefill model as clone of transformed kvcache one.");auto prefill_model = kvcache_model->clone();prefill_model->set_friendly_name(kvcache_model->get_friendly_name() + "_prefill");diff --git a/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/broadcast_u2_zero_point.cpp b/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/broadcast_u2_zero_point.cppnew file mode 100644index 00000000000..5122d8b498d--- /dev/null+++ b/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/broadcast_u2_zero_point.cpp@@ -0,0 +1,76 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "broadcast_u2_zero_point.hpp"++#include <cmath>+#include <cstdint>+#include <memory>+#include <vector>++#include "openvino/op/constant.hpp"+#include "openvino/op/convert.hpp"+#include "openvino/op/subtract.hpp"+#include "openvino/pass/matcher_pass.hpp"+#include "openvino/pass/pattern/op/optional.hpp"+#include "openvino/pass/pattern/op/wrap_type.hpp"++namespace opp = ov::pass::pattern;++namespace {++constexpr float MAX_U2_VALUE = 3.f;++} // namespace++class BroadcastU2ZeroPointMatcher final : public ov::pass::MatcherPass {+public:+ OPENVINO_MATCHER_PASS_RTTI("ov::npuw::BroadcastU2ZeroPointMatcher");++ BroadcastU2ZeroPointMatcher() {+ auto weights = opp::wrap_type<ov::op::v0::Constant>();+ auto weights_convert = opp::wrap_type<ov::op::v0::Convert>({weights});+ auto zero_point = opp::wrap_type<ov::op::v0::Constant>();+ auto zero_point_convert = opp::optional<ov::op::v0::Convert>({zero_point->output(0)});+ auto subtract = opp::wrap_type<ov::op::v1::Subtract>({weights_convert, zero_point_convert});++ ov::matcher_pass_callback callback = [=](opp::Matcher& matcher) {+ const auto& pattern_map = matcher.get_pattern_value_map();+ const auto weights_const =+ ov::as_type_ptr<ov::op::v0::Constant>(pattern_map.at(weights).get_node_shared_ptr());+ const auto zero_point_const =+ ov::as_type_ptr<ov::op::v0::Constant>(pattern_map.at(zero_point).get_node_shared_ptr());+ const auto subtract_node = pattern_map.at(subtract).get_node_shared_ptr();+ // An f32 Constant zero point is matched by AsymmZP::CWAI, which a Convert would break.+ if (subtract_node->get_input_element_type(1) != ov::element::f16 ||+ weights_const->get_element_type() != ov::element::u2 || weights_const->get_shape().size() < 2 ||+ !(zero_point_const->get_element_type().is_real() ||+ zero_point_const->get_element_type().is_integral_number()) ||+ !zero_point_const->get_shape().empty()) {+ return false;+ }+ const float scalar = zero_point_const->cast_vector<float>().front();+ if (scalar != std::floor(scalar) || scalar < 0.f || scalar > MAX_U2_VALUE) {+ return false;+ }++ ov::Shape group_shape = weights_const->get_shape();+ group_shape.back() = 1;+ auto broadcast = ov::op::v0::Constant::create(+ ov::element::u2,+ group_shape,+ std::vector<uint8_t>(ov::shape_size(group_shape), static_cast<uint8_t>(scalar)));+ broadcast->set_friendly_name(zero_point_const->get_friendly_name());+ subtract_node->input(1).replace_source_output(+ std::make_shared<ov::op::v0::Convert>(broadcast, subtract_node->get_input_element_type(1)));+ return true;+ };++ register_matcher(std::make_shared<opp::Matcher>(subtract, "BroadcastU2ZeroPointMatcher"), callback);+ }+};++ov::npuw::BroadcastU2ZeroPoint::BroadcastU2ZeroPoint() {+ add_matcher<BroadcastU2ZeroPointMatcher>();+}diff --git a/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/broadcast_u2_zero_point.hpp b/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/broadcast_u2_zero_point.hppnew file mode 100644index 00000000000..1a73022c207--- /dev/null+++ b/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/broadcast_u2_zero_point.hpp@@ -0,0 +1,23 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#pragma once++#include "openvino/pass/graph_rewrite.hpp"++namespace ov::npuw {++// Broadcasts a scalar zero point in the dequantization chain of u2 weights to a per-group u2 Constant:+// Convert(Constant u2 [..., groups, group_size]) -> Subtract <- [Convert] <- Constant (scalar)+// becomes+// Convert(Constant u2 [..., groups, group_size]) -> Subtract <- Convert(Constant u2 [..., groups, 1])+// The rewrite is applied only when the zero point is an integer in the range of u2, so the numerical result is+// unchanged, and the zero point gets the same element type and the group shape of the weights scales.+class BroadcastU2ZeroPoint : public ov::pass::GraphRewrite {+public:+ OPENVINO_GRAPH_REWRITE_RTTI("ov::npuw::BroadcastU2ZeroPoint");+ BroadcastU2ZeroPoint();+};++} // namespace ov::npuwdiff --git a/src/plugins/intel_npu/src/plugin/sources.cmake b/src/plugins/intel_npu/src/plugin/sources.cmakeindex 36047225657..d9b0672fe9a 100644--- a/src/plugins/intel_npu/src/plugin/sources.cmake+++ b/src/plugins/intel_npu/src/plugin/sources.cmake@@ -122,6 +122,8 @@ set(NPUW_SOURCES${CMAKE_CURRENT_SOURCE_DIR}/npuw/moe_transformations/moe_unroll_patterns.hpp${CMAKE_CURRENT_SOURCE_DIR}/npuw/npuw_transformations/add_position_ids_param.cpp${CMAKE_CURRENT_SOURCE_DIR}/npuw/npuw_transformations/add_position_ids_param.hpp+ ${CMAKE_CURRENT_SOURCE_DIR}/npuw/npuw_transformations/broadcast_u2_zero_point.cpp+ ${CMAKE_CURRENT_SOURCE_DIR}/npuw/npuw_transformations/broadcast_u2_zero_point.hpp${CMAKE_CURRENT_SOURCE_DIR}/npuw/npuw_transformations/collapse_unqdq.cpp${CMAKE_CURRENT_SOURCE_DIR}/npuw/npuw_transformations/collapse_unqdq.hpp${CMAKE_CURRENT_SOURCE_DIR}/npuw/npuw_transformations/convert_kvcache_to_precision.cppdiff --git a/src/plugins/intel_npu/tests/unit/CMakeLists.txt b/src/plugins/intel_npu/tests/unit/CMakeLists.txtindex 41eaaa4c427..015e0e57d5a 100644--- a/src/plugins/intel_npu/tests/unit/CMakeLists.txt+++ b/src/plugins/intel_npu/tests/unit/CMakeLists.txt@@ -113,6 +113,7 @@ ov_add_test_target(${OpenVINO_SOURCE_DIR}/src/plugins/intel_npu/src/plugin/npuw/attn/attn_subgraph.cpp# npuw_transformations${OpenVINO_SOURCE_DIR}/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/add_position_ids_param.cpp+ ${OpenVINO_SOURCE_DIR}/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/broadcast_u2_zero_point.cpp${OpenVINO_SOURCE_DIR}/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/collapse_unqdq.cpp${OpenVINO_SOURCE_DIR}/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/conv_to_matmul.cpp${OpenVINO_SOURCE_DIR}/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/convert_kvcache_to_precision.cpp@@ -222,6 +223,7 @@ target_sources(${TARGET_NAME} PRIVATE${CMAKE_CURRENT_SOURCE_DIR}/npuw/pipeline_passes/add_position_ids_param_test.cpp${CMAKE_CURRENT_SOURCE_DIR}/npuw/pipeline_passes/mask_linear_attention_padding_test.cpp${CMAKE_CURRENT_SOURCE_DIR}/npuw/pipeline_passes/right_align_mask_slice_for_conv_test.cpp+ ${CMAKE_CURRENT_SOURCE_DIR}/npuw/pipeline_passes/broadcast_u2_zero_point_test.cpp${CMAKE_CURRENT_SOURCE_DIR}/npuw/pipeline_passes/collapse_unqdq_test.cpp${CMAKE_CURRENT_SOURCE_DIR}/npuw/pipeline_passes/conv_to_matmul_test.cpp${CMAKE_CURRENT_SOURCE_DIR}/npuw/pipeline_passes/cut_lm_head_test.cppdiff --git a/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/broadcast_u2_zero_point_test.cpp b/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/broadcast_u2_zero_point_test.cppnew file mode 100644index 00000000000..74dc439c483--- /dev/null+++ b/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/broadcast_u2_zero_point_test.cpp@@ -0,0 +1,114 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "npuw_transformations/broadcast_u2_zero_point.hpp"++#include <gtest/gtest.h>++#include <algorithm>+#include <cstdint>+#include <vector>++#include "openvino/op/constant.hpp"+#include "openvino/op/convert.hpp"+#include "openvino/op/result.hpp"+#include "openvino/op/subtract.hpp"++namespace {++std::shared_ptr<ov::Model> build_dequantize_model(const ov::Shape& weights_shape,+ const ov::Output<ov::Node>& zero_point,+ const ov::element::Type& precision = ov::element::f16) {+ const auto weights = ov::op::v0::Constant::create(ov::element::u2,+ weights_shape,+ std::vector<uint8_t>(ov::shape_size(weights_shape), 2));+ const auto weights_convert = std::make_shared<ov::op::v0::Convert>(weights, precision);+ const auto subtract = std::make_shared<ov::op::v1::Subtract>(weights_convert, zero_point);+ return std::make_shared<ov::Model>(ov::ResultVector{std::make_shared<ov::op::v0::Result>(subtract)},+ ov::ParameterVector{});+}++ov::Output<ov::Node> scalar_zero_point(const float value, const ov::element::Type& type = ov::element::f16) {+ return ov::op::v0::Constant::create(type, ov::Shape{}, {value});+}++std::shared_ptr<ov::Node> zero_point_input(const std::shared_ptr<ov::Model>& model) {+ for (const auto& op : model->get_ordered_ops()) {+ if (ov::is_type<ov::op::v1::Subtract>(op)) {+ return op->get_input_node_shared_ptr(1);+ }+ }+ return nullptr;+}++void run_pass(const std::shared_ptr<ov::Model>& model) {+ ov::npuw::BroadcastU2ZeroPoint pass;+ pass.run_on_model(model);+ model->validate_nodes_and_infer_types();+}++void expect_broadcast_to_per_group_u2(const std::shared_ptr<ov::Model>& model) {+ const auto convert = zero_point_input(model);+ ASSERT_TRUE(ov::is_type<ov::op::v0::Convert>(convert));+ const auto zero_point = ov::as_type_ptr<ov::op::v0::Constant>(convert->get_input_node_shared_ptr(0));+ ASSERT_NE(zero_point, nullptr);+ EXPECT_EQ(zero_point->get_element_type(), ov::element::u2);+ EXPECT_EQ(zero_point->get_shape(), (ov::Shape{4, 2, 1}));+ const auto values = zero_point->cast_vector<uint8_t>();+ EXPECT_TRUE(std::all_of(values.begin(), values.end(), [](uint8_t value) {+ return value == 1;+ }));+ EXPECT_EQ(model->get_results().front()->input_value(0).get_element_type(), ov::element::f16);+ EXPECT_EQ(model->get_results().front()->input_value(0).get_shape(), (ov::Shape{4, 2, 8}));+}++void expect_unchanged(const std::shared_ptr<ov::Model>& model) {+ const auto before = zero_point_input(model);+ run_pass(model);+ EXPECT_EQ(zero_point_input(model), before);+}++} // namespace++TEST(BroadcastU2ZeroPointPassTest, BroadcastsScalarZeroPointToPerGroupU2) {+ const auto model = build_dequantize_model({4, 2, 8}, scalar_zero_point(1.f));++ run_pass(model);++ expect_broadcast_to_per_group_u2(model);+}++// The graph of ov::decomposition::low_precision_dequantize() for a u8 zero point and f16 scales.+TEST(BroadcastU2ZeroPointPassTest, BroadcastsU8ZeroPointBehindConvert) {+ const auto zero_point =+ std::make_shared<ov::op::v0::Convert>(scalar_zero_point(1.f, ov::element::u8), ov::element::f16);+ const auto model = build_dequantize_model({4, 2, 8}, zero_point);++ run_pass(model);++ expect_broadcast_to_per_group_u2(model);+}++// AsymmZP::CWAI matches only an f32 Constant zero point on the Subtract.+TEST(BroadcastU2ZeroPointPassTest, KeepsF32ZeroPoint) {+ expect_unchanged(build_dequantize_model({4, 2, 8}, scalar_zero_point(1.f, ov::element::f32), ov::element::f32));+}++TEST(BroadcastU2ZeroPointPassTest, KeepsZeroPointThatDoesNotFitInTwoBits) {+ for (const float zero_point : {4.f, -1.f, 0.5f}) {+ expect_unchanged(build_dequantize_model({4, 2, 8}, scalar_zero_point(zero_point)));+ }+}++TEST(BroadcastU2ZeroPointPassTest, KeepsZeroPointOfWeightsWithRankBelowTwo) {+ expect_unchanged(build_dequantize_model({8}, scalar_zero_point(1.f)));+}++TEST(BroadcastU2ZeroPointPassTest, KeepsPerGroupZeroPoint) {+ const auto per_group =+ ov::op::v0::Constant::create(ov::element::u2, ov::Shape{4, 2, 1}, std::vector<uint8_t>(8, 1));+ const auto zero_point = std::make_shared<ov::op::v0::Convert>(per_group, ov::element::f16);++ expect_unchanged(build_dequantize_model({4, 2, 8}, zero_point));+}