Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1637,32 +1637,35 @@ public static Tensor<T> Resize<T>(Tensor<T> tensor, ReadOnlySpan<nint> lengths)

/// <summary>
/// Copies the data from <paramref name="tensor"/>. If the final shape is smaller all data after that point is ignored.
/// If the final shape is bigger it is filled with 0s.
/// If the final shape is bigger it is filled with 0s (<code>default(T)</code>).
/// </summary>
/// <param name="tensor">Input <see cref="Tensor{T}"/>.</param>
/// <param name="destination">Destination <see cref="TensorSpan{T}"/> with the desired new shape.</param>
/// <remarks>This method does works for incompatible shapes as both source and destination are reinterpreted as flattened.</remarks>
public static void ResizeTo<T>(scoped in Tensor<T> tensor, in TensorSpan<T> destination)
{
ResizeTo(tensor.AsReadOnlyTensorSpan(), destination);
}

/// <summary>
/// Copies the data from <paramref name="tensor"/>. If the final shape is smaller all data after that point is ignored.
/// If the final shape is bigger it is filled with 0s.
/// If the final shape is bigger it is filled with 0s (<code>default(T)</code>).
/// </summary>
/// <param name="tensor">Input <see cref="TensorSpan{T}"/>.</param>
/// <param name="destination">Destination <see cref="TensorSpan{T}"/> with the desired new shape.</param>
/// <remarks>This method does works for incompatible shapes as both source and destination are reinterpreted as flattened.</remarks>
public static void ResizeTo<T>(scoped in TensorSpan<T> tensor, in TensorSpan<T> destination)
{
ResizeTo(tensor.AsReadOnlyTensorSpan(), destination);
}

