Skip to content

fix(conversion): convert aten._grouped_mm when the group offsets are a constant - #5047

Open
SharvenRane wants to merge 1 commit into
pytorch:mainfrom
SharvenRane:fix/5038-grouped-mm-constant-offsets
Open

SharvenRane wants to merge 1 commit into
pytorch:mainfrom
SharvenRane:fix/5038-grouped-mm-constant-offsets

Conversation

@SharvenRane

Copy link
Copy Markdown

Description

When a mixture of experts layer has fixed group sizes, for example a buffer of offsets, the offsets reach the aten._grouped_mm converter as a constant torch.Tensor after freezing. The dense lowering passed them straight to cast_trt_tensor, which needs an ITensor, so dynamo.compile failed with TypeError: add_cast(): incompatible function arguments and the grouped matmul stayed out of TensorRT, splitting the graph into extra engines.

impl.moe.grouped_mm now turns offs into a TensorRT tensor with get_trt_tensor before the cast. The change is in the shared implementation rather than in enforce_tensor_types, because offs can be positional or keyword and enforce_tensor_types is indexed one way only.

Fixes #5038

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Testing

  • New test_grouped_mm_constant_offs covers offsets passed by keyword and by position. conversion/test_moe_aten.py: 2 failed, 7 passed on main; 9 passed with this change.
  • Full tests/py/dynamo/conversion/: the same 42 tests fail on main and with this change (71 of the repo wide failures come from the recent nightly dict.discard change addressed in fix: FakeTensorUpdater with the dict processed_hashes in PyTorch main #5046); the only difference is the 2 new tests, which pass.
  • The repro from the issue now builds one engine and matches eager.
  • Pre-commit hooks: isort, ruff, black and typos pass; mypy reports the same errors as on main.

Tested on an RTX 5070 Ti with TensorRT 11.4 and torch 2.16.0.dev20261115.

Checklist:

  • My code follows the style guidelines of this project (You can use the linters)
  • I have added tests to verify my fix or my feature
  • New and existing unit tests pass locally with my changes

…a constant

Fixed group sizes, such as a buffer that says how many rows each expert gets,
reach the converter as a torch.Tensor after freezing. The dense lowering passed
that tensor straight to cast_trt_tensor, which needs an ITensor, so the
conversion raised a TypeError. Turn the offsets into a TensorRT tensor first,
as the converter already does for its other inputs.

Fixes pytorch#5038
@meta-cla meta-cla Bot added the cla signed label Nov 16, 2026
@github-actions github-actions Bot added component: tests Issues re: Tests component: conversion Issues re: Conversion stage component: core Issues re: The core compiler component: converters Issues re: Specific op converters component: api [Python] Issues re: Python API component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths labels Nov 16, 2026
@github-actions
github-actions Bot requested a review from narendasan November 16, 2026 01:10
@narendasan
narendasan requested review from zewenli98 and removed request for narendasan November 23, 2026 23:31

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla signed component: api [Python] Issues re: Python API component: conversion Issues re: Conversion stage component: converters Issues re: Specific op converters component: core Issues re: The core compiler component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths component: tests Issues re: Tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

aten._grouped_mm converter raises TypeError when the offsets are a constant

1 participant