Skip to content

Fix TensorPrimitives.IndexOfMaxMagnitude signed integer tie#128484

Merged
tannergooding merged 4 commits into
dotnet:mainfrom
lilinus:fix-indexofminmax
Jul 18, 2026
Merged

Fix TensorPrimitives.IndexOfMaxMagnitude signed integer tie#128484
tannergooding merged 4 commits into
dotnet:mainfrom
lilinus:fix-indexofminmax

Conversation

@lilinus

@lilinus lilinus commented May 22, 2026

Copy link
Copy Markdown
Contributor

Fixes #128478

I saw there are reported perf regressions for TensorPrimitives.IndexOfMax in #128088, so I understand if you can't take this PR to fix that (or revert #127454).

PR also includes:

  • Add coverage for fixed bug in IndexOf*Magnitude_Negative1LesserThanPositive1
  • Tighten asserts and don't avoid MinValue in IndexOf*Magnitude_AllLengths
  • Increase coverage in IndexOf*_Negative0LesserThanPositive0 to cover vectorized paths.
  • Ensure floating point tests for TensorPrimtivies are run for NFloat

@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label May 22, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-numerics-tensors
See info in area-owners.md if you want to be subscribed.

@tannergooding

Copy link
Copy Markdown
Member

@lilinus could you resolve the merge conflicts here?

@jeffhandley jeffhandley left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note

This review was generated by the holistic code review workflow being iterated on as part of #130339. Please treat its findings as assistive input for human review.

Holistic Review

Motivation: The linked issue demonstrates a real behavioral bug: for signed integers, IndexOfMaxMagnitude could disagree with MaxMagnitude when equal magnitudes have opposite signs. The fix is appropriately narrow and also hardens adjacent tie-breaking tests.

Approach: The vectorized signed-integer compare now mirrors the scalar T.MaxMagnitude/T.MinMagnitude tie rules while preserving the existing Abs(MinValue) overflow handling. I checked the scalar fallback, the 128/256/512-bit SIMD paths, the final aggregate/index selection logic, and the sibling IndexOfMinMagnitude path for consistency.

Summary: ✅ LGTM. I did not find a correctness issue in the updated tie handling: equal-magnitude signed integer ties now select positive for max-magnitude and negative for min-magnitude, while MinValue overflow still wins/loses as expected. The new tests cover scalar and vector-length inputs, opposite-sign ties, floating-point signed-zero parity, and restore MinValue exposure in the all-length magnitude tests.


Detailed Findings

✅ Correctness — signed integer ties and MinValue overflow remain consistent

In src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorPrimitives.IndexOfMaxMagnitude.cs lines 74-77, 100-103, and 126-129, the new equalResult is included only in the non-overflow comparison and remains masked by Vector*.IsNegative(yMag). That means current = +1, result = -1 now updates, current = -1, result = +1 does not, current = MinValue still updates over any non-MinValue, and an existing MinValue still prevents replacement by a smaller magnitude. The analogous IndexOfMinMagnitude changes in lines 74-77, 100-103, and 126-129 apply the inverse tie rule and keep MinValue treated as maximum magnitude, so it does not become a false minimum.

✅ Scalar/vector parity — the SIMD path matches the scalar operator semantics

The scalar Compare(T x, T y) methods already defer to T.MaxMagnitude/T.MinMagnitude and only update on the documented sign tie-break. The updated vector masks now apply the same replacement rule lane-by-lane, and IndexOfMinMaxCore then keeps the first index for equal retained values by preserving existing indices during same-value ties and selecting the minimum index among lanes equal to the horizontal aggregate. This addresses the linked vectorized-path mismatch without changing public API surface.

✅ Tests — coverage targets the regression and adjacent tie cases

src/libraries/System.Numerics.Tensors/tests/TensorPrimitivesTests.cs adds all-length coverage for equal-magnitude -1/+1 ties (IndexOfMaxMagnitude_Negative1LesserThanPositive1 and IndexOfMinMagnitude_Negative1LesserThanPositive1) and expands signed-zero tests across Helpers.TensorLengths, which exercises scalar and vector-length paths. The IndexOf*Magnitude_AllLengths tests now compute expectations with MaxMagnitude/MinMagnitude and use CreateAndFillTensor, so random signed integer inputs can include MinValue rather than avoiding the overflow-sensitive value. TensorPrimitives.Generic.cs also classifies NFloat, Half, nuint, and UInt128 correctly for these generic test guards.

@tannergooding
tannergooding merged commit 13c03a4 into dotnet:main Jul 18, 2026
88 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview7 milestone Jul 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Numerics.Tensors community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TensorPrimitives.IndexOfMaxMagnitude signed integer magnitude tie bug

3 participants