Skip to content

[BugFix][Relax]: handle ONNX ScatterElements reduction#19527

Open
THINKER-ONLY wants to merge 1 commit intoapache:mainfrom
THINKER-ONLY:fix-onnx-scatter-elements-reduction
Open

[BugFix][Relax]: handle ONNX ScatterElements reduction#19527
THINKER-ONLY wants to merge 1 commit intoapache:mainfrom
THINKER-ONLY:fix-onnx-scatter-elements-reduction

Conversation

@THINKER-ONLY
Copy link
Copy Markdown

Summary

  • Respect the ONNX reduction attribute in the Relax ONNX frontend ScatterElements converter.
  • Preserve existing default behavior by mapping missing reduction and ONNX none to Relax update.
  • Add focused regression coverage for opset 11 default behavior, opset 16 add/mul, and opset 18 none/min/max.

Changes

  • Added a shared helper to normalize and validate ONNX reduction attributes.
  • Implemented ScatterElements opset 16 and opset 18 converters.
  • Reused the existing relax.op.scatter_elements(..., reduction=...) API.
  • Reused the same reduction helper in ScatterND to keep behavior consistent.

Test Plan

  • python -m py_compile python/tvm/relax/frontend/onnx/onnx_frontend.py tests/python/relax/test_frontend_onnx.py
  • python -m pytest tests/python/relax/test_frontend_onnx.py::test_gather_elements tests/python/relax/test_frontend_onnx.py::test_scatter tests/python/relax/test_frontend_onnx.py::test_scatter_elements_reduction tests/python/relax/test_frontend_onnx.py::test_scatter_nd -q

Issue

Fixes #19435

Local Verification Notes

  • WSL conda environment: /home/thinker/.cache/tvm-conda-onnx
  • TVM build directory: /home/thinker/.cache/tvm-build-onnx
  • LLVM runtime check: tvm.runtime.enabled("llvm") == True
  • Relevant ONNX frontend subset: 14 passed, 4 skipped, 1 warning
  • Full tests/python/relax/test_frontend_onnx.py was also attempted. It currently has 14 failures in unrelated Reduce* axes input and TopK tests; running the same selected failures against origin/main reproduces them, so they are not introduced by this PR.

Copilot AI review requested due to automatic review settings May 9, 2026 15:25
Copy link
Copy Markdown
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 adds support for reduction modes in the ScatterElements ONNX operator for opsets 16 and 18 and refactors the reduction attribute processing into a common helper function. Feedback was provided to replace an assert statement with a ValueError to ensure validation is maintained in optimized environments and to improve the error message wording.

Comment on lines +1149 to +1151
assert reduction in valid_reductions, (
f"Only {valid_reductions} reductions are supported, but {reduction} is gotten"
)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Using assert for validating model attributes is discouraged as it can be optimized away in production environments (when running Python with -O). It is better to raise a ValueError to ensure the validation logic always executes. Additionally, the error message "is gotten" is non-idiomatic; "got" or "received" is preferred.

Suggested change
assert reduction in valid_reductions, (
f"Only {valid_reductions} reductions are supported, but {reduction} is gotten"
)
if reduction not in valid_reductions:
raise ValueError(
f"Only {valid_reductions} reductions are supported, but got {reduction}"
)

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes incorrect semantics in the Relax ONNX frontend by honoring the ONNX ScatterElements reduction attribute (introduced in newer opsets) and aligning normalization/validation with existing ScatterND behavior.

Changes:

  • Add a shared helper to normalize ONNX reduction values (including mapping ONNX "none" / missing to Relax "update").
  • Implement ScatterElements converters for opset 16 and opset 18 to support add/mul and min/max.
  • Add regression tests covering multiple opsets and reduction modes for ScatterElements.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
python/tvm/relax/frontend/onnx/onnx_frontend.py Adds reduction normalization helper and implements opset 16/18 ScatterElements conversion; reuses helper for ScatterND.
tests/python/relax/test_frontend_onnx.py Adds parameterized tests validating ScatterElements reduction behavior across opsets.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +1149 to +1151
assert reduction in valid_reductions, (
f"Only {valid_reductions} reductions are supported, but {reduction} is gotten"
)
Comment on lines +964 to +980
@pytest.mark.parametrize(
"reduction, opset, data, indices, updates",
[
(
None,
11,
np.array([[1, 2, 3], [4, 5, 6]], dtype="float32"),
np.array([[2, 0, 1], [1, 2, 0]], dtype="int64"),
np.array([[30, 10, 20], [50, 60, 40]], dtype="float32"),
),
(
"none",
18,
np.array([[1, 2, 3], [4, 5, 6]], dtype="float32"),
np.array([[2, 0, 1], [1, 2, 0]], dtype="int64"),
np.array([[30, 10, 20], [50, 60, 40]], dtype="float32"),
),
@THINKER-ONLY THINKER-ONLY changed the title fix(relax): handle ONNX ScatterElements reduction [BugFix][Relax]: handle ONNX ScatterElements reduction May 9, 2026
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.

[Bug] [Relax][ONNX] ScatterElements ignores reduction attribute, silently produces wrong results

2 participants