-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Fix TensorPrimitives MinNumber/MaxNumber span reductions propagating NaN (#133346) #133628
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jabrailkhalil
wants to merge
1
commit into
dotnet:main
Choose a base branch
from
jabrailkhalil:fix-tensorprimitives-number-nan-reduction
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1772,6 +1772,77 @@ public void StdDev_AllLengths() | |
| }); | ||
| } | ||
| #endregion | ||
|
|
||
| #region Number aggregates ignore NaN | ||
| [Theory] | ||
| [InlineData(1)] | ||
| [InlineData(3)] | ||
| [InlineData(4)] | ||
| [InlineData(5)] | ||
| [InlineData(16)] | ||
| [InlineData(33)] | ||
| public void NumberAggregates_IgnoreNaN(int length) | ||
| { | ||
| // IEEE 754:2019 minimumNumber/maximumNumber ignore a NaN operand when a numeric one is | ||
| // available, while minimum/maximum propagate it. The span reductions must match. | ||
| T nan = T.CreateTruncating(float.NaN); | ||
| T one = T.One; | ||
| T two = one + one; | ||
|
|
||
| if (length == 1) | ||
| { | ||
| AssertEqualAggregate(nan, TensorPrimitives.MinNumber<T>([nan])); | ||
| AssertEqualAggregate(nan, TensorPrimitives.MaxNumber<T>([nan])); | ||
| AssertEqualAggregate(nan, TensorPrimitives.MinMagnitudeNumber<T>([nan])); | ||
| AssertEqualAggregate(nan, TensorPrimitives.MaxMagnitudeNumber<T>([nan])); | ||
| AssertEqualAggregate(nan, TensorPrimitives.Min<T>([nan])); | ||
| AssertEqualAggregate(nan, TensorPrimitives.Max<T>([nan])); | ||
| return; | ||
| } | ||
|
|
||
| T[] values = new T[length]; | ||
|
|
||
| // NaN at the start, in the middle, and at the end of the span, so both the vectorized | ||
| // and the scalar tails of the reduction see it. | ||
| foreach (int nanIndex in new[] { 0, length / 2, length - 1 }) | ||
| { | ||
| Array.Fill(values, two); | ||
| values[nanIndex] = nan; | ||
|
|
||
| // A distinct minimum so the reduction is not trivially the fill value. | ||
| values[(nanIndex + 1) % length] = one; | ||
|
|
||
| // Number variants ignore the NaN and pick the numeric extreme. | ||
| AssertEqualAggregate(one, TensorPrimitives.MinNumber<T>(values)); | ||
| AssertEqualAggregate(two, TensorPrimitives.MaxNumber<T>(values)); | ||
| AssertEqualAggregate(one, TensorPrimitives.MinMagnitudeNumber<T>(values)); | ||
| AssertEqualAggregate(two, TensorPrimitives.MaxMagnitudeNumber<T>(values)); | ||
|
|
||
| // Plain Min/Max still propagate NaN. | ||
| AssertEqualAggregate(nan, TensorPrimitives.Min<T>(values)); | ||
| AssertEqualAggregate(nan, TensorPrimitives.Max<T>(values)); | ||
| AssertEqualAggregate(nan, TensorPrimitives.MinMagnitude<T>(values)); | ||
| AssertEqualAggregate(nan, TensorPrimitives.MaxMagnitude<T>(values)); | ||
| } | ||
|
|
||
| // Signed zeros follow minimumNumber/maximumNumber: +0 is greater than -0. | ||
| T[] signedZeros = { -T.Zero, T.Zero }; | ||
| Assert.True(T.IsNegative(TensorPrimitives.MinNumber<T>(signedZeros))); | ||
| Assert.False(T.IsNegative(TensorPrimitives.MaxNumber<T>(signedZeros))); | ||
|
|
||
| static void AssertEqualAggregate(T expected, T actual) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this helper is not needed, since it seems that |
||
| { | ||
| if (T.IsNaN(expected)) | ||
| { | ||
| Assert.True(T.IsNaN(actual), $"expected NaN, got {actual}"); | ||
| } | ||
| else | ||
| { | ||
| Assert.Equal(expected, actual); | ||
| } | ||
| } | ||
| } | ||
| #endregion | ||
| } | ||
|
|
||
| public unsafe abstract class GenericSignedIntegerTensorPrimitivesTests<T> : GenericIntegerTensorPrimitivesTests<T> | ||
|
|
||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like
MinNumber(ROS)andMaxNumber(ROS)is currently missing any coverage (not only for NaN values, but overall). N.b.MinNumber(ROS, ROS)andMaxNumber(ROS, ROS)seems to be covered. Worth adding more coverage while at it?I think we need to test more values of
lengthif we want to cover all paths (forVector128,Vector256,Vector512etc...). Great opportunity to useAssert.All(Helpers.TensorLengths, ...?Also, looks like
Max/Minare already covered by testsMax_Tensor_NanReturnedandMin_Tensor_NanReturned