Skip to content

Fix TensorPrimitives.IndexOfMax#127454

Merged
tannergooding merged 10 commits intodotnet:mainfrom
lilinus:fix-tensor-indexof
May 6, 2026
Merged

Fix TensorPrimitives.IndexOfMax#127454
tannergooding merged 10 commits intodotnet:mainfrom
lilinus:fix-tensor-indexof

Conversation

@lilinus
Copy link
Copy Markdown
Contributor

@lilinus lilinus commented Apr 27, 2026

Fixes #124233
Fixes #127610

Tried to include feedback from #124274 (comment).

Summary of changes:

  • Change interface IIndexOfOperator to specialized IIndexOfMinMaxOperator, where there are Compare methods that returns masks of results/indices.
  • IndexOfMinMaxCore delegates to ten different methods:
    • IndexOfMinMaxVectorized128/256/512Size4Plus when sizeof(T) is 4 or 8. The result index fits in one vector.
    • IndexOfMinMaxVectorized128/256/512Size2 when sizeof(T) is 2. The result index fits in two vectors.
    • IndexOfMinMaxVectorized128/256/512Size1 when sizeof(T) is 1. The result index fits in four vectors.
    • IndexOfMinMaxFallback as fallback.
  • For vector methods: the final aggregation is done by horizontal-aggregation values in the lanes. Then the corresponding index found by matching that value bitwise.
  • The search is done left-to-right so there is no need for the IndexLessThan methods
  • Reintroduces some commented out TODO unit tests related to IndexOf methods

Copilot AI review requested due to automatic review settings April 27, 2026 14:09
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Apr 27, 2026
@dotnet-policy-service
Copy link
Copy Markdown
Contributor

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

@lilinus lilinus changed the title Fix tensor indexof Fix TensorPrimitives.IndexOfMax Apr 27, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR refactors the IndexOfMin/Max* tensor primitives to fix incorrect indices (notably for small element sizes) by introducing a specialized min/max operator interface and a new IndexOfMinMaxCore implementation with multiple vectorized paths.

Changes:

  • Replaced IIndexOfOperator with IIndexOfMinMaxOperator and moved/rewrote IndexOfMinMaxCore into shared code.
  • Implemented specialized Vector128/256/512 routines for sizeof(T) = 1/2/4/8 plus a naive fallback.
  • Added regression tests for IndexOfMax on byte/ushort when the correct index exceeds the element type’s max value.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
src/libraries/System.Numerics.Tensors/tests/TensorPrimitives.Generic.cs Adds regression tests for IndexOfMax returning indices > 255 and > 65535.
src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorPrimitives.IndexOfMinMagnitude.cs Updates operator to the new interface and comparison/aggregation model.
src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorPrimitives.IndexOfMin.cs Updates operator to the new interface and comparison/aggregation model.
src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorPrimitives.IndexOfMaxMagnitude.cs Updates operator to the new interface and comparison/aggregation model.
src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorPrimitives.IndexOfMax.cs Replaces per-method core logic with the shared core + new operator shape.
src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Common/TensorPrimitives.IIndexOfOperator.cs Introduces IIndexOfMinMaxOperator and the new shared vectorized implementations.

Comment thread src/libraries/System.Numerics.Tensors/tests/TensorPrimitives.Generic.cs Outdated
Comment thread src/libraries/System.Numerics.Tensors/tests/TensorPrimitives.Generic.cs Outdated
Copilot AI review requested due to automatic review settings April 30, 2026 12:27
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 4 comments.

Comment thread src/libraries/System.Numerics.Tensors/tests/Helpers.cs
Copilot AI review requested due to automatic review settings May 1, 2026 19:38
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 10 out of 10 changed files in this pull request and generated 3 comments.

@tannergooding
Copy link
Copy Markdown
Member

/ba-g networking test timeout

@tannergooding tannergooding merged commit a0694cd into dotnet:main May 6, 2026
85 of 90 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Numerics 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 can't handle signed min values TensorPrimitives.IndexOfMax produces incorrect results with vectorized paths

3 participants