Skip to content

Fix duplicate _start offset in Tensor.Resize#128511

Open
prozolic wants to merge 2 commits into
dotnet:mainfrom
prozolic:tensorresize
Open

Fix duplicate _start offset in Tensor.Resize#128511
prozolic wants to merge 2 commits into
dotnet:mainfrom
prozolic:tensorresize

Conversation

@prozolic
Copy link
Copy Markdown
Contributor

@prozolic prozolic commented May 23, 2026

Closes #128512

This PR fix the offset used when creating a ReadOnlySpan<T> that starts at _start in Tensor.Resize. AsTensorSpan()._reference already includes the _start offset, so applying it again via Unsafe.Add caused the span to start at the wrong position.

Changes:

  • Remove Unsafe.Add when creating a ReadOnlySpan<T>
  • Change from tensor.AsTensorSpan to Tensor.AsReadOnlyTensorSpan to match Tensor.ResizeTo
  • Use (int)tensor.FlattenedLength instead of tensor._values.Length - tensor._start

Fix the offset used when creating a ReadOnlySpan<T> that starts
at _start in Tensor.Resize. AsTensorSpan()._reference already
includes the _start offset, so applying it again via Unsafe.Add
caused the span to start at the wrong position.
Copilot AI review requested due to automatic review settings May 23, 2026 00:32
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.

Fixes a bug in Tensor.Resize where the source span incorrectly included data before the tensor's _start offset, causing wrong values to be copied when resizing a tensor created with a non-zero start.

Changes:

  • Replaced manual _start-offset span creation with AsReadOnlyTensorSpan()._reference and used FlattenedLength as the span length.
  • Added a new test TensorResizeWithStartTests covering resize cases for a tensor created with a non-zero start.

Reviewed changes

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

File Description
src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.cs Corrects source span reference and length for dense tensor resize.
src/libraries/System.Numerics.Tensors/tests/TensorTests.cs Adds test coverage for Resize on tensors with a non-zero start.

@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label May 23, 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.

@prozolic prozolic marked this pull request as ready for review May 23, 2026 00:35
Copilot AI review requested due to automatic review settings May 23, 2026 00:35
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 2 out of 2 changed files in this pull request and generated no new comments.

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.

Tensor.Resize returns unexpected values when start is greater than 0

2 participants