Skip to content

[Relax][PyTorch] Support bare operator.add and operator.sub in PyTorch ExportedProgram frontend#18919

Closed
mshr-h wants to merge 1 commit into
apache:mainfrom
mshr-h:add-bare-add-sub
Closed

[Relax][PyTorch] Support bare operator.add and operator.sub in PyTorch ExportedProgram frontend#18919
mshr-h wants to merge 1 commit into
apache:mainfrom
mshr-h:add-bare-add-sub

Conversation

@mshr-h
Copy link
Copy Markdown
Contributor

@mshr-h mshr-h commented Mar 20, 2026

Added "add" and "sub" entries to the ExportedProgram frontend convert_map.

When operator.add or operator.sub appears in an FX graph, its __name__ is "add" or "sub" (without suffix), which wasn't matched by existing entries like "add.Tensor".

…h ExportedProgram frontend

Added `"add"` and `"sub"` entries to the ExportedProgram frontend `convert_map`.

When `operator.add` or `operator.sub` appears in an FX graph, its `__name__` is `"add"` or `"sub"` (without suffix), which wasn't matched by existing entries like `"add.Tensor"`.
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the PyTorch ExportedProgram frontend within TVM Relax by introducing explicit support for bare operator.add and operator.sub operations. Previously, the frontend only recognized more specific forms like "add.Tensor". This change ensures that when operator.add or operator.sub appear in an FX graph with their base names, particularly in contexts involving dynamic shape arithmetic, they are correctly translated into Relax operations, improving the robustness of PyTorch model import.

Highlights

  • PyTorch Frontend Enhancement: Added support for bare operator.add and operator.sub in the PyTorch ExportedProgram frontend.
  • FX Graph Compatibility: Ensured that operator.add and operator.sub are correctly matched when their __name__ is "add" or "sub" in an FX graph.
  • New Test Coverage: Introduced a new test case to verify the proper handling of these bare operators, especially in dynamic shape arithmetic scenarios.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request correctly adds support for bare operator.add and operator.sub in the PyTorch ExportedProgram frontend by updating the convert_map. This is a necessary change for handling dynamic shapes, as torch.export can emit these operators in such scenarios. The new test case effectively validates this functionality. I've identified a minor fragility issue in the test where hardcoded symbolic variable names are used, and I've provided suggestions to make the test more robust.

R.Tensor(("s0", 4), dtype="float32")
):
s0 = T.int64(is_size_var=True)
R.func_attr({"tir_var_lower_bound": {"s77": 2}})
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.

medium

The hardcoded symbolic variable name "s77" in func_attr makes this test fragile, as the name generated by PyTorch's export is not guaranteed to be consistent. While map_free_vars=True can map symbolic variables in shapes, it doesn't apply to string keys in the attributes dictionary. Since the main purpose of this test is to verify the translation of bare add and sub operators, and not the range constraints, I suggest removing this line to make the test more robust.

R.Tensor(("s0", 4), dtype="float32")
):
s0 = T.int64(is_size_var=True)
R.func_attr({"tir_var_lower_bound": {"s77": 2}})
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.

medium

For the same reason as in ExpectedAdd, the hardcoded symbolic variable name "s77" here makes the test fragile. To improve robustness, I recommend removing this line.

@mshr-h mshr-h closed this Mar 20, 2026
@mshr-h mshr-h deleted the add-bare-add-sub branch March 20, 2026 06:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant