Skip to content

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

[NPUW] Match UNQDQ chain without zero-point Subtract in CollapseUNQDQ (#39548)

Koryun Khachatryanc79f2edc036f
diff --git a/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/collapse_unqdq.cpp b/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/collapse_unqdq.cpp
index 43d0381cd8f..c762305689f 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/collapse_unqdq.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/collapse_unqdq.cpp
@@ -4,10 +4,14 @@
#include "collapse_unqdq.hpp"
+#include <cmath>
#include <memory>
+#include <optional>
#include <string>
#include "openvino/core/graph_util.hpp"
+#include "openvino/core/validation_util.hpp"
+#include "openvino/op/constant.hpp"
#include "openvino/op/convert.hpp"
#include "openvino/op/fake_quantize.hpp"
#include "openvino/op/multiply.hpp"
@@ -15,6 +19,7 @@
#include "openvino/pass/matcher_pass.hpp"
#include "openvino/pass/pattern/op/optional.hpp"
#include "openvino/pass/pattern/op/wrap_type.hpp"
+#include "transformations/utils/utils.hpp"
namespace opp = ov::pass::pattern;
@@ -32,6 +37,41 @@ ov::Output<ov::Node> preserve_output_type(const ov::Output<ov::Node>& replacemen
return convert;
}
+// With the zero-point Subtract absent (zp == 0), FQ -> Convert -> Convert -> Multiply(scale)
+// is an identity on the FQ input only if the FQ is the matching quantizer:
+// input_low == output_low * scale and input_high == output_high * scale.
+bool is_identity_quantizer_without_zp(const ov::Output<ov::Node>& in_low_n,
+ const ov::Output<ov::Node>& in_high_n,
+ const ov::Output<ov::Node>& out_low_n,
+ const ov::Output<ov::Node>& out_high_n,
+ const ov::Output<ov::Node>& scale_n) {
+ auto get_constant = [](const ov::Output<ov::Node>& output) -> std::optional<float> {
+ const auto constant = ov::util::get_constant_from_source(output);
+ if (constant == nullptr || ov::util::is_empty_constant_tensor(constant->output(0))) {
+ return std::nullopt;
+ }
+ float value = 0.f;
+ return ov::op::util::get_single_value(constant, value) ? std::optional<float>(value) : std::nullopt;
+ };
+ const auto in_low = get_constant(in_low_n);
+ const auto in_high = get_constant(in_high_n);
+ const auto out_low = get_constant(out_low_n);
+ const auto out_high = get_constant(out_high_n);
+ const auto s = get_constant(scale_n);
+ if (!in_low || !in_high || !out_low || !out_high || !s) {
+ return false;
+ }
+
+ // Tolerance: half a quantization step (|scale| * 0.5)
+ const float tolerance = std::abs(*s) * 0.5f;
+ const auto scaled_out_low = *out_low * *s;
+ const auto scaled_out_high = *out_high * *s;
+ auto isclose = [tolerance](float a, float b) {
+ return std::abs(a - b) <= tolerance;
+ };
+ return isclose(*in_low, scaled_out_low) && isclose(*in_high, scaled_out_high);
+}
+
} // namespace
class CollapseUNQDQChain final : public ov::pass::MatcherPass {
@@ -43,15 +83,19 @@ public:
// FakeQuantize(input, ...)
// → Convert (quantize, e.g. to u16)
// → Convert (dequantize, back to f32)
- // → Subtract (zero-point)
+ // → [Subtract (zero-point)] (optional: dropped for zero ZP)
// → Multiply (scale) ← anchor
auto input = opp::any_input();
- auto fake_q = opp::wrap_type<ov::op::v0::FakeQuantize>(
- {input, opp::any_input(), opp::any_input(), opp::any_input(), opp::any_input()});
+ auto fq_in_low = opp::any_input();
+ auto fq_in_high = opp::any_input();
+ auto fq_out_low = opp::any_input();
+ auto fq_out_high = opp::any_input();
+ auto fake_q = opp::wrap_type<ov::op::v0::FakeQuantize>({input, fq_in_low, fq_in_high, fq_out_low, fq_out_high});
auto cvt_q = opp::wrap_type<ov::op::v0::Convert>({fake_q});
auto cvt_dq = opp::wrap_type<ov::op::v0::Convert>({cvt_q});
- auto subtract = opp::wrap_type<ov::op::v1::Subtract>({cvt_dq, opp::any_input()});
- auto multiply = opp::wrap_type<ov::op::v1::Multiply>({subtract, opp::any_input()});
+ auto subtract = opp::optional<ov::op::v1::Subtract>({cvt_dq, opp::any_input()});
+ auto scale = opp::any_input();
+ auto multiply = opp::wrap_type<ov::op::v1::Multiply>({subtract, scale});
// Note: use [=] so pattern nodes stay alive inside the callback.
ov::matcher_pass_callback callback = [=](opp::Matcher& m) {
@@ -60,6 +104,19 @@ public:
const auto matched_multiply = ov::as_type_ptr<ov::op::v1::Multiply>(m.get_match_root());
const auto matched_input = node_to_output.at(input);
+ // A broadcasting zero point or scale makes the chain non-shape-preserving.
+ if (matched_multiply->get_output_partial_shape(0) != matched_input.get_partial_shape()) {
+ return false;
+ }
+
+ if (!node_to_output.count(subtract) && !is_identity_quantizer_without_zp(node_to_output.at(fq_in_low),
+ node_to_output.at(fq_in_high),
+ node_to_output.at(fq_out_low),
+ node_to_output.at(fq_out_high),
+ node_to_output.at(scale))) {
+ return false;
+ }
+
ov::Output<ov::Node> replacement = matched_input;
replacement = preserve_output_type(replacement,
matched_multiply->get_output_element_type(0),
diff --git a/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/collapse_unqdq_test.cpp b/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/collapse_unqdq_test.cpp
index 92629e67ef4..93b14e6e071 100644
--- a/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/collapse_unqdq_test.cpp
+++ b/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/collapse_unqdq_test.cpp
@@ -7,7 +7,6 @@
#include "npuw_transformations/drop_zp_subtract.hpp"
#include <algorithm>
-
#include <gtest/gtest.h>
#include "openvino/core/validation_util.hpp"
@@ -33,7 +32,8 @@ std::size_t count_ops(const std::shared_ptr<ov::Model>& model) {
});
}
-std::shared_ptr<ov::Model> build_unqdq_model(const ov::element::Type& input_type = ov::element::f32) {
+std::shared_ptr<ov::Model> build_unqdq_model(const ov::element::Type& input_type = ov::element::f32,
+ bool swap_multiply_inputs = false) {
auto input = std::make_shared<ov::op::v0::Parameter>(input_type, ov::Shape{1, 4});
auto input_low = ov::op::v0::Constant::create(ov::element::f32, ov::Shape{}, {-1.0f});
auto input_high = ov::op::v0::Constant::create(ov::element::f32, ov::Shape{}, {1.0f});
@@ -50,13 +50,45 @@ std::shared_ptr<ov::Model> build_unqdq_model(const ov::element::Type& input_type
auto scale = std::make_shared<ov::op::v0::Convert>(
ov::op::v0::Constant::create(ov::element::f16, ov::Shape{}, {0.1f}),
ov::element::f32);
- auto multiply = std::make_shared<ov::op::v1::Multiply>(subtract, scale);
+ auto multiply = swap_multiply_inputs ? std::make_shared<ov::op::v1::Multiply>(scale, subtract)
+ : std::make_shared<ov::op::v1::Multiply>(subtract, scale);
multiply->set_friendly_name("unqdq_out");
return std::make_shared<ov::Model>(ov::ResultVector{std::make_shared<ov::op::v0::Result>(multiply)},
ov::ParameterVector{input},
"unqdq_model");
}
+// FakeQuantize([input_low, input_high] -> [0, 255]) -> Convert(u16) -> Convert(f32) -> Multiply(scale),
+// i.e. the zero point is already dropped. The quantizer is an identity (zp == 0) only when
+// input_low == 0 * scale and input_high == 255 * scale.
+std::shared_ptr<ov::Model> build_unqdq_model_without_zp(float input_low_value,
+ float input_high_value,
+ float scale_value,
+ bool swap_multiply_inputs = false,
+ bool scale_from_subtract = false) {
+ auto input = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::Shape{1, 4});
+ auto input_low = ov::op::v0::Constant::create(ov::element::f32, ov::Shape{}, {input_low_value});
+ auto input_high = ov::op::v0::Constant::create(ov::element::f32, ov::Shape{}, {input_high_value});
+ auto output_low = ov::op::v0::Constant::create(ov::element::f32, ov::Shape{}, {0.0f});
+ auto output_high = ov::op::v0::Constant::create(ov::element::f32, ov::Shape{}, {255.0f});
+ auto fake_quantize =
+ std::make_shared<ov::op::v0::FakeQuantize>(input, input_low, input_high, output_low, output_high, 256);
+ auto quantized_convert = std::make_shared<ov::op::v0::Convert>(fake_quantize, ov::element::u16);
+ auto dequantized_convert = std::make_shared<ov::op::v0::Convert>(quantized_convert, ov::element::f32);
+ std::shared_ptr<ov::Node> scale = ov::op::v0::Constant::create(ov::element::f32, ov::Shape{}, {scale_value});
+ if (scale_from_subtract) {
+ // Unfolded (scale + 1) - 1, so the scale producer is itself a Subtract.
+ scale = std::make_shared<ov::op::v1::Subtract>(
+ ov::op::v0::Constant::create(ov::element::f32, ov::Shape{}, {scale_value + 1.0f}),
+ ov::op::v0::Constant::create(ov::element::f32, ov::Shape{}, {1.0f}));
+ }
+ auto multiply = swap_multiply_inputs ? std::make_shared<ov::op::v1::Multiply>(scale, dequantized_convert)
+ : std::make_shared<ov::op::v1::Multiply>(dequantized_convert, scale);
+ return std::make_shared<ov::Model>(ov::ResultVector{std::make_shared<ov::op::v0::Result>(multiply)},
+ ov::ParameterVector{input},
+ "unqdq_without_zp_model");
+}
+
std::shared_ptr<ov::Model> build_zero_point_subtract_model(bool zero_point_is_zero) {
auto input = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::Shape{1, 4});
auto zero_point = std::make_shared<ov::op::v0::Convert>(
@@ -189,6 +221,100 @@ TEST(CollapseUNQDQPassTest, DropsEntireUnqdqChainFromModel) {
EXPECT_TRUE(ov::is_type<ov::op::v0::Parameter>(output_node));
}
+TEST(CollapseUNQDQPassTest, DropsUnqdqChainWithoutZeroPointSubtractForMatchingQuantizer) {
+ const auto model = build_unqdq_model_without_zp(0.0f, 127.5f, 0.5f);
+
+ ov::npuw::CollapseUNQDQ pass;
+ pass.run_on_model(model);
+ model->validate_nodes_and_infer_types();
+
+ EXPECT_EQ(count_ops<ov::op::v0::FakeQuantize>(model), 0u);
+ EXPECT_EQ(count_ops<ov::op::v1::Multiply>(model), 0u);
+ EXPECT_EQ(count_ops<ov::op::v0::Convert>(model), 0u);
+ EXPECT_TRUE(ov::is_type<ov::op::v0::Parameter>(model->get_results().front()->input_value(0).get_node_shared_ptr()));
+}
+
+TEST(CollapseUNQDQPassTest, KeepsUnqdqChainWithoutZeroPointSubtractWhenInputLowDoesNotMatch) {
+ // The matching quantizer is [0, 127.5] with scale 0.5
+ const auto model = build_unqdq_model_without_zp(-1.0f, 127.5f, 0.5f);
+
+ ov::npuw::CollapseUNQDQ pass;
+ pass.run_on_model(model);
+ model->validate_nodes_and_infer_types();
+
+ EXPECT_EQ(count_ops<ov::op::v0::FakeQuantize>(model), 1u);
+ EXPECT_EQ(count_ops<ov::op::v1::Multiply>(model), 1u);
+ EXPECT_EQ(count_ops<ov::op::v0::Convert>(model), 2u);
+}
+
+TEST(CollapseUNQDQPassTest, KeepsUnqdqChainWithoutZeroPointSubtractWhenInputHighDoesNotMatch) {
+ // The matching quantizer is [0, 127.5] with scale 0.5
+ const auto model = build_unqdq_model_without_zp(0.0f, 100.0f, 0.5f);
+
+ ov::npuw::CollapseUNQDQ pass;
+ pass.run_on_model(model);
+ model->validate_nodes_and_infer_types();
+
+ EXPECT_EQ(count_ops<ov::op::v0::FakeQuantize>(model), 1u);
+ EXPECT_EQ(count_ops<ov::op::v1::Multiply>(model), 1u);
+ EXPECT_EQ(count_ops<ov::op::v0::Convert>(model), 2u);
+}
+
+TEST(CollapseUNQDQPassTest, KeepsUnqdqChainWithoutZeroPointSubtractWhenScaleDoesNotMatch) {
+ // The matching quantizer is [0, 127.5] with scale 0.5
+ const auto model = build_unqdq_model_without_zp(0.0f, 127.5f, 0.25f);
+
+ ov::npuw::CollapseUNQDQ pass;
+ pass.run_on_model(model);
+ model->validate_nodes_and_infer_types();
+
+ EXPECT_EQ(count_ops<ov::op::v0::FakeQuantize>(model), 1u);
+ EXPECT_EQ(count_ops<ov::op::v1::Multiply>(model), 1u);
+ EXPECT_EQ(count_ops<ov::op::v0::Convert>(model), 2u);
+}
+
+// Multiply is commutative, so the pattern also matches with the UNQDQ chain on input 1.
+TEST(CollapseUNQDQPassTest, DropsEntireUnqdqChainWhenMultiplyInputsAreSwapped) {
+ const auto model = build_unqdq_model(ov::element::f32, true);
+
+ ov::npuw::CollapseUNQDQ pass;
+ pass.run_on_model(model);
+ model->validate_nodes_and_infer_types();
+
+ EXPECT_EQ(count_ops<ov::op::v0::FakeQuantize>(model), 0u);
+ EXPECT_EQ(count_ops<ov::op::v1::Multiply>(model), 0u);
+ EXPECT_EQ(count_ops<ov::op::v1::Subtract>(model), 0u);
+ EXPECT_EQ(count_ops<ov::op::v0::Convert>(model), 0u);
+ EXPECT_TRUE(ov::is_type<ov::op::v0::Parameter>(model->get_results().front()->input_value(0).get_node_shared_ptr()));
+}
+
+TEST(CollapseUNQDQPassTest, DropsUnqdqChainWithoutZeroPointSubtractWhenMultiplyInputsAreSwapped) {
+ const auto model = build_unqdq_model_without_zp(0.0f, 127.5f, 0.5f, true);
+
+ ov::npuw::CollapseUNQDQ pass;
+ pass.run_on_model(model);
+ model->validate_nodes_and_infer_types();
+
+ EXPECT_EQ(count_ops<ov::op::v0::FakeQuantize>(model), 0u);
+ EXPECT_EQ(count_ops<ov::op::v1::Multiply>(model), 0u);
+ EXPECT_EQ(count_ops<ov::op::v0::Convert>(model), 0u);
+ EXPECT_TRUE(ov::is_type<ov::op::v0::Parameter>(model->get_results().front()->input_value(0).get_node_shared_ptr()));
+}
+
+// A Subtract-produced scale on input 0 must not be mistaken for the zero-point Subtract.
+TEST(CollapseUNQDQPassTest, KeepsUnqdqChainWithoutZeroPointSubtractWhenSwappedScaleIsSubtract) {
+ // The matching quantizer is [0, 127.5] with scale 0.5
+ const auto model = build_unqdq_model_without_zp(-1.0f, 127.5f, 0.5f, true, true);
+
+ ov::npuw::CollapseUNQDQ pass;
+ pass.run_on_model(model);
+ model->validate_nodes_and_infer_types();
+
+ EXPECT_EQ(count_ops<ov::op::v0::FakeQuantize>(model), 1u);
+ EXPECT_EQ(count_ops<ov::op::v1::Multiply>(model), 1u);
+ EXPECT_EQ(count_ops<ov::op::v0::Convert>(model), 2u);
+}
+
TEST(CollapseUNQDQPassTest, PreservesOriginalOutputElementType) {
const auto model = build_unqdq_model(ov::element::f16);