Skip to content

[Relax][ONNX] Fix get_converter edge case for unsupported low opset#18888

Open
lonelyeagle wants to merge 1 commit intoapache:mainfrom
lonelyeagle:fix_get_converter
Open

[Relax][ONNX] Fix get_converter edge case for unsupported low opset#18888
lonelyeagle wants to merge 1 commit intoapache:mainfrom
lonelyeagle:fix_get_converter

Conversation

@lonelyeagle
Copy link

This PR fixes an edge case in OnnxOpConverter.get_converter in python/tvm/relax/frontend/onnx/onnx_frontend.py.

Previously, when the requested opset was lower than the minimum supported _impl_v* version, the python list index logic could select an invalid previous element (via negative indexing) instead of failing clearly.

This change keeps the existing converter-selection logic and adds a guard:

compute the index of opset in the sorted version list (including opset)
if opset is at index 0, raise NotImplementedError
otherwise continue with existing behavior
This ensures unsupported low opset versions fail fast with an explicit error instead of potentially selecting an incorrect converter.

@gemini-code-assist
Copy link
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 stability of the ONNX frontend by addressing a specific edge case in opset version handling. It introduces a mechanism to gracefully manage requests for ONNX opset versions that are below the minimum supported implementation, thereby preventing potential runtime errors or misinterpretations of available converters.

Highlights

  • Robustness for ONNX opset conversion: Implemented a guard in OnnxOpConverter.get_converter to prevent incorrect converter selection when an unsupported low opset version is requested. This ensures that attempts to use opsets lower than the minimum supported version now explicitly raise a NotImplementedError instead of potentially failing silently or selecting an invalid converter.

🧠 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.

Changelog
  • python/tvm/relax/frontend/onnx/onnx_frontend.py
    • Introduced opset_idx variable to store the index of the requested opset.
    • Added a conditional check to raise NotImplementedError if opset_idx is 0, indicating an unsupported low opset.
    • Adjusted the version calculation to use opset_idx - 1 after the new guard.
Activity
  • No activity has occurred on this pull request yet.
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.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

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
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 addresses an edge case where an unsupported low opset could lead to incorrect behavior due to negative indexing. The added guard ensures a clear failure for such cases. I've included a couple of suggestions to further simplify and improve the efficiency of the implementation.

Note: Security Review did not run due to the size of the PR.

versions = [int(d.replace("_impl_v", "")) for d in dir(cls) if "_impl_v" in d]
versions = sorted(versions + [opset])
version = versions[max([i for i, v in enumerate(versions) if v == opset]) - 1]
opset_idx = max([i for i, v in enumerate(versions) if v == opset])
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

This list comprehension can be inefficient as it builds a full list of indices. A more efficient and Pythonic way to find the last index of an element is to iterate from the end and stop at the first match. Using a generator expression with next would be a good approach here.

Suggested change
opset_idx = max([i for i, v in enumerate(versions) if v == opset])
opset_idx = next(i for i in range(len(versions) - 1, -1, -1) if versions[i] == opset)

Comment on lines 310 to 312
if hasattr(cls, f"_impl_v{version}"):
return getattr(cls, f"_impl_v{version}")
raise NotImplementedError(f"opset version {version} of {cls.__name__} not implemented")
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The version selected at this point is guaranteed to be a supported version from the _impl_v* methods. Therefore, this hasattr check is redundant and can be removed for simplification. You can directly return the converter.

        return getattr(cls, f"_impl_v{version}")

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