-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Open
Milestone
Description
Description
Calling Reshape on a tensor fails if you use a wild card.
The issue is because you calculate the wildcard value into a temporary array: array and then use the provided lengths by accident when calling the constructor.
Reproduction Steps
var tensor = Tensor.Create<int>([1, 2, 3, 4]);
tensor.Reshape([2, -1]) // throwsExpected behavior
Not throw and use the wildcard correctly
Actual behavior
Throws negative value guard.
Regression?
This worked in previous previews of dotnet 10 packages
Known Workarounds
Not use wildcards.
Configuration
dotnet 10 preview 7 sdk
dotnet 10-rc1 package for Tensors.
Other information
This is the current implementation of Reshape:
public static Tensor<T> Reshape<T>(this Tensor<T> tensor, ReadOnlySpan<IntPtr> lengths)
{
...
IntPtr[] array = lengths.ToArray(); // new temporary
int index1 = lengths.IndexOf<IntPtr>(new IntPtr(-1));
if (index1 >= 0)
{
if (lengths.Count<IntPtr>(new IntPtr(-1)) > 1)
ThrowHelper.ThrowArgument_OnlyOneWildcard();
IntPtr flattenedLength = tensor.FlattenedLength;
for (int index2 = 0; index2 < lengths.Length; ++index2)
{
if (lengths[index2] != new IntPtr(-1))
flattenedLength /= lengths[index2];
}
**array[index1] = flattenedLength;**
}
...
return new Tensor<T>(tensor._values, tensor._start, lengths, (ReadOnlySpan<IntPtr>) strides); // unused temporary in constructor
}