Fix TensorPrimitives MinNumber/MaxNumber span reductions propagating NaN (#133346) - #133628
Open
jabrailkhalil wants to merge 2 commits into
Open
jabrailkhalil wants to merge 2 commits into
jabrailkhalil 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. |
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-numerics |
This was referenced Sep 10, 2026
Closed
Author
|
@dotnet-policy-service agree |
lilinus
reviewed
Sep 11, 2026
This was referenced Sep 12, 2026
Author
Author
|
The remaining failures on current head
Could a maintainer please rerun the failed runtime pipeline? The current head also incorporates the inline review feedback by covering all configured tensor lengths and all four Number reductions. |
This branch has not been deployed
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.
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_AllLengthsandNumberAggregates_IgnoreNaN_AllLengthsinTensorPrimitives.Generic.cs: all four*Numberreductions run across every configuredHelpers.TensorLengthsvalue; the NaN suite covers all-NaN spans plus NaN at the start/middle/end, and retains signed-zero checks.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.The full upstream runtime matrix ran on the current head. Its remaining red statuses are unrelated to this change: one macOS job exceeded the 120-minute limit and Helix reported three
System.Net.Sockets.Testswork-item failures; the other displayed runtime matrix jobs passed. The regression tests follow existing conventions inTensorPrimitives.Generic.csand run on all four floating-point instantiations.The .NET Foundation CLA check passes for
jabrailkhalil.