Skip to content

Fix Tensor<T> slice pinning offset - #134727

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

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.

This branch has not been deployed

No deployments
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

1 participant