Skip to content

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

[CPU][GPU] MoveFCReshapeToWeights unification across CPU and GPU (#36711)

Vladislav Golubevf2e4a41044ca
diff --git a/src/common/transformations/include/transformations/common_optimizations/move_fc_reshape_to_weights.hpp b/src/common/transformations/include/transformations/common_optimizations/move_fc_reshape_to_weights.hpp
new file mode 100644
index 00000000000..3297432b444
--- /dev/null
+++ b/src/common/transformations/include/transformations/common_optimizations/move_fc_reshape_to_weights.hpp
@@ -0,0 +1,172 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include <algorithm>
+#include <memory>
+#include <vector>
+
+#include "openvino/core/graph_util.hpp"
+#include "openvino/core/rt_info.hpp"
+#include "openvino/core/shape.hpp"
+#include "openvino/op/constant.hpp"
+#include "openvino/op/convert.hpp"
+#include "openvino/op/multiply.hpp"
+#include "openvino/op/reshape.hpp"
+#include "openvino/op/subtract.hpp"
+#include "openvino/op/transpose.hpp"
+#include "openvino/pass/matcher_pass.hpp"
+#include "openvino/pass/pattern/op/or.hpp"
+#include "openvino/pass/pattern/op/wrap_type.hpp"
+#include "transformations_visibility.hpp"
+
+namespace ov {
+namespace pass {
+
+/**
+ * @ingroup ov_transformation_common_api
+ * @brief MoveFCReshapeToWeights is applied to FC with compressed 3D weights (e.g. u8/i4).
+ * It moves the 3D->2D Reshape on the weights path into the constant subgraph by squeezing
+ * the leading size-1 dimension out of every constant node, allowing ConstantFolding to
+ * eliminate the Reshape entirely.
+ *
+ * Weights(3D) ZP_const(3D) Weights(2D) ZP_const(2D)
+ * | / | /
+ * Convert ZP_convert(opt) Convert ZP_convert(opt)
+ * | / | /
+ * Subtract(opt) ===> Subtract(opt)
+ * | Scale_const(3D) | Scale_const(2D)
+ * \ / \ /
+ * Multiply Multiply
+ * | |
+ * Reshape(2D) |
+ * | |
+ * Data Transpose(opt) Data Transpose(opt)
+ * \ / \ /
+ * FullyConnected FullyConnected
+ */
+template <typename FCType>
+class MoveFCReshapeToWeights : public MatcherPass {
+public:
+ OPENVINO_MATCHER_PASS_RTTI("MoveFCReshapeToWeights");
+ MoveFCReshapeToWeights() {
+ using namespace ov::pass::pattern;
+
+ auto weights_m = wrap_type<ov::op::v0::Constant>(consumers_count(1));
+ auto convert_m = wrap_type<ov::op::v0::Convert>({weights_m}, consumers_count(1));
+
+ auto sub_const_m = wrap_type<ov::op::v0::Constant>(consumers_count(1));
+ auto subtract_wo_convert_m = wrap_type<ov::op::v1::Subtract>({convert_m, sub_const_m}, consumers_count(1));
+ auto sub_convert_m = wrap_type<ov::op::v0::Convert>({sub_const_m}, consumers_count(1));
+ auto subtract_w_convert_m = wrap_type<ov::op::v1::Subtract>({convert_m, sub_convert_m}, consumers_count(1));
+ auto subtract_m = std::make_shared<op::Or>(OutputVector{subtract_wo_convert_m, subtract_w_convert_m});
+
+ auto one_consumer_rank3 = [](const ov::Output<ov::Node>& out) {
+ return consumers_count(1)(out) && rank_equals(3)(out);
+ };
+ auto mul_const_m = wrap_type<ov::op::v0::Constant>(consumers_count(1));
+ auto mul_with_sub_m = wrap_type<ov::op::v1::Multiply>({subtract_m, mul_const_m}, one_consumer_rank3);
+ auto mul_no_sub_m = wrap_type<ov::op::v1::Multiply>({convert_m, mul_const_m}, one_consumer_rank3);
+ auto mul_m = std::make_shared<op::Or>(OutputVector{mul_with_sub_m, mul_no_sub_m});
+
+ auto one_consumer_rank2 = [](const ov::Output<ov::Node>& out) {
+ return consumers_count(1)(out) && rank_equals(2)(out);
+ };
+ auto reshape_const_m = wrap_type<ov::op::v0::Constant>(consumers_count(1));
+ auto reshape_m = wrap_type<ov::op::v1::Reshape>({mul_m, reshape_const_m}, one_consumer_rank2);
+
+ auto transpose_const_m = wrap_type<ov::op::v0::Constant>();
+ auto transpose_m = wrap_type<ov::op::v1::Transpose>({reshape_m, transpose_const_m});
+ auto weights_input_m = std::make_shared<op::Or>(OutputVector{reshape_m, transpose_m});
+
+ auto data_m = any_input();
+ auto bias_m = any_input();
+ auto fully_connected_m = wrap_type<FCType>({data_m, weights_input_m, bias_m});
+
+ ov::matcher_pass_callback callback = [=](ov::pass::pattern::Matcher& m) {
+ const auto fully_connected = m.get_match_root();
+ const auto weights_path = fully_connected->get_input_node_shared_ptr(1);
+ const bool with_transpose = ov::is_type<ov::op::v1::Transpose>(weights_path);
+ if (with_transpose) {
+ const auto transpose_const =
+ ov::as_type_ptr<ov::op::v0::Constant>(weights_path->get_input_node_shared_ptr(1));
+ if (transpose_const->cast_vector<int>() != std::vector<int>{1, 0}) {
+ return false;
+ }
+ }
+
+ const auto& fc_input_shape = fully_connected->get_input_shape(1);
+ const auto reshape = with_transpose ? weights_path->get_input_node_shared_ptr(0) : weights_path;
+
+ // Accepts shapes that are exactly {1, out_channels, 1} (with the out_channels
+ // dimension at the correct index), or any shape that is all-ones / shorter rank
+ // (broadcast-compatible scalars).
+ auto check_decompression_shape = [&](const std::shared_ptr<ov::Node>& node) {
+ ov::Shape expected_shape(3, 1);
+ const size_t out_channels_idx = with_transpose ? 2 : 1;
+ expected_shape[out_channels_idx] = fc_input_shape[0];
+ const auto& node_shape = node->get_output_shape(0);
+ if (node_shape.size() > expected_shape.size()) {
+ return false;
+ }
+ const auto offset = expected_shape.size() - node_shape.size();
+ return std::equal(node_shape.begin(), node_shape.end(), expected_shape.begin() + offset) ||
+ std::all_of(node_shape.cbegin(), node_shape.cend(), [](ov::Shape::value_type d) {
+ return d == 1;
+ });
+ };
+
+ const auto mul = reshape->get_input_node_shared_ptr(0);
+ if (!check_decompression_shape(mul->get_input_node_shared_ptr(1)))
+ return false;
+
+ const auto mul_parent = mul->get_input_node_shared_ptr(0);
+ const bool with_subtract = ov::is_type<ov::op::v1::Subtract>(mul_parent);
+ if (with_subtract && !check_decompression_shape(mul_parent->get_input_node_shared_ptr(1)))
+ return false;
+
+ const auto convert = with_subtract ? mul_parent->get_input_node_shared_ptr(0) : mul_parent;
+ const auto weights = convert->get_input_node_shared_ptr(0);
+ ov::Shape expected_weights_shape(3, 1);
+ expected_weights_shape[1] = fc_input_shape[with_transpose ? 1 : 0];
+ expected_weights_shape[2] = fc_input_shape[with_transpose ? 0 : 1];
+ if (weights->get_output_shape(0) != expected_weights_shape)
+ return false;
+
+ // Only squeeze when the constant has exactly one extra leading dimension.
+ auto squeeze_constant = [&](const std::shared_ptr<ov::Node>& node) {
+ const auto constant = ov::as_type_ptr<ov::op::v0::Constant>(node);
+ OPENVINO_ASSERT(constant, "Expected Constant node, got: ", node->get_type_name());
+ auto shape = constant->get_shape();
+ if (shape.size() == fc_input_shape.size() + 1) {
+ shape.erase(shape.begin());
+ const auto new_constant = std::make_shared<ov::op::v0::Constant>(*constant, shape);
+ ov::replace_node(constant, new_constant);
+ ov::copy_runtime_info(constant, new_constant);
+ new_constant->set_friendly_name(constant->get_friendly_name());
+ }
+ };
+
+ // Remove the 3D->2D reshape by squeezing constants in the weights subgraph.
+ ov::replace_output_update_name(reshape->output(0), reshape->input_value(0));
+ squeeze_constant(mul->get_input_node_shared_ptr(1));
+ squeeze_constant(weights);
+ if (with_subtract) {
+ auto sub_const = mul_parent->get_input_node_shared_ptr(1);
+ if (ov::is_type<ov::op::v0::Convert>(sub_const)) {
+ sub_const = sub_const->get_input_node_shared_ptr(0);
+ }
+ squeeze_constant(sub_const);
+ }
+ return true;
+ };
+
+ auto matcher = std::make_shared<ov::pass::pattern::Matcher>(fully_connected_m);
+ this->register_matcher(matcher, callback);
+ }
+};
+
+} // namespace pass
+} // namespace ov
diff --git a/src/common/transformations/src/transformations/utils/extract_subgraph.cpp b/src/common/transformations/src/transformations/utils/extract_subgraph.cpp
index d2dcb920cf7..457e706f00c 100644
--- a/src/common/transformations/src/transformations/utils/extract_subgraph.cpp
+++ b/src/common/transformations/src/transformations/utils/extract_subgraph.cpp
@@ -17,8 +17,7 @@
namespace ov::util {
namespace {
-ov::SinkVector collect_sinks(const ov::OutputVector& subgraph_outputs,
- const ov::ParameterVector& subgraph_parameters) {
+ov::SinkVector collect_sinks(const ov::OutputVector& subgraph_outputs, const ov::ParameterVector& subgraph_parameters) {
std::unordered_set<ov::Node*> param_set;
for (const auto& p : subgraph_parameters) {
param_set.insert(p.get());
@@ -81,8 +80,7 @@ std::shared_ptr<ov::Model> extract_subgraph(const std::vector<ov::Input<ov::Node
"extract_subgraph: number of subgraph_parameters is not equal to the number of replaced_outputs");
auto sinks = collect_sinks(subgraph_outputs, subgraph_parameters);
- auto subgraph =
- std::make_shared<ov::Model>(subgraph_outputs, sinks, subgraph_parameters)->clone();
+ auto subgraph = std::make_shared<ov::Model>(subgraph_outputs, sinks, subgraph_parameters)->clone();
for (size_t i = 0; i < subgraph_parameters.size(); ++i) {
subgraph_parameters[i]->output(0).replace(replaced_outputs[i]);
diff --git a/src/plugins/intel_cpu/src/transformations/cpu_opset/common/pass/move_fc_reshape_to_weights.cpp b/src/plugins/intel_cpu/src/transformations/cpu_opset/common/pass/move_fc_reshape_to_weights.cpp
deleted file mode 100644
index fc52e615f41..00000000000
--- a/src/plugins/intel_cpu/src/transformations/cpu_opset/common/pass/move_fc_reshape_to_weights.cpp
+++ /dev/null
@@ -1,149 +0,0 @@
-// Copyright (C) 2018-2026 Intel Corporation
-// SPDX-License-Identifier: Apache-2.0
-//
-
-#include "move_fc_reshape_to_weights.hpp"
-
-#include <algorithm>
-#include <cstddef>
-#include <memory>
-#include <openvino/op/constant.hpp>
-#include <openvino/op/convert.hpp>
-#include <openvino/op/multiply.hpp>
-#include <openvino/op/reshape.hpp>
-#include <openvino/op/subtract.hpp>
-#include <openvino/op/transpose.hpp>
-#include <openvino/pass/pattern/op/or.hpp>
-#include <openvino/pass/pattern/op/wrap_type.hpp>
-#include <vector>
-
-#include "openvino/cc/pass/itt.hpp"
-#include "openvino/core/dimension.hpp"
-#include "openvino/core/except.hpp"
-#include "openvino/core/graph_util.hpp"
-#include "openvino/core/node.hpp"
-#include "openvino/core/node_output.hpp"
-#include "openvino/core/rt_info.hpp"
-#include "openvino/core/shape.hpp"
-#include "openvino/core/type.hpp"
-#include "openvino/pass/matcher_pass.hpp"
-#include "openvino/pass/pattern/matcher.hpp"
-#include "openvino/pass/pattern/op/label.hpp"
-#include "openvino/pass/pattern/op/pattern.hpp"
-#include "ov_ops/fully_connected.hpp"
-
-ov::intel_cpu::MoveFCReshapeToWeights::MoveFCReshapeToWeights() {
- MATCHER_SCOPE(MoveFCReshapeToWeights);
- using namespace ov::pass::pattern;
- auto weights_m = wrap_type<ov::op::v0::Constant>(consumers_count(1));
- auto convert_m = wrap_type<ov::op::v0::Convert>({weights_m}, consumers_count(1));
-
- auto one_consumer_rank_equals = [](const ov::Dimension& expected_rank) {
- return [=](const ov::Output<ov::Node>& output) -> bool {
- return consumers_count(1)(output) && rank_equals(expected_rank)(output);
- };
- };
-
- auto sub_const_m = wrap_type<ov::op::v0::Constant>(consumers_count(1));
- auto subtract_wo_convert_m = wrap_type<ov::op::v1::Subtract>({convert_m, sub_const_m}, consumers_count(1));
- auto sub_convert = wrap_type<ov::op::v0::Convert>({sub_const_m}, consumers_count(1));
- auto subtract_w_convert_m = wrap_type<ov::op::v1::Subtract>({convert_m, sub_convert}, consumers_count(1));
- auto subtract_m =
- std::make_shared<ov::pass::pattern::op::Or>(OutputVector{subtract_wo_convert_m, subtract_w_convert_m});
-
- auto mul_const_m = wrap_type<ov::op::v0::Constant>(consumers_count(1));
- auto mul_with_sub_m = wrap_type<ov::op::v1::Multiply>({subtract_m, mul_const_m}, one_consumer_rank_equals(3));
- auto mul_no_sub_m = wrap_type<ov::op::v1::Multiply>({convert_m, mul_const_m}, one_consumer_rank_equals(3));
- auto mul_m = std::make_shared<ov::pass::pattern::op::Or>(OutputVector{mul_with_sub_m, mul_no_sub_m});
-
- auto reshape_const_m = wrap_type<ov::op::v0::Constant>(consumers_count(1));
- auto reshape_m = wrap_type<ov::op::v1::Reshape>({mul_m, reshape_const_m}, one_consumer_rank_equals(2));
-
- auto transpose_const_m = wrap_type<ov::op::v0::Constant>();
- auto transpose_m = wrap_type<ov::op::v1::Transpose>({reshape_m, transpose_const_m});
- auto weights_input_m = std::make_shared<ov::pass::pattern::op::Or>(ov::OutputVector{reshape_m, transpose_m});
-
- auto data_m = any_input();
- auto bias_m = any_input();
- auto fully_connected_m = wrap_type<ov::op::internal::FullyConnected>({data_m, weights_input_m, bias_m});
-
- ov::matcher_pass_callback callback = [&](ov::pass::pattern::Matcher& m) {
- const auto fully_connected = m.get_match_root();
- const auto weights_path = fully_connected->get_input_node_shared_ptr(1);
- const bool with_transpose = ov::is_type<ov::op::v1::Transpose>(weights_path);
- if (with_transpose) {
- const auto transpose_const =
- ov::as_type_ptr<ov::op::v0::Constant>(weights_path->get_input_node_shared_ptr(1));
- if (transpose_const->cast_vector<int>() != std::vector<int>{1, 0}) {
- return false;
- }
- }
-
- const auto& fc_input_shape = fully_connected->get_input_shape(1);
- const auto reshape = with_transpose ? weights_path->get_input_node_shared_ptr(0) : weights_path;
-
- auto check_decompression_shape = [&](const std::shared_ptr<ov::Node>& node) {
- ov::Shape expected_shape(3, 1);
- const size_t out_channels_idx = with_transpose ? 2 : 1;
- expected_shape[out_channels_idx] = fc_input_shape[0];
- const auto& node_shape = node->get_output_shape(0);
- if (node_shape.size() > expected_shape.size()) {
- return false;
- }
-
- const auto comparison_start_pos = expected_shape.size() - node_shape.size();
- return std::equal(node_shape.begin(), node_shape.end(), expected_shape.begin() + comparison_start_pos) ||
- std::all_of(node_shape.cbegin(), node_shape.cend(), [](ov::Shape::value_type dim) {
- return dim == 1;
- });
- };
-
- const auto mul = reshape->get_input_node_shared_ptr(0);
- if (!check_decompression_shape(mul->get_input_node_shared_ptr(1))) {
- return false;
- }
- const auto mul_parent = mul->get_input_node_shared_ptr(0);
- const bool with_subtract = ov::is_type<ov::op::v1::Subtract>(mul_parent);
- if (with_subtract && !check_decompression_shape(mul_parent->get_input_node_shared_ptr(1))) {
- return false;
- }
-
- const auto convert = with_subtract ? mul_parent->get_input_node_shared_ptr(0) : mul_parent;
- const auto weights = convert->get_input_node_shared_ptr(0);
- ov::Shape expected_weights_shape(3, 1);
- expected_weights_shape[1] = fc_input_shape[with_transpose ? 1 : 0];
- expected_weights_shape[2] = fc_input_shape[with_transpose ? 0 : 1];
- if (weights->get_output_shape(0) != expected_weights_shape) {
- return false;
- }
-
- auto squeeze_constant = [&](const std::shared_ptr<ov::Node>& node) {
- const auto constant = ov::as_type_ptr<ov::op::v0::Constant>(node);
- OPENVINO_ASSERT(constant, "squeeze_constant is called for non constant node");
- auto shape = constant->get_shape();
- if (shape.size() - fc_input_shape.size() == 1) {
- shape.erase(shape.begin());
- const auto new_constant = std::make_shared<ov::op::v0::Constant>(*constant, shape);
- ov::replace_node(constant, new_constant);
- ov::copy_runtime_info(constant, new_constant);
- new_constant->set_friendly_name(constant->get_friendly_name());
- }
- };
-
- // We can remove 3D->2D reshape if we manually reshape all constants in the weights subgraph
- ov::replace_output_update_name(reshape->output(0), reshape->input_value(0));
- squeeze_constant(mul->get_input_node_shared_ptr(1));
- squeeze_constant(weights);
- if (with_subtract) {
- auto sub_const = mul_parent->get_input_node_shared_ptr(1);
- if (ov::is_type<ov::op::v0::Convert>(sub_const)) {
- sub_const = sub_const->get_input_node_shared_ptr(0);
- }
- squeeze_constant(sub_const);
- }
- return true;
- };
-
- auto m = std::make_shared<ov::pass::pattern::Matcher>(fully_connected_m, matcher_name);
- this->register_matcher(m, callback);
-}
diff --git a/src/plugins/intel_cpu/src/transformations/cpu_opset/common/pass/move_fc_reshape_to_weights.hpp b/src/plugins/intel_cpu/src/transformations/cpu_opset/common/pass/move_fc_reshape_to_weights.hpp
deleted file mode 100644
index 48244871850..00000000000
--- a/src/plugins/intel_cpu/src/transformations/cpu_opset/common/pass/move_fc_reshape_to_weights.hpp
+++ /dev/null
@@ -1,31 +0,0 @@
-// Copyright (C) 2018-2026 Intel Corporation
-// SPDX-License-Identifier: Apache-2.0
-//
-
-#pragma once
-
-#include "openvino/pass/matcher_pass.hpp"
-
-namespace ov::intel_cpu {
-
-/**
- * This transformation is applied to the FC with compressed 3D u8 weights. It moves Reshape at the weights path to the
- * constants in order to constant fold the Reshape node. Example: Weights(3D) Subtract_const(3D) Weights(2D)
- * Subtract_const(2D) | / | / Convert
- * Subtract_convert(opt) Convert Subtract_convert(opt) | / | / Subtract(opt)
- * Subtract(opt) | Multiply_const(3D) ====> | Multiply_const(2D) | / | /
- * Multiply Multiply
- * | |
- * Reshape(2D) |
- * | |
- * Data Transpose(opt) Data Transpose(opt)
- * \ / \ /
- * FullyConnected FullyConnected
- */
-class MoveFCReshapeToWeights : public ov::pass::MatcherPass {
-public:
- OPENVINO_MATCHER_PASS_RTTI("MoveFCReshapeToWeights");
- MoveFCReshapeToWeights();
-};
-
-} // namespace ov::intel_cpu
diff --git a/src/plugins/intel_cpu/src/transformations/cpu_opset/convert_to_cpu_specific_opset.hpp b/src/plugins/intel_cpu/src/transformations/cpu_opset/convert_to_cpu_specific_opset.hpp
index e224c070296..fd019b1eec2 100644
--- a/src/plugins/intel_cpu/src/transformations/cpu_opset/convert_to_cpu_specific_opset.hpp
+++ b/src/plugins/intel_cpu/src/transformations/cpu_opset/convert_to_cpu_specific_opset.hpp
@@ -14,7 +14,6 @@
#include "common/pass/convert_to_power_static.hpp"
#include "common/pass/convert_to_swish_cpu.hpp"
#include "common/pass/fc_bias_fusion.hpp"
-#include "common/pass/move_fc_reshape_to_weights.hpp"
#include "common/pass/move_readvalue_inputs_to_subgraph.hpp"
#include "common/pass/rnn_sequences_optimization.hpp"
#include "config.h"
@@ -28,6 +27,7 @@
#include "openvino/pass/validate.hpp"
#include "ov_ops/fully_connected.hpp"
#include "transformations/common_optimizations/convert_tiled_moe_block_to_gather_matmuls.hpp"
+#include "transformations/common_optimizations/move_fc_reshape_to_weights.hpp"
#include "transformations/common_optimizations/nop_elimination.hpp"
#include "transformations/common_optimizations/reshape_sequence_fusion.hpp"
#include "transformations/common_optimizations/transpose_to_reshape.hpp"
@@ -91,7 +91,7 @@ inline void ConvertToCPUSpecificOpset(std::shared_ptr<ov::Model>& model, const C
});
CPU_REGISTER_PASS_X64(manager, pass::ConvertFCToFCQuantizedLegacy);
- CPU_REGISTER_PASS_COMMON(manager, MoveFCReshapeToWeights);
+ CPU_REGISTER_PASS_COMMON(manager, ov::pass::MoveFCReshapeToWeights<ov::op::internal::FullyConnected>);
CPU_REGISTER_PASS_COMMON(manager, AlignMatMulInputRanks);
CPU_REGISTER_PASS_COMMON(manager, ConvertTileToSeqTiles);
CPU_REGISTER_PASS_COMMON(manager, ConvertToPowerStatic);
diff --git a/src/plugins/intel_cpu/tests/unit/transformations/move_fc_reshape_to_weights.cpp b/src/plugins/intel_cpu/tests/unit/transformations/move_fc_reshape_to_weights.cpp
index 130344797f3..af9ea962aa4 100644
--- a/src/plugins/intel_cpu/tests/unit/transformations/move_fc_reshape_to_weights.cpp
+++ b/src/plugins/intel_cpu/tests/unit/transformations/move_fc_reshape_to_weights.cpp
@@ -2,16 +2,13 @@
// SPDX-License-Identifier: Apache-2.0
//
-#include <transformations/cpu_opset/common/pass/move_fc_reshape_to_weights.hpp>
+#include "transformations/common_optimizations/move_fc_reshape_to_weights.hpp"
#include <gtest/gtest.h>
-#include <string>
#include <memory>
-
#include <openvino/core/model.hpp>
-#include "openvino/opsets/opset1_decl.hpp"
-#include "ov_ops/fully_connected.hpp"
+#include <string>
#include <transformations/init_node_info.hpp>
#include <transformations/utils/utils.hpp>
@@ -20,9 +17,10 @@
#include "openvino/op/reshape.hpp"
#include "openvino/op/subtract.hpp"
#include "openvino/op/transpose.hpp"
+#include "openvino/opsets/opset1_decl.hpp"
+#include "ov_ops/fully_connected.hpp"
using namespace testing;
-using namespace ov::intel_cpu;
enum class ZeroPointType : uint8_t { NO_ZP, ZP_WEIGHTS_PRC, ZP_DECOMPRESSION_PRC };
inline std::ostream& operator<<(std::ostream& os, ZeroPointType type) {
@@ -131,7 +129,7 @@ protected:
ref_weights_shape.erase(ref_weights_shape.begin());
model = initModel(input_shapes.first, input_shapes.second, add_transpose, zp_type, zp_shape, true);
model_ref = initModel(input_shapes.first, ref_weights_shape, add_transpose, zp_type, zp_shape, false);
- manager.register_pass<MoveFCReshapeToWeights>();
+ manager.register_pass<ov::pass::MoveFCReshapeToWeights<ov::op::internal::FullyConnected>>();
}
};
diff --git a/src/plugins/intel_gpu/src/plugin/transformations/move_fc_reshape_to_weights.cpp b/src/plugins/intel_gpu/src/plugin/transformations/move_fc_reshape_to_weights.cpp
deleted file mode 100644
index d96296a63ae..00000000000
--- a/src/plugins/intel_gpu/src/plugin/transformations/move_fc_reshape_to_weights.cpp
+++ /dev/null
@@ -1,111 +0,0 @@
-// Copyright (C) 2018-2026 Intel Corporation
-// SPDX-License-Identifier: Apache-2.0
-//
-
-#include "move_fc_reshape_to_weights.hpp"
-#include "intel_gpu/op/fully_connected.hpp"
-#include "transformations/utils/utils.hpp"
-#include "openvino/pass/pattern/op/wrap_type.hpp"
-#include "openvino/pass/pattern/op/or.hpp"
-
-#include "openvino/op/matmul.hpp"
-#include "openvino/op/constant.hpp"
-#include "openvino/op/convert.hpp"
-#include "openvino/op/subtract.hpp"
-#include "openvino/op/multiply.hpp"
-#include "openvino/op/transpose.hpp"
-#include "openvino/op/reshape.hpp"
-#include "openvino/core/graph_util.hpp"
-
-namespace ov::intel_gpu {
-
-MoveFCReshapeToWeights::MoveFCReshapeToWeights() {
- using namespace ov::pass::pattern;
-
- auto weights_m = wrap_type<ov::op::v0::Constant>(consumers_count(1));
- auto convert_m = wrap_type<ov::op::v0::Convert>({weights_m});
-
- auto sub_const_m = wrap_type<ov::op::v0::Constant>(consumers_count(1));
- auto subtract_m = wrap_type<ov::op::v1::Subtract>({convert_m, sub_const_m});
-
- auto mul_const_m = wrap_type<ov::op::v0::Constant>(consumers_count(1));
- auto mul_with_sub_m = wrap_type<ov::op::v1::Multiply>({subtract_m, mul_const_m}, rank_equals(3));
- auto mul_no_sub_m = wrap_type<ov::op::v1::Multiply>({convert_m, mul_const_m}, rank_equals(3));
- auto mul_m = std::make_shared<ov::pass::pattern::op::Or>(OutputVector{mul_with_sub_m, mul_no_sub_m});
-
- auto one_consumer_rank_2 = [](const ov::Output<ov::Node>& out) {
- return consumers_count(1)(out) && rank_equals(2)(out);
- };
- auto reshape_const_m = wrap_type<ov::op::v0::Constant>(consumers_count(1));
- auto reshape_m = wrap_type<ov::op::v1::Reshape>({mul_m, reshape_const_m}, one_consumer_rank_2);
-
- auto transpose_const_m = wrap_type<ov::op::v0::Constant>();
- auto transpose_m = wrap_type<ov::op::v1::Transpose>({reshape_m, transpose_const_m});
- auto weights_input_m = std::make_shared<ov::pass::pattern::op::Or>(ov::OutputVector{reshape_m, transpose_m});
-
- auto data_m = any_input();
- auto fully_connected_m = wrap_type<op::FullyConnected>({data_m, weights_input_m, any_input()});
-
- ov::matcher_pass_callback callback = [&](ov::pass::pattern::Matcher& m) {
- const auto fully_connected = m.get_match_root();
- const auto weights_path = fully_connected->get_input_node_shared_ptr(1);
- const bool with_transpose = ov::is_type<ov::op::v1::Transpose>(weights_path);
- if (with_transpose) {
- const auto transpose_const = ov::as_type_ptr<ov::op::v0::Constant>(weights_path->get_input_node_shared_ptr(1));
- if (transpose_const->cast_vector<int>() != std::vector<int>{1, 0}) {
- return false;
- }
- }
-
- const auto& fc_input_shape = fully_connected->get_input_shape(1);
- const auto reshape = with_transpose ? weights_path->get_input_node_shared_ptr(0) : weights_path;
-
- auto check_decompression_const = [&](const std::shared_ptr<ov::Node>& node) {
- if (!ov::is_type<ov::op::v0::Constant>(node))
- return false;
- ov::Shape expected_shape(3, 1);
- const size_t out_channels_idx = with_transpose ? 2 : 1;
- expected_shape[out_channels_idx] = fc_input_shape[0];
- return node->get_output_shape(0) == expected_shape;
- };
-
- const auto mul = reshape->get_input_node_shared_ptr(0);
- if (!check_decompression_const(mul->get_input_node_shared_ptr(1)))
- return false;
- const auto mul_parent = mul->get_input_node_shared_ptr(0);
- const bool with_subtract = ov::is_type<ov::op::v1::Subtract>(mul_parent);
- if (with_subtract && !check_decompression_const(mul_parent->get_input_node_shared_ptr(1)))
- return false;
-
- const auto convert = with_subtract ? mul_parent->get_input_node_shared_ptr(0) : mul_parent;
- const auto weights = convert->get_input_node_shared_ptr(0);
- ov::Shape expected_weights_shape(3, 1);
- expected_weights_shape[1] = fc_input_shape[with_transpose ? 1 : 0];
- expected_weights_shape[2] = fc_input_shape[with_transpose ? 0 : 1];
- if (weights->get_output_shape(0) != expected_weights_shape)
- return false;
-
- auto squeeze_constant = [](const std::shared_ptr<ov::Node>& node) {
- const auto constant = ov::as_type_ptr<ov::op::v0::Constant>(node);
- auto shape = constant->get_shape();
- shape.erase(shape.begin());
- const auto new_constant = std::make_shared<ov::op::v0::Constant>(*constant, shape);
- ov::replace_node(constant, new_constant);
- ov::copy_runtime_info(constant, new_constant);
- new_constant->set_friendly_name(constant->get_friendly_name());
- };
-
- // We can remove 3D->2D reshape if we manually reshape all constants in the weights subgraph
- ov::replace_output_update_name(reshape->output(0), reshape->input_value(0));
- squeeze_constant(mul->get_input_node_shared_ptr(1));
- squeeze_constant(weights);
- if (with_subtract)
- squeeze_constant(mul_parent->get_input_node_shared_ptr(1));
- return true;
- };
-
- auto m = std::make_shared<ov::pass::pattern::Matcher>(fully_connected_m);
- this->register_matcher(m, callback);
-}
-
-} // namespace ov::intel_gpu
diff --git a/src/plugins/intel_gpu/src/plugin/transformations/move_fc_reshape_to_weights.hpp b/src/plugins/intel_gpu/src/plugin/transformations/move_fc_reshape_to_weights.hpp
deleted file mode 100644
index dd443749a7f..00000000000
--- a/src/plugins/intel_gpu/src/plugin/transformations/move_fc_reshape_to_weights.hpp
+++ /dev/null
@@ -1,36 +0,0 @@
-// Copyright (C) 2018-2026 Intel Corporation
-// SPDX-License-Identifier: Apache-2.0
-//
-
-#pragma once
-
-#include "openvino/pass/graph_rewrite.hpp"
-
-namespace ov::intel_gpu {
-
-/**
- * This transformation is applied to the FC with compressed 3D u8 weights. It moves Reshape at the weights path to the constants
- * in order to constant fold the Reshape node.
- * Example:
- * Weights(3D) Weights(2D)
- * | |
- * Convert Subtract_const(3D) Convert Subtract_const(2D)
- * | / | /
- * Subtract(optional) Subtract(optional)
- * | Multiply_const(3D) ====> | Multiply_const(2D)
- * | / | /
- * Multiply Multiply
- * | |
- * Reshape(2D) |
- * | |
- * Data Transpose(optional) Data Transpose(optional)
- * \ / \ /
- * FullyConnected FullyConnected
- */
-class MoveFCReshapeToWeights: public ov::pass::MatcherPass {
-public:
- OPENVINO_MATCHER_PASS_RTTI("MoveFCReshapeToWeights");
- MoveFCReshapeToWeights();
-};
-
-} // namespace ov::intel_gpu
diff --git a/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp b/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
index 9a098deb653..79b317d4bd1 100644
--- a/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
+++ b/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
@@ -106,7 +106,8 @@
#include "plugin/transformations/kv_cache_fusion.hpp"
#include "plugin/transformations/lora_horizontal_fusion.hpp"
#include "plugin/transformations/lora_subgraph_horizontal_fusion.hpp"
-#include "plugin/transformations/move_fc_reshape_to_weights.hpp"
+#include "intel_gpu/op/fully_connected.hpp"
+#include "transformations/common_optimizations/move_fc_reshape_to_weights.hpp"
#include "plugin/transformations/optimize_subsequent_reshapes.hpp"
#include "plugin/transformations/print_model_statistics.hpp"
#include "plugin/transformations/reduce_fc_dimensions.hpp"
@@ -1596,7 +1597,7 @@ void TransformationsPipeline::apply(std::shared_ptr<ov::Model> func) {
manager.register_pass<ov::intel_gpu::IncreaseRMSInputPrecision>();
manager.register_pass<ov::intel_gpu::ClampFP16Output>();
manager.register_pass<ov::intel_gpu::ConvertMatMulToFullyConnected>(device_info.supports_immad);
- manager.register_pass<ov::intel_gpu::MoveFCReshapeToWeights>();
+ manager.register_pass<ov::pass::MoveFCReshapeToWeights<ov::intel_gpu::op::FullyConnected>>();
if (!device_info.supports_immad) {
manager.register_pass<ov::intel_gpu::ReduceFCDimensions>();
}
diff --git a/src/plugins/intel_gpu/tests/unit/transformations/move_fc_reshape_to_weights.cpp b/src/plugins/intel_gpu/tests/unit/transformations/move_fc_reshape_to_weights.cpp
index 615f979b81b..c1f7448ef71 100644
--- a/src/plugins/intel_gpu/tests/unit/transformations/move_fc_reshape_to_weights.cpp
+++ b/src/plugins/intel_gpu/tests/unit/transformations/move_fc_reshape_to_weights.cpp
@@ -2,25 +2,24 @@
// SPDX-License-Identifier: Apache-2.0
//
-#include <plugin/transformations/move_fc_reshape_to_weights.hpp>
+#include "transformations/common_optimizations/move_fc_reshape_to_weights.hpp"
#include <gtest/gtest.h>
-#include <string>
+#include <intel_gpu/op/placeholder.hpp>
#include <memory>
-
#include <openvino/core/model.hpp>
-#include "openvino/opsets/opset1_decl.hpp"
-#include <intel_gpu/op/fully_connected.hpp>
-#include <intel_gpu/op/placeholder.hpp>
+#include <string>
#include <transformations/init_node_info.hpp>
#include <transformations/utils/utils.hpp>
#include "common_test_utils/ov_test_utils.hpp"
+#include "intel_gpu/op/fully_connected.hpp"
#include "openvino/op/multiply.hpp"
#include "openvino/op/reshape.hpp"
#include "openvino/op/subtract.hpp"
#include "openvino/op/transpose.hpp"
+#include "openvino/opsets/opset1_decl.hpp"
using namespace testing;
using namespace ov::intel_gpu;
@@ -73,7 +72,7 @@ public:
auto transpose_const = ov::opset1::Constant::create(ov::element::i32, {2}, {1, 0});
weights_path = std::make_shared<ov::opset1::Transpose>(weights_path, transpose_const);
}
- auto no_bias = std::make_shared<ov::intel_gpu::op::Placeholder>();
+ auto no_bias = std::make_shared<op::Placeholder>();
auto fully_connected = std::make_shared<op::FullyConnected>(data, weights_path, no_bias);
return std::make_shared<ov::Model>(ov::OutputVector{fully_connected}, ov::ParameterVector{data});
@@ -89,7 +88,7 @@ protected:
ref_weights_shape.erase(ref_weights_shape.begin());
model = init_model(input_shapes.first, input_shapes.second, add_transpose, add_subtract, true);
model_ref = init_model(input_shapes.first, ref_weights_shape, add_transpose, add_subtract, false);
- manager.register_pass<MoveFCReshapeToWeights>();
+ manager.register_pass<ov::pass::MoveFCReshapeToWeights<op::FullyConnected>>();
}
};