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 |
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_reduce.twowithreduce="mean"silently produced wrong results after ONNX export because the implementation mapped"mean"→ ONNXreduction="none"(a plain overwrite), with a comment acknowledging it was unsupported but no correct fallback.Root cause
ScatterElementsin ONNX has no nativemeanreduction. The previous code fell through toreduction="none", which simply overwrites each output position with the last scattered value instead of averaging.Fix
Implements mean explicitly as
sum / countusing twoScatterElements(reduction="add")passes:srcvalues at each output indexinclude_self=True: addsselfto both sum (+self) and count (+1)include_self=False: usesmax(count, 1)to avoid 0/0 — positions with no incoming values havesum=0so0/1=0matches PyTorch semanticsTest changes
xfail(variant_name="mean")fromops_test_data.py— this reduction mode is now correctly supportedinclude_self=Falseandinclude_self=TrueOriginal 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: