Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion python/tvm/relax/frontend/onnx/onnx_frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
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)

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")
Comment on lines 310 to 312
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}")

Expand Down