Fix scatter_reduce(reduce="mean") producing incorrect ONNX export results#2829
Draft
Fix scatter_reduce(reduce="mean") producing incorrect ONNX export results#2829
Conversation
ONNX ScatterElements doesn't support 'mean' reduction. Previously the code mapped 'mean' to 'none' (no reduction), which just overwrote values instead of computing the mean. The fix implements mean as sum/count: - scatter_sum: ScatterElements with reduction='add' onto zeros - scatter_count: ScatterElements of ones with reduction='add' onto zeros - For include_self=True: add self to sum and 1 to count - For include_self=False: use max(count, 1) to avoid div-by-zero (positions with count=0 also have sum=0, so 0/1=0 is correct) Also removes the xfail for scatter_reduce mean in ops_test_data.py and adds e2e tests for both include_self=True and include_self=False cases. Co-authored-by: justinchuby <11205048+justinchuby@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix incorrect results for scatter_reduce with ONNX export
Fix scatter_reduce(reduce="mean") producing incorrect ONNX export results
Feb 25, 2026
❌ 4 Tests Failed:
View the top 3 failed test(s) by shortest run time
View the full list of 1 ❄️ flaky test(s)
To view more test analytics, go to the Test Analytics Dashboard |
Collaborator
|
@copilot CI errors |
Co-authored-by: justinchuby <11205048+justinchuby@users.noreply.github.com>
Contributor
Author
Fixed in 6614c55. There were two CI failures:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
aten_scatter_reduceincore.pyto implementreduce="mean"correctlyinclude_self=True: sum/count approach including self in bothinclude_self=False: useWhereto preserveself[i]for positions with no scattered values (fixes CI failure where original values were incorrectly replaced with 0)e2e_ops_tests.py(forward method signature too long)xfailinops_test_data.pyto remove general mean xfailscatter_reducewithreduce="mean"for bothinclude_selfvaluesOriginal prompt
This section details on the original issue you should resolve
<issue_title>torch.export + torch.onnx.export(dynamo=True) gives incorrect results for scatter_reduce_(reduce="mean")</issue_title>
<issue_description>### 🐛 Describe the bug
Observed behaviour:
scatter_reduce(mean)shows a large mismatch after export:max_abs_diff: 10.0mean_abs_diff: 5.5sum/countcontrol matches exactly:max_abs_diff: 0.0mean_abs_diff: 0.0Expected behaviour:
scatter_reduce_(reduce="mean", include_self=False)Impact:
Environment:
torch:2.7.1onnxruntime:1.24.1python:3.12os:macOS (darwin 24.6.0)Code example:
Example output: