Fix non-dense TensorSpan handling in SequenceEqual, Fill*, Resize*, IndexOf*, and ConcatenateOnDimension#124225
Fix non-dense TensorSpan handling in SequenceEqual, Fill*, Resize*, IndexOf*, and ConcatenateOnDimension#124225
Conversation
…or spans Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
|
Tagging subscribers to this area: @dotnet/area-system-numerics-tensors |
There was a problem hiding this comment.
Pull request overview
Fixes Tensor.SequenceEqual / TensorSpan.SequenceEqual so that comparisons involving non-dense tensor spans compare only the logical elements (ignoring unobserved gap/padding in the underlying buffer), while preserving the dense fast-path.
Changes:
- Updated
ReadOnlyTensorSpan<T>.SequenceEqualto use a dense fast-path and a non-dense element-by-element slow-path. - Simplified the
TensorSpan<T>overload to delegate to theReadOnlyTensorSpan<T>implementation. - Added unit tests covering non-dense vs non-dense, dense vs non-dense (both directions), differing logical elements, and the
TensorSpan<T>overload.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.cs | Fixes SequenceEqual semantics for non-dense spans and centralizes logic via the ReadOnlyTensorSpan<T> overload. |
| src/libraries/System.Numerics.Tensors/tests/TensorTests.cs | Adds regression tests validating correct SequenceEqual behavior across dense/non-dense scenarios. |
src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.cs
Show resolved
Hide resolved
src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.cs
Outdated
Show resolved
Hide resolved
|
@copilot do other methods have the same or similar problem? e.g. IndexOf*, ResizeTo, Fill*, etc.? Please investigate and fix any other similar problems in the whole library. |
…dd tests Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Investigated all methods in the library that use
Methods using |
src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.cs
Show resolved
Hide resolved
|
@copilot invoke the code-review skill and post your analysis/results as a comment on this PR |
🤖 Copilot Code Review — PR #123681Holistic AssessmentMotivation: The PR fixes a real correctness bug. Multiple Approach: The Summary: Detailed Findings
|
This is still an issue. |
|
@copilot please address the review feedback in #124225 (comment) |
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
|
@copilot try again |
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Implemented the follow-up fixes and retried with fresh validation in ec7f3ab. I added a new regression test for |
Description
This PR fixes incorrect behavior for non-dense (strided) tensor spans where methods were operating over raw underlying storage rather than logical tensor elements.
In addition to the original
SequenceEqualfix, this now includes follow-up correctness and consistency updates requested in review feedback.Changes Made
SequenceEqualfor non-dense spans to compare logical elements only.FillGaussianNormalDistributionFillUniformDistributionResizeResizeToIndexOfMaxIndexOfMinIndexOfMaxMagnitudeIndexOfMinMagnitudeFlattenedLengthinstead of_shape.LinearLength.SequenceEqualto align withIEquatable<T>.Equalssemantics used by span-based comparisons.IndexOf*Fallbackimplementations to useArrayPool<T>.Sharedinstead of allocating new arrays per call.ComputeLogicalOffsets.ConcatenateOnDimensionbehavior for non-dense destination spans by handling destination layout correctly.TensorConcatenateNonDenseDestinationTestsTesting
./build.sh clr+libs -rc releasedotnet build /t:test src/libraries/System.Numerics.Tensors/tests/System.Numerics.Tensors.Tests.csprojSystem.Numerics.Tensors.Teststotal: 5401Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.