Skip to content
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

Tensor<T> select. Iteration along dimensions #113697

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

michaelgsharp
Copy link
Member

Fixes #113068.

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).

Copy link

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
Copy link

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.

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 functionality for operating on subdimensions of tensors by adding methods to slice along a given dimension and perform per-dimension operations via the new DimensionCollection.

  • Adjusts enumerator initialization and termination in TensorSpan and ReadOnlyTensorSpan.
  • Adds a DimensionCollection (with enumerators) to Tensor, TensorSpan, and ReadOnlyTensorSpan for iterating over tensor segments.
  • Introduces new methods SliceAlongDimension and Select (with associated delegate types) to process tensor segments.

Reviewed Changes

Copilot reviewed 10 out of 11 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
TensorSpan.cs Updated enumerator initialization/termination and added a DimensionCollection property.
ReadOnlyTensorSpan.cs Updated enumerator logic and introduced a DimensionCollection.
Tensor.cs Added SliceAlongDimension, a DimensionCollection property, and new Select-overload in Tensor. Also introduced a regression in AsReadOnlyTensorSpan.
TensorExtensions.cs Added new Select extension methods and related delegate types.
Tests files Added tests for dimensions functionality and sub-tensor slicing.
Interface/ref files Updated API surface for new methods and properties.
ThrowHelper.cs Minor adjustments to support new exception messages.
Files not reviewed (1)
  • src/libraries/System.Numerics.Tensors/src/Resources/Strings.resx: Language not supported

Comment on lines +442 to 443
public ReadOnlyTensorSpan<T> AsReadOnlyTensorSpan(params scoped ReadOnlySpan<nint> start) => AsReadOnlyTensorSpan().Slice(start);

Copy link
Preview

Copilot AI Mar 19, 2025

Choose a reason for hiding this comment

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

This method recursively calls itself instead of invoking AsTensorSpan() to produce a valid ReadOnlyTensorSpan, which can lead to a stack overflow. It should likely call AsTensorSpan().Slice(start) instead.

Suggested change
public ReadOnlyTensorSpan<T> AsReadOnlyTensorSpan(params scoped ReadOnlySpan<nint> start) => AsReadOnlyTensorSpan().Slice(start);
public ReadOnlyTensorSpan<T> AsReadOnlyTensorSpan(params scoped ReadOnlySpan<nint> start) => AsTensorSpan().Slice(start);

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[API Proposal]: Tensor Operations Per Dimension
1 participant