Skip to content

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

- Allow quantize fusion for non per-tensor cases if no shape change (#35184)

Ashutosh Singhcdc71a242723
diff --git a/src/plugins/intel_gpu/src/graph/graph_optimizer/prepare_primitive_fusing.cpp b/src/plugins/intel_gpu/src/graph/graph_optimizer/prepare_primitive_fusing.cpp
index 69a8fd6cfee..a64fc77650b 100644
--- a/src/plugins/intel_gpu/src/graph/graph_optimizer/prepare_primitive_fusing.cpp
+++ b/src/plugins/intel_gpu/src/graph/graph_optimizer/prepare_primitive_fusing.cpp
@@ -955,14 +955,6 @@ void prepare_primitive_fusing::fuse_simple_primitives(program &p) {
auto out_dt_is_i8_u8 = data_type_traits::is_i8_u8(out_dt);
auto in_dt_is_i8_u8 = data_type_traits::is_i8_u8(in_dt);
- bool per_tensor_values = quantize_node.get_scale_shift_opt() &&
- quantize_node.get_per_tensor_input_scale() &&
- quantize_node.get_per_tensor_input_shift() &&
- quantize_node.get_per_tensor_input_range() &&
- quantize_node.get_per_tensor_output_scale() &&
- quantize_node.get_per_tensor_output_shift() &&
- quantize_node.get_per_tensor_output_range();
-
bool should_fuse = input_data.is_type<convolution>() && conv_supports_fusings(input_data.as<convolution>()) &&
quantize_node.get_scale_shift_opt() &&
((out_dt == data_types::f32 || out_dt == data_types::f16) ||
@@ -1032,7 +1024,7 @@ void prepare_primitive_fusing::fuse_simple_primitives(program &p) {
should_fuse |= input_data.is_type<softmax>() &&
input_data.as<softmax>().get_primitive()->dimension == 1 &&
- per_tensor_values;
+ quantize_node.has_per_tensor_values();
should_fuse |= input_data.is_type<broadcast>() && broadcast_supports_fusings(input_data.as<broadcast>());
diff --git a/src/plugins/intel_gpu/src/graph/graph_optimizer/prepare_primitive_fusing_through.cpp b/src/plugins/intel_gpu/src/graph/graph_optimizer/prepare_primitive_fusing_through.cpp
index 8d9d9854db9..7aa211b172e 100644
--- a/src/plugins/intel_gpu/src/graph/graph_optimizer/prepare_primitive_fusing_through.cpp
+++ b/src/plugins/intel_gpu/src/graph/graph_optimizer/prepare_primitive_fusing_through.cpp
@@ -85,16 +85,10 @@ void prepare_primitive_fusing_through::run(program& p) {
input_node = &node->get_dependency(0);
} else if (node->is_type<quantize>()) {
auto& quantize_node = node->as<quantize>();
- bool per_tensor_values = quantize_node.get_scale_shift_opt() &&
- quantize_node.get_per_tensor_input_scale() &&
- (quantize_node.get_per_tensor_input_shift() || !quantize_node.get_need_pre_shift()) &&
- quantize_node.get_per_tensor_input_range() &&
- quantize_node.get_per_tensor_output_scale() &&
- (quantize_node.get_per_tensor_output_shift() || !quantize_node.get_need_post_shift()) &&
- quantize_node.get_per_tensor_output_range();
-
- if (!per_tensor_values)
+
+ if (!quantize_node.has_per_tensor_values() && !quantize_node.get_scale_shift_opt()) {
continue;
+ }
input_node = &node->get_dependency(0);
} else if (node->is_type<eltwise>()) {
@@ -134,6 +128,16 @@ void prepare_primitive_fusing_through::run(program& p) {
auto new_prev = fuse_through_order[fuse_through_order.size() - 1];
auto new_next = fuse_through_order[fuse_through_order.size() - 2];
+ // For per-channel quantize, allow only when the fused target's output shape
+ // matches the quantize's current input shape. This ensures per-channel
+ // parameters remain correctly broadcast-aligned at the new position.
+ if (node->is_type<quantize>() && !node->as<quantize>().has_per_tensor_values()) {
+ auto target_shape = new_prev->get_output_layout().get_partial_shape();
+ auto current_input_shape = node->get_input_layout(0).get_partial_shape();
+ if ((target_shape != current_input_shape) || target_shape.is_dynamic() || current_input_shape.is_dynamic())
+ continue;
+ }
+
// Check broadcastable for fused eltwise's output
if (node->is_type<eltwise>()) {
auto out_shape = new_prev->get_output_layout().get_partial_shape(); // new_prev's layout became node's new layout after fusing
diff --git a/src/plugins/intel_gpu/src/graph/include/quantize_inst.h b/src/plugins/intel_gpu/src/graph/include/quantize_inst.h
index 727d8c7ecc6..de6007f0541 100644
--- a/src/plugins/intel_gpu/src/graph/include/quantize_inst.h
+++ b/src/plugins/intel_gpu/src/graph/include/quantize_inst.h
@@ -192,6 +192,16 @@ public:
float get_output_lo_val() const { return get_primitive()->out_lo; }
float get_output_hi_val() const { return get_primitive()->out_hi; }
+ bool has_per_tensor_values() const {
+ return get_scale_shift_opt() &&
+ get_per_tensor_input_scale() &&
+ (get_per_tensor_input_shift() || !get_need_pre_shift()) &&
+ get_per_tensor_input_range() &&
+ get_per_tensor_output_scale() &&
+ (get_per_tensor_output_shift() || !get_need_post_shift()) &&
+ get_per_tensor_output_range();
+ }
+
std::shared_ptr<NodeFuseParams> get_fuse_params() const override {
return std::make_shared<QuantizeFuseParams>(get_output_layout(),
get_primitive()->scale_shift_opt,
diff --git a/src/plugins/intel_gpu/tests/unit/passes/prepare_primitive_fusing_through_test.cpp b/src/plugins/intel_gpu/tests/unit/passes/prepare_primitive_fusing_through_test.cpp
new file mode 100644
index 00000000000..f1bdfccd93b
--- /dev/null
+++ b/src/plugins/intel_gpu/tests/unit/passes/prepare_primitive_fusing_through_test.cpp
@@ -0,0 +1,163 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "test_utils.h"
+
+#include "intel_gpu/runtime/engine.hpp"
+
+#include "intel_gpu/graph/network.hpp"
+#include "intel_gpu/graph/program.hpp"
+#include "data_inst.h"
+#include "quantize_inst.h"
+#include "reshape_inst.h"
+#include "fully_connected_inst.h"
+#include "pass_manager.h"
+#include "to_string_utils.h"
+
+#include "program_wrapper.h"
+
+#include <memory>
+
+using namespace cldnn;
+using namespace ::tests;
+
+// Positive test: per-channel quantize should fuse through reshape chain when shapes match (round-trip).
+// Topology: Input[4,3,1,1] -> FC[4,3,1,1] -> Reshape1[1,3,2,2] -> Reshape2[4,3,1,1] -> Quantize(per-ch) -> Reorder
+// After pass: Quantize should be moved next to FC (quantize's dep 0 == "fc").
+TEST(prepare_primitive_fusing_through, fuse_quantize_per_channel_through_reshape_chain) {
+ auto& engine = get_test_engine();
+ auto in_layout = layout{ov::PartialShape{4, 3, 1, 1}, data_types::f32, format::bfyx};
+ auto weights_mem = engine.allocate_memory({ov::PartialShape{3, 3}, data_types::f32, format::bfyx});
+ auto in_lo_mem = engine.allocate_memory({ov::PartialShape{1, 3, 1, 1}, data_types::f32, format::bfyx});
+ auto in_hi_mem = engine.allocate_memory({ov::PartialShape{1, 3, 1, 1}, data_types::f32, format::bfyx});
+ auto out_lo_mem = engine.allocate_memory({ov::PartialShape{1, 3, 1, 1}, data_types::f32, format::bfyx});
+ auto out_hi_mem = engine.allocate_memory({ov::PartialShape{1, 3, 1, 1}, data_types::f32, format::bfyx});
+
+ set_values<float>(in_lo_mem, {0.f, 0.f, 0.f});
+ set_values<float>(in_hi_mem, {1.f, 2.f, 3.f});
+ set_values<float>(out_lo_mem, {0.f, 0.f, 0.f});
+ set_values<float>(out_hi_mem, {255.f, 255.f, 255.f});
+
+ topology topology;
+ topology.add(input_layout("input", in_layout));
+ topology.add(data("weights", weights_mem));
+ topology.add(fully_connected("fc", input_info("input"), "weights"));
+ topology.add(reshape("reshape1", input_info("fc"), tensor(1, 3, 2, 2)));
+ topology.add(reshape("reshape2", input_info("reshape1"), tensor(4, 3, 1, 1)));
+ topology.add(data("in_lo", in_lo_mem));
+ topology.add(data("in_hi", in_hi_mem));
+ topology.add(data("out_lo", out_lo_mem));
+ topology.add(data("out_hi", out_hi_mem));
+ topology.add(quantize("quantize", input_info("reshape2"),
+ input_info("in_lo"), input_info("in_hi"),
+ input_info("out_lo"), input_info("out_hi"), 256, data_types::u8));
+ topology.add(reorder("output", input_info("quantize"), format::bfyx, data_types::f32));
+
+ ExecutionConfig config;
+ config.set_property(ov::intel_gpu::optimize_data(true));
+ auto prog = program::build_program(engine, topology, config, false, true);
+
+ program_wrapper::apply_opt_pass<prepare_quantization>(*prog);
+ program_wrapper::apply_opt_pass<prepare_primitive_fusing_through>(*prog);
+
+ ASSERT_NE(prog, nullptr);
+ auto& quant_node = prog->get_node("quantize");
+ // After the pass, quantize should have been moved next to FC
+ ASSERT_EQ(quant_node.get_dependency(0).id(), "fc");
+}
+
+// Negative test: per-channel quantize should NOT fuse through reshape when shapes don't match.
+// Topology: Input[4,3,1,1] -> FC[4,3,1,1] -> Reshape1[1,3,2,2] -> Quantize(per-ch) -> Reorder
+// FC output is {4,3,1,1} but quantize input is {1,3,2,2} — mismatch blocks fusing.
+TEST(prepare_primitive_fusing_through, dont_fuse_quantize_per_channel_shape_mismatch) {
+ auto& engine = get_test_engine();
+ auto in_layout = layout{ov::PartialShape{4, 3, 1, 1}, data_types::f32, format::bfyx};
+ auto weights_mem = engine.allocate_memory({ov::PartialShape{3, 3}, data_types::f32, format::bfyx});
+ auto in_lo_mem = engine.allocate_memory({ov::PartialShape{1, 3, 1, 1}, data_types::f32, format::bfyx});
+ auto in_hi_mem = engine.allocate_memory({ov::PartialShape{1, 3, 1, 1}, data_types::f32, format::bfyx});
+ auto out_lo_mem = engine.allocate_memory({ov::PartialShape{1, 3, 1, 1}, data_types::f32, format::bfyx});
+ auto out_hi_mem = engine.allocate_memory({ov::PartialShape{1, 3, 1, 1}, data_types::f32, format::bfyx});
+
+ set_values<float>(in_lo_mem, {0.f, 0.f, 0.f});
+ set_values<float>(in_hi_mem, {1.f, 2.f, 3.f});
+ set_values<float>(out_lo_mem, {0.f, 0.f, 0.f});
+ set_values<float>(out_hi_mem, {255.f, 255.f, 255.f});
+
+ topology topology;
+ topology.add(input_layout("input", in_layout));
+ topology.add(data("weights", weights_mem));
+ topology.add(fully_connected("fc", input_info("input"), "weights"));
+ topology.add(reshape("reshape1", input_info("fc"), tensor(1, 3, 2, 2)));
+ topology.add(data("in_lo", in_lo_mem));
+ topology.add(data("in_hi", in_hi_mem));
+ topology.add(data("out_lo", out_lo_mem));
+ topology.add(data("out_hi", out_hi_mem));
+ topology.add(quantize("quantize", input_info("reshape1"),
+ input_info("in_lo"), input_info("in_hi"),
+ input_info("out_lo"), input_info("out_hi"), 256, data_types::u8));
+ topology.add(reorder("output", input_info("quantize"), format::bfyx, data_types::f32));
+
+ ExecutionConfig config;
+ config.set_property(ov::intel_gpu::optimize_data(true));
+ auto prog = program::build_program(engine, topology, config, false, true);
+
+ program_wrapper::apply_opt_pass<prepare_quantization>(*prog);
+ program_wrapper::apply_opt_pass<prepare_primitive_fusing_through>(*prog);
+
+ ASSERT_NE(prog, nullptr);
+ auto& quant_node = prog->get_node("quantize");
+ // Quantize should NOT have moved — its input should still be reshape1
+ ASSERT_EQ(quant_node.get_dependency(0).id(), "reshape1");
+}
+
+// Negative test: per-channel quantize should NOT fuse through when shapes are dynamic.
+// Topology: Input[-1,3,1,1] -> FC[-1,3,1,1] -> Reshape[-1,3,1,1] -> Quantize(per-ch) -> Reorder
+// FC output and quantize input have the same dynamic shape,
+// but fusing is conservatively blocked since shape equality cannot be guaranteed at graph compilation time.
+TEST(prepare_primitive_fusing_through, dont_fuse_quantize_per_channel_dynamic_shapes) {
+ auto& engine = get_test_engine();
+ auto in_layout = layout{ov::PartialShape{-1, 3, 1, 1}, data_types::f32, format::bfyx};
+ auto weights_mem = engine.allocate_memory({ov::PartialShape{1, 1}, data_types::f32, format::bfyx});
+ auto in_lo_mem = engine.allocate_memory({ov::PartialShape{1, 3, 1, 1}, data_types::f32, format::bfyx});
+ auto in_hi_mem = engine.allocate_memory({ov::PartialShape{1, 3, 1, 1}, data_types::f32, format::bfyx});
+ auto out_lo_mem = engine.allocate_memory({ov::PartialShape{1, 3, 1, 1}, data_types::f32, format::bfyx});
+ auto out_hi_mem = engine.allocate_memory({ov::PartialShape{1, 3, 1, 1}, data_types::f32, format::bfyx});
+
+ set_values<float>(in_lo_mem, {0.f, 0.f, 0.f});
+ set_values<float>(in_hi_mem, {1.f, 2.f, 3.f});
+ set_values<float>(out_lo_mem, {0.f, 0.f, 0.f});
+ set_values<float>(out_hi_mem, {255.f, 255.f, 255.f});
+
+ topology topology;
+ topology.add(input_layout("input", in_layout));
+ topology.add(data("weights", weights_mem));
+ topology.add(fully_connected("fc", input_info("input"), "weights"));
+ topology.add(reshape("reshape1", input_info("fc"), true, std::vector<int64_t>{0, 0, 0, 0}, ov::PartialShape{-1, 3, 1, 1}));
+ topology.add(data("in_lo", in_lo_mem));
+ topology.add(data("in_hi", in_hi_mem));
+ topology.add(data("out_lo", out_lo_mem));
+ topology.add(data("out_hi", out_hi_mem));
+ topology.add(quantize("quantize",
+ input_info("reshape1"),
+ input_info("in_lo"),
+ input_info("in_hi"),
+ input_info("out_lo"),
+ input_info("out_hi"),
+ 256,
+ data_types::u8));
+ topology.add(reorder("output", input_info("quantize"), format::bfyx, data_types::f32));
+
+ ExecutionConfig config = get_test_default_config(engine);
+ config.set_property(ov::intel_gpu::optimize_data(true));
+ config.set_property(ov::intel_gpu::allow_new_shape_infer(true));
+ auto prog = program::build_program(engine, topology, config, false, true);
+
+ program_wrapper::apply_opt_pass<prepare_quantization>(*prog);
+ program_wrapper::apply_opt_pass<prepare_primitive_fusing_through>(*prog);
+
+ ASSERT_NE(prog, nullptr);
+ auto& quant_node = prog->get_node("quantize");
+ // With dynamic shapes, quantize should NOT have moved — stays after reshape
+ ASSERT_EQ(quant_node.get_dependency(0).id(), "reshape1");
+}