Fix TensorPrimitives MinNumber/MaxNumber span reductions propagating NaN (#133346) - #133628
Fix TensorPrimitives MinNumber/MaxNumber span reductions propagating NaN (#133346)#133628jabrailkhalil wants to merge 2 commits into
Conversation
|
Azure Pipelines: Successfully started running 3 pipeline(s). 13 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
|
Tagging subscribers to this area: @dotnet/area-system-numerics |
|
@dotnet-policy-service agree |
| [InlineData(5)] | ||
| [InlineData(16)] | ||
| [InlineData(33)] | ||
| public void NumberAggregates_IgnoreNaN(int length) |
There was a problem hiding this comment.
Looks like MinNumber(ROS) and MaxNumber(ROS) is currently missing any coverage (not only for NaN values, but overall). N.b. MinNumber(ROS, ROS) and MaxNumber(ROS, ROS) seems to be covered. Worth adding more coverage while at it?
I think we need to test more values of length if we want to cover all paths (for Vector128, Vector256, Vector512 etc...). Great opportunity to use Assert.All(Helpers.TensorLengths, ...?
Also, looks like Max/Min are already covered by tests Max_Tensor_NanReturned and Min_Tensor_NanReturned
| Assert.True(T.IsNegative(TensorPrimitives.MinNumber<T>(signedZeros))); | ||
| Assert.False(T.IsNegative(TensorPrimitives.MaxNumber<T>(signedZeros))); | ||
|
|
||
| static void AssertEqualAggregate(T expected, T actual) |
There was a problem hiding this comment.
I think this helper is not needed, since it seems that Assert.Equal is enough for asserting NaNs in e.g. Max_Tensor_NanReturned
|
Thanks @lilinus! I have addressed the feedback in commit 8710e9a by expanding the test coverage for \MinNumber(ROS)\ and \MaxNumber(ROS)\ across tensor lengths and simplifying the NaN assertions using \Assert.Equal\ directly. |
Summary
Fixes #133346
TensorPrimitives.MinNumber(ReadOnlySpan<T>)(andMaxNumber,MinMagnitudeNumber,MaxMagnitudeNumber) returnedNaNas soon as the reduction encountered aNaN, while the scalarT.MinNumber/T.MaxNumbersemantics ignore a NaN operand when a numeric one is available (IEEE 754:2019minimumNumber/maximumNumber).The span reductions share
MinMaxCore<T, TMinMaxOperator>with the plain Min/Max/Magnitude reductions, and that core early-exits on the first NaN at every vector width and in its scalar tail. The early exit is correct forminimum/maximum(which propagate NaN), but wrong for the*Numberfamily.Changes
IAggregationOperator<T>gainsstatic virtual bool PropagatesNaNs => true.*NumberOperator<T>structs override it withfalse.MinMaxCoregates its NaN early-exits onTMinMaxOperator.PropagatesNaNs(Vector512/256/128 paths plus the scalar tail), so*Numberreductions proceed and the lane-wise*Numberoperator ignores the NaN, while plain Min/Max/Magnitude behavior is unchanged.NumberAggregates_IgnoreNaNinTensorPrimitives.Generic.cs: NaN at the start/middle/end of spans of several lengths (scalar and vector paths) for all four*Numberreductions, the all-NaN case, plus signed-zero ordering and the plain Min/Max NaN propagation control.Validation
Reproduced on the released .NET 10 SDK (10.0.401):
TensorPrimitives.MinNumber<float>([1f, NaN, 2f])returnedNaN, and the same forMaxNumber,MinMagnitudeNumber,MaxMagnitudeNumber.The fixed sources were compiled with the SDK (all
src/System.Numerics.Tensorsnetcore sources) and verified against a 25-case matrix:MinNumber(float) [1, NaN, 2]FAILED withgot NaN, expected 1.MinNumber/MaxNumber/MinMagnitudeNumber/MaxMagnitudeNumberignore NaN (float, double, Half; NaN first/middle/last; vector-sized and scalar inputs), all-NaN inputs still return NaN,-0vs+0ordering preserved, and plainMin/Max/MinMagnitude/MaxMagnitudestill propagate NaN.Not run locally: the repo's full library test suite (requires the arcade toolchain setup). The regression test follows existing conventions in
TensorPrimitives.Generic.cs(GenericFloatingPointNumberTensorPrimitivesTests<T>) and runs on all four floating-point instantiations in CI.Note: the .NET Foundation CLA must be signed for this PR to be mergeable (the account
jabrailkhalilwill need to sign at https://cla.dotnetfoundation.org).