Skip to content

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

[GPU] Selectively keep FullyConnected weights non-transposed for large matmuls (#36437)

Chon Ming Leecbdb5d5eca7d
diff --git a/src/plugins/intel_gpu/include/intel_gpu/op/fully_connected.hpp b/src/plugins/intel_gpu/include/intel_gpu/op/fully_connected.hpp
index 2ffb5284718..75f9512f199 100644
--- a/src/plugins/intel_gpu/include/intel_gpu/op/fully_connected.hpp
+++ b/src/plugins/intel_gpu/include/intel_gpu/op/fully_connected.hpp
@@ -18,7 +18,8 @@ public:
FullyConnected(const ov::Output<Node>& A,
const ov::Output<Node>& B,
const ov::Output<Node>& bias,
- const ov::element::Type output_type = ov::element::dynamic);
+ const ov::element::Type output_type = ov::element::dynamic,
+ const bool transpose_b = true);
bool visit_attributes(ov::AttributeVisitor &visitor) override;
@@ -28,8 +29,11 @@ public:
ov::element::Type get_output_type() const { return m_output_type; }
+ bool get_transpose_b() const { return m_transpose_b; }
+
protected:
ov::element::Type m_output_type;
+ bool m_transpose_b = true;
};
} // namespace ov::intel_gpu::op
diff --git a/src/plugins/intel_gpu/include/intel_gpu/op/fully_connected_compressed.hpp b/src/plugins/intel_gpu/include/intel_gpu/op/fully_connected_compressed.hpp
index ff8e2b6e91c..3755732c274 100644
--- a/src/plugins/intel_gpu/include/intel_gpu/op/fully_connected_compressed.hpp
+++ b/src/plugins/intel_gpu/include/intel_gpu/op/fully_connected_compressed.hpp
@@ -22,20 +22,23 @@ public:
const ov::Output<Node>& a_decompression_scale,
const ov::Output<Node>& a_decompression_zero_point,
const ov::Output<Node>& a_precomputed_reduction,
- const ov::element::Type output_type = ov::element::dynamic);
+ const ov::element::Type output_type = ov::element::dynamic,
+ const bool transpose_b = true);
FullyConnectedCompressed(const ov::Output<Node>& A,
const ov::Output<Node>& B,
const ov::Output<Node>& bias,
const ov::Output<Node>& w_decompression_scale,
const ov::Output<Node>& w_decompression_zero_point,
- const ov::element::Type output_type = ov::element::dynamic);
+ const ov::element::Type output_type = ov::element::dynamic,
+ const bool transpose_b = true);
FullyConnectedCompressed(const ov::Output<Node>& A,
const ov::Output<Node>& B,
const ov::Output<Node>& bias,
const ov::Output<Node>& w_decompression_scale,
- const ov::element::Type output_type = ov::element::dynamic);
+ const ov::element::Type output_type = ov::element::dynamic,
+ const bool transpose_b = true);
std::shared_ptr<Node> clone_with_new_inputs(const ov::OutputVector& new_args) const override;
};
diff --git a/src/plugins/intel_gpu/include/intel_gpu/primitives/fully_connected.hpp b/src/plugins/intel_gpu/include/intel_gpu/primitives/fully_connected.hpp
index 5c9b10da2e4..8dcf1835619 100644
--- a/src/plugins/intel_gpu/include/intel_gpu/primitives/fully_connected.hpp
+++ b/src/plugins/intel_gpu/include/intel_gpu/primitives/fully_connected.hpp
@@ -46,12 +46,14 @@ struct fully_connected : public primitive_base<fully_connected> {
const primitive_id& weights,
const primitive_id& bias = "",
const size_t input_size = 2,
- const size_t weights_rank = 2)
+ const size_t weights_rank = 2,
+ const bool weights_transposed = true)
: primitive_base(id, {input}),
weights(weights),
bias(bias),
input_size(input_size),
- weights_rank(weights_rank)
+ weights_rank(weights_rank),
+ weights_transposed(weights_transposed)
{}
/// @brief Constructs fully connected layer.
@@ -65,12 +67,14 @@ struct fully_connected : public primitive_base<fully_connected> {
const primitive_id& bias,
const data_types data_type,
const size_t input_size = 2,
- const size_t weights_rank = 2)
+ const size_t weights_rank = 2,
+ const bool weights_transposed = true)
: primitive_base(id, { input }, 1, {optional_data_type{data_type}}),
weights(weights),
bias(bias),
input_size(input_size),
- weights_rank(weights_rank)
+ weights_rank(weights_rank),
+ weights_transposed(weights_transposed)
{}
/// @brief Constructs fully connected compressed layer.
@@ -88,7 +92,8 @@ struct fully_connected : public primitive_base<fully_connected> {
const primitive_id& decompression_zero_point,
const data_types data_type,
const size_t input_size = 2,
- const size_t weights_rank = 2)
+ const size_t weights_rank = 2,
+ const bool weights_transposed = true)
: primitive_base(id, { input }, 1, {optional_data_type{data_type}}),
weights(weights),
bias(bias),
@@ -98,7 +103,8 @@ struct fully_connected : public primitive_base<fully_connected> {
dynamic_quantized_activation(false),
dynamic_quantized_activation_zp(false),
input_size(input_size),
- weights_rank(weights_rank) {
+ weights_rank(weights_rank),
+ weights_transposed(weights_transposed) {
OPENVINO_ASSERT(!decompression_scale.empty(), "[GPU] Compressed fully connected requires at least decompression scale input");
}
@@ -123,7 +129,8 @@ struct fully_connected : public primitive_base<fully_connected> {
const input_info& activation_precomputed_reduction,
const data_types data_type,
const size_t input_size = 2,
- const size_t weights_rank = 2)
+ const size_t weights_rank = 2,
+ const bool weights_transposed = true)
: primitive_base(id, { input }, 1, {optional_data_type{data_type}}),
weights(weights),
bias(bias),
@@ -136,7 +143,8 @@ struct fully_connected : public primitive_base<fully_connected> {
activation_zero_point(activation_zero_point),
activation_precomputed_reduction(activation_precomputed_reduction),
input_size(input_size),
- weights_rank(weights_rank) {
+ weights_rank(weights_rank),
+ weights_transposed(weights_transposed) {
if (activation_scale.is_valid())
dynamic_quantized_activation = true;
if (activation_zero_point.is_valid())
@@ -167,11 +175,15 @@ struct fully_connected : public primitive_base<fully_connected> {
size_t input_size = 2;
/// @brief Primitive weights rank.
size_t weights_rank = 2;
+ /// @brief Weights transposed.
+ bool weights_transposed = true;
+
size_t hash() const override {
size_t seed = primitive::hash();
seed = hash_combine(seed, input_size);
seed = hash_combine(seed, weights_rank);
+ seed = hash_combine(seed, weights_transposed);
seed = hash_combine(seed, bias.is_valid());
seed = hash_combine(seed, compressed_weights);
seed = hash_combine(seed, !decompression_scale.is_valid());
@@ -192,6 +204,7 @@ struct fully_connected : public primitive_base<fully_connected> {
return input_size == rhs_casted.input_size &&
weights_rank == rhs_casted.weights_rank &&
+ weights_transposed == rhs_casted.weights_transposed &&
bias.is_valid() == rhs_casted.bias.is_valid() &&
compressed_weights == rhs_casted.compressed_weights &&
decompression_scale.is_valid() == rhs_casted.decompression_scale.is_valid() &&
@@ -214,6 +227,7 @@ struct fully_connected : public primitive_base<fully_connected> {
ob << activation_precomputed_reduction;
ob << input_size;
ob << weights_rank;
+ ob << weights_transposed;
ob << dynamic_quantized_activation;
ob << dynamic_quantized_activation_zp;
ob << dynamic_quantized_precomputed_reduction;
@@ -239,6 +253,7 @@ struct fully_connected : public primitive_base<fully_connected> {
ib >> activation_precomputed_reduction;
ib >> input_size;
ib >> weights_rank;
+ ib >> weights_transposed;
ib >> dynamic_quantized_activation;
ib >> dynamic_quantized_activation_zp;
ib >> dynamic_quantized_precomputed_reduction;
diff --git a/src/plugins/intel_gpu/src/graph/fully_connected.cpp b/src/plugins/intel_gpu/src/graph/fully_connected.cpp
index f0f6f44e876..38d4e149efd 100644
--- a/src/plugins/intel_gpu/src/graph/fully_connected.cpp
+++ b/src/plugins/intel_gpu/src/graph/fully_connected.cpp
@@ -111,10 +111,14 @@ layout fully_connected_inst::calc_output_layout(fully_connected_node const& node
const auto supports_immad = node.get_program().get_engine().get_device_info().supports_immad;
- auto reshape_to_2d = [](const ov::PartialShape& shape, int64_t feature) {
+ auto reshape_to_2d = [&](const ov::PartialShape& shape, int64_t feature) {
auto staticShape = shape.to_shape();
size_t total = std::accumulate(staticShape.begin(), staticShape.end(), static_cast<size_t>(1), std::multiplies<size_t>());
- std::vector<int64_t> reshapeSize = { static_cast<int64_t>(total) / feature, feature };
+ std::vector<int64_t> reshapeSize;
+ if (desc->weights_transposed)
+ reshapeSize = { static_cast<int64_t>(total) / feature, feature };
+ else
+ reshapeSize = { feature, static_cast<int64_t>(total) / feature };
return reshapeSize;
};
@@ -130,16 +134,17 @@ layout fully_connected_inst::calc_output_layout(fully_connected_node const& node
}
if (supports_immad) {
- ov::PartialShape out_pshape = {input_layout.batch(), weights_layout.batch(), 1, 1};
+ auto out_features = desc->weights_transposed ? weights_layout.batch() : weights_layout.feature();
+ ov::PartialShape out_pshape = {input_layout.batch(), out_features, 1, 1};
if (desc->input_size == 3) {
- out_pshape = {input_layout.batch(), input_layout.feature(), weights_layout.batch(), 1};
+ out_pshape = {input_layout.batch(), input_layout.feature(), out_features, 1};
} else if (desc->input_size == 4) {
- out_pshape = {input_layout.batch(), input_layout.feature(), input_layout.spatial(1), weights_layout.batch()};
+ out_pshape = {input_layout.batch(), input_layout.feature(), input_layout.spatial(1), out_features};
} else if (desc->input_size == 5) {
- out_pshape = {input_layout.batch(), input_layout.feature(), input_layout.spatial(2), input_layout.spatial(1), weights_layout.batch()};
+ out_pshape = {input_layout.batch(), input_layout.feature(), input_layout.spatial(2), input_layout.spatial(1), out_features};
} else if (desc->input_size == 6) {
out_pshape = {input_layout.batch(), input_layout.feature(), input_layout.spatial(3),
- input_layout.spatial(2), input_layout.spatial(1), weights_layout.batch()};
+ input_layout.spatial(2), input_layout.spatial(1), out_features};
}
format output_format = get_preferred_format(node, impl_param);
@@ -150,13 +155,14 @@ layout fully_connected_inst::calc_output_layout(fully_connected_node const& node
input_layout.set_partial_shape(reshape_to_2d(input_pshape, feature));
}
- auto output_size = tensor(input_layout.batch(), weights_layout.batch(), 1, 1);
+ auto out_features = desc->weights_transposed ? weights_layout.batch() : weights_layout.feature();
+ auto output_size = tensor(input_layout.batch(), out_features, 1, 1);
if (desc->input_size == 3) {
- output_size = tensor(input_layout.batch(), input_layout.feature(), 1, weights_layout.batch());
+ output_size = tensor(input_layout.batch(), input_layout.feature(), 1, out_features);
} else if (desc->input_size == 4) {
- output_size = tensor(input_layout.batch(), input_layout.feature(), weights_layout.batch(), input_layout.spatial(1));
+ output_size = tensor(input_layout.batch(), input_layout.feature(), out_features, input_layout.spatial(1));
} else if (desc->input_size == 5) {
- output_size = tensor(input_layout.batch(), input_layout.feature(), weights_layout.batch(), input_layout.spatial(1), input_layout.spatial(2));
+ output_size = tensor(input_layout.batch(), input_layout.feature(), out_features, input_layout.spatial(1), input_layout.spatial(2));
}
format output_format = get_preferred_format(node, impl_param);
@@ -180,7 +186,7 @@ std::vector<layout> fully_connected_inst::calc_output_layouts(fully_connected_no
}
ov::op::v0::MatMul matmul_op;
- matmul_op.set_transpose_b(true);
+ matmul_op.set_transpose_b(desc->weights_transposed);
std::vector<ShapeType> input_shapes = {
input_layout.get<ShapeType>(),
weights_layout.get<ShapeType>()
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl/fully_connected.cpp b/src/plugins/intel_gpu/src/graph/impls/ocl/fully_connected.cpp
index 4d01fc87984..70d3617da22 100644
--- a/src/plugins/intel_gpu/src/graph/impls/ocl/fully_connected.cpp
+++ b/src/plugins/intel_gpu/src/graph/impls/ocl/fully_connected.cpp
@@ -137,8 +137,9 @@ public:
auto input0_pshape = input_layouts[0].get_partial_shape();
auto input1_pshape = input_layouts[1].get_partial_shape();
- ov::PartialShape updated_out_pshape {input0_pshape[0], input1_pshape[0]};
- const auto output_feature_size = swiglu_fused ? input1_pshape[0] / 2 : input1_pshape[0];
+ const auto out_features_dim = primitive->weights_transposed ? input1_pshape[0] : input1_pshape[1];
+ ov::PartialShape updated_out_pshape {input0_pshape[0], out_features_dim};
+ const auto output_feature_size = swiglu_fused ? out_features_dim / 2 : out_features_dim;
if (primitive->input_size == 3) {
updated_out_pshape = { input0_pshape[0], input0_pshape[1], output_feature_size};
diff --git a/src/plugins/intel_gpu/src/graph/impls/onednn/fully_connected_onednn.cpp b/src/plugins/intel_gpu/src/graph/impls/onednn/fully_connected_onednn.cpp
index 244f18aeda9..2e147b273b3 100644
--- a/src/plugins/intel_gpu/src/graph/impls/onednn/fully_connected_onednn.cpp
+++ b/src/plugins/intel_gpu/src/graph/impls/onednn/fully_connected_onednn.cpp
@@ -109,22 +109,23 @@ protected:
dnnl::memory::format_tag target_fmt;
dnnl::memory::format_tag weights_fmt;
+ bool weights_transposed = impl_params.typed_desc<fully_connected>()->weights_transposed;
if (prim_input_size == 3) {
target_fmt = dnnl::memory::format_tag::abc;
- weights_fmt = dnnl::memory::format_tag::acb;
+ weights_fmt = weights_transposed ? dnnl::memory::format_tag::acb : dnnl::memory::format_tag::abc;
} else if (prim_input_size == 4) {
target_fmt = dnnl::memory::format_tag::abcd;
- weights_fmt = dnnl::memory::format_tag::abdc;
+ weights_fmt = weights_transposed ? dnnl::memory::format_tag::abdc : dnnl::memory::format_tag::abcd;
} else if (prim_input_size == 5) {
target_fmt = dnnl::memory::format_tag::abcde;
- weights_fmt = dnnl::memory::format_tag::abced;
+ weights_fmt = weights_transposed ? dnnl::memory::format_tag::abced : dnnl::memory::format_tag::abcde;
} else if (prim_input_size == 6) {
target_fmt = dnnl::memory::format_tag::abcdef;
- weights_fmt = dnnl::memory::format_tag::abcdfe;
+ weights_fmt = weights_transposed ? dnnl::memory::format_tag::abcdfe : dnnl::memory::format_tag::abcdef;
} else {
target_fmt = dnnl::memory::format_tag::ab;
- weights_fmt = dnnl::memory::format_tag::ba;
+ weights_fmt = weights_transposed ? dnnl::memory::format_tag::ba : dnnl::memory::format_tag::ab;
}
if (prim_input_size < 4) {
diff --git a/src/plugins/intel_gpu/src/plugin/ops/fully_connected.cpp b/src/plugins/intel_gpu/src/plugin/ops/fully_connected.cpp
index fc2b6a49582..5975c37ac93 100644
--- a/src/plugins/intel_gpu/src/plugin/ops/fully_connected.cpp
+++ b/src/plugins/intel_gpu/src/plugin/ops/fully_connected.cpp
@@ -63,8 +63,8 @@ static void CreateFullyConnectedCompressedOp(ProgramBuilder& p, const std::share
activation_precomputed_reduction,
cldnn::element_type_to_data_type(op->get_output_element_type(0)),
op->get_input_partial_shape(0).size(),
- op->get_input_partial_shape(1).size());
-
+ op->get_input_partial_shape(1).size(),
+ op->get_transpose_b());
if (has_scalar_zp) {
fc.decompression_zero_point_scalar = zp_value;
@@ -122,8 +122,8 @@ static void CreateFullyConnectedOp(ProgramBuilder& p, const std::shared_ptr<op::
bias_name,
cldnn::element_type_to_data_type(op->get_output_element_type(0)),
rank_a,
- rank_b);
-
+ rank_b,
+ op->get_transpose_b());
p.add_primitive(*op, fcPrim);
if (shape_a.size() > 3 && !p.use_new_shape_infer()) {
diff --git a/src/plugins/intel_gpu/src/plugin/transformations/convert_fc_to_compressed.cpp b/src/plugins/intel_gpu/src/plugin/transformations/convert_fc_to_compressed.cpp
index 3a82018f774..9350ae439a4 100644
--- a/src/plugins/intel_gpu/src/plugin/transformations/convert_fc_to_compressed.cpp
+++ b/src/plugins/intel_gpu/src/plugin/transformations/convert_fc_to_compressed.cpp
@@ -180,9 +180,9 @@ ConvertFullyConnectedToFullyConnectedCompressed::ConvertFullyConnectedToFullyCon
std::shared_ptr<ov::Node> new_fc = nullptr;
if (with_zero_point) {
new_fc =
- std::make_shared<op::FullyConnectedCompressed>(fc_input_a, fc_input_b, fc_input_bias, fc_input_scale, fc_input_zp, fc->get_output_type());
+ std::make_shared<op::FullyConnectedCompressed>(fc_input_a, fc_input_b, fc_input_bias, fc_input_scale, fc_input_zp, fc->get_output_type(), fc->get_transpose_b());
} else {
- new_fc = std::make_shared<op::FullyConnectedCompressed>(fc_input_a, fc_input_b, fc_input_bias, fc_input_scale, fc->get_output_type());
+ new_fc = std::make_shared<op::FullyConnectedCompressed>(fc_input_a, fc_input_b, fc_input_bias, fc_input_scale, fc->get_output_type(), fc->get_transpose_b());
}
result_nodes.push_back(new_fc);
diff --git a/src/plugins/intel_gpu/src/plugin/transformations/convert_matmul_to_fc.cpp b/src/plugins/intel_gpu/src/plugin/transformations/convert_matmul_to_fc.cpp
index 470433d00de..715d51fd58c 100644
--- a/src/plugins/intel_gpu/src/plugin/transformations/convert_matmul_to_fc.cpp
+++ b/src/plugins/intel_gpu/src/plugin/transformations/convert_matmul_to_fc.cpp
@@ -214,23 +214,70 @@ ConvertMatMulToFullyConnected::ConvertMatMulToFullyConnected(bool supports_immad
}
// Weights normalization
- bool can_reuse_transpose = false;
- if (!matmul->get_transpose_b()) {
- if (transpose_node && transpose_node->get_input_size() == 2) {
- auto order_constant = ov::as_type_ptr<ov::op::v0::Constant>(transpose_node->get_input_node_shared_ptr(1));
- if (order_constant) {
- std::vector<size_t> order = order_constant->cast_vector<size_t>();
-
- std::vector<size_t> expected_order(fc_input_b.get_partial_shape().size());
- std::iota(expected_order.begin(), expected_order.end(), 0);
- std::swap(*(expected_order.end() - 1), *(expected_order.end() - 2));
-
- can_reuse_transpose = order == expected_order;
+ bool is_small_matmul = true;
+ if (shape_a.is_static() && shape_b.is_static()) {
+ auto output_shape = matmul->get_output_shape(0);
+ size_t k = 0;
+ if (matmul->get_transpose_a())
+ k = shape_a[shape_a.rank().get_length() - 2].get_length();
+ else
+ k = shape_a[shape_a.rank().get_length() - 1].get_length();
+ // M is the row/token dimension and N the output dimension of the matmul.
+ size_t m = output_shape.size() >= 2 ? output_shape[output_shape.size() - 2] : 1;
+ size_t n = output_shape.size() >= 1 ? output_shape[output_shape.size() - 1] : 1;
+ // Empirical benchdnn study (f16 GPU matmul, see
+ // temp/gemm_transpose_study): the non-transposed weight layout
+ // (onednn abc) wins for matmuls with a large reduction dimension
+ // that stay memory-bound, i.e. thin in at least one output
+ // dimension. When both M and N are large the GEMM becomes
+ // compute-bound and the transposed layout (acb) is faster (the
+ // non-transposed kernel can regress by up to ~1.3x there). Restrict
+ // the non-transposed path to K >= 8192 and (M <= 512 or N <= 4096)
+ // to keep the wins while avoiding those regressions.
+ if (k >= 8192 && (m <= 512 || n <= 4096) &&
+ matmul->get_input_element_type(0) == ov::element::f16 && !is_compressed_weight) {
+ is_small_matmul = false;
+ }
+ }
+
+ if (is_small_matmul) {
+ // Weights normalization: FullyConnected expects weights in [N, K] layout (transpose_b=true).
+ bool can_reuse_transpose = false;
+ if (!matmul->get_transpose_b()) {
+ if (transpose_node && transpose_node->get_input_size() == 2) {
+ auto order_constant = ov::as_type_ptr<ov::op::v0::Constant>(transpose_node->get_input_node_shared_ptr(1));
+ if (order_constant) {
+ std::vector<size_t> order = order_constant->cast_vector<size_t>();
+
+ std::vector<size_t> expected_order(fc_input_b.get_partial_shape().size());
+ std::iota(expected_order.begin(), expected_order.end(), 0);
+ std::swap(*(expected_order.end() - 1), *(expected_order.end() - 2));
+
+ can_reuse_transpose = order == expected_order;
+ }
}
- }
- fc_input_b = can_reuse_transpose ? transpose_node
- : create_transpose(fc_input_b, matmul->get_friendly_name() + "/transpose_b");
+ fc_input_b = can_reuse_transpose ? transpose_node
+ : create_transpose(fc_input_b, matmul->get_friendly_name() + "/transpose_b");
+ }
+ } else {
+ if (!matmul->get_transpose_b()) {
+ if (transpose_node && transpose_node->get_input_size() == 2) {
+ auto order_constant = ov::as_type_ptr<ov::op::v0::Constant>(transpose_node->get_input_node_shared_ptr(1));
+ if (order_constant) {
+ std::vector<size_t> order = order_constant->cast_vector<size_t>();
+
+ std::vector<size_t> expected_order(fc_input_b.get_partial_shape().size());
+ std::iota(expected_order.begin(), expected_order.end(), 0);
+ std::swap(*(expected_order.end() - 1), *(expected_order.end() - 2));
+
+ if (order == expected_order)
+ fc_input_b = transpose_node;
+ }
+ }
+ } else {
+ fc_input_b = create_transpose(fc_input_b, matmul->get_friendly_name() + "/transpose_b");
+ }
}
// Input normalization
@@ -250,7 +297,8 @@ ConvertMatMulToFullyConnected::ConvertMatMulToFullyConnected(bool supports_immad
auto no_bias = std::make_shared<op::Placeholder>();
// Create FullyConnected
- auto fc = std::make_shared<op::FullyConnected>(fc_input_a, fc_input_b, no_bias, matmul->get_output_element_type(0));
+ const bool transpose_b = is_small_matmul;
+ auto fc = std::make_shared<op::FullyConnected>(fc_input_a, fc_input_b, no_bias, matmul->get_output_element_type(0), transpose_b);
fc->set_friendly_name(matmul->get_friendly_name());
new_ops.push_back(fc);
ov::copy_runtime_info(matmul, new_ops);
diff --git a/src/plugins/intel_gpu/src/plugin/transformations/dynamic_quantize_fully_connected.cpp b/src/plugins/intel_gpu/src/plugin/transformations/dynamic_quantize_fully_connected.cpp
index 73a4e22b64b..72c31fe3053 100644
--- a/src/plugins/intel_gpu/src/plugin/transformations/dynamic_quantize_fully_connected.cpp
+++ b/src/plugins/intel_gpu/src/plugin/transformations/dynamic_quantize_fully_connected.cpp
@@ -124,7 +124,8 @@ DynamicQuantizeFullyConnected::DynamicQuantizeFullyConnected(uint64_t group_size
dyn_quan->output(1),
optional_a_zp,
optional_precomputed_reduction,
- output_type);
+ output_type,
+ m_fc->get_transpose_b());
ov::replace_node(m_fc, new_fc);
diff --git a/src/plugins/intel_gpu/src/plugin/transformations/fc_convert_fusion.cpp b/src/plugins/intel_gpu/src/plugin/transformations/fc_convert_fusion.cpp
index 33b5f7564e4..ccb652e3b88 100644
--- a/src/plugins/intel_gpu/src/plugin/transformations/fc_convert_fusion.cpp
+++ b/src/plugins/intel_gpu/src/plugin/transformations/fc_convert_fusion.cpp
@@ -41,23 +41,35 @@ FullyConnectedConvertFusion::FullyConnectedConvertFusion() {
auto it = pattern_map.find(fully_connected);
if (it != pattern_map.end()) {
m_fc = it->second.get_node_shared_ptr();
- new_fc = std::make_shared<op::FullyConnected>(m_fc->input_value(0), m_weights, m_bias, output_type);
+ auto m_fc_typed = ov::as_type_ptr<op::FullyConnected>(m_fc);
+ OPENVINO_ASSERT(m_fc_typed);
+ new_fc = std::make_shared<op::FullyConnected>(m_fc->input_value(0),
+ m_weights,
+ m_bias,
+ output_type,
+ m_fc_typed->get_transpose_b());
+
} else {
m_fc = pattern_map.at(fully_connected_compressed).get_node_shared_ptr();
+ auto m_fc_typed = ov::as_type_ptr<op::FullyConnectedCompressed>(m_fc);
+ OPENVINO_ASSERT(m_fc_typed);
+
if (m_fc->input_values().size() == 4)
new_fc = std::make_shared<op::FullyConnectedCompressed>(m_fc->input_value(0),
m_weights,
m_bias,
m_fc->input_value(3),
- output_type);
+ output_type,
+ m_fc_typed->get_transpose_b());
else
new_fc = std::make_shared<op::FullyConnectedCompressed>(m_fc->input_value(0),
m_weights,
m_bias,
m_fc->input_value(3),
m_fc->input_value(4),
- output_type);
+ output_type,
+ m_fc_typed->get_transpose_b());
}
new_fc->set_friendly_name(m_convert->get_friendly_name());
copy_runtime_info(m.get_matched_nodes(), new_fc);
diff --git a/src/plugins/intel_gpu/src/plugin/transformations/fc_horizontal_fusion.cpp b/src/plugins/intel_gpu/src/plugin/transformations/fc_horizontal_fusion.cpp
index 1453e45b5b2..f4c66823fe1 100644
--- a/src/plugins/intel_gpu/src/plugin/transformations/fc_horizontal_fusion.cpp
+++ b/src/plugins/intel_gpu/src/plugin/transformations/fc_horizontal_fusion.cpp
@@ -128,6 +128,7 @@ FullyConnectedHorizontalFusion::FullyConnectedHorizontalFusion(bool fuse_mlp_swi
const size_t k_axis = 1;
auto weight_dtype = fc_nodes[0]->get_input_element_type(weight_idx);
auto k_size = fc_nodes[0]->get_input_shape(weight_idx)[k_axis];
+ const bool transpose_b = fc_nodes[0]->get_transpose_b();
std::vector<int64_t> orig_n_sizes;
// merge weights, scale, zp
for (auto fc : fc_nodes) {
@@ -135,6 +136,8 @@ FullyConnectedHorizontalFusion::FullyConnectedHorizontalFusion(bool fuse_mlp_swi
return false;
if (weight_dtype != fc->get_input_element_type(weight_idx))
return false;
+ if (transpose_b != fc->get_transpose_b())
+ return false;
orig_n_sizes.push_back(fc->get_input_shape(weight_idx)[n_axis]);
}
ov::OutputVector weight_nodes_as_output_vector;
@@ -278,13 +281,15 @@ FullyConnectedHorizontalFusion::FullyConnectedHorizontalFusion(bool fuse_mlp_swi
fused_bias,
fused_scale,
fused_zps,
- fc_nodes[0]->get_output_type());
+ fc_nodes[0]->get_output_type(),
+ transpose_b);
else
new_fc = std::make_shared<op::FullyConnectedCompressed>(input_node,
fused_weight,
fused_bias,
fused_scale,
- fc_nodes[0]->get_output_type());
+ fc_nodes[0]->get_output_type(),
+ transpose_b);
auto new_fc_name = fc_nodes[0]->get_friendly_name() + "_fused_" + std::to_string(fc_nodes.size()) + "FCs";
new_fc->set_friendly_name(new_fc_name);
diff --git a/src/plugins/intel_gpu/src/plugin/transformations/op/fully_connected.cpp b/src/plugins/intel_gpu/src/plugin/transformations/op/fully_connected.cpp
index d029b70bd80..55602456ff0 100644
--- a/src/plugins/intel_gpu/src/plugin/transformations/op/fully_connected.cpp
+++ b/src/plugins/intel_gpu/src/plugin/transformations/op/fully_connected.cpp
@@ -10,15 +10,16 @@ namespace ov::intel_gpu::op {
FullyConnected::FullyConnected(const ov::Output<Node>& A,
const ov::Output<Node>& B,
const ov::Output<Node>& bias,
- const ov::element::Type output_type)
- : Op({A, B, bias}), m_output_type(output_type) {
+ const ov::element::Type output_type,
+ const bool transpose_b)
+ : Op({A, B, bias}), m_output_type(output_type), m_transpose_b(transpose_b) {
validate_and_infer_types();
}
std::shared_ptr<ov::Node> FullyConnected::clone_with_new_inputs(const ov::OutputVector& new_args) const {
check_new_args_count(this, new_args);
- return std::make_shared<FullyConnected>(new_args.at(0), new_args.at(1), new_args.at(2), m_output_type);
+ return std::make_shared<FullyConnected>(new_args.at(0), new_args.at(1), new_args.at(2), m_output_type, m_transpose_b);
}
void FullyConnected::validate_and_infer_types() {
@@ -31,7 +32,7 @@ void FullyConnected::validate_and_infer_types() {
ov::op::v0::MatMul op;
op.set_transpose_a(false);
- op.set_transpose_b(true);
+ op.set_transpose_b(m_transpose_b);
auto out_shapes = ov::op::v0::shape_infer(&op, std::vector<ov::PartialShape>{get_input_partial_shape(0), get_input_partial_shape(1)});
@@ -41,6 +42,7 @@ void FullyConnected::validate_and_infer_types() {
bool FullyConnected::visit_attributes(ov::AttributeVisitor &visitor) {
visitor.on_attribute("output_type", m_output_type);
+ visitor.on_attribute("transpose_b", m_transpose_b);
return true;
}
diff --git a/src/plugins/intel_gpu/src/plugin/transformations/op/fully_connected_compressed.cpp b/src/plugins/intel_gpu/src/plugin/transformations/op/fully_connected_compressed.cpp
index 2535d954315..cb8bb197ffb 100644
--- a/src/plugins/intel_gpu/src/plugin/transformations/op/fully_connected_compressed.cpp
+++ b/src/plugins/intel_gpu/src/plugin/transformations/op/fully_connected_compressed.cpp
@@ -14,8 +14,9 @@ FullyConnectedCompressed::FullyConnectedCompressed(const ov::Output<Node>& A,
const ov::Output<Node>& a_decompression_scale,
const ov::Output<Node>& a_decompression_zero_point,
const ov::Output<Node>& a_precomputed_reduction,
- const ov::element::Type output_type)
- : FullyConnected(A, B, bias, output_type) {
+ const ov::element::Type output_type,
+ const bool transpose_b)
+ : FullyConnected(A, B, bias, output_type, transpose_b) {
set_argument(3, w_decompression_scale);
set_argument(4, w_decompression_zero_point);
set_argument(5, a_decompression_scale);
@@ -29,8 +30,9 @@ FullyConnectedCompressed::FullyConnectedCompressed(const ov::Output<Node>& A,
const ov::Output<Node>& bias,
const ov::Output<Node>& w_decompression_scale,
const ov::Output<Node>& w_decompression_zero_point,
- const ov::element::Type output_type)
- : FullyConnected(A, B, bias, output_type) {
+ const ov::element::Type output_type,
+ const bool transpose_b)
+ : FullyConnected(A, B, bias, output_type, transpose_b) {
set_argument(3, w_decompression_scale);
set_argument(4, w_decompression_zero_point);
validate_and_infer_types();
@@ -40,8 +42,9 @@ FullyConnectedCompressed::FullyConnectedCompressed(const ov::Output<Node>& A,
const ov::Output<Node>& B,
const ov::Output<Node>& bias,
const ov::Output<Node>& w_decompression_scale,
- const ov::element::Type output_type)
- : FullyConnected(A, B, bias, output_type) {
+ const ov::element::Type output_type,
+ const bool transpose_b)
+ : FullyConnected(A, B, bias, output_type, transpose_b) {
set_argument(3, w_decompression_scale);
validate_and_infer_types();
}
@@ -54,14 +57,16 @@ std::shared_ptr<ov::Node> FullyConnectedCompressed::clone_with_new_inputs(const
new_args.at(1),
new_args.at(2),
new_args.at(3),
- m_output_type);
+ m_output_type,
+ m_transpose_b);
else if (new_args.size() == 5)
return std::make_shared<FullyConnectedCompressed>(new_args.at(0),
new_args.at(1),
new_args.at(2),
new_args.at(3),
new_args.at(4),
- m_output_type);
+ m_output_type,
+ m_transpose_b);
else if (new_args.size() == 8)
return std::make_shared<FullyConnectedCompressed>(new_args.at(0),
new_args.at(1),
@@ -71,7 +76,9 @@ std::shared_ptr<ov::Node> FullyConnectedCompressed::clone_with_new_inputs(const
new_args.at(5),
new_args.at(6),
new_args.at(7),
- m_output_type);
+ m_output_type,
+ m_transpose_b);
else
- OPENVINO_THROW("Unexpected inputs count for FullyConnectedCompressed op: ", new_args.size());}
+ OPENVINO_THROW("Unexpected inputs count for FullyConnectedCompressed op: ", new_args.size());
+}
} // namespace ov::intel_gpu::op
diff --git a/src/plugins/intel_gpu/tests/unit/transformations/convert_matmul_to_fc_test.cpp b/src/plugins/intel_gpu/tests/unit/transformations/convert_matmul_to_fc_test.cpp
index 87a72daeac8..a12ec42865c 100644
--- a/src/plugins/intel_gpu/tests/unit/transformations/convert_matmul_to_fc_test.cpp
+++ b/src/plugins/intel_gpu/tests/unit/transformations/convert_matmul_to_fc_test.cpp
@@ -847,3 +847,131 @@ TEST_F(TransformationTestsF, ConvertMatMulToFullyConnectedTest_rank_a_less_than_
model_ref = std::make_shared<ov::Model>(ov::OutputVector{matmul}, ov::ParameterVector{activation});
}
}
+
+// Large f16 matmul K>=8192, M<=512: non-transposed path (transpose_b=false).
+TEST_F(TransformationTestsF, ConvertMatMulToFullyConnected_LargeF16_NonTransposed_M_Thin) {
+ {
+ auto input = std::make_shared<ov::opset1::Parameter>(ov::element::f16, ov::Shape{512, 8192});
+ auto weights = ov::opset1::Constant::create(ov::element::f16, ov::Shape{8192, 4096}, {1});
+ auto matmul = std::make_shared<ov::opset1::MatMul>(input, weights, false, false);
+
+ model = std::make_shared<ov::Model>(ov::OutputVector{matmul}, ov::ParameterVector{input});
+ manager.register_pass<ConvertMatMulToFullyConnected>();
+ }
+ {
+ auto input = std::make_shared<ov::opset1::Parameter>(ov::element::f16, ov::Shape{512, 8192});
+ auto weights = ov::opset1::Constant::create(ov::element::f16, ov::Shape{8192, 4096}, {1});
+ auto no_bias = std::make_shared<ov::intel_gpu::op::Placeholder>();
+ auto fc = std::make_shared<op::FullyConnected>(input, weights, no_bias, ov::element::f16, false);
+
+ model_ref = std::make_shared<ov::Model>(ov::OutputVector{fc}, ov::ParameterVector{input});
+ }
+}
+
+// Large f16 matmul K>=8192, N<=4096: non-transposed path (transpose_b=false).
+TEST_F(TransformationTestsF, ConvertMatMulToFullyConnected_LargeF16_NonTransposed_N_Thin) {
+ {
+ auto input = std::make_shared<ov::opset1::Parameter>(ov::element::f16, ov::Shape{4096, 8192});
+ auto weights = ov::opset1::Constant::create(ov::element::f16, ov::Shape{8192, 256}, {1});
+ auto matmul = std::make_shared<ov::opset1::MatMul>(input, weights, false, false);
+
+ model = std::make_shared<ov::Model>(ov::OutputVector{matmul}, ov::ParameterVector{input});
+ manager.register_pass<ConvertMatMulToFullyConnected>();
+ }
+ {
+ auto input = std::make_shared<ov::opset1::Parameter>(ov::element::f16, ov::Shape{4096, 8192});
+ auto weights = ov::opset1::Constant::create(ov::element::f16, ov::Shape{8192, 256}, {1});
+ auto no_bias = std::make_shared<ov::intel_gpu::op::Placeholder>();
+ auto fc = std::make_shared<op::FullyConnected>(input, weights, no_bias, ov::element::f16, false);
+
+ model_ref = std::make_shared<ov::Model>(ov::OutputVector{fc}, ov::ParameterVector{input});
+ }
+}
+
+// Compute-bound large f16 matmul: M>512 and N>4096 → transposed path (transpose_b=true).
+TEST_F(TransformationTestsF, ConvertMatMulToFullyConnected_LargeF16_ComputeBound_Transposed) {
+ {
+ auto input = std::make_shared<ov::opset1::Parameter>(ov::element::f16, ov::Shape{4096, 8192});
+ auto weights = ov::opset1::Constant::create(ov::element::f16, ov::Shape{8192, 8192}, {1});
+ auto matmul = std::make_shared<ov::opset1::MatMul>(input, weights, false, false);
+
+ model = std::make_shared<ov::Model>(ov::OutputVector{matmul}, ov::ParameterVector{input});
+ manager.register_pass<ConvertMatMulToFullyConnected>();
+ }
+ {
+ auto input = std::make_shared<ov::opset1::Parameter>(ov::element::f16, ov::Shape{4096, 8192});
+ auto weights = ov::opset1::Constant::create(ov::element::f16, ov::Shape{8192, 8192}, {1});
+ auto transpose_constant = ov::opset1::Constant::create(ov::element::i32, ov::Shape{2}, {1, 0});
+ auto transpose = std::make_shared<ov::opset1::Transpose>(weights, transpose_constant);
+ auto no_bias = std::make_shared<ov::intel_gpu::op::Placeholder>();
+ auto fc = std::make_shared<op::FullyConnected>(input, transpose, no_bias, ov::element::f16, true);
+
+ model_ref = std::make_shared<ov::Model>(ov::OutputVector{fc}, ov::ParameterVector{input});
+ }
+}
+
+// Small f16 matmul K<8192 → transposed path (transpose_b=true).
+TEST_F(TransformationTestsF, ConvertMatMulToFullyConnected_SmallF16_Transposed) {
+ {
+ auto input = std::make_shared<ov::opset1::Parameter>(ov::element::f16, ov::Shape{256, 4096});
+ auto weights = ov::opset1::Constant::create(ov::element::f16, ov::Shape{4096, 512}, {1});
+ auto matmul = std::make_shared<ov::opset1::MatMul>(input, weights, false, false);
+
+ model = std::make_shared<ov::Model>(ov::OutputVector{matmul}, ov::ParameterVector{input});
+ manager.register_pass<ConvertMatMulToFullyConnected>();
+ }
+ {
+ auto input = std::make_shared<ov::opset1::Parameter>(ov::element::f16, ov::Shape{256, 4096});
+ auto weights = ov::opset1::Constant::create(ov::element::f16, ov::Shape{4096, 512}, {1});
+ auto transpose_constant = ov::opset1::Constant::create(ov::element::i32, ov::Shape{2}, {1, 0});
+ auto transpose = std::make_shared<ov::opset1::Transpose>(weights, transpose_constant);
+ auto no_bias = std::make_shared<ov::intel_gpu::op::Placeholder>();
+ auto fc = std::make_shared<op::FullyConnected>(input, transpose, no_bias, ov::element::f16, true);
+
+ model_ref = std::make_shared<ov::Model>(ov::OutputVector{fc}, ov::ParameterVector{input});
+ }
+}
+
+// Large f16 matmul with matmul transpose_b=true → non-transposed FC inserts a reverse Transpose.
+TEST_F(TransformationTestsF, ConvertMatMulToFullyConnected_LargeF16_MatMulTransposeB_True_NonTransposedPath) {
+ {
+ auto input = std::make_shared<ov::opset1::Parameter>(ov::element::f16, ov::Shape{256, 8192});
+ auto weights = ov::opset1::Constant::create(ov::element::f16, ov::Shape{4096, 8192}, {1});
+ auto matmul = std::make_shared<ov::opset1::MatMul>(input, weights, false, true);
+
+ model = std::make_shared<ov::Model>(ov::OutputVector{matmul}, ov::ParameterVector{input});
+ manager.register_pass<ConvertMatMulToFullyConnected>();
+ }
+ {
+ auto input = std::make_shared<ov::opset1::Parameter>(ov::element::f16, ov::Shape{256, 8192});
+ auto weights = ov::opset1::Constant::create(ov::element::f16, ov::Shape{4096, 8192}, {1});
+ auto transpose_constant = ov::opset1::Constant::create(ov::element::i32, ov::Shape{2}, {1, 0});
+ auto transpose = std::make_shared<ov::opset1::Transpose>(weights, transpose_constant);
+ auto no_bias = std::make_shared<ov::intel_gpu::op::Placeholder>();
+ auto fc = std::make_shared<op::FullyConnected>(input, transpose, no_bias, ov::element::f16, false);
+
+ model_ref = std::make_shared<ov::Model>(ov::OutputVector{fc}, ov::ParameterVector{input});
+ }
+}
+
+// Large f32 matmul (not f16) → transposed path (transpose_b=true) regardless of dimensions.
+TEST_F(TransformationTestsF, ConvertMatMulToFullyConnected_LargeF32_Transposed) {
+ {
+ auto input = std::make_shared<ov::opset1::Parameter>(ov::element::f32, ov::Shape{256, 8192});
+ auto weights = ov::opset1::Constant::create(ov::element::f32, ov::Shape{8192, 4096}, {1});
+ auto matmul = std::make_shared<ov::opset1::MatMul>(input, weights, false, false);
+
+ model = std::make_shared<ov::Model>(ov::OutputVector{matmul}, ov::ParameterVector{input});
+ manager.register_pass<ConvertMatMulToFullyConnected>();
+ }
+ {
+ auto input = std::make_shared<ov::opset1::Parameter>(ov::element::f32, ov::Shape{256, 8192});
+ auto weights = ov::opset1::Constant::create(ov::element::f32, ov::Shape{8192, 4096}, {1});
+ auto transpose_constant = ov::opset1::Constant::create(ov::element::i32, ov::Shape{2}, {1, 0});
+ auto transpose = std::make_shared<ov::opset1::Transpose>(weights, transpose_constant);
+ auto no_bias = std::make_shared<ov::intel_gpu::op::Placeholder>();
+ auto fc = std::make_shared<op::FullyConnected>(input, transpose, no_bias, ov::element::f32, true);
+
+ model_ref = std::make_shared<ov::Model>(ov::OutputVector{fc}, ov::ParameterVector{input});
+ }
+}