Skip to content

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

[GPU] Decompose 2Dx3D GroupedMatMul for non-f16 inference precision (#40003)

Andrew Kwangwoong Park9429e1518841
diff --git a/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp b/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
index a4871255d24..103b61519fb 100644
--- a/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
+++ b/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
@@ -684,6 +684,36 @@ bool has_dequantization_absorbing_consumer(const std::shared_ptr<const ov::Node>
}
return false;
}
+
+// 2Dx3D GroupedMatMul runs on oneDNN grouped_gemm, which supports only f16 activations, so it is decomposed into MatMuls for
+// other activation types. With dynamic inference precision the activation type is kept as is, so it is checked per node.
+// 3Dx3D GroupedMatMul is lowered to a batched FullyConnected and stays as is. Without immad or oneDNN, every GroupedMatMul
+// is decomposed.
+void register_grouped_matmul_decomposition(ov::pass::Manager& manager,
+ const cldnn::device_info& device_info,
+ const ExecutionConfig& config,
+ ov::element::Type infer_precision) {
+ if (GPU_DEBUG_VALUE_OR(ExecutionConfig::get_enable_mlir(), false)) {
+ return;
+ }
+ if (!device_info.supports_immad || !config.get_use_onednn()) {
+ manager.register_pass<ov::pass::ConvertGroupedMatMulToMatMul>();
+ return;
+ }
+ if (infer_precision == ov::element::f16) {
+ return;
+ }
+ auto needs_decomposition = [infer_precision](const std::shared_ptr<const ov::Node>& node) {
+ return node->get_input_partial_shape(0).rank() == 2 && (infer_precision != ov::element::dynamic || node->get_input_element_type(0) != ov::element::f16);
+ };
+ auto pass_config = manager.get_pass_config();
+ // ConvertGroupedMatMulToGroupedMatMulCompressed is registered before: keep the GroupedMatMul to be decomposed uncompressed.
+ pass_config->set_callback<ov::pass::ConvertGroupedMatMulToGroupedMatMulCompressed>(needs_decomposition);
+ pass_config->set_callback<ov::pass::ConvertGroupedMatMul2Dx3DToMatMul>([needs_decomposition](const std::shared_ptr<const ov::Node>& node) {
+ return !needs_decomposition(node);
+ });
+ manager.register_pass<ov::pass::ConvertGroupedMatMul2Dx3DToMatMul>();
+}
} // namespace
bool TransformationsPipeline::fuse_type_to_convert(const std::shared_ptr<ov::Node>& node, const precisions_map& precisions) {
@@ -1151,9 +1181,7 @@ void TransformationsPipeline::apply(std::shared_ptr<ov::Model> func) {
}
}
- if ((!device_info.supports_immad || !config.get_use_onednn()) && !GPU_DEBUG_VALUE_OR(ExecutionConfig::get_enable_mlir(), false)) {
- manager.register_pass<ov::pass::ConvertGroupedMatMulToMatMul>();
- }
+ register_grouped_matmul_decomposition(manager, device_info, config, infer_precision);
manager.register_pass<ov::pass::GatedDeltaNetFusion>();
manager.register_pass<ov::pass::InitNodeInfo>();
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 e09d004c9c0..7c115ab2131 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
@@ -115,6 +115,25 @@ INSTANTIATE_TEST_SUITE_P(smoke_GroupedMatMul_f16_3d3d,
::testing::Values(expected_primitive("GroupedMatMul", "Gemm"))),
GroupedMatMulLayerTest::getTestCaseName);
+// The native 2Dx3D implementation (oneDNN grouped_gemm) supports only f16 activations, so with f32 inference
+// precision (set by core_configuration for f32 models) 2Dx3D GroupedMatMul is decomposed on every device.
+INSTANTIATE_TEST_SUITE_P(smoke_GroupedMatMul_f32_2d3d,
+ GroupedMatMulLayerTest,
+ ::testing::Combine(::testing::ValuesIn(shapes_2d_3d),
+ ::testing::Values(ov::element::f32),
+ ::testing::Values(ov::test::utils::DEVICE_GPU),
+ ::testing::Values(std::string("FullyConnected"))),
+ GroupedMatMulLayerTest::getTestCaseName);
+
+// 3Dx3D GroupedMatMul is lowered to a batched FullyConnected, which also supports f32, so it stays native.
+INSTANTIATE_TEST_SUITE_P(smoke_GroupedMatMul_f32_3d3d,
+ GroupedMatMulLayerTest,
+ ::testing::Combine(::testing::ValuesIn(shapes_3d_3d),
+ ::testing::Values(ov::element::f32),
+ ::testing::Values(ov::test::utils::DEVICE_GPU),
+ ::testing::Values(expected_primitive("GroupedMatMul", "Gemm"))),
+ GroupedMatMulLayerTest::getTestCaseName);
+
INSTANTIATE_TEST_SUITE_P(smoke_GroupedMatMul_Compressed_2d3d,
GroupedMatMulCompressedLayerTest_GPU,
::testing::Combine(::testing::ValuesIn(shapes_2d_3d),
@@ -146,4 +165,56 @@ INSTANTIATE_TEST_SUITE_P(smoke_GroupedMatMul_Compressed_3d3d,
::testing::Values(expected_primitive("GroupedMatMulCompressed", "Gemm")),
::testing::Values(ov::AnyMap{})),
GroupedMatMulCompressedLayerTest_GPU::getTestCaseName);
+
+INSTANTIATE_TEST_SUITE_P(smoke_GroupedMatMul_Compressed_f32_2d3d,
+ GroupedMatMulCompressedLayerTest_GPU,
+ ::testing::Combine(::testing::ValuesIn(shapes_2d_3d),
+ ::testing::Values(ov::element::f32),
+ ::testing::ValuesIn(weights_precisions),
+ ::testing::ValuesIn(decompression_precisions),
+ ::testing::Values(ov::element::f16),
+ ::testing::Values(DecompressionType::full),
+ ::testing::ValuesIn(subtract_types),
+ ::testing::Values(true),
+ ::testing::Values(-1, 128),
+ ::testing::Values(ov::test::utils::DEVICE_GPU),
+ ::testing::Values(std::string("FullyConnected")),
+ ::testing::Values(ov::AnyMap{})),
+ GroupedMatMulCompressedLayerTest_GPU::getTestCaseName);
+
+// With dynamic inference precision (also selected by ov::hint::ExecutionMode::ACCURACY) the activation type is kept
+// as is, so 2Dx3D GroupedMatMul is decomposed for f32 activations and stays native for f16 ones.
+const ov::AnyMap dynamic_precision_config = {ov::hint::inference_precision(ov::element::dynamic)};
+
+INSTANTIATE_TEST_SUITE_P(smoke_GroupedMatMul_Compressed_DynamicPrecision_f32_2d3d,
+ GroupedMatMulCompressedLayerTest_GPU,
+ ::testing::Combine(::testing::ValuesIn(shapes_2d_3d),
+ ::testing::Values(ov::element::f32),
+ ::testing::ValuesIn(weights_precisions),
+ ::testing::ValuesIn(decompression_precisions),
+ ::testing::Values(ov::element::f16),
+ ::testing::Values(DecompressionType::full),
+ ::testing::ValuesIn(subtract_types),
+ ::testing::Values(true),
+ ::testing::Values(-1, 128),
+ ::testing::Values(ov::test::utils::DEVICE_GPU),
+ ::testing::Values(std::string("FullyConnected")),
+ ::testing::Values(dynamic_precision_config)),
+ GroupedMatMulCompressedLayerTest_GPU::getTestCaseName);
+
+INSTANTIATE_TEST_SUITE_P(smoke_GroupedMatMul_Compressed_DynamicPrecision_f16_2d3d,
+ GroupedMatMulCompressedLayerTest_GPU,
+ ::testing::Combine(::testing::ValuesIn(shapes_2d_3d),
+ ::testing::Values(ov::element::f16),
+ ::testing::ValuesIn(weights_precisions),
+ ::testing::ValuesIn(decompression_precisions),
+ ::testing::Values(ov::element::f16),
+ ::testing::Values(DecompressionType::full),
+ ::testing::ValuesIn(subtract_types),
+ ::testing::Values(true),
+ ::testing::Values(-1, 128),
+ ::testing::Values(ov::test::utils::DEVICE_GPU),
+ ::testing::Values(expected_primitive("GroupedMatMulCompressed", "FullyConnected")),
+ ::testing::Values(dynamic_precision_config)),
+ GroupedMatMulCompressedLayerTest_GPU::getTestCaseName);
} // namespace