Notiz
Zougrëff op dës Säit erfuerdert Autorisatioun. Dir kënnt probéieren, Iech unzemellen oder Verzeechnesser ze änneren.
Zougrëff op dës Säit erfuerdert Autorisatioun. Dir kënnt probéieren, Verzeechnesser ze änneren.
Starting in .NET 11, multidimensional Tensor copying and destination-taking operations reject unsupported overlapping storage layouts with ArgumentException before they write output. Tensor-span constructors also validate the element type of System.Array storage.
The change is delivered in the System.Numerics.Tensors package, including its supported target frameworks. Updating the package can affect apps that target earlier .NET versions. The separate TensorPrimitives APIs retain their existing overlap contract.
Version introduced
.NET 11
Previous behavior
CopyTo, FlattenTo, TryCopyTo, and TryFlattenTo documented support for overlapping source and destination storage. The Try methods described handling the source as if it had first been copied to temporary storage.
For example, the following source and destination refer to overlapping regions of the same array:
int[] storage = { 1, 2, 3, 4, 5, 6 };
ReadOnlyTensorSpan<int> source = new(storage, new nint[] { 2, 2 }, new nint[] { 3, 1 });
TensorSpan<int> destination = new(storage, new nint[] { 2, 2 });
source.CopyTo(destination);
The documented snapshot-style contract implied that the destination would receive [1, 2, 4, 5]. Actual results for unsupported overlap could depend on layout and traversal order; the implementation didn't reliably meet that contract.
Constructors that took System.Array didn't consistently validate whether the array's element type was compatible with T.
New behavior
The example throws ArgumentException before copying. The same unsupported overlap also throws from TryCopyTo and TryFlattenTo when their shape and capacity checks succeed.
Supported overlap remains operation-specific:
| Operation | Supported overlap |
|---|---|
CopyTo and TryCopyTo |
Dense source and destination with equal element counts, or exactly identical views. |
FlattenTo and TryFlattenTo |
A dense source and the destination span. |
| Destination-taking elementwise operations | Identical input/output views without repeated logical storage locations. Shifted overlap and overlapping broadcast inputs are rejected. |
Tensor.ResizeTo |
Dense source and destination. Unsupported strided overlap is rejected. |
| Reversal into the same view | Dense in-place reversal, or a no-op reversal. |
The Try copying methods still return false, without copying, when the destination is too short. TryCopyTo also returns false for incompatible shapes. Unsupported overlap is an invalid layout, not a capacity failure.
Tensor.ResizeTo and concatenation into an existing destination reject a zero-stride dimension that contains more than one logical element. Empty destinations and zero strides in singleton dimensions remain permitted.
TensorSpan<T> and ReadOnlyTensorSpan<T> constructors that take System.Array throw ArrayTypeMismatchException for incompatible array element types. Writable tensor spans also reject covariant arrays; read-only tensor spans permit compatible reference-type covariance.
Type of breaking change
This is a behavioral change.
Reason for change
Correct copies between arbitrarily overlapping strided tensors can require temporary storage proportional to the entire tensor. Explicit rejection avoids traversal-dependent corruption without silently allocating full-tensor snapshots.
Repeated output storage locations can't represent distinct logical results. Array element-type validation prevents incompatible storage from being interpreted as T, and prevents writes through covariant array storage.
Recommended action
Use nonoverlapping output storage, or explicitly snapshot the source before an overlapping copy. For the preceding example:
int[] snapshot = new int[checked((int)source.FlattenedLength)];
source.FlattenTo(snapshot);
ReadOnlyTensorSpan<int> independentSource = new(snapshot, source.Lengths);
independentSource.CopyTo(destination);
The snapshot contains [1, 2, 4, 5]. For destination-taking elementwise operations, snapshot each overlapping input or allocate a separate result. Replace broadcast destinations with independently addressable output storage.
For a tensor span over System.Array, supply compatible element storage. For a writable view, use an array that isn't covariant, or copy the elements to a new T[].
Don't assume that a Try copying method can't throw. A false result handles insufficient capacity or, for TryCopyTo, incompatible shapes. No compatibility switch restores arbitrary-overlap behavior.
Affected APIs
- System.Numerics.Tensors.IReadOnlyTensor<TSelf,T>.CopyTo.
- System.Numerics.Tensors.IReadOnlyTensor<TSelf,T>.FlattenTo.
- System.Numerics.Tensors.IReadOnlyTensor<TSelf,T>.TryCopyTo.
- System.Numerics.Tensors.IReadOnlyTensor<TSelf,T>.TryFlattenTo.
- The corresponding copy and flatten methods on Tensor<T>, TensorSpan<T>, and ReadOnlyTensorSpan<T>.
- Destination-taking elementwise methods on Tensor, including unary, binary, and tensor/scalar operations.
- System.Numerics.Tensors.Tensor.ResizeTo (all overloads).
- System.Numerics.Tensors.Tensor.Reverse and System.Numerics.Tensors.Tensor.ReverseDimension (overloads that take a destination).
- System.Numerics.Tensors.Tensor.Concatenate (overload that takes a destination).
- TensorSpan<T> and ReadOnlyTensorSpan<T> (the two overloads on each type that take
System.Array).