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

[release/9.0-staging] fix stack 2x2 tensor along dimension 1 #110053

Open
wants to merge 1 commit into
base: release/9.0-staging
Choose a base branch
from
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 @@ -3456,7 +3456,7 @@ public static Tensor<T> StackAlongDimension<T>(int dimension, params ReadOnlySpa
Tensor<T>[] outputs = new Tensor<T>[tensors.Length];
for (int i = 0; i < tensors.Length; i++)
{
outputs[i] = Tensor.Unsqueeze(tensors[0], dimension);
outputs[i] = Tensor.Unsqueeze(tensors[i], dimension);
}
return Tensor.ConcatenateOnDimension<T>(dimension, outputs);
}
Expand Down Expand Up @@ -3494,7 +3494,7 @@ public static ref readonly TensorSpan<T> StackAlongDimension<T>(scoped ReadOnlyS
Tensor<T>[] outputs = new Tensor<T>[tensors.Length];
for (int i = 0; i < tensors.Length; i++)
{
outputs[i] = Tensor.Unsqueeze(tensors[0], dimension);
outputs[i] = Tensor.Unsqueeze(tensors[i], dimension);
}
return ref Tensor.ConcatenateOnDimension<T>(dimension, tensors, destination);
}
Expand Down
21 changes: 21 additions & 0 deletions src/libraries/System.Numerics.Tensors/tests/TensorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1074,6 +1074,27 @@ public static void TensorStackTests()
Assert.Equal(8, resultTensor[1, 3, 1]);
Assert.Equal(9, resultTensor[1, 4, 0]);
Assert.Equal(9, resultTensor[1, 4, 1]);

// stacking 2x2 tensors along dimention 1
Tensor<int> v1 = Tensor.Create([1, 2, 3, 4], [2, 2]);
Tensor<int> v2 = Tensor.Create([10, 20, 30, 40], [2, 2]);

resultTensor = Tensor.StackAlongDimension(1, [v1, v2]);

Assert.Equal(3, resultTensor.Rank);
Assert.Equal(2, resultTensor.Lengths[0]);
Assert.Equal(2, resultTensor.Lengths[1]);
Assert.Equal(2, resultTensor.Lengths[2]);

Assert.Equal(1, resultTensor[0, 0, 0]);
Assert.Equal(2, resultTensor[0, 0, 1]);
Assert.Equal(10, resultTensor[0, 1, 0]);
Assert.Equal(20, resultTensor[0, 1, 1]);

Assert.Equal(3, resultTensor[1, 0, 0]);
Assert.Equal(4, resultTensor[1, 0, 1]);
Assert.Equal(30, resultTensor[1, 1, 0]);
Assert.Equal(40, resultTensor[1, 1, 1]);
}

[Fact]
Expand Down
Loading