/// <summary>
/// Copies the data from <paramref name="tensor"/>. If the final shape is smaller all data after that point is ignored.
/// If the final shape is bigger it is filled with 0s.
/// If the final shape is bigger it is filled with 0s (<code>default(T)</code>).
/// </summary>
/// <param name="tensor">Input <see cref="ReadOnlyTensorSpan{T}"/>.</param>
/// <param name="destination">Destination <see cref="TensorSpan{T}"/> with the desired new shape.</param>
/// <remarks>This method does works for incompatible shapes as both source and destination are reinterpreted as flattened.</remarks>
public static void ResizeTo<T>(scoped in ReadOnlyTensorSpan<T> tensor, in TensorSpan<T> destination)
{
if (tensor.IsDense && destination.IsDense)
Expand All @@ -1672,6 +1675,7 @@ public static void ResizeTo<T>(scoped in ReadOnlyTensorSpan<T> tensor, in Tensor
if (ospan.Length >= span.Length)
{
span.CopyTo(ospan);
ospan.Slice(span.Length).Clear();
}
else
{
Expand All @@ -1691,6 +1695,19 @@ public static void ResizeTo<T>(scoped in ReadOnlyTensorSpan<T> tensor, in Tensor
Debug.Assert(srcMoved && dstMoved);
dstEnumerator.Current = srcEnumerator.Current;
}

if (destination.IsDense)
{
Span<T> ospan = MemoryMarshal.CreateSpan(ref destination._reference, (int)destination.FlattenedLength);
ospan.Slice((int)copyLength).Clear();
}
else
{
while (dstEnumerator.MoveNext())
{
dstEnumerator.Current = default!;
}
}
}
}
#endregion
Expand Down
111 changes: 105 additions & 6 deletions src/libraries/System.Numerics.Tensors/tests/TensorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1081,14 +1081,15 @@ public static void TensorIndexOfMinMagnitudeNonDenseTests(int[] data, nint[] sha

[Theory]
[MemberData(nameof(NonDenseTensorData))]
public static void TensorResizeToNonDenseSourceTests(int[] data, nint[] shape, nint[] strides, int[] expectedLogical)
public static void TensorResizeToBigNonDenseSourceTests(int[] data, nint[] shape, nint[] strides, int[] expectedLogical)
{
var src = new ReadOnlyTensorSpan<int>(data, shape, strides);
Assert.False(src.IsDense);

// Resize to a larger dense destination
nint srcFlatLen = src.FlattenedLength;
int[] dstData = new int[(int)srcFlatLen + 2];
Array.Fill(dstData, -1);
var dst = new TensorSpan<int>(dstData, [(nint)dstData.Length], [1]);
Assert.True(dst.IsDense);

Expand All @@ -1107,32 +1108,130 @@ public static void TensorResizeToNonDenseSourceTests(int[] data, nint[] shape, n

[Theory]
[MemberData(nameof(NonDenseTensorData))]
public static void TensorResizeToNonDenseDestinationTests(int[] data, nint[] shape, nint[] strides, int[] expectedLogical)
public static void TensorResizeToSmallNonDenseSourceTests(int[] data, nint[] shape, nint[] strides, int[] expectedLogical)
{
var src = new ReadOnlyTensorSpan<int>(data, shape, strides);
Assert.False(src.IsDense);

// Resize to a smaller dense destination
nint srcFlatLen = src.FlattenedLength;
int[] dstData = new int[(int)srcFlatLen - 1];
Array.Fill(dstData, -1);
var dst = new TensorSpan<int>(dstData);
Assert.True(dst.IsDense);

Tensor.ResizeTo(src, dst);

for (int i = 0; i < dstData.Length; i++)
{
Assert.Equal(expectedLogical[i], dstData[i]);
}
}

[Theory]
[MemberData(nameof(NonDenseTensorData))]
public static void TensorResizeToBigNonDenseDestinationTests(int[] data, nint[] shape, nint[] strides, int[] expectedLogical)
{
// Create a dense source with known values
var src = new ReadOnlyTensorSpan<int>(expectedLogical, [(nint)expectedLogical.Length], [1]);
var srcData = expectedLogical.AsSpan().Slice(0, expectedLogical.Length - 1);
var src = new ReadOnlyTensorSpan<int>(srcData);
Assert.True(src.IsDense);

// Create a non-dense destination
int[] dstData = new int[data.Length];
Array.Fill(dstData, -1);
var dst = new TensorSpan<int>(dstData, shape, strides);
Assert.False(dst.IsDense);

nint copyLength = Math.Min(src.FlattenedLength, dst.FlattenedLength);
Tensor.ResizeTo(src, dst);

// Verify logical elements were written correctly
int logicalIdx = 0;
foreach (int val in dst)
{
if (logicalIdx < expectedLogical.Length)
if (logicalIdx < srcData.Length)
{
Assert.Equal(expectedLogical[logicalIdx], val);
Assert.Equal(srcData[logicalIdx], val);
}
else
{
Assert.Equal(0, val);
}
logicalIdx++;
}
}

[Theory]
[MemberData(nameof(NonDenseTensorData))]
public static void TensorResizeToSmallNonDenseDestinationTests(int[] data, nint[] shape, nint[] strides, int[] expectedLogical)
{
// Create a dense source with known values
var srcData = new int[expectedLogical.Length + 1];
expectedLogical.CopyTo(srcData);
srcData[^1] = -2;
var src = new ReadOnlyTensorSpan<int>(srcData, [(nint)expectedLogical.Length + 1], [1]);
Assert.True(src.IsDense);

// Create a non-dense destination
int[] dstData = new int[data.Length];
Array.Fill(dstData, -1);
var dst = new TensorSpan<int>(dstData, shape, strides);
Assert.False(dst.IsDense);

Tensor.ResizeTo(src, dst);

// Verify logical elements were written correctly
int logicalIdx = 0;
foreach (int val in dst)
{
Assert.Equal(srcData[logicalIdx], val);
logicalIdx++;
}
}

[Fact]
public static void TensorResizeToDenseSmallDestinationTests()
{
// Create a dense source with known values
var srcData = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
var src = new ReadOnlyTensorSpan<int>(srcData, [9]);
Assert.True(src.IsDense);

var dstData = new int[4];
var dst = new TensorSpan<int>(dstData, [2, 2]);
Assert.True(src.IsDense);

Tensor.ResizeTo(src, dst);

Assert.Equal(1, dstData[0]);
Assert.Equal(2, dstData[1]);
Assert.Equal(3, dstData[2]);
Assert.Equal(4, dstData[3]);
}

[Fact]
public static void TensorResizeToDenseBigDestinationTests()
{
// Create a dense source with known values
var srcData = new int[] { 1, 2, 3, 4 };
var src = new ReadOnlyTensorSpan<int>(srcData, [4]);
Assert.True(src.IsDense);

var dstData = new int[6];
Array.Fill(dstData, -1);
var dst = new TensorSpan<int>(dstData, [3, 2]);
Assert.True(src.IsDense);

Tensor.ResizeTo(src, dst);

Assert.Equal(1, dstData[0]);
Assert.Equal(2, dstData[1]);
Assert.Equal(3, dstData[2]);
Assert.Equal(4, dstData[3]);
Assert.Equal(0, dstData[4]);
Assert.Equal(0, dstData[5]);
}

[Fact]
public static void TensorResizeNonDenseTests()
{
Expand Down
Loading