Skip to content

Tensor<T> select. Iteration along dimensions #113697

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 14, 2025

Conversation

michaelgsharp
Copy link
Contributor

@michaelgsharp michaelgsharp commented Mar 19, 2025

Fixes #113068 and fixes #113863

Adds a new method, SliceAlongDimension, that lets you slice any given subdimension out of a tensor.

Adds a new method, Select, that lets you run Tensor operations on segments of the Tensor instead of the whole Tensor.

Adds a DimensionCollection, which is similar to a Dictionary's KeyCollection, in that it has an Enumerator that lets you iterate over the Tensor along a given dimension (compared to the KeyCollection that lets you iterate by the keys).

@michaelgsharp michaelgsharp self-assigned this Mar 19, 2025
@Copilot Copilot AI review requested due to automatic review settings March 19, 2025 16:26
@ghost
Copy link

ghost commented Mar 19, 2025

Note regarding the new-api-needs-documentation label:

This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change.

1 similar comment
@ghost
Copy link

ghost commented Mar 19, 2025

Note regarding the new-api-needs-documentation label:

This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change.

Copy link
Contributor

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

Copilot

This comment was marked as outdated.

@tjwald
Copy link

tjwald commented Mar 23, 2025

My comments on the new API:
#113068 (comment)

@ericstj
Copy link
Member

ericstj commented May 6, 2025

@michaelgsharp what's the state of this PR? Are you returning to it, or is it no longer necessary?

Copy link
Contributor

@Copilot 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

This PR introduces new slicing and selection operations for Tensor by adding methods such as SliceAlongDimension and Select, as well as new types for iterating along tensor dimensions. Key changes include new unit tests for dimension slicing, additions to the Tensor and TensorSpan implementations to support GetDimensionSpan, and corresponding project file and suppression updates.

Reviewed Changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated no comments.

Show a summary per file
File Description
tests/TensorDimensionSpanTests.cs Added thorough tests for dimension slicing on tensors in various shapes
src/System/Numerics/Tensors/netcore/Tensor_1.cs New GetDimensionSpan method and tweak in IDisposable.Dispose implementation
src/System/Numerics/Tensors/netcore/TensorSpan_1.cs Added GetDimensionSpan method for TensorSpan
src/System/Numerics/Tensors/netcore/TensorShape.cs Added AdjustToNextIndex and updated GetLinearOffset methods using DivRem
src/System/Numerics/Tensors/netcore/TensorDimensionSpan_1.cs New implementation of TensorDimensionSpan for slicing a tensor along a given dimension
csproj and CompatibilitySuppressions.xml Updated to include the new files and suppress expected compatibility diagnostics
Comments suppressed due to low confidence (1)

src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorDimensionSpan_1.cs:25

  • Consider adding a clarifying comment here to explain why the 'dimension' parameter is incremented, which would help improve readability and understanding of the slicing logic.
dimension += 1;

Copy link
Member

@ericstj ericstj left a comment

Choose a reason for hiding this comment

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

Looks good for the most part - shared a suggestion to cleanup suppressions and bring ref up to date.

/// <summary>Enumerates the spans of a tensor dimension span.</summary>
public ref struct Enumerator
#if NET9_0_OR_GREATER
: IEnumerator<ReadOnlyTensorSpan<T>>
Copy link
Member

Choose a reason for hiding this comment

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

Should this implement other interfaces in older TFMs?

Copy link
Member

Choose a reason for hiding this comment

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

What other interfaces would it implement?

@tannergooding
Copy link
Member

/ba-g unrelated failures

@tannergooding tannergooding merged commit 9ed11b9 into dotnet:main Jun 14, 2025
147 of 150 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Jul 15, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[API Proposal]: Tensor Dimensions Refining [API Proposal]: Tensor Operations Per Dimension
4 participants