gemini-code-assist[bot] commented on code in PR #18888:
URL: https://github.com/apache/tvm/pull/18888#discussion_r2900764372
##########
python/tvm/relax/frontend/onnx/onnx_frontend.py:
##########
@@ -303,7 +303,10 @@ def get_converter(cls, opset):
"""
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])
Review 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.
```suggestion
opset_idx = next(i for i in range(len(versions) - 1, -1, -1) if
versions[i] == opset)
```
##########
python/tvm/relax/frontend/onnx/onnx_frontend.py:
##########
@@ -303,7 +303,10 @@ def get_converter(cls, opset):
"""
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])
+ if opset_idx == 0:
+ raise NotImplementedError(f"opset version {opset} of
{cls.__name__} not implemented")
+ version = versions[opset_idx - 1]
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")
Review Comment:

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.
```python
return getattr(cls, f"_impl_v{version}")
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]