Skip to content

Commit 1ee1930

Browse files
[release/11.0] Backport Tensor correctness and shape consistency fixes (#135281)
Fixes Issue <!-- Issue Number --> [#133459](#133459), [#134691](#134691), [#128555](#128555), and [#121463](#121463). main PR <!-- Link to PR if any that fixed this in the main branch. --> [#134914](#134914) and [#135060](#135060). # Description <!-- Give a brief summary of the issue and how the pull request is fixing it. --> Backport both merged Tensor fixes to `release/11.0` in their original order: - `137a6701515dd7427e78bc46bcdd520c8d6e3a76`: fix array element-type validation, slice pinning, shape/storage validation, broadcasting, overlap handling, resize zero-fill, enumeration, and numerical operations. Restore efficient dense and contiguous-slice dispatch, including the tangent range-reduction accuracy correction needed by that dispatch. - `be7b579e4230520b5c47cd956513147e13f737a6`: make shape alignment consistent across operations, treat default rank-zero empties as effective shape `[0]` without changing their stored metadata, fix all ten internal `Any` span kernels, and preserve native-width counts, overlap-safe copy ordering, and valid empty-view origins. Both cherry-picks applied without conflicts or backport-specific source changes. The combined implementation, regression tests, README, and API remarks match the merged changes; release-specific compatibility suppressions are left unchanged. No public API is added. # Customer Impact <!-- What is the impact to customers of not taking this fix? --> Without these fixes, incompatible `System.Array` storage can be interpreted as the wrong element type and read beyond its bounds. Pinning a tensor slice can pass the parent's data, rather than the slice's data, to native consumers such as inference engines. Other affected operations can produce incorrect results, mishandle empty or broadcast shapes, omit the required resize zero-fill, or corrupt overlapping output. The existing cropped/gapped `FlattenTo` performance regression and per-element overhead in dense and contiguous-slice operations also remain. Native-backed spans with more than `int.MaxValue` logical elements retain paths that prematurely narrow counts or offsets. Taking both fixes together keeps the initial correctness/dispatch changes paired with the subsequent shape-consistency and native-width corrections. # Regression <!-- Is this fixing a problem that was introduced in the most recent release, ie., fixing a regression? --> Yes, in part, but these are not all newly introduced .NET 11 regressions. The array type-safety issue and cropped `FlattenTo` regression were introduced by the .NET 10 Tensor rewrite in [#114927](#114927) and remain in `release/11.0`. Slice pinning was reproduced with the 10.0.9 package. The remaining changes address existing correctness and consistency defects rather than one recent servicing regression. # Testing <!-- What kind of testing has been done with the fix. --> Local Windows x64 validation on the actual `release/11.0` backport: - `.\build.cmd clr+libs -rc release -lc release` on the unmodified release baseline: passed, zero warnings/errors. - `.\dotnet.cmd build src\libraries\System.Numerics.Tensors\System.Numerics.Tensors.slnx -c Release /p:RuntimeConfiguration=Release` after both cherry-picks: passed, zero warnings/errors. This built the configured `net11.0`, `net10.0`, `netstandard2.0`, and `net462` implementations and both test targets. - `.\dotnet.cmd build src\libraries\System.Numerics.Tensors\tests\System.Numerics.Tensors.Tests.csproj -t:Test -c Release /p:RuntimeConfiguration=Release`: passed in the default configuration. - The same full test command with `/p:testnobuild=true --no-restore` was rerun under each environment override below: all passed. Fresh XML results were checked for the full counts, with no failures, errors, or skips. | Configuration | Environment override | net11.0 passed | net481 passed | | --- | --- | ---: | ---: | | Default intrinsics | None | 6,266 | 200 | | 256-bit vectors | `DOTNET_PreferredVectorBitWidth=256` | 6,266 | 200 | | 128-bit vectors | `DOTNET_PreferredVectorBitWidth=128` | 6,266 | 200 | | AVX2/FMA disabled | `DOTNET_EnableAVX2=0` | 6,266 | 200 | | All intrinsics disabled | `DOTNET_EnableHWIntrinsic=0` | 6,266 | 200 | Coverage includes incompatible arrays, pinning, dense/strided/broadcast layouts, rejection before writes, empty shapes/views, high ranks, native-width logical counts, forced copy/reduction chunk boundaries, NaNs/ties/signed zero, and protected-memory boundaries. Local BenchmarkDotNet ShortRun comparisons used the same built Release runtime with distinct baseline/backport Tensor assemblies on a Ryzen 9 7950X. Assembly loading and the active vector/FMA configuration were verified. The 10 default-intrinsic jobs and four non-FMA tangent jobs all produced measured results: | Scenario | Release baseline | Combined backport | | --- | ---: | ---: | | Dense float Add, 64x16 | 16.37 us | 0.114 us | | Gapped-row float Add, 64x16, strides `[32, 1]` | 16.25 us | 2.05 us | | Gapped-row float FlattenTo, same layout | 7.92 us | 0.875 us | | Float Tan, 4,096 values in `[-1, 1]`, FMA | 1.80 us | 1.72 us | | Float Tan, 4,096 values in `[-50, 50]`, FMA | 1.63 us | 1.53 us | | Float Tan, same small range, no FMA | 4.98 us | 5.08 us | | Float Tan, same wide range, no FMA | 5.60 us | 9.19 us | No managed allocation was measured in these samples. Small timing differences are not claimed as precise gains or proof of performance neutrality; the non-FMA wide-range slowdown is an accuracy tradeoff, discussed below. These checks do not replace the more extensive measurements in the main PRs. Linux/macOS, Arm64, Mono, NativeAOT, and browser/WASM were not locally validated. # Risk <!-- Please assess the risk of taking this fix. Provide details backing up your assessment. --> Low-to-moderate risk. The changes are consistent backports of two fixes already merged on main, applied without conflicts or release-specific source adaptations. The extensive regression tests passed in all five local configurations, and the scope is confined to `System.Numerics.Tensors`. Remaining risk is in the intentional behavior changes and edge cases described below, rather than the patch size. Intentional behavior changes include accepting/rejecting shape combinations consistently, interpreting default empties as effective shape `[0]`, rejecting incompatible array element types and unsupported overlapping layouts before writes, and zero-filling newly exposed resized elements. Consumers relying on the previous inconsistent behavior can be affected. There is no compatibility switch. On hardware without FMA, cancellation-sensitive tangent vectors now use scalar evaluation to avoid inaccurate results. The wide-range local sample increased from 5.60 to 9.19 us, approximately 64%; this is an acknowledged throughput cost for correctness, not a performance-neutral change. FMA-capable hardware retains fused vector range reduction. Breaking-change documentation is already tracked by [dotnet/docs#56290](dotnet/docs#56290) and [dotnet/docs#56305](dotnet/docs#56305). Both currently name .NET 12 Preview 1; their shipment-version metadata needs to be aligned with the approved .NET 11 delivery if this backport is accepted. # Package authoring no longer needed in .NET 9 IMPORTANT: Starting with .NET 9, you no longer need to edit a NuGet package's csproj to enable building and bump the version. Keep in mind that we still need package authoring in .NET 8 and older versions. > [!NOTE] > This pull request description was generated with assistance from GitHub Copilot. --------- Co-authored-by: Copilot App <[email protected]>
1 parent ffee866 commit 1ee1930

22 files changed

Lines changed: 5124 additions & 811 deletions
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,65 @@
11
# System.Numerics.Tensors
22

33
Provides APIs for performing primitive operations over tensors represented by spans of memory.
4+
5+
Some shape and storage behavior intentionally differs from NumPy:
6+
7+
- Creating a tensor from an empty shape (`[]`) produces shape `[0]`, with no elements. In NumPy,
8+
shape `()` has rank zero but contains one scalar element; `np.empty(())` leaves that element
9+
uninitialized ("empty" refers to initialization, not the number of elements).
10+
- `Tensor<T>.Empty` and default tensor spans retain rank-zero metadata but contain no
11+
elements, unlike NumPy's scalar shape `()`. Tensor computations treat them as empty
12+
vectors with effective shape `[0]`, corresponding to NumPy's `(0,)`. This includes
13+
equality, broadcasting, reshaping, stacking, concatenation, splitting, slicing, and
14+
dimension operations. Explicitly ranked empty shapes retain their axes.
15+
- Squeezing shape `[1, 1]` produces shape `[1]`, rather than NumPy's rank-zero shape `()`.
16+
Both contain one element and can broadcast as a scalar; code that inspects rank, selects
17+
axes, or indexes the result must account for the retained dimension.
18+
- Directional broadcasting can discard excess leading singleton dimensions. For example,
19+
a source with shape `[1, 1, 3]` can broadcast into destination shape `[3]`, whereas
20+
NumPy's `broadcast_to` rejects a target with fewer dimensions than the source.
21+
This also applies to tensor copies and elementwise operations with supplied destinations.
22+
Only leading singleton dimensions are redundant: `[2, 1]` and `[1, 2]` are distinct,
23+
and zero-length dimensions cannot be discarded. Source metadata and explicitly requested
24+
destination shapes are preserved.
25+
- Shape equality also ignores leading singleton padding, without broadcasting other
26+
dimensions. Stacking requires equivalent input shapes; concatenation requires matching
27+
aligned dimensions except along its selected axis. These operations use the first
28+
input's effective shape to interpret the axis and determine the result's rank. Other
29+
inputs and supplied destinations can add or omit leading singleton padding, but cannot
30+
omit significant axes. For example, stacking `[2]` with `[1, 2]` at axis `0` produces
31+
`[2, 2]`, while stacking `[1, 2]` with `[2]` at axis `0` produces `[2, 1, 2]`: inserting
32+
the new axis makes the first input's retained singleton axis non-leading. Existing
33+
axes are not renumbered or removed from the first input.
34+
- Strides must be nonnegative. NumPy can represent a reversed view with a negative stride
35+
(for example, `array[::-1]`); use `Tensor.ReverseDimension` with dimension `0` to
36+
reverse the first axis instead. This produces a new tensor, not a reversed view.
37+
- When growing a tensor, `Tensor.Resize` and `Tensor.ResizeTo` fill new elements with
38+
`default(T)`. Resizing `[1, 2]` to five `int` elements yields `[1, 2, 0, 0, 0]`,
39+
whereas `np.resize` repeats the input and yields `[1, 2, 1, 2, 1]`.
40+
- `Tensor.ResizeTo` and concatenation into an existing destination reject a zero-stride
41+
dimension with more than one logical element: multiple output indexes would refer to the
42+
same storage and could not hold distinct values. Zero strides in singleton dimensions
43+
and empty destinations do not have this conflict.
44+
45+
When strides are omitted, a shape containing a zero-length dimension has zero strides
46+
in every dimension. Its element count and storage requirement are zero regardless of
47+
the other dimension lengths. Negative lengths remain invalid, and explicitly supplied
48+
strides must still satisfy the normal layout validation.
49+
Empty slices and empty dimension views retain their source's storage origin: endpoint
50+
ranges do not move an empty view beyond its backing storage.
51+
52+
Native-backed tensor spans can have more than `int.MaxValue` logical elements. Operations
53+
retain native-sized lengths and offsets, using bounded spans or indexed iteration rather
54+
than narrowing the total element count. Dense copies preserve overlap-safe ordering.
55+
Index-of-min/max reductions use `TensorPrimitives` over dense spans and dense suffixes.
56+
Other layouts retain the flattened primitive path for span-sized inputs and use bounded
57+
gathered blocks for native-width inputs. Chunk results retain logical native-sized indexes,
58+
including first-NaN and tie handling.
59+
60+
Overlapping sources and destinations are supported for equal-length dense copies, which
61+
use the same overlap-safe behavior as `Span<T>.CopyTo`, and for elementwise operations on identical
62+
non-broadcast views. An in-place reversal of a dense tensor also needs no temporary
63+
storage. Other overlapping tensor layouts throw `ArgumentException` before writing:
64+
copying them correctly could require buffering an amount of data proportional to the
65+
tensor's size. Nonoverlapping strided copies do not create such a buffer.

‎src/libraries/System.Numerics.Tensors/src/Resources/Strings.resx‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@
129129
<data name="Argument_InputAndDestinationSpanMustNotOverlap" xml:space="preserve">
130130
<value>The destination span may only overlap with an input span if the two spans start at the same memory location.</value>
131131
</data>
132+
<data name="Argument_OverlappingTensorLayoutsNotSupported" xml:space="preserve">
133+
<value>The input and destination tensor spans overlap in an unsupported layout.</value>
134+
</data>
132135
<data name="Argument_DestinationSpansMustNotOverlap" xml:space="preserve">
133136
<value>Destination spans must not overlap with each other.</value>
134137
</data>
@@ -138,6 +141,9 @@
138141
<data name="Argument_InvalidEnumValue" xml:space="preserve">
139142
<value>The value '{0}' is not valid for this usage of the type {1}.</value>
140143
</data>
144+
<data name="InvalidOperation_EnumerationNotPositioned" xml:space="preserve">
145+
<value>Enumeration has not started or has already finished.</value>
146+
</data>
141147
<data name="Argument_TypeContainsReferences" xml:space="preserve">
142148
<value>The type '{0}' is not supported because it contains references.</value>
143149
</data>
@@ -168,6 +174,9 @@
168174
<data name="ThrowArgument_DimensionsNotSame" xml:space="preserve">
169175
<value>Number of dimensions to slice does not equal the number of dimensions in the span</value>
170176
</data>
177+
<data name="ThrowArgument_DestinationHasOverlappingElements" xml:space="preserve">
178+
<value>The destination contains overlapping elements and cannot represent distinct output values.</value>
179+
</data>
171180
<data name="ThrowArgument_FilterTensorMustEqualTensorLength" xml:space="preserve">
172181
<value>The total length of the filter tensor must equal the length of the tensor to be filtered.</value>
173182
</data>

‎src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Common/TensorPrimitives.IIndexOfMinMaxOperator.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace System.Numerics.Tensors
99
{
1010
public static unsafe partial class TensorPrimitives
1111
{
12-
private interface IIndexOfMinMaxOperator<T>
12+
internal interface IIndexOfMinMaxOperator<T>
1313
{
1414
static abstract T Aggregate(Vector128<T> value);
1515
static abstract T Aggregate(Vector256<T> value);
@@ -20,7 +20,7 @@ private interface IIndexOfMinMaxOperator<T>
2020
static abstract Vector512<T> Compare(Vector512<T> x, Vector512<T> y);
2121
}
2222

23-
private static int IndexOfMinMaxCore<T, TOperator>(ReadOnlySpan<T> x)
23+
internal static int IndexOfMinMaxCore<T, TOperator>(ReadOnlySpan<T> x)
2424
where T : INumber<T> where TOperator : struct, IIndexOfMinMaxOperator<T>
2525
{
2626
if (x.IsEmpty)

‎src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/IReadOnlyTensor_1.cs‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ public interface IReadOnlyTensor<TSelf, T> : IReadOnlyTensor
4949

5050
/// <summary>Copies the contents of the tensor into a destination tensor span.</summary>
5151
/// <param name="destination">The destination tensor span.</param>
52-
/// <exception cref="ArgumentException"><paramref name="destination" /> is shorter than the source tensor.</exception>
53-
/// <remarks>This method copies all of the source tensor to <paramref name="destination" /> even if they overlap.</remarks>
52+
/// <exception cref="ArgumentException"><paramref name="destination" /> is shorter than the source tensor, or the source and destination overlap in an unsupported layout.</exception>
53+
/// <remarks>Overlapping dense tensors with equal element counts and identical views are supported. Other overlapping layouts are rejected before copying.</remarks>
5454
void CopyTo(scoped in TensorSpan<T> destination);
5555

5656
/// <summary>Flattens the contents of the tensor into a destination span.</summary>
5757
/// <param name="destination">The destination span.</param>
58-
/// <exception cref="ArgumentException"><paramref name="destination" /> is shorter than the source tensor.</exception>
59-
/// <remarks>This method copies all of the source tensor to <paramref name="destination" /> even if they overlap.</remarks>
58+
/// <exception cref="ArgumentException"><paramref name="destination" /> is shorter than the source tensor, or the source and destination overlap in an unsupported layout.</exception>
59+
/// <remarks>Overlapping dense tensors are supported. Other overlapping layouts are rejected before copying.</remarks>
6060
void FlattenTo(scoped Span<T> destination);
6161

6262
/// <summary>Returns a span that can be used to access the flattened elements for a given dimension.</summary>
@@ -110,17 +110,19 @@ public interface IReadOnlyTensor<TSelf, T> : IReadOnlyTensor
110110
/// <summary>Attempts to copy the contents of this tensor into a destination tensor span and returns a value to indicate whether or not the operation succeeded.</summary>
111111
/// <param name="destination">The target of the copy operation.</param>
112112
/// <returns><see langword="true"/> if the copy operation succeeded; otherwise, <c>false</c>.</returns>
113+
/// <exception cref="ArgumentException">The source and <paramref name="destination" /> overlap in an unsupported layout.</exception>
113114
/// <remarks>
114-
/// <para>If the source and <paramref name="destination" /> overlap, the entirety of the source is handled as if it was copied to a temporary location before it is copied to <paramref name="destination" />.</para>
115+
/// <para>Overlapping dense tensors with equal element counts and identical views are supported. Other overlapping layouts throw <see cref="ArgumentException" /> before copying.</para>
115116
/// <para>If the <paramref name="destination" /> length is shorter than the source, no items are copied and the method returns <c>false</c>.</para>
116117
/// </remarks>
117118
bool TryCopyTo(scoped in TensorSpan<T> destination);
118119

119120
/// <summary>Attempts to flatten the contents of this tensor into a destination span and returns a value to indicate whether or not the operation succeeded.</summary>
120121
/// <param name="destination">The target of the copy operation.</param>
121122
/// <returns><see langword="true"/> if the copy operation succeeded; otherwise, <c>false</c>.</returns>
123+
/// <exception cref="ArgumentException">The source and <paramref name="destination" /> overlap in an unsupported layout.</exception>
122124
/// <remarks>
123-
/// <para>If the source and <paramref name="destination" /> overlap, the entirety of the source is handled as if it was flattened to a temporary location before it is copied to <paramref name="destination" />.</para>
125+
/// <para>Overlapping dense tensors are supported. Other overlapping layouts throw <see cref="ArgumentException" /> before copying.</para>
124126
/// <para>If the <paramref name="destination" /> length is shorter than the source, no items are copied and the method returns <c>false</c>.</para>
125127
/// </remarks>
126128
bool TryFlattenTo(scoped Span<T> destination);

‎src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/ReadOnlyTensorDimensionSpan_1.cs‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,24 @@ public readonly ref struct ReadOnlyTensorDimensionSpan<T>
1818

1919
internal ReadOnlyTensorDimensionSpan(ReadOnlyTensorSpan<T> tensor, int dimension)
2020
{
21-
if ((uint)dimension >= tensor.Rank)
21+
int rank = tensor.Rank;
22+
if (rank == 0)
23+
{
24+
tensor = new ReadOnlyTensorSpan<T>(in tensor._reference, TensorShape.Normalize(tensor._shape));
25+
rank = tensor.Rank;
26+
}
27+
28+
if ((uint)dimension >= rank)
2229
{
2330
ThrowHelper.ThrowArgumentOutOfRangeException();
2431
}
2532
dimension += 1;
2633

34+
ReadOnlySpan<nint> lengths = tensor.Lengths;
2735
_tensor = tensor;
28-
_length = TensorPrimitives.Product(tensor.Lengths[..dimension]);
36+
_length = TensorShape.GetProduct(lengths[..dimension]);
2937
_dimension = dimension;
30-
_sliceShape = TensorShape.Create((dimension != tensor.Rank) ? tensor.Lengths[dimension..] : [1], tensor.Strides[dimension..], tensor.IsPinned);
38+
_sliceShape = TensorShape.Create((dimension != rank) ? lengths[dimension..] : [1], tensor.Strides[dimension..], tensor.IsPinned);
3139
}
3240

3341
/// <summary>Gets <c>true</c> if the slices that exist within the tracked dimension are dense; otherwise, <c>false</c>.</summary>

‎src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/ReadOnlyTensorSpan_1.cs‎

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,11 @@ public ReadOnlyTensorSpan(ReadOnlySpan<T> span, scoped ReadOnlySpan<nint> length
148148
/// <para>Returns default when <paramref name="array"/> is null.</para>
149149
/// <para>The created tensor span has a single dimension that is the same length as <paramref name="array" />.</para>
150150
/// </remarks>
151+
/// <exception cref="ArrayTypeMismatchException">The type of <paramref name="array"/> is not compatible with an array of <typeparamref name="T"/>.</exception>
151152
public ReadOnlyTensorSpan(Array? array)
152153
{
154+
ThrowHelper.ThrowIfArrayTypeMismatch<T>(array, isReadOnly: true);
155+
153156
_shape = TensorShape.Create(array);
154157
_reference = ref (array is not null)
155158
? ref Unsafe.As<byte, T>(ref MemoryMarshal.GetArrayDataReference(array))
@@ -175,8 +178,11 @@ public ReadOnlyTensorSpan(Array? array)
175178
/// * <paramref name="strides" /> is not empty and contains an element that is negative.
176179
/// * <paramref name="strides" /> is not empty and contains an element that is zero in a non leading position.
177180
/// </exception>
181+
/// <exception cref="ArrayTypeMismatchException">The type of <paramref name="array"/> is not compatible with an array of <typeparamref name="T"/>.</exception>
178182
public ReadOnlyTensorSpan(Array? array, scoped ReadOnlySpan<int> start, scoped ReadOnlySpan<nint> lengths, scoped ReadOnlySpan<nint> strides)
179183
{
184+
ThrowHelper.ThrowIfArrayTypeMismatch<T>(array, isReadOnly: true);
185+
180186
_shape = TensorShape.Create(array, start, lengths, strides, out nint linearOffset);
181187
_reference = ref (array is not null)
182188
? ref Unsafe.Add(ref Unsafe.As<byte, T>(ref MemoryMarshal.GetArrayDataReference(array)), linearOffset)
@@ -237,7 +243,7 @@ public unsafe ReadOnlyTensorSpan(T* data, nint dataLength, scoped ReadOnlySpan<n
237243

238244
internal ReadOnlyTensorSpan(ref readonly T data, nint dataLength, scoped ReadOnlySpan<nint> lengths, scoped ReadOnlySpan<nint> strides, bool pinned)
239245
{
240-
_shape = TensorShape.Create(in data, dataLength, lengths, strides, pinned);
246+
_shape = TensorShape.CreateForView(dataLength, lengths, strides, pinned);
241247
_reference = ref Unsafe.AsRef(in data);
242248
}
243249

@@ -402,7 +408,7 @@ public ReadOnlySpan<T> GetSpan(scoped ReadOnlySpan<NIndex> startIndexes, int len
402408
/// <inheritdoc cref="IReadOnlyTensor{TSelf, T}.Slice(ReadOnlySpan{nint})" />
403409
public ReadOnlyTensorSpan<T> Slice(params scoped ReadOnlySpan<nint> startIndexes)
404410
{
405-
TensorShape shape = _shape.Slice<TensorShape.GetOffsetAndLengthForNInt, nint>(startIndexes, out nint linearOffset);
411+
TensorShape shape = _shape.Slice<TensorShape.GetOffsetAndLengthForSlice, nint>(startIndexes, out nint linearOffset);
406412
return new ReadOnlyTensorSpan<T>(
407413
ref Unsafe.Add(ref _reference, linearOffset),
408414
shape
@@ -447,7 +453,8 @@ ref Unsafe.Add(ref _reference, linearOffset),
447453
/// <inheritdoc cref="IReadOnlyTensor{TSelf, T}.TryCopyTo(in TensorSpan{T})" />
448454
public bool TryCopyTo(scoped in TensorSpan<T> destination)
449455
{
450-
if (TensorShape.AreCompatible(destination._shape, _shape, false))
456+
if ((_shape.FlattenedLength <= destination.FlattenedLength) &&
457+
TensorShape.AreCompatible(destination._shape, _shape, false))
451458
{
452459
TensorOperation.Invoke<TensorOperation.CopyTo<T>, T, T>(this, destination);
453460
return true;
@@ -536,46 +543,55 @@ ReadOnlyTensorSpan<T> IReadOnlyTensor<ReadOnlyTensorSpan<T>, T>.ToDenseTensor()
536543
public ref struct Enumerator : IEnumerator<T>
537544
{
538545
private readonly ReadOnlyTensorSpan<T> _span;
539-
private readonly nint[] _indexes;
540546
private nint _linearOffset;
541547
private nint _itemsEnumerated;
548+
private bool _hasCurrent;
542549

543550
internal Enumerator(ReadOnlyTensorSpan<T> span)
544551
{
545552
_span = span;
546-
_indexes = new nint[span.Rank];
547-
548-
_indexes[^1] = -1;
549-
550-
_linearOffset = 0 - (!span.IsEmpty ? span.Strides[^1] : 0);
553+
_linearOffset = 0;
551554
_itemsEnumerated = 0;
555+
_hasCurrent = false;
552556
}
553557

554558
/// <summary>Gets the element at the current position of the enumerator.</summary>
555-
public readonly ref readonly T Current => ref Unsafe.Add(ref _span._reference, _linearOffset);
559+
public readonly ref readonly T Current
560+
{
561+
get
562+
{
563+
if (!_hasCurrent)
564+
{
565+
ThrowHelper.ThrowInvalidOperation_EnumerationNotPositioned();
566+
}
567+
return ref Unsafe.Add(ref _span._reference, _linearOffset);
568+
}
569+
}
556570

557571
/// <summary>Advances the enumerator to the next element of the tensor span.</summary>
558572
public bool MoveNext()
559573
{
560574
if (_itemsEnumerated == _span._shape.FlattenedLength)
561575
{
576+
_hasCurrent = false;
562577
return false;
563578
}
564579

565-
_linearOffset = _span._shape.AdjustToNextIndex(_span._shape, _linearOffset, _indexes);
580+
_linearOffset = _span.IsDense
581+
? _itemsEnumerated
582+
: _span._shape.GetLinearOffsetForDimension(_itemsEnumerated, _span.Rank);
566583

567584
_itemsEnumerated++;
585+
_hasCurrent = true;
568586
return true;
569587
}
570588

571589
/// <summary>Sets the enumerator to its initial position, which is before the first element in the tensor span.</summary>
572590
public void Reset()
573591
{
574-
Array.Clear(_indexes);
575-
_indexes[^1] = -1;
576-
577-
_linearOffset = 0 - (!_span.IsEmpty ? _span.Strides[^1] : 0);
592+
_linearOffset = 0;
578593
_itemsEnumerated = 0;
594+
_hasCurrent = false;
579595
}
580596

581597
//

0 commit comments

Comments
 (0)