[Relax][ONNX] Fix get_converter edge case for unsupported low opset#18888
[Relax][ONNX] Fix get_converter edge case for unsupported low opset#18888lonelyeagle wants to merge 1 commit intoapache:mainfrom
Conversation
Summary of ChangesHello, 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
🧠 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
Activity
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
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]) |
There was a problem hiding this comment.
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.
| 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) |
| 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") |
There was a problem hiding this comment.
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.