You are viewing a plain text version of this content. The canonical link for it is here.
Posted to discuss-archive@tvm.apache.org by "swjng (via GitHub)" <gi...@apache.org> on 2026/04/09 08:47:47 UTC
[PR] [BugFix][Relax] Fix ONNX get_converter index underflow for old opsets [tvm]
swjng opened a new pull request, #19376:
URL: https://github.com/apache/tvm/pull/19376
## Description
`OnnxOpConverter.get_converter()` has an index underflow bug: when a model's opset is below all implemented converter versions, the selection index wraps to -1 (Python negative indexing), silently picking the **latest** implementation instead of the earliest.
### Root cause
```python
versions = sorted(versions + [opset])
version = versions[max([i for i, v in enumerate(versions) if v == opset]) - 1]
# ^^^
# opset=11, versions=[11, 13, 18] → index=0 → 0-1=-1 → versions[-1]=18
```
### Impact
14 operators affected. For opset 11-12 models, these ops silently produce wrong results:
| Operator | Impl versions | Opset 11 dispatches to | Correct |
|----------|:---:|:---:|:---:|
| ReduceMean | [13, 18] | **v18** | v13 |
| ReduceL1/L2 | [13, 18] | **v18** | v13 |
| ReduceLogSum | [13, 18] | **v18** | v13 |
| ReduceLogSumExp | [13, 18] | **v18** | v13 |
| ReduceProd | [13, 18] | **v18** | v13 |
| ReduceSumSquare | [13, 18] | **v18** | v13 |
| ReduceMax/Min | [11, 18] | correct | correct |
| Pad, Scatter, ScatterND, RoiAlign | various | **wrong** | — |
The v18 implementations read `axes` from **inputs**, but opset 11-12 passes `axes` as **attributes** — so axes becomes None and the op reduces over all dimensions.
Example: `ReduceMean(axes=[2,3])` on shape `(2,3,4,4)`:
- Before fix: output shape `(1,1,1,1)` (wrong, all-axis reduction)
- After fix: output shape `(2,3,1,1)` (correct)
### Fix
Replace the index arithmetic with an explicit filter:
```python
candidates = [v for v in impl_versions if v <= opset]
version = max(candidates) if candidates else impl_versions[0]
```
### Testing
Added opset 11 test cases for 7 Reduce operators in `test_all_reduce_funcs_axes_attr`. All 20 new tests pass. Existing tests unaffected (570 pass, 14 pre-existing failures in axes_input/topk unrelated to this change).
```bash
pytest tests/python/relax/test_frontend_onnx.py -k "axes_attr and 11" -v
```
--
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: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org
Re: [PR] [BugFix][Relax] Fix ONNX `get_converter` index underflow for old opsets [tvm]
Posted by "gemini-code-assist[bot] (via GitHub)" <gi...@apache.org>.
gemini-code-assist[bot] commented on code in PR #19376:
URL: https://github.com/apache/tvm/pull/19376#discussion_r3056594602
##########
python/tvm/relax/frontend/onnx/onnx_frontend.py:
##########
@@ -302,9 +302,11 @@ def get_converter(cls, opset):
converter, which should be `_impl_vx`. Number x is the biggest
number smaller than or equal to opset belongs to all support versions.
"""
- 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]
+ impl_versions = sorted(int(d.replace("_impl_v", "")) for d in dir(cls) if "_impl_v" in d)
+ # Select the largest implemented version that is <= opset.
+ # If opset is below all implementations, fall back to the smallest.
+ candidates = [v for v in impl_versions if v <= opset]
+ version = max(candidates) if candidates else impl_versions[0]
Review Comment:

While the new logic is much clearer and fixes the underflow bug, it could be made more robust. If an `OnnxOpConverter` subclass is defined without any `_impl_v...` methods, `impl_versions` will be an empty list. This would cause an `IndexError` on line 309 when trying to access `impl_versions[0]`.
To prevent this and provide a more informative error message, it would be good to add an explicit check for an empty `impl_versions` list.
```python
impl_versions = sorted(int(d.replace("_impl_v", "")) for d in dir(cls) if "_impl_v" in d)
if not impl_versions:
raise NotImplementedError(f"No ONNX op implementation found for {cls.__name__}")
candidates = [v for v in impl_versions if v <= opset]
version = max(candidates) if candidates else impl_versions[0]
```
--
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: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org
Re: [PR] [BugFix][Relax] Fix ONNX `get_converter` index underflow for old opsets [tvm]
Posted by "tlopex (via GitHub)" <gi...@apache.org>.
tlopex merged PR #19376:
URL: https://github.com/apache/tvm/pull/19376
--
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: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org