Skip to content

Add converter for aten::_grouped_mm.default - #2805

Open
Sid (Sid-V5) wants to merge 14 commits into
microsoft:mainfrom
Sid-V5:add-grouped-mm-2795
Open

Add converter for aten::_grouped_mm.default#2805
Sid (Sid-V5) wants to merge 14 commits into
microsoft:mainfrom
Sid-V5:add-grouped-mm-2795

Conversation

@Sid-V5

@Sid-V5 Sid (Sid-V5) commented Feb 12, 2026

Copy link
Copy Markdown

Implemented the converter for aten::_grouped_mm.default to address #2795.

Changes

  • Added aten_grouped_mm function in onnxscript/function_libs/torch_lib/ops/core.py

Implementation Details

The aten::_grouped_mm operator performs grouped matrix multiplication. This implementation handles the batch/dense mode (when offs is None), where groups are implicit in the batch dimension:

  • self: (G, M, K), mat2: (G, K, N) → result: (G, M, N)
  • Uses op.MatMul for the core computation
  • Supports optional bias addition via op.Add
  • Supports optional out_dtype casting via op.Cast

The offset-based mode (when offs is provided) raises NotImplementedError, as it requires segment-level matrix multiplications that are not directly expressible with standard ONNX operators.

Testing

The function follows the same patterns as other converters in core.py (e.g., aten_bmm, aten_mm) and uses the @torch_op decorator for automatic registration.

Fixes #2795

@Sid-V5

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

@codecov

codecov Bot commented Feb 16, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 60.00000% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 72.64%. Comparing base (3ba2bf7) to head (9ba30a4).

Files with missing lines Patch % Lines
onnxscript/function_libs/torch_lib/ops/core.py 60.00% 2 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2805      +/-   ##
==========================================
- Coverage   72.64%   72.64%   -0.01%     
==========================================
  Files         265      265              
  Lines       32251    32261      +10     
  Branches     3050     3053       +3     
==========================================
+ Hits        23429    23435       +6     
- Misses       7786     7788       +2     
- Partials     1036     1038       +2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

