[CPU][Ref] Fix Reduce ops to produce stable (zero val) output for empty input #27423
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.
Details:
Fix Reduce ops to produce stable output (filled with zeroes instead of random values) for an empty input:
including update of CPU and reference implementations.
Such behavior has been requested for NNCF
Originally random values has been produced, because common shape inference of the Reduce can produce non empty output shape for an empty input, for example Reduce with keep_dims=False:
InputShape{2, 0}, reduce_axes=1, OutputShape{2}
Common CPU Reduce::isExecutable was returning
False
for an empty input tensor (with 0 dim in the input Shape), and the Reduce::execute has not been executed at all for such case.The proposal of updated behavior is to return
False
for an empty output shape (with 0 dim in the output Shape),but when the input is empty, but the output is not, the output will be filled with 0.
Note: This change is considered as partially backward compatible with relatively low risk. Some of the Reduce ops already has defined default value other than zero in reference. Unifying the default value to be zero for all Reduce ops, makes onnx reduce prod and reduce min with empty input tests fail: logs
Alternatively Reduce ops common
shape_inference
can be updated:or
But those options are currently considered as not backward compatible, and would require new version of each Reduce* op.
Tickets: