Skip to content
21 changes: 21 additions & 0 deletions onnxscript/function_libs/torch_lib/ops/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -4539,6 +4539,27 @@ def aten_grid_sampler_3d_backward(
raise NotImplementedError()


@torch_op("aten::_grouped_mm", trace_only=True)
def aten_grouped_mm(
self: TFloat,
mat2: TFloat,
offs: Optional[TInt] = None,
bias: Optional[TFloat] = None,
out_dtype: int = -1,
) -> TFloat:
"""_grouped_mm(Tensor self, Tensor mat2, *, Tensor? offs=None, Tensor? bias=None, int? out_dtype=None) -> Tensor"""

if offs is not None:
raise NotImplementedError("Grouped matmul with offsets (ragged/MoE) is not supported.")

res = op.MatMul(self, mat2)
if bias is not None:
res = op.Add(res, bias)
if out_dtype is not None and out_dtype != -1:
res = op.Cast(res, to=out_dtype)
return res


def aten_gru_cell(
input: TensorType,
hx: TensorType,
Expand Down
63 changes: 63 additions & 0 deletions tests/function_libs/torch_lib/extra_opinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,61 @@
M = 10


def sample_inputs_grouped_mm(op_info, device, dtype, requires_grad, **kwargs):
del op_info
del kwargs

make_arg = functools.partial(
torch_testing.make_tensor,
device=device,
dtype=dtype,
requires_grad=requires_grad,
)

cases = [
# (G, M, K), (G, K, N)
((2, 3, 4), (2, 4, 5)),
((1, 2, 2), (1, 2, 1)),
]

for self_shape, mat2_shape in cases:
self_t = make_arg(self_shape)
mat2_t = make_arg(mat2_shape)

# Test without bias and without out_dtype
yield opinfo_core.SampleInput(self_t, args=(mat2_t,))

Comment thread
Sid-V5 marked this conversation as resolved.
# Test with bias
g, _, _ = self_shape
_, _, n = mat2_shape
bias_t = make_arg((g, 1, n))
yield opinfo_core.SampleInput(self_t, args=(mat2_t,), kwargs={"bias": bias_t})

# Test with bias and out_dtype
if dtype in (torch.float16, torch.bfloat16):
yield opinfo_core.SampleInput(
self_t,
args=(mat2_t,),
kwargs={"bias": bias_t, "out_dtype": torch.float32},
)


def _mock_grouped_mm(self, mat2, offs=None, bias=None, out_dtype=None):
Comment thread Fixed
Comment thread Fixed
if hasattr(torch.ops.aten, "_grouped_mm"):
try:
return torch.ops.aten._grouped_mm(
self, mat2, offs=offs, bias=bias, out_dtype=out_dtype
)
except (TypeError, RuntimeError):
pass
res = torch.matmul(self, mat2)
if bias is not None:
res = res + bias
Comment on lines +49 to +75
Comment on lines +65 to +75
if out_dtype is not None:
res = res.to(out_dtype)
return res


def sample_inputs_scalar_tensor(op_info, device, dtype, requires_grad, **kwargs):
del op_info
del kwargs
Expand Down Expand Up @@ -3142,4 +3197,12 @@ def sample_inputs_masked_scatter(op_info, device, dtype, requires_grad, **kwargs
sample_inputs_func=sample_inputs_masked_scatter,
supports_out=False,
),
opinfo_core.OpInfo(
"ops.aten._grouped_mm",
aten_name="_grouped_mm",
op=_mock_grouped_mm,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use

Suggested change
op=_mock_grouped_mm,
op=torch.ops.aten._grouped_mm,

?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also this

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dtypes=common_dtype.floating_types(),
sample_inputs_func=sample_inputs_grouped_mm,
supports_out=False,
),
]
1 change: 1 addition & 0 deletions tests/function_libs/torch_lib/ops_test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,7 @@ def _where_input_wrangler(
reason="fixme: ORT does not support empty tensors as input",
),
TorchLibOpInfo("ge", core_ops.aten_ge),
TorchLibOpInfo("ops.aten._grouped_mm", core_ops.aten_grouped_mm),
TorchLibOpInfo("gt", core_ops.aten_gt),
TorchLibOpInfo("histc", core_ops.aten_histc)
.skip(
Expand Down