Comment thread onnxscript/function_libs/torch_lib/ops/core.py Outdated
Comment thread tests/function_libs/torch_lib/extra_opinfo.py Outdated
@justinchuby Justin Chu (justinchuby) added the module: torchlib Related to the torch/aten function lib in development label Mar 13, 2026
Comment thread tests/function_libs/torch_lib/extra_opinfo.py Fixed
Comment thread tests/function_libs/torch_lib/extra_opinfo.py Fixed

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Adds an ONNXScript converter for PyTorch’s aten::_grouped_mm (dense/batched mode) and wires it into the TorchLib op test infrastructure to address missing dispatch for aten._grouped_mm.default (#2795).

Changes:

  • Implemented aten_grouped_mm converter using MatMul, optional Add (bias), and optional Cast (out_dtype).
  • Registered the new op in TorchLib’s tested op list.
  • Added OpInfo + sample inputs scaffolding for _grouped_mm in the extra op database.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
onnxscript/function_libs/torch_lib/ops/core.py Adds the aten::_grouped_mm converter implementation.
tests/function_libs/torch_lib/ops_test_data.py Enables testing by adding a TorchLibOpInfo entry for ops.aten._grouped_mm.
tests/function_libs/torch_lib/extra_opinfo.py Adds OpInfo registration and sample input generation for _grouped_mm.

Comment thread tests/function_libs/torch_lib/extra_opinfo.py Outdated
Comment thread tests/function_libs/torch_lib/extra_opinfo.py Outdated
Comment thread onnxscript/function_libs/torch_lib/ops/core.py Outdated
Comment thread tests/function_libs/torch_lib/extra_opinfo.py
@justinchuby

Copy link
Copy Markdown
Collaborator

Two items. (1) The CONFLICTING flag is trivial — a git merge-tree against main resolves cleanly (insertions into non-overlapping regions), so a rebase clears it. (2) Real correctness issue: the offs argument is silently ignored — the code has only a # TODO and falls through to MatMul (~core.py L4515-4523), but the PR description says offset mode "raises NotImplementedError." With offs provided (the ragged/MoE case) this emits silently-incorrect results instead of failing fast; please add an explicit if offs is not None: raise NotImplementedError(...). Also, the OpInfo currently compares against a hand-written torch.matmul-based mock rather than the real torch._grouped_mm, so it's near-tautological — worth strengthening toward real-op parity and covering bias / out_dtype.

@justinchuby

Copy link
Copy Markdown
Collaborator

Sid (@Sid-V5) would you like to update this PR?

Sid (Sid-V5) and others added 3 commits July 8, 2026 23:03
Implements the converter for aten::_grouped_mm.default to address issue microsoft#2795. Handles the batch/dense mode where groups are implicit in the batch dimension using MatMul, with optional bias addition and dtype casting.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@Sid-V5
Sid (Sid-V5) force-pushed the add-grouped-mm-2795 branch from d73b04f to 1501909 Compare July 8, 2026 17:42
@Sid-V5

Copy link
Copy Markdown
Author

Justin Chu (@justinchuby)
Rebased onto main to resolve the conflict.
updated core.py to raise NotImplementedError if offs is not None, and updated the OpInfo tests to cover bias/out_dtype and use the real torch._grouped_mm when available

Comment thread tests/function_libs/torch_lib/extra_opinfo.py Fixed
Comment thread tests/function_libs/torch_lib/extra_opinfo.py Fixed
@Sid-V5
Sid (Sid-V5) force-pushed the add-grouped-mm-2795 branch from 1501909 to 0fa4617 Compare July 9, 2026 08:13

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

Comment thread onnxscript/function_libs/torch_lib/ops/core.py
Comment thread tests/function_libs/torch_lib/extra_opinfo.py
Comment thread tests/function_libs/torch_lib/extra_opinfo.py
Comment thread tests/function_libs/torch_lib/extra_opinfo.py Outdated
Sid (Sid-V5) and others added 2 commits July 16, 2026 10:38
- Changed out_dtype default from Optional[int]=None to int=-1, matching
  the sentinel convention used throughout core.py (e.g. aten_sum).
  Guards for both None and -1 to prevent invalid op.Cast(..., to=-1).

- Pass offs, bias, out_dtype as keyword arguments in SampleInput and
  in the native torch.ops.aten._grouped_mm call, since they are
  keyword-only in the aten schema (after *). This prevents the try:
  path from silently failing with TypeError.

Addresses Copilot review comments and RUFF/RUF059 lint feedback.
@justinchuby

Copy link
Copy Markdown
Collaborator

Could you fix lint?

Copilot AI review requested due to automatic review settings July 23, 2026 16:57

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

Comment thread tests/function_libs/torch_lib/extra_opinfo.py Outdated
Copilot AI review requested due to automatic review settings July 23, 2026 17:07

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

Copilot AI review requested due to automatic review settings July 28, 2026 12:32

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

Copilot AI review requested due to automatic review settings July 31, 2026 17:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

Suppressed comments (1)

onnxscript/function_libs/torch_lib/ops/core.py:4603

  • out_dtype handling should follow the existing pattern used elsewhere (e.g., quantized_decomposed.py:62-66): treat -1 and None as “not set”, and assert that any provided dtype id is valid (>0) before passing it to op.Cast. This avoids accidentally emitting an invalid ONNX Cast(to=0) if a caller ever forwards an unset/unknown dtype value.
    if out_dtype is not None and out_dtype != -1:
        res = op.Cast(res, to=out_dtype)
    return res

@justinchuby

Copy link
Copy Markdown
Collaborator

Sid (@Sid-V5) just my inline reviews and it should be ready to merge after that

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.

As commented, please change this to

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

Then in TorchLibOpInfo, you may skip the test if not hasattr(torch.ops.aten, "_grouped_mm")

@justinchuby Justin Chu (justinchuby) left a comment

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.

Nit as commented

@github-project-automation github-project-automation Bot moved this from Done to In Progress in ONNX Script Review Board Aug 31, 2026
… unavailable

- Update OpInfo in extra_opinfo.py to reference torch.ops.aten._grouped_mm
  directly instead of _mock_grouped_mm.
- Remove _mock_grouped_mm reference helper.
- Add .skip(enabled_if=not hasattr(torch.ops.aten, '_grouped_mm')) to
  TorchLibOpInfo in ops_test_data.py to cleanly skip on PyTorch versions
  without _grouped_mm support.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

module: torchlib Related to the torch/aten function lib in development

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

Missing converter for OpOverload(op='aten._grouped_mm', overload='default')

6 participants