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
[PythonAPI] GroupedMatMul Python API (#36686)
Katarzyna Mitrusa10da36f6a81
diff --git a/src/bindings/python/src/openvino/opset17/__init__.py b/src/bindings/python/src/openvino/opset17/__init__.pyindex c799ef21c18..04e8bff1cc8 100644--- a/src/bindings/python/src/openvino/opset17/__init__.py+++ b/src/bindings/python/src/openvino/opset17/__init__.py@@ -4,6 +4,7 @@# New operations added in Opset17from openvino.opset17.ops import erfinv+from openvino.opset17.ops import grouped_matmul# Operators from previous opsets# TODO (ticket: 179247): Add previous opset operators at the end of opset17 developmentdiff --git a/src/bindings/python/src/openvino/opset17/ops.py b/src/bindings/python/src/openvino/opset17/ops.pyindex 3d4a4e0dd2e..9c466a39258 100644--- a/src/bindings/python/src/openvino/opset17/ops.py+++ b/src/bindings/python/src/openvino/opset17/ops.py@@ -7,9 +7,9 @@ from functools import partialfrom typing import Optionalfrom openvino import Node-from openvino.utils.decorators import unary_op+from openvino.utils.decorators import nameable_op, unary_opfrom openvino.utils.node_factory import _get_node_factory-from openvino.utils.types import NodeInput+from openvino.utils.types import NodeInput, as_nodes_get_node_factory_opset17 = partial(_get_node_factory, "opset17")@@ -25,3 +25,41 @@ def erfinv(node: NodeInput, name: Optional[str] = None) -> Node::return: The new node performing the element-wise ErfInv operation."""return _get_node_factory_opset17().create("ErfInv", [node])+++@nameable_op+def grouped_matmul(+ mat_a: NodeInput,+ mat_b: NodeInput,+ offsets: Optional[NodeInput] = None,+ name: Optional[str] = None,+) -> Node:+ """Return a node which performs Grouped Matrix Multiplication for Mixture of Experts (MoE).++ Computes multiple matrix multiplications where each group processes a subset of the input+ data. Two input combinations are supported:++ - Case 1 (2D x 3D), MoE forward pass, requires ``offsets``:+ - mat_a: (total_tokens, K) - rows partitioned by offsets+ - mat_b: (G, N, K) - per-group weights+ - output: (total_tokens, N)+ - Case 2 (3D x 3D), batched uniform, no offsets:+ - mat_a: (G, M, K) - per-group inputs+ - mat_b: (G, N, K) - per-group weights+ - output: (G, M, N)++ :param mat_a: The first input tensor.+ :param mat_b: The second input tensor with per-group weights.+ :param offsets: 1D tensor of cumulative end-offsets of shape (G,) indicating+ group boundaries. Required for the 2D x 3D case.+ :param name: The optional name for the new output node.++ :return: The new node performing GroupedMatMul operation.+ """+ inputs = [mat_a, mat_b]+ if offsets is not None:+ inputs.append(offsets)+ return _get_node_factory_opset17().create(+ "GroupedMatMul",+ as_nodes(*inputs, name=name),+ )diff --git a/src/bindings/python/tests/test_graph/test_grouped_matmul.py b/src/bindings/python/tests/test_graph/test_grouped_matmul.pynew file mode 100644index 00000000000..5310220687b--- /dev/null+++ b/src/bindings/python/tests/test_graph/test_grouped_matmul.py@@ -0,0 +1,40 @@+# -*- coding: utf-8 -*-+# Copyright (C) 2018-2026 Intel Corporation+# SPDX-License-Identifier: Apache-2.0++from openvino import Type, PartialShape+from openvino.opset15 import parameter+import openvino.opset17 as ov+import pytest++[email protected](("a_shape", "b_shape", "out_shape"), [+ ((2, 4, 8), (2, 16, 8), (2, 4, 16)),+ ((3, 5, 8), (3, 8, 8), (3, 5, 8)),+])+def test_grouped_matmul_3d_3d(a_shape, b_shape, out_shape):+ mat_a = parameter(a_shape, name="mat_a", dtype=Type.f32)+ mat_b = parameter(b_shape, name="mat_b", dtype=Type.f32)+ node = ov.grouped_matmul(mat_a, mat_b)++ assert node.get_type_name() == "GroupedMatMul"+ assert node.get_output_size() == 1+ assert node.get_output_element_type(0) == Type.f32+ assert node.get_output_partial_shape(0) == PartialShape(list(out_shape))++[email protected]("dtype_offsets", [Type.i32, Type.i64])[email protected](("a_shape", "b_shape", "offs_shape"), [+ ((24, 8), (3, 16, 8), (3,)),+ ((16, 8), (2, 8, 8), (2,)),+])+def test_grouped_matmul_2d_3d_with_offsets(dtype_offsets, a_shape, b_shape, offs_shape):+ mat_a = parameter(a_shape, name="mat_a", dtype=Type.f32)+ mat_b = parameter(b_shape, name="mat_b", dtype=Type.f32)+ offsets = parameter(offs_shape, name="offsets", dtype=dtype_offsets)+ node = ov.grouped_matmul(mat_a, mat_b, offsets)++ assert node.get_type_name() == "GroupedMatMul"+ assert node.get_output_size() == 1+ assert node.get_output_element_type(0) == Type.f32+ assert node.get_output_partial_shape(0) == PartialShape([a_shape[0], b_shape[1]])