OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[NPUW] GQA: fold single-element dequantization chains to constant (#40023)
Koryun Khachatryan661a9785f74c
diff --git a/src/plugins/intel_npu/src/plugin/npuw/gqa_compiled_model.cpp b/src/plugins/intel_npu/src/plugin/npuw/gqa_compiled_model.cppindex 7438452dd17..66d57a01813 100644--- a/src/plugins/intel_npu/src/plugin/npuw/gqa_compiled_model.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/gqa_compiled_model.cpp@@ -24,6 +24,7 @@#include "npuw_transformations/collapse_unqdq.hpp"#include "npuw_transformations/conv_to_matmul.hpp"#include "npuw_transformations/drop_zp_subtract.hpp"+#include "npuw_transformations/fold_scalar_dq.hpp"#include "npuw_transformations/reshape_to_static_gqa.hpp"#include "npuw_transformations/untangle_dq_scale.hpp"#include "openvino/core/descriptor/tensor.hpp"@@ -342,6 +343,9 @@ ov::npuw::GQACompiledModel::PreparedState ov::npuw::GQACompiledModel::prepare(co// multiple layers; NPUW's FOLD pass requires per-instance scalars.ov::npuw::UntangleDQScale untangle_dq_scale;untangle_dq_scale.run_on_model(model);+ // Fold single-element all-Constant DQ chains into one Constant.+ ov::npuw::FoldScalarDQ fold_scalar_dq;+ fold_scalar_dq.run_on_model(model);// Drop all-zero zero-point Subtract nodes so ConvToMatMul sees a clean// Convert(Parameter) → Multiply(scale) weight chain.ov::npuw::DropZPSubtract drop_zp_subtract;diff --git a/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/fold_scalar_dq.cpp b/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/fold_scalar_dq.cppnew file mode 100644index 00000000000..a8b6d5ddd61--- /dev/null+++ b/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/fold_scalar_dq.cpp@@ -0,0 +1,65 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "fold_scalar_dq.hpp"++#include <memory>++#include "openvino/core/graph_util.hpp"+#include "openvino/core/rt_info.hpp"+#include "openvino/core/validation_util.hpp"+#include "openvino/op/constant.hpp"+#include "openvino/op/convert.hpp"+#include "openvino/op/multiply.hpp"+#include "openvino/op/subtract.hpp"+#include "openvino/pass/matcher_pass.hpp"+#include "openvino/pass/pattern/op/optional.hpp"+#include "openvino/pass/pattern/op/or.hpp"+#include "openvino/pass/pattern/op/wrap_type.hpp"++namespace opp = ov::pass::pattern;++class FoldScalarDQMatcher final : public ov::pass::MatcherPass {+public:+ OPENVINO_MATCHER_PASS_RTTI("ov::npuw::FoldScalarDQMatcher");++ FoldScalarDQMatcher() {+ auto weight = opp::wrap_type<ov::op::v0::Constant>();+ auto weight_convert = opp::wrap_type<ov::op::v0::Convert>({weight});+ auto zero_point = opp::wrap_type<ov::op::v0::Constant>();+ auto zero_point_convert = opp::optional<ov::op::v0::Convert>({zero_point});+ auto weight_minus_zp = opp::wrap_type<ov::op::v1::Subtract>({weight_convert, zero_point_convert});+ auto zp_minus_weight = opp::wrap_type<ov::op::v1::Subtract>({zero_point_convert, weight_convert});+ auto multiply_input_or =+ std::make_shared<opp::op::Or>(ov::OutputVector{weight_convert, weight_minus_zp, zp_minus_weight});+ auto scale = opp::wrap_type<ov::op::v0::Constant>();+ auto scale_convert = opp::optional<ov::op::v0::Convert>({scale});+ auto multiply = opp::wrap_type<ov::op::v1::Multiply>({multiply_input_or, scale_convert});++ ov::matcher_pass_callback callback = [=](opp::Matcher& matcher) {+ const auto multiply_node = matcher.get_pattern_value_map().at(multiply).get_node_shared_ptr();++ const auto& shape = multiply_node->output(0).get_partial_shape();+ if (shape.is_dynamic() || ov::shape_size(shape.to_shape()) != 1) {+ return false;+ }++ const auto folded = ov::util::constantfold_subgraph(multiply_node->output(0));+ if (folded == nullptr) {+ return false;+ }++ folded->set_friendly_name(multiply_node->get_friendly_name());+ ov::copy_runtime_info(matcher.get_matched_nodes(), folded);+ ov::replace_node(multiply_node, folded);+ return true;+ };++ register_matcher(std::make_shared<opp::Matcher>(multiply, "FoldScalarDQMatcher"), callback);+ }+};++ov::npuw::FoldScalarDQ::FoldScalarDQ() {+ add_matcher<FoldScalarDQMatcher>();+}diff --git a/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/fold_scalar_dq.hpp b/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/fold_scalar_dq.hppnew file mode 100644index 00000000000..d44f9cc1c2d--- /dev/null+++ b/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/fold_scalar_dq.hpp@@ -0,0 +1,41 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#pragma once++#include "openvino/pass/graph_rewrite.hpp"++namespace ov::npuw {++/**+ * @brief Folds a single-element, all-Constant dequantization chain into one Constant.+ *+ * Per-tensor quantized scalars (e.g. ONNX DequantizeLinear of a norm scale) keep several tiny+ * Constants and Convert/Subtract/Multiply nodes for a value known at compile time. Folding them+ * leaves only real compressed weights as DQ chains for the following NPUW weight passes.+ *+ * Before: After:+ *+ * Const(w) Const(zp) Const((w - zp) * scale)+ * │ │ │+ * Convert [Convert] │+ * └─────┬─────┘ │+ * [Subtract] Const(scale) │+ * │ │ │+ * │ [Convert] │+ * └────┬─────┘ │+ * Multiply │+ * │ │+ *+ * [ ] marks optional nodes; Subtract and Multiply inputs may be in either order. Applied only when the+ * Multiply output has a static shape with exactly one element, so compressed weights stay+ * untouched.+ */+class FoldScalarDQ : public ov::pass::GraphRewrite {+public:+ OPENVINO_GRAPH_REWRITE_RTTI("ov::npuw::FoldScalarDQ");+ FoldScalarDQ();+};++} // namespace ov::npuwdiff --git a/src/plugins/intel_npu/src/plugin/sources.cmake b/src/plugins/intel_npu/src/plugin/sources.cmakeindex d9b0672fe9a..24e50b566b2 100644--- a/src/plugins/intel_npu/src/plugin/sources.cmake+++ b/src/plugins/intel_npu/src/plugin/sources.cmake@@ -138,6 +138,8 @@ set(NPUW_SOURCES${CMAKE_CURRENT_SOURCE_DIR}/npuw/npuw_transformations/detect_causal_mask.hpp${CMAKE_CURRENT_SOURCE_DIR}/npuw/npuw_transformations/drop_zp_subtract.cpp${CMAKE_CURRENT_SOURCE_DIR}/npuw/npuw_transformations/drop_zp_subtract.hpp+ ${CMAKE_CURRENT_SOURCE_DIR}/npuw/npuw_transformations/fold_scalar_dq.cpp+ ${CMAKE_CURRENT_SOURCE_DIR}/npuw/npuw_transformations/fold_scalar_dq.hpp${CMAKE_CURRENT_SOURCE_DIR}/npuw/npuw_transformations/right_align_mask_slice_for_conv.cpp${CMAKE_CURRENT_SOURCE_DIR}/npuw/npuw_transformations/right_align_mask_slice_for_conv.hpp${CMAKE_CURRENT_SOURCE_DIR}/npuw/npuw_transformations/kv_axes_position.hppdiff --git a/src/plugins/intel_npu/tests/unit/CMakeLists.txt b/src/plugins/intel_npu/tests/unit/CMakeLists.txtindex b7db2f10c8e..42c0cd7d8f7 100644--- a/src/plugins/intel_npu/tests/unit/CMakeLists.txt+++ b/src/plugins/intel_npu/tests/unit/CMakeLists.txt@@ -119,6 +119,7 @@ ov_add_test_target(${OpenVINO_SOURCE_DIR}/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/convert_kvcache_to_precision.cpp${OpenVINO_SOURCE_DIR}/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/cut_lm_head.cpp${OpenVINO_SOURCE_DIR}/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/drop_zp_subtract.cpp+ ${OpenVINO_SOURCE_DIR}/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/fold_scalar_dq.cpp${OpenVINO_SOURCE_DIR}/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/untangle_dq_scale.cpp${OpenVINO_SOURCE_DIR}/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/lora_stateful_to_stateless.cpp${OpenVINO_SOURCE_DIR}/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/mask_linear_attention_padding.cpp@@ -228,6 +229,7 @@ target_sources(${TARGET_NAME} PRIVATE${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/fold_scalar_dq_test.cpp${CMAKE_CURRENT_SOURCE_DIR}/npuw/pipeline_passes/cut_lm_head_test.cpp${CMAKE_CURRENT_SOURCE_DIR}/npuw/pipeline_passes/patch_sliding_mask_test.cpp${CMAKE_CURRENT_SOURCE_DIR}/npuw/pipeline_passes/detect_causal_mask_test.cppdiff --git a/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/fold_scalar_dq_test.cpp b/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/fold_scalar_dq_test.cppnew file mode 100644index 00000000000..40b579d625c--- /dev/null+++ b/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/fold_scalar_dq_test.cpp@@ -0,0 +1,195 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "npuw_transformations/fold_scalar_dq.hpp"++#include <gtest/gtest.h>++#include <limits>+#include <memory>+#include <string>+#include <tuple>++#include "common_test_utils/ov_test_utils.hpp"+#include "openvino/op/constant.hpp"+#include "openvino/op/convert.hpp"+#include "openvino/op/multiply.hpp"+#include "openvino/op/parameter.hpp"+#include "openvino/op/subtract.hpp"++namespace {++using namespace ov;++constexpr uint16_t kWeight = 3;+constexpr uint16_t kZeroPoint = 65535;+constexpr float kScale = 0.25f;++struct DQParams {+ bool with_subtract = true;+ bool swap_subtract = false;+ bool zp_convert = true;+ bool swap_multiply = false;+ bool scale_convert = false;+ bool weight_parameter = false;+ bool weight_convert = true;+ Shape weight_shape{1};+};++float expected_value(const DQParams& p) {+ const float w = kWeight, zp = kZeroPoint;+ const float shifted = !p.with_subtract ? w : p.swap_subtract ? zp - w : w - zp;+ return shifted * kScale;+}++// x * DQ(weight), where DQ = Convert(weight) -> [Subtract([Convert](zp))] -> Multiply(scale)+std::shared_ptr<Model> getModel(const DQParams& p) {+ auto x = std::make_shared<op::v0::Parameter>(element::f32, Shape{1, 4});+ ParameterVector params{x};++ std::shared_ptr<Node> weight;+ if (p.weight_parameter) {+ auto weight_param = std::make_shared<op::v0::Parameter>(element::u16, p.weight_shape);+ params.push_back(weight_param);+ weight = weight_param;+ } else {+ weight = op::v0::Constant::create(p.weight_convert ? element::u16 : element::f32, p.weight_shape, {kWeight});+ }+ std::shared_ptr<Node> value = p.weight_convert ? std::make_shared<op::v0::Convert>(weight, element::f32) : weight;+ if (p.with_subtract) {+ std::shared_ptr<Node> zp =+ op::v0::Constant::create(p.zp_convert ? element::u16 : element::f32, Shape{}, {kZeroPoint});+ if (p.zp_convert) {+ zp = std::make_shared<op::v0::Convert>(zp, element::f32);+ }+ value = p.swap_subtract ? std::make_shared<op::v1::Subtract>(zp, value)+ : std::make_shared<op::v1::Subtract>(value, zp);+ }+ std::shared_ptr<Node> scale =+ op::v0::Constant::create(p.scale_convert ? element::f16 : element::f32, Shape{}, {kScale});+ if (p.scale_convert) {+ scale = std::make_shared<op::v0::Convert>(scale, element::f32);+ }+ auto dq = p.swap_multiply ? std::make_shared<op::v1::Multiply>(scale, value)+ : std::make_shared<op::v1::Multiply>(value, scale);++ auto out = std::make_shared<op::v1::Multiply>(x, dq);+ return std::make_shared<Model>(OutputVector{out}, params);+}++// x * (Convert(weight) * Convert(scale))+std::shared_ptr<Model> getConvertScaleModel(element::Type weight_type,+ double weight_value,+ element::Type scale_type,+ double scale_value) {+ auto x = std::make_shared<op::v0::Parameter>(element::f32, Shape{1, 4});+ auto weight = std::make_shared<op::v0::Convert>(+ op::v0::Constant::create(weight_type, Shape{1}, std::vector<double>{weight_value}),+ element::f32);+ auto scale = std::make_shared<op::v0::Convert>(+ op::v0::Constant::create(scale_type, Shape{}, std::vector<double>{scale_value}),+ element::f32);+ auto dq = std::make_shared<op::v1::Multiply>(weight, scale);+ auto out = std::make_shared<op::v1::Multiply>(x, dq);+ return std::make_shared<Model>(OutputVector{out}, ParameterVector{x});+}++// x * Const(value)+std::shared_ptr<Model> getModelRef(const Shape& dq_shape, float value) {+ auto x = std::make_shared<op::v0::Parameter>(element::f32, Shape{1, 4});+ auto dq = op::v0::Constant::create(element::f32, dq_shape, {value});+ auto out = std::make_shared<op::v1::Multiply>(x, dq);+ return std::make_shared<Model>(OutputVector{out}, ParameterVector{x});+}++class FoldScalarDQTests : public TransformationTestsF {+public:+ FoldScalarDQTests() {+ comparator.enable(FunctionsComparator::CONST_VALUES);+ }++protected:+ void SetUp() override {+ TransformationTestsF::SetUp();+ manager.register_pass<npuw::FoldScalarDQ>();+ }+};++using FoldScalarDQParams = std::tuple<bool, bool, bool, bool, bool, Shape>;++class FoldScalarDQParamTests : public FoldScalarDQTests, public ::testing::WithParamInterface<FoldScalarDQParams> {+public:+ static std::string getTestCaseName(const ::testing::TestParamInfo<FoldScalarDQParams>& info) {+ const auto& [with_subtract, swap_subtract, zp_convert, swap_multiply, scale_convert, weight_shape] = info.param;+ std::string name = with_subtract ? "WithZP" : "NoZP";+ name += swap_subtract ? "_SwappedSub" : "";+ name += zp_convert ? "" : "_ZPNoConvert";+ name += swap_multiply ? "_SwappedMul" : "";+ name += scale_convert ? "_ScaleConvert" : "";+ name += "_rank" + std::to_string(weight_shape.size());+ return name;+ }++protected:+ void SetUp() override {+ FoldScalarDQTests::SetUp();+ DQParams p;+ std::tie(p.with_subtract, p.swap_subtract, p.zp_convert, p.swap_multiply, p.scale_convert, p.weight_shape) =+ GetParam();+ model = getModel(p);+ model_ref = getModelRef(p.weight_shape, expected_value(p));+ }+};++TEST_P(FoldScalarDQParamTests, FoldsChain) {}++INSTANTIATE_TEST_SUITE_P(NPUW,+ FoldScalarDQParamTests,+ ::testing::Combine(::testing::Bool(),+ ::testing::Bool(),+ ::testing::Bool(),+ ::testing::Bool(),+ ::testing::Bool(),+ ::testing::Values(Shape{}, Shape{1}, Shape{1, 1})),+ FoldScalarDQParamTests::getTestCaseName);++TEST_F(FoldScalarDQTests, NotAppliedForParameterWeight) {+ DQParams p;+ p.weight_parameter = true;+ model = getModel(p);+}++TEST_F(FoldScalarDQTests, NotAppliedForMultiElementWeight) {+ DQParams p;+ p.weight_shape = Shape{4};+ model = getModel(p);+}++TEST_F(FoldScalarDQTests, NotAppliedWithoutSubtractConvert) {+ DQParams p;+ p.weight_convert = false;+ p.zp_convert = false;+ model = getModel(p);+}++TEST_F(FoldScalarDQTests, NotAppliedWithoutWeightConvertAndSubtract) {+ DQParams p;+ p.weight_convert = false;+ p.with_subtract = false;+ model = getModel(p);+}++// Bound evaluation maps a source type's max to the destination type's max; folding must use real values.+TEST_F(FoldScalarDQTests, KeepsF16MaxScale) {+ model = getConvertScaleModel(element::u16, 1.0, element::f16, 65504.0);+ model_ref = getModelRef(Shape{1}, 65504.0f);+}++TEST_F(FoldScalarDQTests, KeepsI32MaxWeight) {+ constexpr double i32_max = std::numeric_limits<int32_t>::max();+ model = getConvertScaleModel(element::i32, i32_max, element::f32, 1.0);+ model_ref = getModelRef(Shape{1}, static_cast<float>(i32_max));+}++} // namespace