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 Operation fixes when input is sliced (non-contiguous) #106985

Merged
merged 2 commits into from
Aug 29, 2024

Conversation

michaelgsharp
Copy link
Member

This pr Fixes #106280.

Basically when the input was being sliced, the Tensor operations weren't always respecting that. This caused extra data to be processed and in some cases changed the final output. Will be backported to 9.0.

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

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.

Comment on lines +100 to +113
internal static bool IsContiguousAndDense<T>(scoped in ReadOnlyTensorSpan<T> tensor)
{
// Right most dimension must be 1 for a dense tensor.
if (tensor._shape.Strides[^1] != 1)
return false;

// For other dimensions, the stride must be equal to the product of the dimensions to the right.
for (int i = tensor._shape._rank - 2; i >= 0; i--)
{
if (tensor._shape.Strides[i] != TensorPrimitives.Product(tensor.Lengths.Slice(i + 1, tensor.Lengths.Length - i - 1)))
return false;
}
return true;
}
Copy link
Member

Choose a reason for hiding this comment

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

For .NET 10 we should think about if we can use a bit or two to track that the span is contiguous/dense.

We should also ensure a tracking issue exists to ensure we can operate "row by row" and avoid allocating in the case that its not dense.

@michaelgsharp
Copy link
Member Author

/backport to release/9.0-staging

Copy link
Contributor

Started backporting to release/9.0-staging: https://github.com/dotnet/runtime/actions/runs/10601443057

Copy link
Contributor

@michaelgsharp an error occurred while backporting to release/9.0-staging, please check the run log for details!

Error: The specified backport target branch release/9.0-staging wasn't found in the repo.

@michaelgsharp
Copy link
Member Author

/backport to release/9.0

Copy link
Contributor

Started backporting to release/9.0: https://github.com/dotnet/runtime/actions/runs/10601530979

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.

Incorrect result computed for tensor slices
2 participants