OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[GPU] Grouped matmul support for GPU (#36666)
Mingyu Kim5c40e8bb741e
diff --git a/src/common/transformations/include/ov_ops/grouped_matmul_compressed.hpp b/src/common/transformations/include/ov_ops/grouped_matmul_compressed.hppnew file mode 100644index 00000000000..ab5e1eb15b2--- /dev/null+++ b/src/common/transformations/include/ov_ops/grouped_matmul_compressed.hpp@@ -0,0 +1,68 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#pragma once++#include <memory>++#include "openvino/core/node.hpp"+#include "openvino/core/node_vector.hpp"+#include "openvino/op/grouped_matmul.hpp"+#include "transformations_visibility.hpp"++namespace ov::op::internal {++/// @brief Internal op mirroring ov::op::v17::GroupedMatMul with extra inputs+/// carrying the weight decompression scale (and optional zero-point).+///+/// Case 1 — 2D x 3D form (with offsets):+/// 0 : mat_a (2D)+/// 1 : mat_b (compressed weight constant, u4/u8/i4/i8/...)+/// 2 : offsets (1D)+/// 3 : decompression scale+/// 4 : (optional) decompression zero-point+///+/// Case 2 — 3D x 3D form (no offsets, batched uniform):+/// 0 : mat_a (3D)+/// 1 : mat_b (compressed weight constant, u4/u8/i4/i8/...)+/// 2 : decompression scale+/// 3 : (optional) decompression zero-point+///+class TRANSFORMATIONS_API GroupedMatMulCompressed : public ov::op::v17::GroupedMatMul {+public:+ OPENVINO_OP("GroupedMatMulCompressed", "", ov::op::v17::GroupedMatMul);++ GroupedMatMulCompressed() = default;++ // 2D x 3D constructors (with offsets)+ GroupedMatMulCompressed(const ov::Output<Node>& mat_a,+ const ov::Output<Node>& mat_b,+ const ov::Output<Node>& offsets,+ const ov::Output<Node>& decompression_scale);++ GroupedMatMulCompressed(const ov::Output<Node>& mat_a,+ const ov::Output<Node>& mat_b,+ const ov::Output<Node>& offsets,+ const ov::Output<Node>& decompression_scale,+ const ov::Output<Node>& decompression_zero_point);++ // 3D x 3D factories (no offsets). Exposed as static functions to avoid a+ // 4-argument constructor overload collision with the 2D x 3D form above.+ static std::shared_ptr<GroupedMatMulCompressed> make_3d(const ov::Output<Node>& mat_a,+ const ov::Output<Node>& mat_b,+ const ov::Output<Node>& decompression_scale);++ static std::shared_ptr<GroupedMatMulCompressed> make_3d(const ov::Output<Node>& mat_a,+ const ov::Output<Node>& mat_b,+ const ov::Output<Node>& decompression_scale,+ const ov::Output<Node>& decompression_zero_point);++ /// @brief True if the op carries an offsets input (2D x 3D form).+ bool has_offsets() const;++ void validate_and_infer_types() override;+ std::shared_ptr<ov::Node> clone_with_new_inputs(const ov::OutputVector& new_args) const override;+};++} // namespace ov::op::internaldiff --git a/src/common/transformations/include/transformations/op_conversions/convert_grouped_matmul_to_compressed.hpp b/src/common/transformations/include/transformations/op_conversions/convert_grouped_matmul_to_compressed.hppnew file mode 100644index 00000000000..e2c1f948262--- /dev/null+++ b/src/common/transformations/include/transformations/op_conversions/convert_grouped_matmul_to_compressed.hpp@@ -0,0 +1,26 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#pragma once++#include <vector>++#include "openvino/core/type/element_type.hpp"+#include "openvino/pass/matcher_pass.hpp"+#include "transformations_visibility.hpp"++namespace ov::pass {++// Matches v17::GroupedMatMul in both legal input arities:+// - 2 inputs (3D x 3D form): GroupedMatMul(data, compressed_weights)+// - 3 inputs (2D x 3D form): GroupedMatMul(data, compressed_weights, offsets)+// and rewrites it into ov::op::internal::GroupedMatMulCompressed.+class TRANSFORMATIONS_API ConvertGroupedMatMulToGroupedMatMulCompressed : public ov::pass::MatcherPass {+public:+ OPENVINO_MATCHER_PASS_RTTI("ConvertGroupedMatMulToGroupedMatMulCompressed");+ explicit ConvertGroupedMatMulToGroupedMatMulCompressed(+ const std::vector<ov::element::Type>& supported_weights_types);+};++} // namespace ov::passdiff --git a/src/common/transformations/src/ov_ops/grouped_matmul_compressed.cpp b/src/common/transformations/src/ov_ops/grouped_matmul_compressed.cppnew file mode 100644index 00000000000..6ac97d6b074--- /dev/null+++ b/src/common/transformations/src/ov_ops/grouped_matmul_compressed.cpp@@ -0,0 +1,81 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "ov_ops/grouped_matmul_compressed.hpp"++#include "grouped_matmul_shape_inference.hpp"++namespace ov::op::internal {++GroupedMatMulCompressed::GroupedMatMulCompressed(const ov::Output<Node>& mat_a,+ const ov::Output<Node>& mat_b,+ const ov::Output<Node>& offsets,+ const ov::Output<Node>& decompression_scale)+ : ov::op::v17::GroupedMatMul() {+ set_arguments(ov::OutputVector{mat_a, mat_b, offsets, decompression_scale});+ validate_and_infer_types();+}++GroupedMatMulCompressed::GroupedMatMulCompressed(const ov::Output<Node>& mat_a,+ const ov::Output<Node>& mat_b,+ const ov::Output<Node>& offsets,+ const ov::Output<Node>& decompression_scale,+ const ov::Output<Node>& decompression_zero_point)+ : ov::op::v17::GroupedMatMul() {+ set_arguments(ov::OutputVector{mat_a, mat_b, offsets, decompression_scale, decompression_zero_point});+ validate_and_infer_types();+}++std::shared_ptr<GroupedMatMulCompressed> GroupedMatMulCompressed::make_3d(const ov::Output<Node>& mat_a,+ const ov::Output<Node>& mat_b,+ const ov::Output<Node>& decompression_scale) {+ auto op = std::make_shared<GroupedMatMulCompressed>();+ op->set_arguments(ov::OutputVector{mat_a, mat_b, decompression_scale});+ op->validate_and_infer_types();+ return op;+}++std::shared_ptr<GroupedMatMulCompressed> GroupedMatMulCompressed::make_3d(+ const ov::Output<Node>& mat_a,+ const ov::Output<Node>& mat_b,+ const ov::Output<Node>& decompression_scale,+ const ov::Output<Node>& decompression_zero_point) {+ auto op = std::make_shared<GroupedMatMulCompressed>();+ op->set_arguments(ov::OutputVector{mat_a, mat_b, decompression_scale, decompression_zero_point});+ op->validate_and_infer_types();+ return op;+}++bool GroupedMatMulCompressed::has_offsets() const {+ // The 2D x 3D form always carries an offsets input; the 3D x 3D form does not.+ // Detection uses the rank of mat_a since scale/zp are always attached after the+ // base GroupedMatMul inputs.+ const auto& a_rank = get_input_partial_shape(0).rank();+ return a_rank.is_static() && a_rank.get_length() == 2;+}++void GroupedMatMulCompressed::validate_and_infer_types() {+ const auto input_size = get_input_size();+ NODE_VALIDATION_CHECK(this,+ input_size >= 3,+ "GroupedMatMulCompressed expects at least 3 inputs, got: ",+ input_size);++ std::vector<ov::PartialShape> input_shapes{get_input_partial_shape(0), get_input_partial_shape(1)};+ if (has_offsets()) {+ input_shapes.push_back(get_input_partial_shape(2));+ }+ const auto out_shapes = ov::op::v17::shape_infer(this, input_shapes);+ set_output_type(0, get_input_element_type(0), out_shapes[0]);+}++std::shared_ptr<ov::Node> GroupedMatMulCompressed::clone_with_new_inputs(const ov::OutputVector& new_args) const {+ check_new_args_count(this, new_args);+ auto op = std::make_shared<GroupedMatMulCompressed>();+ op->set_arguments(new_args);+ op->validate_and_infer_types();+ return op;+}++} // namespace ov::op::internaldiff --git a/src/common/transformations/src/transformations/op_conversions/convert_grouped_matmul_to_compressed.cpp b/src/common/transformations/src/transformations/op_conversions/convert_grouped_matmul_to_compressed.cppnew file mode 100644index 00000000000..a81b1fdb2ff--- /dev/null+++ b/src/common/transformations/src/transformations/op_conversions/convert_grouped_matmul_to_compressed.cpp@@ -0,0 +1,119 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "transformations/op_conversions/convert_grouped_matmul_to_compressed.hpp"++#include <cstddef>+#include <memory>+#include <set>+#include <vector>++#include "openvino/core/graph_util.hpp"+#include "openvino/core/node_vector.hpp"+#include "openvino/core/rt_info.hpp"+#include "openvino/core/type/element_type.hpp"+#include "openvino/op/grouped_matmul.hpp"+#include "openvino/pass/pattern/op/or.hpp"+#include "openvino/pass/pattern/op/pattern.hpp"+#include "openvino/pass/pattern/op/wrap_type.hpp"+#include "openvino/util/pp.hpp"+#include "ov_ops/grouped_matmul_compressed.hpp"+#include "transformations/op_conversions/convert_fc_to_compressed.hpp"+#include "transformations/pattern_blocks/compressed_weights_block.hpp"++ov::pass::ConvertGroupedMatMulToGroupedMatMulCompressed::ConvertGroupedMatMulToGroupedMatMulCompressed(+ const std::vector<ov::element::Type>& supported_weights_types) {+ using namespace ov::pass::pattern;+ using ov::op::internal::GroupedMatMulCompressed;++ auto data_2d_m = any_input(rank_equals(2));+ auto data_3d_m = any_input(rank_equals(3));+ auto offsets_m = any_input();+ auto weights_block =+ std::make_shared<ov::pass::pattern::op::CompressedWeightsBlock>(supported_weights_types, std::set<size_t>{3});++ // v17::GroupedMatMul has two legal input arities. Match both with a single Or root so+ // one MatcherPass covers 2D x 3D (with offsets) and 3D x 3D (no offsets). Rank is+ // enforced by the pattern predicates above so the callback doesn't need to re-check it.+ auto gmm_no_offsets_m = wrap_type<ov::op::v17::GroupedMatMul>({data_3d_m, weights_block});+ auto gmm_with_offsets_m = wrap_type<ov::op::v17::GroupedMatMul>({data_2d_m, weights_block, offsets_m});+ auto gmm_m = std::make_shared<ov::pass::pattern::op::Or>(ov::OutputVector{gmm_no_offsets_m, gmm_with_offsets_m});++ ov::matcher_pass_callback callback = [OV_CAPTURE_CPY_AND_THIS](ov::pass::pattern::Matcher& m) {+ const auto& pattern_map = m.get_pattern_value_map();+ auto gmm = ov::as_type_ptr<ov::op::v17::GroupedMatMul>(m.get_match_root());+ if (!gmm || transformation_callback(gmm)) {+ return false;+ }++ // If the CompressedWeightsBlock matched a Transpose on the weights branch, the+ // original graph is producing [G, N, K] mat_b from a differently-laid-out constant.+ // The shared helper bakes that Transpose into the weights/scale/zp constants when+ // `has_transpose=true`, so the emitted GroupedMatMulCompressed still sees [G, N, K].+ const bool has_transpose = weights_block->get_anchor("transpose", pattern_map).has_value();++ // Which arm of the Or matched — 2D x 3D (with offsets) or 3D x 3D (no offsets).+ const bool with_offsets = pattern_map.count(data_2d_m) > 0;+ const auto& data_value = with_offsets ? pattern_map.at(data_2d_m) : pattern_map.at(data_3d_m);++ const auto& weights_shape = gmm->get_input_shape(1);+ // GroupedMatMul-17 always has rank-3 mat_b ([G, N, K])+ OPENVINO_ASSERT(weights_shape.size() == 3,+ "GroupedMatMul mat_b must be rank 3, got rank ",+ weights_shape.size());++ // If the scale/zero-point has one extra dimension (rank 4), the K axis has been split+ // into sub-groups that each share a scale, e.g. shape [G, N, K/group_size, group_size].+ const auto scale_shape = weights_block->get_anchor("mul_const", pattern_map).value().get_shape();+ const bool grouped = scale_shape.size() == weights_shape.size() + 1;++ ov::NodeVector result_nodes;+ // `batched_weights=true` selects a final weights rank of 3 in the shared helper, which+ // matches the rank-3 mat_b of GroupedMatMul.+ const auto [gmm_input_b, gmm_input_scale, gmm_input_zp] =+ ov::pass::ConvertFullyConnectedToFullyConnectedCompressed::process_compressed_weights(+ weights_block,+ pattern_map,+ /*convert_u4zp_to_u8=*/true,+ has_transpose,+ grouped,+ /*batched_weights=*/true,+ result_nodes);++ const bool with_zero_point = weights_block->get_anchor("sub_no_convert", pattern_map).has_value() ||+ weights_block->get_anchor("sub_with_convert", pattern_map).has_value();++ std::shared_ptr<ov::Node> new_gmm;+ if (with_offsets) {+ const auto& gmm_input_offsets = gmm->input_value(2);+ if (with_zero_point) {+ new_gmm = std::make_shared<GroupedMatMulCompressed>(data_value,+ gmm_input_b,+ gmm_input_offsets,+ gmm_input_scale,+ gmm_input_zp);+ } else {+ new_gmm = std::make_shared<GroupedMatMulCompressed>(data_value,+ gmm_input_b,+ gmm_input_offsets,+ gmm_input_scale);+ }+ } else {+ if (with_zero_point) {+ new_gmm = GroupedMatMulCompressed::make_3d(data_value, gmm_input_b, gmm_input_scale, gmm_input_zp);+ } else {+ new_gmm = GroupedMatMulCompressed::make_3d(data_value, gmm_input_b, gmm_input_scale);+ }+ }++ result_nodes.push_back(new_gmm);+ new_gmm->set_friendly_name(gmm->get_friendly_name());+ ov::copy_runtime_info(m.get_matched_nodes(), result_nodes);+ ov::replace_node(gmm, new_gmm);+ return true;+ };++ auto matcher = std::make_shared<ov::pass::pattern::Matcher>(gmm_m, "ConvertGroupedMatMulToGroupedMatMulCompressed");+ this->register_matcher(matcher, callback);+}diff --git a/src/plugins/intel_gpu/include/intel_gpu/plugin/primitives_list.hpp b/src/plugins/intel_gpu/include/intel_gpu/plugin/primitives_list.hppindex d326a3ad5c9..e5ec7628baa 100644--- a/src/plugins/intel_gpu/include/intel_gpu/plugin/primitives_list.hpp+++ b/src/plugins/intel_gpu/include/intel_gpu/plugin/primitives_list.hpp@@ -289,6 +289,7 @@ REGISTER_FACTORY(v16, SegmentMax);// ------------------------------ Supported v17 ops ----------------------------- //REGISTER_FACTORY(v17, ErfInv);+REGISTER_FACTORY(v17, GroupedMatMul);// --------------------------- Supported internal ops --------------------------- //REGISTER_FACTORY(internal, NonMaxSuppressionIEInternal);@@ -325,4 +326,5 @@ REGISTER_FACTORY(internal, GatedDeltaNet);REGISTER_FACTORY(internal, PagedCausalConv1D);REGISTER_FACTORY(internal, GatherMatmul);REGISTER_FACTORY(internal, GatherMatmulCompressed);+REGISTER_FACTORY(internal, GroupedMatMulCompressed);REGISTER_FACTORY(internal, Atan2);diff --git a/src/plugins/intel_gpu/include/intel_gpu/primitives/grouped_matmul.hpp b/src/plugins/intel_gpu/include/intel_gpu/primitives/grouped_matmul.hppnew file mode 100644index 00000000000..d6690002027--- /dev/null+++ b/src/plugins/intel_gpu/include/intel_gpu/primitives/grouped_matmul.hpp@@ -0,0 +1,128 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#pragma once+#include <optional>++#include "primitive.hpp"++namespace cldnn {++/// @brief Grouped matmul for MoE forward pass (2D x 3D case).+/// @details+/// Computes multiple matrix multiplications where each group processes a subset+/// of the input rows. Matches ov::op::v17::GroupedMatMul 2D x 3D semantics.+/// 3D x 3D case is handled by vanilla matmul, instead of grouped matmul+/// because the behaviors are identical.+///+/// mat_a : [total_tokens, K] - rows partitioned by @p offsets+/// mat_b : [G, N, K] - per-group weights, stored transposed (B^T)+/// offsets : [G] (s32) - cumulative end-offsets partitioning mat_a rows+/// output : [total_tokens, N]+///+/// The primitive also supports a compressed weights variant, in which mat_b is a+/// low-precision (u4/i4/u8/i8) constant and additional inputs carry the per-group+/// dequantization scale (and optional zero-point). At runtime, the oneDNN impl+/// consumes the compressed weight directly via primitive attribute scales / zero-points.+struct grouped_matmul : public primitive_base<grouped_matmul> {+ CLDNN_DECLARE_PRIMITIVE(grouped_matmul)++ enum GroupedMatmulInputIdx {+ INPUT = 0, // mat_a : [total_tokens, K]+ WEIGHT = 1, // mat_b : [G, N, K]+ OFFSETS = 2, // offsets: [G] cumulative end-offsets (s32)+ };++ grouped_matmul() : primitive_base("", {}) {}++ /// @brief Constructs a non-compressed grouped matmul.+ grouped_matmul(const primitive_id& id,+ const std::vector<input_info>& inputs,+ const data_types output_data_type)+ : primitive_base(id, inputs, 1, {optional_data_type{output_data_type}}) {}++ /// @brief Constructs a compressed-weights grouped matmul.+ /// @param inputs Base inputs [mat_a, mat_b, offsets]. Scale/zp are attached+ /// separately via @p decompression_scale / @p decompression_zero_point+ /// so that shape-inference dependencies remain limited to mat_a/mat_b.+ grouped_matmul(const primitive_id& id,+ const std::vector<input_info>& inputs,+ const input_info& decompression_scale,+ const input_info& decompression_zero_point,+ const data_types output_data_type)+ : primitive_base(id, inputs, 1, {optional_data_type{output_data_type}}),+ compressed_weights(true),+ decompression_scale(decompression_scale),+ decompression_zero_point(decompression_zero_point) {+ OPENVINO_ASSERT(decompression_scale.is_valid(),+ "[GPU] Compressed grouped_matmul requires a decompression scale input");+ }++ bool compressed_weights = false;+ input_info decompression_scale = {};+ input_info decompression_zero_point = {};+ std::optional<float> decompression_zero_point_scalar = std::optional<float>();++ size_t hash() const override {+ size_t seed = primitive::hash();+ seed = hash_combine(seed, compressed_weights);+ seed = hash_combine(seed, decompression_scale.is_valid());+ seed = hash_combine(seed, decompression_zero_point.is_valid());+ seed = hash_combine(seed, decompression_zero_point_scalar.has_value());+ seed = hash_combine(seed, decompression_zero_point_scalar.value_or(0.0f));+ return seed;+ }++ bool operator==(const primitive& rhs) const override {+ if (!compare_common_params(rhs))+ return false;+ auto rhs_casted = downcast<const grouped_matmul>(rhs);+ return compressed_weights == rhs_casted.compressed_weights &&+ decompression_scale.is_valid() == rhs_casted.decompression_scale.is_valid() &&+ decompression_zero_point.is_valid() == rhs_casted.decompression_zero_point.is_valid() &&+ decompression_zero_point_scalar.value_or(0.0f) ==+ rhs_casted.decompression_zero_point_scalar.value_or(0.0f);+ }++ void save(BinaryOutputBuffer& ob) const override {+ primitive_base<grouped_matmul>::save(ob);+ ob << compressed_weights;+ ob << decompression_scale;+ ob << decompression_zero_point;+ if (decompression_zero_point_scalar.has_value()) {+ ob << true;+ ob << make_data(&decompression_zero_point_scalar.value(), sizeof(float));+ } else {+ ob << false;+ }+ }++ void load(BinaryInputBuffer& ib) override {+ primitive_base<grouped_matmul>::load(ib);+ ib >> compressed_weights;+ ib >> decompression_scale;+ ib >> decompression_zero_point;+ bool has_value = false;+ ib >> has_value;+ if (has_value) {+ float v = 0.f;+ ib >> make_data(&v, sizeof(float));+ decompression_zero_point_scalar = v;+ } else {+ decompression_zero_point_scalar = std::optional<float>();+ }+ }++protected:+ std::map<size_t, const input_info*> get_dependencies_map() const override {+ auto ret = std::map<size_t, const input_info*>{};+ auto idx = input.size();+ if (decompression_scale.is_valid())+ ret[idx++] = &decompression_scale;+ if (decompression_zero_point.is_valid())+ ret[idx++] = &decompression_zero_point;+ return ret;+ }+};+} // namespace cldnndiff --git a/src/plugins/intel_gpu/src/graph/grouped_matmul.cpp b/src/plugins/intel_gpu/src/graph/grouped_matmul.cppnew file mode 100644index 00000000000..1c294a70379--- /dev/null+++ b/src/plugins/intel_gpu/src/graph/grouped_matmul.cpp@@ -0,0 +1,62 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "grouped_matmul_inst.h"+#include "primitive_type_base.h"+#include "json_object.h"+#include "to_string_utils.h"++#include <string>+#include <vector>++namespace cldnn {+GPU_DEFINE_PRIMITIVE_TYPE_ID(grouped_matmul)++layout grouped_matmul_inst::calc_output_layout(grouped_matmul_node const& node, kernel_impl_params const& impl_param) {+ return calc_output_layouts<ov::PartialShape>(node, impl_param)[0];+}++template <typename ShapeType>+std::vector<layout> grouped_matmul_inst::calc_output_layouts(grouped_matmul_node const& /*node*/,+ const kernel_impl_params& impl_param) {+ // mat_a: [total_tokens, K], mat_b: [G, N, K], out: [total_tokens, N]+ auto input_layout = impl_param.get_input_layout(grouped_matmul::GroupedMatmulInputIdx::INPUT);+ auto weight_layout = impl_param.get_input_layout(grouped_matmul::GroupedMatmulInputIdx::WEIGHT);++ const auto& a_pshape = input_layout.get_partial_shape();+ const auto& b_pshape = weight_layout.get_partial_shape();++ OPENVINO_ASSERT(a_pshape.rank().is_static() && a_pshape.size() == 2,+ "grouped_matmul mat_a rank must be equal to 2, got ", a_pshape);+ OPENVINO_ASSERT(b_pshape.rank().is_static() && b_pshape.size() == 3,+ "grouped_matmul mat_b rank must be equal to 3, got ", b_pshape);++ const auto desc = impl_param.typed_desc<grouped_matmul>();+ const auto output_type = desc->output_data_types[0].value_or(input_layout.data_type);++ ov::PartialShape output_pshape{a_pshape[0], b_pshape[1]};+ return {layout{output_pshape, output_type, input_layout.format}};+}++template std::vector<layout> grouped_matmul_inst::calc_output_layouts<ov::PartialShape>(+ grouped_matmul_node const& node,+ const kernel_impl_params& impl_param);++std::string grouped_matmul_inst::to_string(grouped_matmul_node const& node) {+ auto node_info = node.desc_to_json();+ auto desc = node.get_primitive();++ std::stringstream primitive_description;++ json_composite info;+ if (desc->output_data_types[0].has_value())+ info.add("out dt: ", dt_to_str(*desc->output_data_types[0]));+ node_info->add("grouped_matmul_info", info);+ node_info->dump(primitive_description);++ return primitive_description.str();+}++grouped_matmul_inst::typed_primitive_inst(network& network, grouped_matmul_node const& node) : parent(network, node) {}+} // namespace cldnndiff --git a/src/plugins/intel_gpu/src/graph/impls/onednn/grouped_gemm_onednn.cpp b/src/plugins/intel_gpu/src/graph/impls/onednn/grouped_gemm_onednn.cppnew file mode 100644index 00000000000..2175d7890bc--- /dev/null+++ b/src/plugins/intel_gpu/src/graph/impls/onednn/grouped_gemm_onednn.cpp@@ -0,0 +1,222 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "grouped_gemm_onednn.hpp"+#include "intel_gpu/runtime/utils.hpp"+#include "primitive_onednn_base.h"+#include "utils.hpp"++#include <oneapi/dnnl/dnnl.hpp>++#include <memory>++namespace cldnn {+namespace onednn {++// Executes the GroupedMatMul-17 2D x 3D case via dnnl::matmul with grouped+// memory encoding. Layout:+// mat_a : [total_tokens, K] -> dnnl grouped src, variable dim = 0+// mat_b : [G, N, K] -> dnnl weights, format_tag::acb (logical [G, K, N])+// offsets : [G] s32 -> attached to src/dst as buffer 1+// out : [total_tokens, N] -> dnnl grouped dst, variable dim = 0+struct grouped_gemm_onednn : typed_primitive_onednn_impl<grouped_matmul> {+ using parent = typed_primitive_onednn_impl<grouped_matmul>;+ using parent::parent;++ DECLARE_OBJECT_TYPE_SERIALIZATION(cldnn::onednn::grouped_gemm_onednn)++ // Weights-quantization mask bits for oneDNN grouped matmul weights logical layout [G, K, N]:+ // bit 0 -> G (expert / batch), bit 1 -> K (reduction), bit 2 -> N (output channels).+ // Weights zero-points/scales masks accepted by grouped_micro_gemm: only PER_OC or GROUPED.+ static constexpr int WEI_MASK_G = 1 << 0;+ static constexpr int WEI_MASK_K = 1 << 1;+ static constexpr int WEI_MASK_N = 1 << 2;+ static constexpr int WEI_MASK_PER_OC = WEI_MASK_G | WEI_MASK_N; // mask 5+ static constexpr int WEI_MASK_GROUPED = WEI_MASK_G | WEI_MASK_K | WEI_MASK_N; // mask 7++protected:+ std::unique_ptr<primitive_impl> clone() const override {+ return std::make_unique<grouped_gemm_onednn>(*this);+ }++ std::unordered_map<int, dnnl::memory> get_arguments(grouped_matmul_inst& instance) const override {+ std::unordered_map<int, dnnl::memory> args;++ auto& input = instance.input_memory(grouped_matmul::GroupedMatmulInputIdx::INPUT);+ auto& weights = instance.input_memory(grouped_matmul::GroupedMatmulInputIdx::WEIGHT);+ auto& offsets = instance.input_memory(grouped_matmul::GroupedMatmulInputIdx::OFFSETS);+ auto& output = instance.output_memory(0);++ args.insert({DNNL_ARG_SRC, input.get_onednn_grouped_memory(_pd.src_desc(0), offsets)});+ args.insert({DNNL_ARG_WEIGHTS, weights.get_onednn_memory(_pd.weights_desc(0), 0)});+ args.insert({DNNL_ARG_DST, output.get_onednn_grouped_memory(_pd.dst_desc(0), offsets)});++ const auto& prim = instance.get_impl_params()->typed_desc<grouped_matmul>();+ if (prim->compressed_weights) {+ // scale/zp are attached as extra dependencies after the 3 regular inputs.+ size_t idx = grouped_matmul::GroupedMatmulInputIdx::OFFSETS + 1;++ OPENVINO_ASSERT(prim->decompression_scale.is_valid(),+ "[GPU] Compressed grouped_matmul requires a decompression scale");+ auto scale_mem = instance.dep_memory_ptr(idx++);+ const auto& scale_shape = scale_mem->get_layout().get_shape();+ const auto& wei_shape = instance.get_input_layout(grouped_matmul::GroupedMatmulInputIdx::WEIGHT).get_shape();+ OPENVINO_ASSERT(wei_shape.size() == 3, "[GPU] grouped_matmul weights must be rank 3");+ const dnnl::memory::dim G = static_cast<dnnl::memory::dim>(wei_shape[0]);+ const dnnl::memory::dim N = static_cast<dnnl::memory::dim>(wei_shape[1]);+ const dnnl::memory::dim K = static_cast<dnnl::memory::dim>(wei_shape[2]);++ dnnl::memory::dims scale_dims;+ dnnl::memory::format_tag scale_fmt;+ if (scale_shape.size() == 2) {+ // Per-OC: scale is [G, N]. oneDNN weights are [G, K, N]+ scale_dims = dnnl::memory::dims{G, N};+ scale_fmt = dnnl::memory::format_tag::ab;+ } else if (scale_shape.size() == 3) {+ // Grouped: scale physical is [G, N, K/gs]; onednn expects logical [G, K/gs, N] fmt abc.+ const dnnl::memory::dim num_groups = static_cast<dnnl::memory::dim>(scale_shape[2]);+ OPENVINO_ASSERT(num_groups > 0 && K % num_groups == 0,+ "[GPU] grouped_matmul scale groups (", num_groups,+ ") must evenly divide K (", K, ")");+ scale_dims = dnnl::memory::dims{G, num_groups, N};+ scale_fmt = dnnl::memory::format_tag::abc;+ } else {+ OPENVINO_THROW("[GPU] grouped_matmul scale must be rank 2 or 3, got ", scale_shape.size());+ }+ dnnl::memory::desc scale_md(scale_dims, convert_data_type(scale_mem->get_layout().data_type), scale_fmt);+ args.insert({DNNL_ARG_ATTR_SCALES | DNNL_ARG_WEIGHTS, scale_mem->get_onednn_memory(scale_md, 0)});++ if (prim->decompression_zero_point.is_valid()) {+ auto zp_mem = instance.dep_memory_ptr(idx++);+ const auto& zp_layout = zp_mem->get_layout();+ const auto zp_dt = convert_data_type(zp_layout.data_type);+ OPENVINO_ASSERT(zp_layout.count() > 1, "[GPU] grouped_matmul zero-point must be rank > 0");+ const auto& zp_shape = zp_layout.get_shape();+ OPENVINO_ASSERT(zp_shape == scale_shape,+ "[GPU] OneDNN requires that grouped_matmul zero-point shape ", zp_shape,+ " must match scale shape ", scale_shape);+ dnnl::memory::desc zp_md(scale_dims, zp_dt, scale_fmt);+ args.insert({DNNL_ARG_ATTR_ZERO_POINTS | DNNL_ARG_WEIGHTS, zp_mem->get_onednn_memory(zp_md, 0)});+ }+ }++ return args;+ }++ static std::shared_ptr<dnnl::matmul::primitive_desc> get_primitive_descriptor(+ const kernel_impl_params& impl_params,+ const dnnl::primitive_attr& attr = dnnl::primitive_attr()) {+ auto& engine = impl_params.prog->get_engine();++ const auto& in_layout = impl_params.get_input_layout(grouped_matmul::GroupedMatmulInputIdx::INPUT);+ const auto& wei_layout = impl_params.get_input_layout(grouped_matmul::GroupedMatmulInputIdx::WEIGHT);+ const auto& out_layout = impl_params.get_output_layout();++ const auto& in_shape = in_layout.get_shape();+ const auto& wei_shape = wei_layout.get_shape();++ OPENVINO_ASSERT(in_shape.size() == 2,+ "grouped_gemm_onednn expects mat_a rank 2, got ", in_shape.size());+ OPENVINO_ASSERT(wei_shape.size() == 3,+ "grouped_gemm_onednn expects mat_b rank 3, got ", wei_shape.size());++ const dnnl::memory::dim total_tokens = static_cast<dnnl::memory::dim>(in_shape[0]);+ const dnnl::memory::dim K = static_cast<dnnl::memory::dim>(in_shape[1]);+ const dnnl::memory::dim num_experts = static_cast<dnnl::memory::dim>(wei_shape[0]);+ const dnnl::memory::dim N = static_cast<dnnl::memory::dim>(wei_shape[1]);+ OPENVINO_ASSERT(static_cast<dnnl::memory::dim>(wei_shape[2]) == K,+ "K must match between weight and input: ", wei_shape[2], " - ", K);++ // dnnl grouped matmul: src=[T,K] with variable dim 0, weights logical=[G,K,N] stored acb (=> physical [G,N,K]).+ auto src_md = dnnl::memory::desc::grouped(dnnl::memory::dims{total_tokens, K},+ convert_data_type(in_layout.data_type),+ /*variable_dim_idx=*/0,+ num_experts);+ auto dst_md = dnnl::memory::desc::grouped(dnnl::memory::dims{total_tokens, N},+ convert_data_type(out_layout.data_type),+ /*variable_dim_idx=*/0,+ num_experts);+ auto wei_md = dnnl::memory::desc(dnnl::memory::dims{num_experts, K, N},+ convert_data_type(wei_layout.data_type),+ dnnl::memory::format_tag::acb);++ return std::make_shared<dnnl::matmul::primitive_desc>(engine.get_onednn_engine(),+ src_md,+ wei_md,+ dst_md,+ attr);+ }++public:++ static std::unique_ptr<primitive_impl> create(const grouped_matmul_node& arg,+ const kernel_impl_params& impl_params) {+ auto& engine = impl_params.prog->get_engine();+ auto& config = impl_params.prog->get_config();+ auto attr = impl_params.attrs_onednn;++ const auto& prim = impl_params.typed_desc<grouped_matmul>();+ if (prim->compressed_weights) {+ size_t idx = grouped_matmul::GroupedMatmulInputIdx::OFFSETS + 1;++ const auto& scale_layout = arg.get_dependency(idx++).get_output_layout();+ const auto scale_dt = convert_data_type(scale_layout.data_type);+ const auto& scale_shape = scale_layout.get_shape();+ const auto& wei_shape = impl_params.get_input_layout(grouped_matmul::GroupedMatmulInputIdx::WEIGHT).get_shape();+ OPENVINO_ASSERT(wei_shape.size() == 3, "[GPU] grouped_matmul weights must be rank 3");+ const int64_t K = static_cast<int64_t>(wei_shape[2]);++ // Mask 5 = bit0(G) | bit2(N) for per-OC. Mask 7 adds bit1(K) for grouped-K.+ if (scale_shape.size() == 2) {+ attr->set_scales(DNNL_ARG_WEIGHTS, WEI_MASK_PER_OC, {}, scale_dt);+ } else if (scale_shape.size() == 3) {+ const int64_t num_groups = static_cast<int64_t>(scale_shape[2]);+ OPENVINO_ASSERT(num_groups > 0 && K % num_groups == 0,+ "[GPU] grouped_matmul scale groups (", num_groups,+ ") must evenly divide K (", K, ")");+ const dnnl::memory::dim group_size = K / num_groups;+ attr->set_scales(DNNL_ARG_WEIGHTS, WEI_MASK_GROUPED, {group_size, 1}, scale_dt);+ } else {+ OPENVINO_THROW("[GPU] Unexpected decompression scale rank ", scale_shape.size());+ }++ if (prim->decompression_zero_point.is_valid()) {+ const auto& zp_layout = arg.get_dependency(idx++).get_output_layout();+ const auto zp_dt = convert_data_type(zp_layout.data_type);+ const auto& zp_shape = zp_layout.get_shape();+ OPENVINO_ASSERT(zp_layout.count() > 1, "[GPU] grouped_matmul zero-point must be rank > 0");+ if (zp_shape.size() == 2) {+ attr->set_zero_points(DNNL_ARG_WEIGHTS, WEI_MASK_PER_OC, {}, zp_dt);+ } else if (zp_shape.size() == 3) {+ const int64_t num_groups = static_cast<int64_t>(zp_shape[2]);+ OPENVINO_ASSERT(num_groups > 0 && K % num_groups == 0,+ "[GPU] grouped_matmul zp groups (", num_groups,+ ") must evenly divide K (", K, ")");+ const dnnl::memory::dim group_size = K / num_groups;+ attr->set_zero_points(DNNL_ARG_WEIGHTS,+ WEI_MASK_GROUPED,+ {group_size, 1},+ zp_dt);+ } else {+ OPENVINO_THROW("[GPU] Unexpected decompression zero-point rank ", zp_shape.size());+ }+ }+ }++ auto prim_desc = get_primitive_descriptor(impl_params, *attr);+ return std::make_unique<grouped_gemm_onednn>(engine, config, attr, *prim_desc);+ }+};++std::unique_ptr<primitive_impl> GroupedMatmulImplementationManager::create_impl(const program_node& node,+ const kernel_impl_params& params) const {+ OPENVINO_ASSERT(node.is_type<grouped_matmul>());+ return onednn::grouped_gemm_onednn::create(static_cast<const grouped_matmul_node&>(node), params);+}++} // namespace onednn+} // namespace cldnn++BIND_BINARY_BUFFER_WITH_TYPE(cldnn::grouped_matmul)+BIND_BINARY_BUFFER_WITH_TYPE(cldnn::onednn::grouped_gemm_onednn)diff --git a/src/plugins/intel_gpu/src/graph/impls/onednn/grouped_gemm_onednn.hpp b/src/plugins/intel_gpu/src/graph/impls/onednn/grouped_gemm_onednn.hppnew file mode 100644index 00000000000..e6e6cf90396--- /dev/null+++ b/src/plugins/intel_gpu/src/graph/impls/onednn/grouped_gemm_onednn.hpp@@ -0,0 +1,92 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#pragma once+#ifndef ENABLE_ONEDNN_FOR_GPU+ #define ENABLE_ONEDNN_FOR_GPU 1+#endif++#include "grouped_matmul_inst.h"+#include "intel_gpu/runtime/utils.hpp"+#include "registry/implementation_manager.hpp"++#include <memory>++namespace cldnn {+namespace onednn {++struct GroupedMatmulImplementationManager : public ImplementationManager {+ OV_GPU_PRIMITIVE_IMPL("onednn::grouped_gemm")+ GroupedMatmulImplementationManager(shape_types shape_type)+ : ImplementationManager(impl_types::onednn, shape_type) {}+ std::unique_ptr<primitive_impl> create_impl(const program_node& node, const kernel_impl_params& params) const override;++ bool validate_impl(const program_node& node) const override {+ OPENVINO_ASSERT(node.is_type<grouped_matmul>());+ const auto& config = node.get_program().get_config();+ const auto& info = node.get_program().get_engine().get_device_info();+ if (!info.supports_immad || info.arch == gpu_arch::unknown || !config.get_use_onednn())+ return false;++ static const std::vector<format> supported_fmts = {+ format::bfyx,+ };++ static const std::vector<ov::element::Type_t> supported_activation_types = {+ ov::element::f16,+ };++ static const std::vector<ov::element::Type_t> supported_compressed_weight_types = {+ ov::element::u8,+ ov::element::i8,+ ov::element::u4,+ ov::element::i4,+ };++ static const std::vector<ov::element::Type_t> supported_offset_types = {+ ov::element::i32,+ };++ const auto& in0 = node.get_input_layout(grouped_matmul::GroupedMatmulInputIdx::INPUT);+ const auto& in1 = node.get_input_layout(grouped_matmul::GroupedMatmulInputIdx::WEIGHT);+ const auto& off = node.get_input_layout(grouped_matmul::GroupedMatmulInputIdx::OFFSETS);+ const auto& out = node.get_output_layout(0);++ const auto* desc = node.as<grouped_matmul>().get_primitive().get();+ const bool compressed = desc && desc->compressed_weights;++ if (!one_of(in0.format, supported_fmts) || !one_of(in0.data_type, supported_activation_types))+ return false;+ if (!one_of(in1.format, supported_fmts))+ return false;+ if (compressed) {+ if (!one_of(in1.data_type, supported_compressed_weight_types))+ return false;+ } else if (!one_of(in1.data_type, supported_activation_types)) {+ return false;+ }+ if (!one_of(off.format, supported_fmts) || !one_of(off.data_type, supported_offset_types))+ return false;+ if (!one_of(out.format, supported_fmts) || !one_of(out.data_type, supported_activation_types))+ return false;++ // oneDNN does not support mixed fp16 x bf16 configurations+ if (!compressed &&+ ((in0.data_type == data_types::f16 && in1.data_type == data_types::bf16) ||+ (in0.data_type == data_types::bf16 && in1.data_type == data_types::f16)))+ return false;++ return true;+ }++ in_out_fmts_t query_formats(const program_node& node) const override {+ OPENVINO_ASSERT(node.is_type<grouped_matmul>());+ std::vector<format::type> in_fmts(node.get_dependencies().size(), format::bfyx);+ std::vector<format::type> out_fmts(node.get_outputs_count(), format::bfyx);+ return {in_fmts, out_fmts};+ }+};++} // namespace onednn+} // namespace cldnndiff --git a/src/plugins/intel_gpu/src/graph/include/grouped_matmul_inst.h b/src/plugins/intel_gpu/src/graph/include/grouped_matmul_inst.hnew file mode 100644index 00000000000..531f16175f6--- /dev/null+++ b/src/plugins/intel_gpu/src/graph/include/grouped_matmul_inst.h@@ -0,0 +1,45 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#pragma once+#include "intel_gpu/primitives/grouped_matmul.hpp"+#include "primitive_inst.h"++#include <memory>+#include <string>++namespace cldnn {++template <>+struct typed_program_node<grouped_matmul> : public typed_program_node_base<grouped_matmul> {+ using parent = typed_program_node_base<grouped_matmul>;+ typed_program_node(const std::shared_ptr<grouped_matmul> prim, program& prog) : parent(prim, prog) {+ support_padding_all(true);+ }++public:+ using parent::parent;++ std::vector<size_t> get_shape_infer_dependencies() const override { return {}; }+ program_node& input() const { return get_dependency(0); }+};++using grouped_matmul_node = typed_program_node<grouped_matmul>;++template <>+class typed_primitive_inst<grouped_matmul> : public typed_primitive_inst_base<grouped_matmul> {+ using parent = typed_primitive_inst_base<grouped_matmul>;+ using parent::parent;++public:+ template <typename ShapeType>+ static std::vector<layout> calc_output_layouts(grouped_matmul_node const& node, const kernel_impl_params& impl_param);+ static layout calc_output_layout(grouped_matmul_node const& node, kernel_impl_params const& impl_param);+ static std::string to_string(grouped_matmul_node const& node);++ typed_primitive_inst(network& network, grouped_matmul_node const& node);+};++using grouped_matmul_inst = typed_primitive_inst<grouped_matmul>;+} // namespace cldnndiff --git a/src/plugins/intel_gpu/src/graph/layout_optimizer.cpp b/src/plugins/intel_gpu/src/graph/layout_optimizer.cppindex e23a34f2adb..766fad3be4b 100644--- a/src/plugins/intel_gpu/src/graph/layout_optimizer.cpp+++ b/src/plugins/intel_gpu/src/graph/layout_optimizer.cpp@@ -23,6 +23,7 @@#include "gated_mlp_inst.h"#include "gemm_inst.h"#include "moe_gemm_inst.h"+#include "grouped_matmul_inst.h"#include "deconvolution_inst.h"#include "fully_connected_inst.h"#include "gru_seq_inst.h"@@ -1613,6 +1614,7 @@ void layout_optimizer::add_all_onednn_impls_optimization_attribute() {enable_onednn_for<reduce>();enable_onednn_for<reorder>();enable_onednn_for<moe_gemm>();+ enable_onednn_for<grouped_matmul>();}bool layout_optimizer::has_all_enabled_onednn_impls_optimization_attribute() {diff --git a/src/plugins/intel_gpu/src/graph/registry/grouped_matmul_impls.cpp b/src/plugins/intel_gpu/src/graph/registry/grouped_matmul_impls.cppnew file mode 100644index 00000000000..8953a4291f3--- /dev/null+++ b/src/plugins/intel_gpu/src/graph/registry/grouped_matmul_impls.cpp@@ -0,0 +1,25 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "registry.hpp"+#include "intel_gpu/primitives/grouped_matmul.hpp"+#include "primitive_inst.h"++#if OV_GPU_WITH_ONEDNN+ #include "impls/onednn/grouped_gemm_onednn.hpp"+#endif++namespace ov::intel_gpu {++using namespace cldnn;++const std::vector<std::shared_ptr<cldnn::ImplementationManager>>& Registry<grouped_matmul>::get_implementations() {+ static const std::vector<std::shared_ptr<ImplementationManager>> impls = {+ OV_GPU_CREATE_INSTANCE_ONEDNN(onednn::GroupedMatmulImplementationManager, shape_types::static_shape)+ };++ return impls;+}++} // namespace ov::intel_gpudiff --git a/src/plugins/intel_gpu/src/graph/registry/registry.hpp b/src/plugins/intel_gpu/src/graph/registry/registry.hppindex 927571776e6..73361ed6c07 100644--- a/src/plugins/intel_gpu/src/graph/registry/registry.hpp+++ b/src/plugins/intel_gpu/src/graph/registry/registry.hpp@@ -192,6 +192,7 @@ REGISTER_IMPLS(moe_gemm);REGISTER_IMPLS(moe_scatter_reduction);REGISTER_IMPLS(moe_gather);REGISTER_IMPLS(gather_matmul);+REGISTER_IMPLS(grouped_matmul);REGISTER_DEFAULT_IMPLS(assign, CPU_S, CPU_D);REGISTER_DEFAULT_IMPLS(read_value, CPU_S, CPU_D);diff --git a/src/plugins/intel_gpu/src/plugin/ops/grouped_matmul.cpp b/src/plugins/intel_gpu/src/plugin/ops/grouped_matmul.cppnew file mode 100644index 00000000000..76d547b5af1--- /dev/null+++ b/src/plugins/intel_gpu/src/plugin/ops/grouped_matmul.cpp@@ -0,0 +1,170 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "intel_gpu/primitives/grouped_matmul.hpp"++#include "intel_gpu/plugin/common_utils.hpp"+#include "intel_gpu/plugin/program_builder.hpp"+#include "intel_gpu/primitives/fully_connected.hpp"+#include "intel_gpu/primitives/reorder.hpp"+#include "openvino/op/constant.hpp"+#include "openvino/op/grouped_matmul.hpp"+#include "ov_ops/grouped_matmul_compressed.hpp"++namespace ov::intel_gpu {++// onednn grouped_gemm impl requires i32 offsets; insert a reorder if the graph feeds i64.+static void ensure_i32_offsets(ProgramBuilder& p,+ const std::shared_ptr<ov::Node>& op,+ std::vector<cldnn::input_info>& inputs,+ size_t offsets_idx) {+ const auto offsets_dtype = cldnn::element_type_to_data_type(op->get_input_element_type(offsets_idx));+ if (offsets_dtype == cldnn::data_types::i32)+ return;+ auto reorder_id = inputs[offsets_idx].pid + "_" + op->get_friendly_name() + ProgramBuilder::m_preProcessTag;+ auto fmt = cldnn::format::get_default_format(op->get_input_partial_shape(offsets_idx).size());+ auto reorder_prim = cldnn::reorder(reorder_id, inputs[offsets_idx], fmt, cldnn::data_types::i32);+ p.add_primitive(*op, reorder_prim);+ inputs[offsets_idx] = cldnn::input_info(reorder_id);+}++static void CreateGroupedMatMulOp(ProgramBuilder& p, const std::shared_ptr<ov::op::v17::GroupedMatMul>& op) {+ // GroupedMatMul-17 has two cases:+ // 2D x 3D: mat_a[T,K] x mat_b[G,N,K] + offsets[G] -> [T,N] (MoE forward pass)+ // 3D x 3D: mat_a[G,M,K] x mat_b[G,N,K] -> [G,M,N] (batched uniform)+ //+ // The 3D x 3D case is a plain batched matmul with transpose_b=true (since mat_b is+ // stored pre-transposed as [G, N, K]), so we lower it here to cldnn::fully_connected+ // (with weights_rank=3) and let the regular FC impls handle it. The 2D x 3D case has+ // no direct equivalent and is dispatched to the specialized cldnn::grouped_matmul+ // primitive.+ validate_inputs_count(op, {2, 3});+ auto inputs = p.GetInputInfo(op);++ const auto& a_shape = op->get_input_partial_shape(0);+ const auto& b_shape = op->get_input_partial_shape(1);+ OPENVINO_ASSERT(a_shape.rank().is_static() && (a_shape.size() == 2 || a_shape.size() == 3),+ "[GPU] GroupedMatMul: mat_a must be rank 2 or 3, got shape ", a_shape);+ OPENVINO_ASSERT(b_shape.rank().is_static() && b_shape.size() == 3,+ "[GPU] GroupedMatMul: only 3D mat_b is supported, got shape ", b_shape);++ const std::string layer_name = layer_type_name_ID(op);+ const auto output_dt = cldnn::element_type_to_data_type(op->get_output_element_type(0));+ if (a_shape.size() == 3) {+ // 3D x 3D: lower to a batched fully_connected with transpose_b=true.+ // A:[G,M,K] x B:[G,N,K] == A @ B.T -> [G,M,N]+ auto fc_prim = cldnn::fully_connected(layer_name,+ inputs[0],+ /*weights=*/inputs[1].pid,+ /*bias=*/"",+ output_dt,+ /*input_size=*/3,+ /*weights_rank=*/3,+ /*weights_transposed=*/true);+ p.add_primitive(*op, fc_prim);+ return;+ }++ // 2D x 3D: dispatch to the specialized grouped_matmul primitive.+ OPENVINO_ASSERT(inputs.size() == 3,+ "[GPU] GroupedMatMul: 2D x 3D case requires 3 inputs (mat_a, mat_b, offsets)");++ ensure_i32_offsets(p, op, inputs, /*offsets_idx=*/2);++ const cldnn::grouped_matmul prim(layer_name, inputs, output_dt);+ p.add_primitive(*op, prim);+}++static void CreateGroupedMatMulCompressedOp(ProgramBuilder& p,+ const std::shared_ptr<ov::op::internal::GroupedMatMulCompressed>& op) {+ // Two layouts, distinguished by the presence of the offsets input:+ // 2D x 3D: [mat_a, mat_b(compressed), offsets, scale, (zp)]+ // -> cldnn::grouped_matmul (specialized primitive)+ // 3D x 3D: [mat_a, mat_b(compressed), scale, (zp)]+ // -> cldnn::fully_connected (batched, weights_rank=3, compressed)+ validate_inputs_count(op, {3, 4, 5});+ auto inputs = p.GetInputInfo(op);++ const auto& a_shape = op->get_input_partial_shape(0);+ const auto& b_shape = op->get_input_partial_shape(1);+ OPENVINO_ASSERT(a_shape.rank().is_static() && (a_shape.size() == 2 || a_shape.size() == 3),+ "[GPU] GroupedMatMulCompressed: mat_a must be rank 2 or 3, got shape ", a_shape);+ OPENVINO_ASSERT(b_shape.rank().is_static() && b_shape.size() == 3,+ "[GPU] GroupedMatMulCompressed: mat_b must be rank 3, got shape ", b_shape);++ const std::string layer_name = layer_type_name_ID(op);+ const auto output_dt = cldnn::element_type_to_data_type(op->get_output_element_type(0));+ const bool with_offsets = op->has_offsets();++ if (!with_offsets) {+ // 3D x 3D compressed path: lower to a batched compressed fully_connected.+ // A:[G,M,K] x B:[G,N,K] == A @ B.T -> [G,M,N] with weights_transposed=true.+ OPENVINO_ASSERT(inputs.size() >= 3, "[GPU] GroupedMatMulCompressed 3Dx3D requires at least 3 inputs (mat_a, mat_b, scale)");++ const auto weights_pid = inputs[1].pid;+ const auto scale_pid = inputs[2].pid;++ cldnn::primitive_id zp_pid = "";+ float zp_scalar_value = 0.0f;+ bool has_scalar_zp = false;+ if (op->get_input_size() > 3) {+ constexpr size_t zp_idx = 3;+ zp_pid = inputs[zp_idx].pid;+ auto zp_const = ov::as_type_ptr<ov::op::v0::Constant>(op->get_input_node_shared_ptr(zp_idx));+ if (zp_const && ov::shape_size(zp_const->get_output_shape(0)) == 1) {+ has_scalar_zp = true;+ zp_scalar_value = zp_const->cast_vector<float>()[0];+ }+ }++ auto fc_prim = cldnn::fully_connected(layer_name,+ inputs[0],+ weights_pid /*weights=*/,+ "" /*bias=*/,+ scale_pid /*decompression_scale=*/,+ zp_pid /*decompression_zero_point=*/,+ output_dt,+ 3 /*input_size=*/,+ 3 /*weights_rank=*/,+ true /*weights_transposed=*/);+ if (has_scalar_zp) {+ fc_prim.decompression_zero_point_scalar = zp_scalar_value;+ }+ p.add_primitive(*op, fc_prim);++ return;+ }++ // 2D x 3D compressed path.+ OPENVINO_ASSERT(inputs.size() >= 4, "[GPU] GroupedMatMulCompressed 2Dx3D requires at least 4 inputs (mat_a, mat_b, offsets, scale)");++ ensure_i32_offsets(p, op, inputs, /*offsets_idx=*/2);++ const auto scale = inputs[3];+ cldnn::input_info zp = {};+ float zp_scalar_value = 0.0f;+ bool has_scalar_zp = false;+ if (op->get_input_size() > 4) {+ constexpr size_t zp_idx = 4;+ zp = inputs[zp_idx];+ auto zp_const = ov::as_type_ptr<ov::op::v0::Constant>(op->get_input_node_shared_ptr(zp_idx));+ if (zp_const && ov::shape_size(zp_const->get_output_shape(0)) == 1) {+ has_scalar_zp = true;+ zp_scalar_value = zp_const->cast_vector<float>()[0];+ }+ }++ // base inputs are mat_a / mat_b / offsets; scale + zp are attached via primitive fields.+ std::vector<cldnn::input_info> base_inputs{inputs[0], inputs[1], inputs[2]};+ cldnn::grouped_matmul prim(layer_name, base_inputs, scale, zp, output_dt);+ if (has_scalar_zp) {+ prim.decompression_zero_point_scalar = zp_scalar_value;+ }+ p.add_primitive(*op, prim);+}++REGISTER_FACTORY_IMPL(v17, GroupedMatMul);+REGISTER_FACTORY_IMPL(internal, GroupedMatMulCompressed);++} // namespace ov::intel_gpudiff --git a/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp b/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cppindex 9a514f5dea9..c626f8c876c 100644--- a/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp+++ b/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp@@ -47,6 +47,7 @@#include "openvino/op/convolution.hpp"#include "openvino/op/gated_delta_net.hpp"#include "openvino/op/gather.hpp"+#include "openvino/op/grouped_matmul.hpp"#include "openvino/op/group_conv.hpp"#include "openvino/op/gru_cell.hpp"#include "openvino/op/gru_sequence.hpp"@@ -82,6 +83,7 @@#include "plugin/transformations/clamp_fp16_output.hpp"#include "plugin/transformations/convert_convolution.hpp"#include "plugin/transformations/convert_fc_to_compressed.hpp"+#include "transformations/op_conversions/convert_grouped_matmul_to_compressed.hpp"#include "plugin/transformations/convert_matmul_to_fc.hpp"#include "plugin/transformations/fuse_moe_shared_expert.hpp"#include "transformations/common_optimizations/moe_op_fusion.hpp"@@ -238,6 +240,7 @@ static bool is_decompression_multiply(const std::shared_ptr<const ov::Node> nodestd::vector<ov::DiscreteTypeInfo> target_consumers = {ov::opset1::MatMul::get_type_info_static(),ov::op::internal::MOE::get_type_info_static(),ov::op::v8::Gather::get_type_info_static(),+ ov::op::v17::GroupedMatMul::get_type_info_static(),ov::op::v1::Convolution::get_type_info_static(),ov::opset1::Convolution::get_type_info_static(),ov::op::v1::ConvolutionBackpropData::get_type_info_static(),@@ -596,11 +599,12 @@ void TransformationsPipeline::apply(std::shared_ptr<ov::Model> func) {// Gated on supports_immad (systolic-only) and oneDNN (required for expert GEMM dispatch).// Note: even though we are already inside `if (supports_immad)`, oneDNN can still be explicitly disabled by the user.if (device_info.supports_immad && config.get_use_onednn()) {- manager.register_pass<ov::pass::ConvertGroupedMatMulToGatherMatmul>();const std::vector<ov::element::Type> supported_compressed_weights_types{ov::element::u4,ov::element::i4,ov::element::i8,ov::element::u8};+ manager.register_pass<ov::pass::ConvertGroupedMatMulToGroupedMatMulCompressed>(+ supported_compressed_weights_types);manager.register_pass<ov::pass::ConvertTiledMoeBlockToGatherMatmuls>(supported_compressed_weights_types);// f32 listed because this pass runs before ConvertPrecision (line ~588);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.cppindex a6c421e7f15..d1d940af0a0 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@@ -15,10 +15,14 @@ using ov::test::GroupedMatMulShapeParams;using ov::test::TokensPerExpert;using ov::test::utils::DecompressionType;-const std::vector<GroupedMatMulShapeParams> shapes = {+const std::vector<GroupedMatMulShapeParams> shapes_3d_3d = {// 3D x 3D: A:[G,M,K] x B:[G,N,K] -> [G,M,N], dynamic M dim.+ // The GPU plugin lowers this case to a fully_connected (see ops/grouped_matmul.cpp),{{ov::PartialShape{4, -1, 128}, {{4, 8, 128}, {4, 1, 128}, {4, 16, 128}}}, {4, 256, 128}, {}},{{ov::PartialShape{8, -1, 256}, {{8, 4, 256}, {8, 1, 256}}}, {8, 512, 256}, {}},+};++const std::vector<GroupedMatMulShapeParams> shapes_2d_3d = {// 2D x 3D: A:[T,K] x B:[G,N,K] -> [T,N], dynamic T dim.{{ov::PartialShape{-1, 128}, {{16, 128}, {32, 128}, {8, 128}}}, {4, 256, 128}, TokensPerExpert{{8, 0, 8, 0}, {0, 16, 0, 16}, {4, 4, 0, 0}}},{{ov::PartialShape{-1, 128}, {{12, 128}, {20, 128}}}, {4, 256, 128}, TokensPerExpert{{4, 5, 3, 0}, {0, 7, 6, 7}}},@@ -29,17 +33,40 @@ const std::vector<ov::element::Type> weights_precisions = {ov::element::u8, ov::const std::vector<ov::element::Type> decompression_precisions = {ov::element::f16};const std::vector<DecompressionType> subtract_types = {DecompressionType::full, DecompressionType::empty};-INSTANTIATE_TEST_SUITE_P(smoke_GroupedMatMul,+INSTANTIATE_TEST_SUITE_P(smoke_GroupedMatMul_f16_2d3d,+ GroupedMatMulLayerTest,+ ::testing::Combine(::testing::ValuesIn(shapes_2d_3d),+ ::testing::Values(ov::element::f16),+ ::testing::Values(ov::test::utils::DEVICE_GPU),+ ::testing::Values("GroupedMatMul")),+ GroupedMatMulLayerTest::getTestCaseName);++INSTANTIATE_TEST_SUITE_P(smoke_GroupedMatMul_f16_3d3d,GroupedMatMulLayerTest,- ::testing::Combine(::testing::ValuesIn(shapes),+ ::testing::Combine(::testing::ValuesIn(shapes_3d_3d),::testing::Values(ov::element::f16),::testing::Values(ov::test::utils::DEVICE_GPU),- ::testing::Values("gather_matmul")),+ ::testing::Values("GroupedMatMul")),GroupedMatMulLayerTest::getTestCaseName);-INSTANTIATE_TEST_SUITE_P(smoke_GroupedMatMul_Compressed,+INSTANTIATE_TEST_SUITE_P(smoke_GroupedMatMul_Compressed_2d3d,+ GroupedMatMulCompressedLayerTest,+ ::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("GroupedMatMulCompressed")),+ GroupedMatMulCompressedLayerTest::getTestCaseName);++INSTANTIATE_TEST_SUITE_P(smoke_GroupedMatMul_Compressed_3d3d,GroupedMatMulCompressedLayerTest,- ::testing::Combine(::testing::ValuesIn(shapes),+ ::testing::Combine(::testing::ValuesIn(shapes_3d_3d),::testing::Values(ov::element::f16),::testing::ValuesIn(weights_precisions),::testing::ValuesIn(decompression_precisions),@@ -49,6 +76,6 @@ INSTANTIATE_TEST_SUITE_P(smoke_GroupedMatMul_Compressed,::testing::Values(true),::testing::Values(-1, 128),::testing::Values(ov::test::utils::DEVICE_GPU),- ::testing::Values("gather_matmul")),+ ::testing::Values("GroupedMatMulCompressed")),GroupedMatMulCompressedLayerTest::getTestCaseName);} // namespacediff --git a/src/plugins/intel_gpu/tests/unit/test_cases/gemm_gpu_test.cpp b/src/plugins/intel_gpu/tests/unit/test_cases/gemm_gpu_test.cppindex dd240fc1d0b..de2a893c6ec 100644--- a/src/plugins/intel_gpu/tests/unit/test_cases/gemm_gpu_test.cpp+++ b/src/plugins/intel_gpu/tests/unit/test_cases/gemm_gpu_test.cpp@@ -7,10 +7,15 @@#include <intel_gpu/primitives/input_layout.hpp>#include <intel_gpu/primitives/gemm.hpp>+#include <intel_gpu/primitives/grouped_matmul.hpp>#include <intel_gpu/primitives/crop.hpp>+#include <intel_gpu/primitives/reorder.hpp>#include "openvino/reference/matmul.hpp"#include "openvino/reference/transpose.hpp"#include "openvino/reference/reshape.hpp"+#include "openvino/reference/grouped_matmul.hpp"++ #include <numeric>#include "intel_gpu/runtime/compilation_context.hpp"#include "gemm_inst.h"@@ -3891,4 +3896,91 @@ TEST_F(gemm_gpu_tests, transpose_matmul_transpose_dynamic_4d_cached) {this->test_transpose_matmul_transpose(4, true, true);}#endif++#ifdef ENABLE_ONEDNN_FOR_GPU+// Smoke test: cldnn::grouped_matmul (2D x 3D case of GroupedMatMul-17) via onednn.+// Compared against ov::reference::grouped_matmul_2d_3d.+TEST(grouped_matmul_gpu_tests, smoke_2d_3d_f16) {+ auto& engine = get_test_engine();+ if (!engine.get_device_info().supports_immad)+ GTEST_SKIP() << "grouped_matmul onednn impl requires XMX/DPAS support (Xe-HPG+)";++ // Small shapes to keep the smoke test cheap.+ constexpr size_t G = 3; // number of experts+ constexpr size_t K = 128; // hidden size (multiple of 16 for onednn tile)+ constexpr size_t N = 64; // output features++ // Token distribution across experts: [4, 0, 6] -> total 10 rows.+ const std::vector<int32_t> tokens_per_expert = {4, 0, 6};+ const size_t total_tokens = std::accumulate(tokens_per_expert.begin(), tokens_per_expert.end(), size_t{0});++ // Cumulative end-offsets: [4, 4, 10].+ std::vector<int32_t> offsets(G);+ offsets[0] = tokens_per_expert[0];+ for (size_t i = 1; i < G; ++i)+ offsets[i] = offsets[i - 1] + tokens_per_expert[i];++ tests::random_generator rg("grouped_matmul_smoke");+ auto mat_a_f32 = rg.generate_random_1d<float>(total_tokens * K, -0.5f, 0.5f);+ auto mat_b_f32 = rg.generate_random_1d<float>(G * N * K, -0.5f, 0.5f);++ std::vector<ov::float16> mat_a_f16(mat_a_f32.size());+ std::vector<ov::float16> mat_b_f16(mat_b_f32.size());+ for (size_t i = 0; i < mat_a_f32.size(); ++i) mat_a_f16[i] = ov::float16(mat_a_f32[i]);+ for (size_t i = 0; i < mat_b_f32.size(); ++i) mat_b_f16[i] = ov::float16(mat_b_f32[i]);++ // Allocate device memory.+ auto mat_a_mem = engine.allocate_memory({ov::PartialShape{static_cast<int64_t>(total_tokens), K},+ data_types::f16, format::bfyx});+ auto mat_b_mem = engine.allocate_memory({ov::PartialShape{G, N, K}, data_types::f16, format::bfyx});+ auto off_mem = engine.allocate_memory({ov::PartialShape{G}, data_types::i32, format::bfyx});++ set_values(mat_a_mem, mat_a_f16);+ set_values(mat_b_mem, mat_b_f16);+ set_values(off_mem, offsets);++ // Dynamic input layouts: total_tokens and G are unknown at compile time.+ layout mat_a_dyn_layout{ov::PartialShape{-1, static_cast<int64_t>(K)}, data_types::f16, format::bfyx};+ layout offsets_dyn_layout{ov::PartialShape{-1}, data_types::i32, format::bfyx};++ topology tp;+ tp.add(input_layout("mat_a", mat_a_dyn_layout));+ tp.add(data("mat_b", mat_b_mem));+ tp.add(input_layout("offsets", offsets_dyn_layout));+ tp.add(grouped_matmul("gmm",+ {input_info("mat_a"), input_info("mat_b"), input_info("offsets")},+ data_types::f16));+ tp.add(reorder("output", input_info("gmm"), format::bfyx, data_types::f32));++ ExecutionConfig config = get_test_default_config(engine);+ config.set_property(ov::intel_gpu::optimize_data(true));+ config.set_property(ov::intel_gpu::allow_new_shape_infer(true));++ network net(engine, tp, config);+ net.set_input_data("mat_a", mat_a_mem);+ net.set_input_data("offsets", off_mem);+ auto out_mem = net.execute().at("output").get_memory();+ cldnn::mem_lock<float> out_ptr(out_mem, get_test_stream());+ std::vector<float> gpu_out(out_ptr.begin(), out_ptr.end());++ // Reference computed on f16 inputs to match GPU accumulation precision profile.+ std::vector<ov::float16> ref_out_f16(total_tokens * N, ov::float16(0.f));+ ov::reference::grouped_matmul_2d_3d<ov::float16, int32_t>(mat_a_f16.data(),+ mat_b_f16.data(),+ offsets.data(),+ ref_out_f16.data(),+ ov::Shape{total_tokens, K},+ ov::Shape{G, N, K});+ std::vector<float> ref_out(ref_out_f16.size());+ for (size_t i = 0; i < ref_out_f16.size(); ++i) ref_out[i] = static_cast<float>(ref_out_f16[i]);++ ASSERT_EQ(gpu_out.size(), ref_out.size());+ // f16 GEMM accumulation over K -> relative tolerance ~ few percent.+ for (size_t i = 0; i < gpu_out.size(); ++i) {+ const float diff = std::abs(gpu_out[i] - ref_out[i]);+ const float tol = 0.05f * std::max(1.f, std::abs(ref_out[i]));+ ASSERT_LE(diff, tol) << "mismatch at i=" << i << ": gpu=" << gpu_out[i] << ", ref=" << ref_out[i];+ }+}+#endif // ENABLE_ONEDNN_FOR_GPU} // namespacediff --git a/src/tests/functional/plugin/shared/include/shared_test_classes/single_op/grouped_matmul.hpp b/src/tests/functional/plugin/shared/include/shared_test_classes/single_op/grouped_matmul.hppindex de163596c67..789f3fff0c6 100644--- a/src/tests/functional/plugin/shared/include/shared_test_classes/single_op/grouped_matmul.hpp+++ b/src/tests/functional/plugin/shared/include/shared_test_classes/single_op/grouped_matmul.hpp@@ -38,6 +38,8 @@ protected:GroupedMatMulShapeParams shape_params_;ov::element::Type act_type_;std::string model_name_;+ // ov op type name expected to appear in the executed network (via ProfilingInfo::node_type).+ // Empty = skip the check.std::string expected_primitive_;void SetUp() override;@@ -50,7 +52,7 @@ using GroupedMatMulParams = std::tuple<GroupedMatMulShapeParams, // shape bundleov::element::Type, // element typestd::string, // target device- std::string // expected primitive type (empty = skip check)+ std::string // expected ov op type in the executed network (empty = skip check)>;class GroupedMatMulLayerTest : public testing::WithParamInterface<GroupedMatMulParams>,@@ -74,7 +76,7 @@ using GroupedMatMulCompressedParams = std::tuple<bool, // reshape_on_decompressionint, // decompression_group_size (-1 = per-OC)std::string, // target device- std::string // expected primitive type (empty = skip check)+ std::string // expected ov op type in the executed network (empty = skip check)>;class GroupedMatMulCompressedLayerTestdiff --git a/src/tests/functional/plugin/shared/src/single_op/grouped_matmul.cpp b/src/tests/functional/plugin/shared/src/single_op/grouped_matmul.cppindex c2981733651..5aa0a2b18d7 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@@ -13,6 +13,7 @@#include "openvino/op/grouped_matmul.hpp"#include "openvino/op/parameter.hpp"#include "openvino/op/result.hpp"+#include "openvino/runtime/properties.hpp"namespace ov {namespace test {@@ -63,13 +64,29 @@ void GroupedMatMulTestBase::SetUp() {abs_threshold = 0.05f;rel_threshold = 0.01f;++ if (!expected_primitive_.empty()) {+ // Populate ov::ProfilingInfo::node_type at inference time; this is what+ // validate() inspects to confirm the expected op is what actually ran.+ configuration[ov::enable_profiling.name()] = true;+ }}void GroupedMatMulTestBase::validate() {SubgraphBaseTest::validate();- if (!expected_primitive_.empty()) {- CheckNumberOfNodesWithType(compiledModel, expected_primitive_, 1);++ // 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_;+ });+ EXPECT_TRUE(it != profiling.end())+ << "Executed network does not contain a '" << expected_primitive_+ << "' node — the expected transformation did not fire on this test's model.";}void GroupedMatMulTestBase::generate_inputs(const std::vector<ov::Shape>& targetInputStaticShapes) {@@ -205,6 +222,14 @@ void GroupedMatMulCompressedLayerTest::SetUp() {group_size_ = group_size;targetDevice = _targetDevice;GroupedMatMulTestBase::SetUp();++ // Loosen tolerance for low-bit weight-compressed variants; dequantization+ // introduces additional rounding error compared to the uncompressed path.+ if (weights_prec_ == ov::element::u4 || weights_prec_ == ov::element::i4) {+ abs_threshold = 0.2f;+ } else if (weights_prec_ == ov::element::u8 || weights_prec_ == ov::element::i8) {+ abs_threshold = 0.1f;+ }}std::shared_ptr<ov::Node> GroupedMatMulCompressedLayerTest::build_weights() {