Skip to content

Fix Tensor<T> slice pinning offset - #134727

Closed
aw0lid wants to merge 1 commit into
dotnet:mainfrom
aw0lid:fix/tensor-slice-pinnable-reference
Closed

aw0lid wants to merge 1 commit into
dotnet:mainfrom
aw0lid:fix/tensor-slice-pinnable-reference

Conversation

@aw0lid

@aw0lid aw0lid commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

Description

Tensor<T>.GetPinnableReference() returned a reference to the first element of the backing array without accounting for the slice's starting offset. As a result, pinning a non-zero-offset slice could expose the parent array's data instead of the slice's data.

Fix

Updated GetPinnableReference() to apply the tensor slice's _start offset using Unsafe.Add:

ret = ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(_values), _start);

GetPinnedHandle() already uses GetPinnableReference(), so no changes were required there.

Tests

Added a regression test covering a non-zero-offset tensor slice. The test verifies that:

  • The slice contains the expected values.
  • GetPinnableReference() points to the first element of the slice.
  • GetPinnedHandle() exposes the expected slice data.

closes #134691.

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

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

@aw0lid

aw0lid commented Oct 1, 2026

Copy link
Copy Markdown
Contributor Author

fixed in #134914

@aw0lid aw0lid closed this Oct 1, 2026
@aw0lid
aw0lid deleted the fix/tensor-slice-pinnable-reference branch October 1, 2026 18:29
@tannergooding

Copy link
Copy Markdown
Member

Sorry for both the delay and not directly looking at this one. There was a number of issues and reports and they needed to be handled end to end, so it was easier to do my own audit and fixes to get everything in "one go".

There's still a couple other minor edges I'm fixing and then separately a perf PR that will be going up.

@aw0lid

aw0lid commented Oct 1, 2026

Copy link
Copy Markdown
Contributor Author

No worries at all! Thanks for the explanation.
Makes sense to handle everything together. Looking forward to the other fixes and the perf PR.

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.

Tensor<T> slice pinning starts at the parent array instead of the slice

2 participants