Fix TensorPrimitives MinNumber/MaxNumber span reductions propagating NaN (#133346) - #133628
Fix TensorPrimitives MinNumber/MaxNumber span reductions propagating NaN (#133346)#133628jabrailkhalil wants to merge 1 commit 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. |
|
@jabrailkhalil please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement ( “Agreement” ) is agreed to by the party signing below ( “You” ), 1. Definitions. “Code” means the computer software code, whether in human-readable or machine-executable form, “Project” means any of the projects owned or managed by .NET Foundation and offered under a license “Submit” is the act of uploading, submitting, transmitting, or distributing code or other content to any “Submission” means the Code and any other copyrightable material Submitted by You, including any 2. Your Submission. You must agree to the terms of this Agreement before making a Submission to any 3. Originality of Work. You represent that each of Your Submissions is entirely Your 4. Your Employer. References to “employer” in this Agreement include Your employer or anyone else 5. Licenses. a. Copyright License. You grant .NET Foundation, and those who receive the Submission directly b. Patent License. You grant .NET Foundation, and those who receive the Submission directly or c. Other Rights Reserved. Each party reserves all rights not expressly granted in this Agreement. 6. Representations and Warranties. You represent that You are legally entitled to grant the above 7. Notice to .NET Foundation. You agree to notify .NET Foundation in writing of any facts or 8. Information about Submissions. You agree that contributions to Projects and information about 9. Governing Law/Jurisdiction. This Agreement is governed by the laws of the State of Washington, and 10. Entire Agreement/Assignment. This Agreement is the entire agreement between the parties, and .NET Foundation dedicates this Contribution License Agreement to the public domain according to the Creative Commons CC0 1. |
|
Tagging subscribers to this area: @dotnet/area-system-numerics |
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).