Skip to content

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

[Transformation] GroupedMatMul decomposition (#37510)

Katarzyna Mitrus974f480b612a
diff --git a/src/common/transformations/include/sources.cmake b/src/common/transformations/include/sources.cmake
index d6abfdf1154..eab7bbafaf0 100644
--- a/src/common/transformations/include/sources.cmake
+++ b/src/common/transformations/include/sources.cmake
@@ -209,6 +209,7 @@ set(OP_CONVERSIONS_HEADERS
${CMAKE_CURRENT_LIST_DIR}/transformations/op_conversions/convert_gp9_to_gp_ie_internal.hpp
${CMAKE_CURRENT_LIST_DIR}/transformations/op_conversions/convert_grouped_matmul_to_compressed.hpp
${CMAKE_CURRENT_LIST_DIR}/transformations/op_conversions/convert_grouped_matmul_to_gather_matmul.hpp
+ ${CMAKE_CURRENT_LIST_DIR}/transformations/op_conversions/convert_grouped_matmul_to_matmul.hpp
${CMAKE_CURRENT_LIST_DIR}/transformations/op_conversions/convert_interpolate11_downgrade.hpp
${CMAKE_CURRENT_LIST_DIR}/transformations/op_conversions/convert_interpolate1_to_interpolate4.hpp
${CMAKE_CURRENT_LIST_DIR}/transformations/op_conversions/convert_matrix_nms_to_matrix_nms_ie.hpp
diff --git a/src/common/transformations/include/transformations/op_conversions/convert_grouped_matmul_to_matmul.hpp b/src/common/transformations/include/transformations/op_conversions/convert_grouped_matmul_to_matmul.hpp
new file mode 100644
index 00000000000..7aba64c425e
--- /dev/null
+++ b/src/common/transformations/include/transformations/op_conversions/convert_grouped_matmul_to_matmul.hpp
@@ -0,0 +1,52 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include "openvino/pass/graph_rewrite.hpp"
+#include "openvino/pass/matcher_pass.hpp"
+#include "transformations_visibility.hpp"
+
+namespace ov::pass {
+
+/// @brief Decomposes the 3Dx3D v17::GroupedMatMul (no offsets) into a single batched MatMul.
+///
+/// A:[G,M,K], B:[G,N,K] -> out:[G,M,N]
+/// out = MatMul(A, B, transpose_a=false, transpose_b=true) // B is stored transposed
+class TRANSFORMATIONS_API ConvertGroupedMatMul3Dx3DToMatMul : public ov::pass::MatcherPass {
+public:
+ OPENVINO_MATCHER_PASS_RTTI("ConvertGroupedMatMul3Dx3DToMatMul");
+ ConvertGroupedMatMul3Dx3DToMatMul();
+};
+
+/// @brief Decomposes the 2Dx3D v17::GroupedMatMul (with offsets) into per-group MatMuls.
+///
+/// A:[T,K], B:[G,N,K], offs:[G] -> out:[T,N]
+/// `offsets` holds cumulative end offsets, so the per-group row counts are
+/// `offsets - [0, offsets[:-1]]`. A is partitioned by those lengths and each
+/// partition is multiplied by its own weight matrix:
+/// A_0..A_{G-1} = VariadicSplit(A, axis=0, split_lengths) // [Mg, K]
+/// B_0..B_{G-1} = Squeeze(Split(B, axis=0, G), axis=0) // [N, K]
+/// out_g = MatMul(A_g, B_g, transpose_a=false, transpose_b=true)
+/// out = Concat(out_0, ..., out_{G-1}, axis=0) // [T, N]
+/// Requires the group dimension G (mat_b.shape[0]) to be static so that a
+/// fixed number of MatMuls can be generated.
+class TRANSFORMATIONS_API ConvertGroupedMatMul2Dx3DToMatMul : public ov::pass::MatcherPass {
+public:
+ OPENVINO_MATCHER_PASS_RTTI("ConvertGroupedMatMul2Dx3DToMatMul");
+ ConvertGroupedMatMul2Dx3DToMatMul();
+};
+
+/// @brief Decomposes the public op v17::GroupedMatMul into a regular v0::MatMul operations graph,
+/// covering both the 3Dx3D and 2Dx3D (with offsets) cases.
+class TRANSFORMATIONS_API ConvertGroupedMatMulToMatMul : public ov::pass::GraphRewrite {
+public:
+ OPENVINO_GRAPH_REWRITE_RTTI("ConvertGroupedMatMulToMatMul");
+ ConvertGroupedMatMulToMatMul() {
+ add_matcher<ConvertGroupedMatMul3Dx3DToMatMul>();
+ add_matcher<ConvertGroupedMatMul2Dx3DToMatMul>();
+ }
+};
+
+} // namespace ov::pass
diff --git a/src/common/transformations/src/sources.cmake b/src/common/transformations/src/sources.cmake
index d27384a4304..92a81d136aa 100644
--- a/src/common/transformations/src/sources.cmake
+++ b/src/common/transformations/src/sources.cmake
@@ -171,6 +171,7 @@ set(OP_CONVERSIONS_SOURCES
${CMAKE_CURRENT_LIST_DIR}/transformations/op_conversions/convert_gp9_to_gp_ie_internal.cpp
${CMAKE_CURRENT_LIST_DIR}/transformations/op_conversions/convert_grouped_matmul_to_compressed.cpp
${CMAKE_CURRENT_LIST_DIR}/transformations/op_conversions/convert_grouped_matmul_to_gather_matmul.cpp
+ ${CMAKE_CURRENT_LIST_DIR}/transformations/op_conversions/convert_grouped_matmul_to_matmul.cpp
${CMAKE_CURRENT_LIST_DIR}/transformations/op_conversions/convert_interpolate11_downgrade.cpp
${CMAKE_CURRENT_LIST_DIR}/transformations/op_conversions/convert_interpolate1_to_interpolate4.cpp
${CMAKE_CURRENT_LIST_DIR}/transformations/op_conversions/convert_matrix_nms_to_matrix_nms_ie.cpp
diff --git a/src/common/transformations/src/transformations/op_conversions/convert_grouped_matmul_to_matmul.cpp b/src/common/transformations/src/transformations/op_conversions/convert_grouped_matmul_to_matmul.cpp
new file mode 100644
index 00000000000..cd2977815c8
--- /dev/null
+++ b/src/common/transformations/src/transformations/op_conversions/convert_grouped_matmul_to_matmul.cpp
@@ -0,0 +1,139 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "transformations/op_conversions/convert_grouped_matmul_to_matmul.hpp"
+
+#include <cstdint>
+#include <memory>
+
+#include "itt.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/type/element_type.hpp"
+#include "openvino/op/concat.hpp"
+#include "openvino/op/constant.hpp"
+#include "openvino/op/grouped_matmul.hpp"
+#include "openvino/op/matmul.hpp"
+#include "openvino/op/slice.hpp"
+#include "openvino/op/split.hpp"
+#include "openvino/op/squeeze.hpp"
+#include "openvino/op/subtract.hpp"
+#include "openvino/op/variadic_split.hpp"
+#include "openvino/pass/node_registry.hpp"
+#include "openvino/pass/pattern/matcher.hpp"
+#include "openvino/pass/pattern/op/pattern.hpp"
+#include "openvino/pass/pattern/op/wrap_type.hpp"
+
+namespace ov::pass {
+
+namespace {
+
+namespace v0 = ov::op::v0;
+namespace v1 = ov::op::v1;
+namespace v8 = ov::op::v8;
+namespace v17 = ov::op::v17;
+
+} // namespace
+
+ConvertGroupedMatMul3Dx3DToMatMul::ConvertGroupedMatMul3Dx3DToMatMul() {
+ MATCHER_SCOPE(ConvertGroupedMatMul3Dx3DToMatMul);
+ using namespace ov::pass::pattern;
+
+ // 3Dx3D: A:[G,M,K] B:[G,N,K]
+ auto matrix_a_3d = any_input(rank_equals(3));
+ auto matrix_b_3d = any_input(rank_equals(3));
+ auto gmm_3d_3d = wrap_type<v17::GroupedMatMul>({matrix_a_3d, matrix_b_3d});
+
+ ov::matcher_pass_callback callback = [OV_CAPTURE_CPY_AND_THIS](ov::pass::pattern::Matcher& m) {
+ auto gmm = ov::as_type_ptr<v17::GroupedMatMul>(m.get_match_root());
+ if (!gmm || transformation_callback(gmm)) {
+ return false;
+ }
+ const auto mat_a = gmm->input_value(0);
+ const auto mat_b = gmm->input_value(1);
+
+ NodeRegistry rg;
+ // A:[G,M,K] B:[G,N,K] -> MatMul(A, B, transpose_b=true) -> [G,M,N]
+ auto replacement = rg.make<v0::MatMul>(mat_a, mat_b, false, true);
+
+ replacement->set_friendly_name(gmm->get_friendly_name());
+ ov::copy_runtime_info(gmm, rg.get());
+ ov::replace_node(gmm, replacement);
+ return true;
+ };
+
+ auto matcher = std::make_shared<ov::pass::pattern::Matcher>(gmm_3d_3d, matcher_name);
+ register_matcher(matcher, callback);
+}
+
+ConvertGroupedMatMul2Dx3DToMatMul::ConvertGroupedMatMul2Dx3DToMatMul() {
+ MATCHER_SCOPE(ConvertGroupedMatMul2Dx3DToMatMul);
+ using namespace ov::pass::pattern;
+
+ // 2Dx3D: A:[T,K] B:[G,N,K] offsets:[G]
+ // A fixed MatMul per group is emitted, so the group count G (B.shape[0]) must be static.
+ auto matrix_a_2d = any_input(rank_equals(2));
+ auto matrix_b_3d = any_input(rank_equals(3) && has_static_dim(0));
+ auto offsets = any_input(rank_equals(1) && type_matches_any({ov::element::i32, ov::element::i64}));
+ auto gmm_2d_3d = wrap_type<v17::GroupedMatMul>({matrix_a_2d, matrix_b_3d, offsets});
+
+ ov::matcher_pass_callback callback = [OV_CAPTURE_CPY_AND_THIS](ov::pass::pattern::Matcher& m) {
+ auto gmm = ov::as_type_ptr<v17::GroupedMatMul>(m.get_match_root());
+ if (!gmm || transformation_callback(gmm)) {
+ return false;
+ }
+ const auto mat_a = gmm->input_value(0);
+ const auto mat_b = gmm->input_value(1);
+
+ const auto num_groups = mat_b.get_partial_shape()[0].get_length();
+
+ using ov::element::i32;
+
+ NodeRegistry rg;
+
+ const auto offsets = gmm->input_value(2);
+ const auto offsets_et = offsets.get_element_type();
+
+ auto axis_scalar = rg.make<v0::Constant>(i32, ov::Shape{}, 0);
+ auto axis_1d = rg.make<v0::Constant>(i32, ov::Shape{1}, 0);
+
+ // offsets are cumulative ends, so per-group row counts are offsets - [0, offsets[:-1]].
+ ov::Output<ov::Node> split_lengths = offsets;
+ if (num_groups > 1) {
+ // Only Concat requires a matching type; Slice and VariadicSplit take any integral one.
+ auto zero = rg.make<v0::Constant>(offsets_et, ov::Shape{1}, 0);
+ auto head_stop = rg.make<v0::Constant>(i32, ov::Shape{1}, num_groups - 1);
+ auto step = rg.make<v0::Constant>(i32, ov::Shape{1}, 1);
+ auto offsets_head = rg.make<v8::Slice>(offsets, zero, head_stop, step, axis_1d);
+ auto shifted = rg.make<v0::Concat>(ov::OutputVector{zero, offsets_head}, 0);
+ split_lengths = rg.make<v1::Subtract>(offsets, shifted);
+ }
+
+ auto a_split = rg.make<v1::VariadicSplit>(mat_a, axis_scalar, split_lengths); // G x [Mg, K]
+ auto b_split = rg.make<v1::Split>(mat_b, axis_scalar, num_groups); // G x [1, N, K]
+
+ ov::OutputVector group_outputs;
+ group_outputs.reserve(static_cast<size_t>(num_groups));
+
+ for (int64_t g = 0; g < num_groups; ++g) {
+ auto b_g = rg.make<v0::Squeeze>(b_split->output(g), axis_1d); // [N, K]
+ auto mm = rg.make<v0::MatMul>(a_split->output(g), b_g, false, true); // [Mg, N]
+ group_outputs.push_back(std::move(mm));
+ }
+
+ auto replacement = rg.make<v0::Concat>(group_outputs, 0); // [T, N]
+
+ replacement->set_friendly_name(gmm->get_friendly_name());
+ ov::copy_runtime_info(gmm, rg.get());
+ ov::replace_node(gmm, replacement);
+ return true;
+ };
+
+ auto matcher = std::make_shared<ov::pass::pattern::Matcher>(gmm_2d_3d, matcher_name);
+ register_matcher(matcher, callback);
+}
+
+} // namespace ov::pass
diff --git a/src/common/transformations/tests/op_conversions/convert_grouped_matmul_to_matmul_test.cpp b/src/common/transformations/tests/op_conversions/convert_grouped_matmul_to_matmul_test.cpp
new file mode 100644
index 00000000000..6f43b5e5c5e
--- /dev/null
+++ b/src/common/transformations/tests/op_conversions/convert_grouped_matmul_to_matmul_test.cpp
@@ -0,0 +1,159 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "transformations/op_conversions/convert_grouped_matmul_to_matmul.hpp"
+
+#include <gtest/gtest.h>
+
+#include <memory>
+#include <tuple>
+#include <vector>
+
+#include "common_test_utils/ov_test_utils.hpp"
+#include "openvino/core/model.hpp"
+#include "openvino/op/concat.hpp"
+#include "openvino/op/constant.hpp"
+#include "openvino/op/grouped_matmul.hpp"
+#include "openvino/op/matmul.hpp"
+#include "openvino/op/parameter.hpp"
+#include "openvino/op/slice.hpp"
+#include "openvino/op/split.hpp"
+#include "openvino/op/squeeze.hpp"
+#include "openvino/op/subtract.hpp"
+#include "openvino/op/variadic_split.hpp"
+#include "openvino/pass/manager.hpp"
+
+namespace v0 = ov::op::v0;
+namespace v1 = ov::op::v1;
+namespace v8 = ov::op::v8;
+namespace v17 = ov::op::v17;
+
+namespace {
+
+constexpr auto f32 = ov::element::f32;
+constexpr auto i32 = ov::element::i32;
+constexpr auto i64 = ov::element::i64;
+
+// Reference decomposition of the 2Dx3D (offsets) case into per-group MatMuls.
+ov::Output<ov::Node> ref_2dx3d_decomposition(const ov::Output<ov::Node>& mat_a,
+ const ov::Output<ov::Node>& mat_b,
+ const ov::Output<ov::Node>& offsets,
+ int64_t num_groups) {
+ const auto offsets_et = offsets.get_element_type();
+
+ auto axis_scalar = v0::Constant::create(i32, ov::Shape{}, {0});
+ auto axis_1d = v0::Constant::create(i32, ov::Shape{1}, {0});
+
+ ov::Output<ov::Node> split_lengths = offsets;
+ if (num_groups > 1) {
+ auto zero = v0::Constant::create(offsets_et, ov::Shape{1}, {0});
+ auto head_stop = v0::Constant::create(i32, ov::Shape{1}, {num_groups - 1});
+ auto step = v0::Constant::create(i32, ov::Shape{1}, {1});
+ auto offsets_head = std::make_shared<v8::Slice>(offsets, zero, head_stop, step, axis_1d);
+ auto shifted = std::make_shared<v0::Concat>(ov::OutputVector{zero, offsets_head}, 0);
+ split_lengths = std::make_shared<v1::Subtract>(offsets, shifted);
+ }
+
+ auto a_split = std::make_shared<v1::VariadicSplit>(mat_a, axis_scalar, split_lengths);
+ auto b_split = std::make_shared<v1::Split>(mat_b, axis_scalar, num_groups);
+
+ ov::OutputVector group_outputs;
+ for (int64_t g = 0; g < num_groups; ++g) {
+ auto b_g = std::make_shared<v0::Squeeze>(b_split->output(g), axis_1d);
+ auto mm = std::make_shared<v0::MatMul>(a_split->output(g), b_g, /*transpose_a=*/false, /*transpose_b=*/true);
+ group_outputs.push_back(mm);
+ }
+ return std::make_shared<v0::Concat>(group_outputs, 0);
+}
+
+} // namespace
+
+// 2Dx3D with dynamic group dimension: cannot decompose into a fixed number of MatMuls
+TEST_F(TransformationTestsF, ConvertGroupedMatMulToMatMul_2Dx3D_DynamicGroupsNoChange) {
+ using ov::Dimension;
+ using ov::PartialShape;
+ constexpr size_t K = 8, N = 16;
+
+ auto mat_a = std::make_shared<v0::Parameter>(f32, PartialShape{Dimension::dynamic(), K});
+ auto mat_b = std::make_shared<v0::Parameter>(f32, PartialShape{Dimension::dynamic(), N, K});
+ auto offsets = std::make_shared<v0::Parameter>(i32, PartialShape{Dimension::dynamic()});
+ auto gmm = std::make_shared<v17::GroupedMatMul>(mat_a, mat_b, offsets);
+ model = std::make_shared<ov::Model>(ov::OutputVector{gmm}, ov::ParameterVector{mat_a, mat_b, offsets});
+
+ manager.register_pass<ov::pass::ConvertGroupedMatMulToMatMul>();
+ // No model_ref: transformation must be a no-op.
+}
+
+// {G, M, K, N}
+using TestParams3Dx3D = std::tuple<size_t, size_t, size_t, size_t>;
+class ConvertGroupedMatMulToMatMul3Dx3DAccuracy : public TransformationTestsF,
+ public testing::WithParamInterface<TestParams3Dx3D> {};
+
+TEST_P(ConvertGroupedMatMulToMatMul3Dx3DAccuracy, MatchesReference) {
+ const auto [G, M, K, N] = GetParam();
+ comparator.enable(FunctionsComparator::CmpValues::ACCURACY);
+
+ {
+ auto mat_a = std::make_shared<v0::Parameter>(f32, ov::Shape{G, M, K});
+ auto mat_b = std::make_shared<v0::Parameter>(f32, ov::Shape{G, N, K});
+ auto gmm = std::make_shared<v17::GroupedMatMul>(mat_a, mat_b);
+ model = std::make_shared<ov::Model>(ov::OutputVector{gmm}, ov::ParameterVector{mat_a, mat_b});
+
+ manager.register_pass<ov::pass::ConvertGroupedMatMulToMatMul>();
+ }
+ {
+ auto mat_a = std::make_shared<v0::Parameter>(f32, ov::Shape{G, M, K});
+ auto mat_b = std::make_shared<v0::Parameter>(f32, ov::Shape{G, N, K});
+ auto mm = std::make_shared<v0::MatMul>(mat_a, mat_b, /*transpose_a=*/false, /*transpose_b=*/true);
+ model_ref = std::make_shared<ov::Model>(ov::OutputVector{mm}, ov::ParameterVector{mat_a, mat_b});
+ }
+}
+
+INSTANTIATE_TEST_SUITE_P(Shapes3Dx3D,
+ ConvertGroupedMatMulToMatMul3Dx3DAccuracy,
+ testing::Values(TestParams3Dx3D{1, 8, 16, 8},
+ TestParams3Dx3D{2, 3, 8, 16},
+ TestParams3Dx3D{4, 1, 8, 8},
+ TestParams3Dx3D{3, 7, 16, 8}));
+
+// {T, K, N, cumulative offsets}
+using ShapeParams2Dx3D = std::tuple<size_t, size_t, size_t, std::vector<int32_t>>;
+// shapes + offsets element type
+using TestParams2Dx3D = std::tuple<ShapeParams2Dx3D, ov::element::Type>;
+
+class ConvertGroupedMatMulToMatMul2Dx3DAccuracy : public TransformationTestsF,
+ public testing::WithParamInterface<TestParams2Dx3D> {};
+
+TEST_P(ConvertGroupedMatMulToMatMul2Dx3DAccuracy, MatchesReference) {
+ const auto& [shapes, offsets_et] = GetParam();
+ const auto& [T, K, N, offsets_vec] = shapes;
+ const auto G = static_cast<int64_t>(offsets_vec.size());
+ comparator.enable(FunctionsComparator::CmpValues::ACCURACY);
+
+ {
+ auto mat_a = std::make_shared<v0::Parameter>(f32, ov::Shape{T, K});
+ auto mat_b = std::make_shared<v0::Parameter>(f32, ov::Shape{offsets_vec.size(), N, K});
+ auto offsets = v0::Constant::create(offsets_et, ov::Shape{offsets_vec.size()}, offsets_vec);
+ auto gmm = std::make_shared<v17::GroupedMatMul>(mat_a, mat_b, offsets);
+ model = std::make_shared<ov::Model>(ov::OutputVector{gmm}, ov::ParameterVector{mat_a, mat_b});
+
+ manager.register_pass<ov::pass::ConvertGroupedMatMulToMatMul>();
+ }
+ {
+ auto mat_a = std::make_shared<v0::Parameter>(f32, ov::Shape{T, K});
+ auto mat_b = std::make_shared<v0::Parameter>(f32, ov::Shape{offsets_vec.size(), N, K});
+ auto offsets = v0::Constant::create(offsets_et, ov::Shape{offsets_vec.size()}, offsets_vec);
+ auto out = ref_2dx3d_decomposition(mat_a, mat_b, offsets, G);
+ model_ref = std::make_shared<ov::Model>(ov::OutputVector{out}, ov::ParameterVector{mat_a, mat_b});
+ }
+}
+
+INSTANTIATE_TEST_SUITE_P(Shapes2Dx3D,
+ ConvertGroupedMatMulToMatMul2Dx3DAccuracy,
+ testing::Combine(testing::Values(ShapeParams2Dx3D{8, 8, 16, {8}},
+ ShapeParams2Dx3D{16, 16, 8, {8, 16}},
+ ShapeParams2Dx3D{24, 8, 8, {8, 16, 24}},
+ // experts without tokens -> zero-length groups
+ ShapeParams2Dx3D{16, 8, 8, {0, 8, 8, 16}}),
+ testing::Values(i32, i64)));
diff --git a/src/common/transformations/tests/sources.cmake b/src/common/transformations/tests/sources.cmake
index f4e591759aa..88fc83610c2 100644
--- a/src/common/transformations/tests/sources.cmake
+++ b/src/common/transformations/tests/sources.cmake
@@ -183,6 +183,7 @@ set(OP_CONVERSIONS_TESTS_SRCS
${CMAKE_CURRENT_LIST_DIR}/op_conversions/convert_gather_to_compressed_test.cpp
${CMAKE_CURRENT_LIST_DIR}/op_conversions/convert_gather_upgrade_test.cpp
${CMAKE_CURRENT_LIST_DIR}/op_conversions/convert_grouped_matmul_to_gather_matmul_test.cpp
+ ${CMAKE_CURRENT_LIST_DIR}/op_conversions/convert_grouped_matmul_to_matmul_test.cpp
${CMAKE_CURRENT_LIST_DIR}/op_conversions/convert_interpolate11_downgrade_test.cpp
${CMAKE_CURRENT_LIST_DIR}/op_conversions/convert_interpolate1_to_interpolate4_test.cpp
${CMAKE_CURRENT_LIST_DIR}/op_conversions/convert_matrix_nms_to_matrix_nms_ie_internal.cpp
diff --git a/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp b/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
index 43fbaa794fa..5e4d459de0b 100644
--- a/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
+++ b/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
@@ -161,6 +161,7 @@
#include "transformations/common_optimizations/mvn_fusion.hpp"
#include "transformations/common_optimizations/convert_tiled_moe_block_to_gather_matmuls.hpp"
#include "transformations/op_conversions/convert_grouped_matmul_to_gather_matmul.hpp"
+#include "transformations/op_conversions/convert_grouped_matmul_to_matmul.hpp"
#include "transformations/common_optimizations/nop_elimination.hpp"
#include "transformations/common_optimizations/rms_fusion.hpp"
#include "transformations/fold_rms_transposes.hpp"
@@ -744,6 +745,11 @@ void TransformationsPipeline::apply(std::shared_ptr<ov::Model> func) {
manager.register_pass<ov::intel_gpu::FuseMOESharedExpert>();
}
}
+
+ if ((!device_info.supports_immad || !config.get_use_onednn()) && !GPU_DEBUG_VALUE_OR(ExecutionConfig::get_enable_mlir(), false)) {
+ manager.register_pass<ov::pass::ConvertGroupedMatMulToMatMul>();
+ }
+
manager.register_pass<ov::pass::GatedDeltaNetFusion>();
manager.register_pass<ov::pass::InitNodeInfo>();
manager.register_pass<EinsumDecomposition>();
diff --git a/src/plugins/intel_gpu/tests/functional/shared_tests_instances/single_layer_tests/grouped_matmul.cpp b/src/plugins/intel_gpu/tests/functional/shared_tests_instances/single_layer_tests/grouped_matmul.cpp
index 5a9b6cb46e4..e09d004c9c0 100644
--- a/src/plugins/intel_gpu/tests/functional/shared_tests_instances/single_layer_tests/grouped_matmul.cpp
+++ b/src/plugins/intel_gpu/tests/functional/shared_tests_instances/single_layer_tests/grouped_matmul.cpp
@@ -4,10 +4,14 @@
#include "single_op_tests/grouped_matmul.hpp"
+#include <algorithm>
+#include <string>
#include <vector>
+#include "common_test_utils/ov_plugin_cache.hpp"
#include "common_test_utils/test_constants.hpp"
#include "openvino/runtime/exec_model_info.hpp"
+#include "openvino/runtime/intel_gpu/properties.hpp"
namespace {
using ov::test::GroupedMatMulCompressedLayerTest;
@@ -17,6 +21,18 @@ using ov::test::GroupedMatMulShapeParams;
using ov::test::TokensPerExpert;
using ov::test::utils::DecompressionType;
+bool immad_supported() {
+ auto core = ov::test::utils::create_core();
+ const auto caps = core.get_property(ov::test::utils::DEVICE_GPU, ov::device::capabilities);
+ return std::find(caps.begin(), caps.end(), ov::intel_gpu::capability::HW_MATMUL) != caps.end();
+}
+
+// When GroupedMatMul unsupported natively it is converted to MatMul by ConvertGroupedMatMulToMatMul:
+// the 2Dx3D case folds into per-group FullyConnected, the 3Dx3D case into a batched Gemm.
+std::string expected_primitive(const std::string& native, const std::string& decomposed) {
+ return immad_supported() ? native : decomposed;
+}
+
class GroupedMatMulCompressedLayerTest_GPU : public GroupedMatMulCompressedLayerTest {
protected:
void check_results() {
@@ -29,8 +45,8 @@ protected:
const bool has_zero_point = (subtract_type != DecompressionType::empty);
auto runtime_model = compiledModel.get_runtime_model();
- auto nodes = ov::test::GetNodesWithTypes(runtime_model, {"FullyConnected", "grouped_matmul"});
- ASSERT_FALSE(nodes.empty()) << "No FullyConnected or grouped_matmul node found in the runtime model";
+ auto nodes = ov::test::GetNodesWithTypes(runtime_model, {"FullyConnected", "grouped_matmul", "Gemm"});
+ ASSERT_FALSE(nodes.empty()) << "No FullyConnected, grouped_matmul, or Gemm node found in the runtime model";
for (const auto& n : nodes) {
const auto& rt = n->get_rt_info();
@@ -88,7 +104,7 @@ INSTANTIATE_TEST_SUITE_P(smoke_GroupedMatMul_f16_2d3d,
::testing::Combine(::testing::ValuesIn(shapes_2d_3d),
::testing::Values(ov::element::f16),
::testing::Values(ov::test::utils::DEVICE_GPU),
- ::testing::Values("GroupedMatMul")),
+ ::testing::Values(expected_primitive("GroupedMatMul", "FullyConnected"))),
GroupedMatMulLayerTest::getTestCaseName);
INSTANTIATE_TEST_SUITE_P(smoke_GroupedMatMul_f16_3d3d,
@@ -96,7 +112,7 @@ INSTANTIATE_TEST_SUITE_P(smoke_GroupedMatMul_f16_3d3d,
::testing::Combine(::testing::ValuesIn(shapes_3d_3d),
::testing::Values(ov::element::f16),
::testing::Values(ov::test::utils::DEVICE_GPU),
- ::testing::Values("GroupedMatMul")),
+ ::testing::Values(expected_primitive("GroupedMatMul", "Gemm"))),
GroupedMatMulLayerTest::getTestCaseName);
INSTANTIATE_TEST_SUITE_P(smoke_GroupedMatMul_Compressed_2d3d,
@@ -111,7 +127,7 @@ INSTANTIATE_TEST_SUITE_P(smoke_GroupedMatMul_Compressed_2d3d,
::testing::Values(true),
::testing::Values(-1, 128),
::testing::Values(ov::test::utils::DEVICE_GPU),
- ::testing::Values("GroupedMatMulCompressed"),
+ ::testing::Values(expected_primitive("GroupedMatMulCompressed", "FullyConnected")),
::testing::Values(ov::AnyMap{})),
GroupedMatMulCompressedLayerTest_GPU::getTestCaseName);
@@ -127,7 +143,7 @@ INSTANTIATE_TEST_SUITE_P(smoke_GroupedMatMul_Compressed_3d3d,
::testing::Values(true),
::testing::Values(-1, 128),
::testing::Values(ov::test::utils::DEVICE_GPU),
- ::testing::Values("GroupedMatMulCompressed"),
+ ::testing::Values(expected_primitive("GroupedMatMulCompressed", "Gemm")),
::testing::Values(ov::AnyMap{})),
GroupedMatMulCompressedLayerTest_GPU::getTestCaseName);
} // namespace
diff --git a/src/plugins/intel_gpu/tests/functional/shared_tests_instances/skip_tests_config.cpp b/src/plugins/intel_gpu/tests/functional/shared_tests_instances/skip_tests_config.cpp
index a8babcba76a..2d8d8c095c6 100644
--- a/src/plugins/intel_gpu/tests/functional/shared_tests_instances/skip_tests_config.cpp
+++ b/src/plugins/intel_gpu/tests/functional/shared_tests_instances/skip_tests_config.cpp
@@ -202,8 +202,6 @@ const std::vector<std::regex>& disabled_test_patterns() {
patterns.push_back(std::regex(R"(.*smoke_MatMulCompressedWeights_3D_weight.*)"));
// MoE patterns are not supported on the platforms without immad (fusion transformation are disabled)
patterns.push_back(std::regex(R"(.*smoke_MoE3GemmCompressedFusion.*)"));
- // Ticket 183099: enable after GroupedMatMul support on GPU
- patterns.push_back(std::regex(R"(.*smoke_GroupedMatMul.*)"));
// Dynamic quantization of fp8 and mxfp8 models supported only through oneDNN (disabled for non-systolic platforms)
patterns.push_back(std::regex(R"(.*smoke_MatMulCompressedWeights_dyn_quan_.*fp8.*)"));
}
diff --git a/src/tests/functional/plugin/shared/src/single_op/grouped_matmul.cpp b/src/tests/functional/plugin/shared/src/single_op/grouped_matmul.cpp
index 44426fe5044..22ce34f47e8 100644
--- a/src/tests/functional/plugin/shared/src/single_op/grouped_matmul.cpp
+++ b/src/tests/functional/plugin/shared/src/single_op/grouped_matmul.cpp
@@ -76,10 +76,10 @@ void GroupedMatMulTestBase::validate() {
SubgraphBaseTest::validate();
// Confirm that the executed network contains the expected ov op type.
-
if (expected_primitive_.empty()) {
return;
}
+
const auto profiling = inferRequest.get_profiling_info();
auto it = std::find_if(profiling.begin(), profiling.end(), [this](const ov::ProfilingInfo& pi) {
return pi.node_type == expected_primitive_;
diff --git a/tests/layer_tests/pytorch_tests/test_grouped_mm.py b/tests/layer_tests/pytorch_tests/test_grouped_mm.py
index 8367bf50426..7507e70b501 100644
--- a/tests/layer_tests/pytorch_tests/test_grouped_mm.py
+++ b/tests/layer_tests/pytorch_tests/test_grouped_mm.py
@@ -2,7 +2,6 @@
# SPDX-License-Identifier: Apache-2.0
import pytest
-import openvino as ov
from pytorch_layer_test_class import PytorchLayerTest
@@ -48,9 +47,6 @@ class TestGroupedMMConstWeights(PytorchLayerTest):
if ie_device.startswith("GPU"):
if self.use_torch_export():
pytest.skip("skip GPU BF16 torch export (FX) grouped_mm with no offsets")
- caps = ov.Core().get_property(ie_device, ov.properties.device.capabilities)
- if "GPU_HW_MATMUL" not in caps:
- pytest.skip("not supported on GPU without GPU_HW_MATMUL (immad)")
if precision == "FP32":
pytest.skip("GPU gather_matmul kernel does not support FP32")
self._test(*self.create_model(b_shape=b_shape, bf16=self.use_torch_export()), ie_device, precision, ir_version,
@@ -95,9 +91,6 @@ class TestGroupedMMOffsetsConstWeights(PytorchLayerTest):
@pytest.mark.precommit_torch_export
def test_grouped_mm_offs_const_b(self, total_tokens, k, n, offsets, ie_device, precision, ir_version):
if ie_device.startswith("GPU"):
- caps = ov.Core().get_property(ie_device, ov.properties.device.capabilities)
- if "GPU_HW_MATMUL" not in caps:
- pytest.skip("not supported on GPU without GPU_HW_MATMUL (immad)")
if precision == "FP32":
pytest.skip("GPU gather_matmul kernel does not support FP32")
self._test(*self.create_model(k=k, n=n, num_groups=len(offsets), bf16=self.use_torch_export()),
@@ -146,9 +139,6 @@ class TestFunctionalGroupedMMConstWeights(PytorchLayerTest):
if ie_device.startswith("GPU"):
if self.use_torch_export():
pytest.skip("skip GPU BF16 torch export (FX) grouped_mm with no offsets")
- caps = ov.Core().get_property(ie_device, ov.properties.device.capabilities)
- if "GPU_HW_MATMUL" not in caps:
- pytest.skip("not supported on GPU without GPU_HW_MATMUL (immad)")
if precision == "FP32":
pytest.skip("GPU gather_matmul kernel does not support FP32")
self._test(*self.create_model(b_shape=b_shape, bf16=self.use_torch_export()), ie_device, precision, ir_version,
@@ -193,9 +183,6 @@ class TestFunctionalGroupedMMOffsetsConstWeights(PytorchLayerTest):
@pytest.mark.precommit_torch_export
def test_functional_grouped_mm_offs_const_b(self, total_tokens, k, n, offsets, ie_device, precision, ir_version):
if ie_device.startswith("GPU"):
- caps = ov.Core().get_property(ie_device, ov.properties.device.capabilities)
- if "GPU_HW_MATMUL" not in caps:
- pytest.skip("not supported on GPU without GPU_HW_MATMUL (immad)")
if precision == "FP32":
pytest.skip("GPU gather_matmul kernel does not support FP32")
self._test(*self.create_model(k=k, n=n, num_groups=len(offsets), bf16=self.use_torch_export()),