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

Fixing 0 length issue with constructor #107427

Merged
Merged
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 @@ -129,7 +129,7 @@ public ReadOnlyTensorSpan(ReadOnlySpan<T> span, scoped ReadOnlySpan<nint> length
strides = strides.IsEmpty ? (ReadOnlySpan<nint>)TensorSpanHelpers.CalculateStrides(lengths, linearLength) : strides;
TensorSpanHelpers.ValidateStrides(strides, lengths);
nint maxElements = TensorSpanHelpers.ComputeMaxLinearIndex(strides, lengths);
if (maxElements >= span.Length && span.Length != 0)
if (span.IsEmpty ? maxElements != 0 : maxElements >= span.Length)
ThrowHelper.ThrowArgument_InvalidStridesAndLengths();

_shape = new TensorShape(span.Length, lengths, strides);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public TensorSpan(Span<T> span, scoped ReadOnlySpan<nint> lengths, scoped ReadOn
TensorSpanHelpers.ValidateStrides(strides, lengths);

nint maxElements = TensorSpanHelpers.ComputeMaxLinearIndex(strides, lengths);
if (maxElements >= span.Length && span.Length != 0)
if (span.IsEmpty ? maxElements != 0 : maxElements >= span.Length)
ThrowHelper.ThrowArgument_InvalidStridesAndLengths();

_shape = new TensorShape(span.Length, lengths, strides);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ public static nint ComputeLinearIndex(ReadOnlySpan<NIndex> indexes, ReadOnlySpan

public static void ValidateStrides(ReadOnlySpan<nint> strides, ReadOnlySpan<nint> lengths)
{
Debug.Assert(strides.Length == lengths.Length);
if (strides.Length != lengths.Length)
ThrowHelper.ThrowArgument_InvalidStridesAndLengths();

if (strides.Length == 0)
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,11 @@ public static void ReadOnlyTensorSpanSystemArrayConstructorTests()
[Fact]
public static void ReadOnlyTensorSpanArrayConstructorTests()
{
// Make sure exception is thrown if lengths and strides would let you go past the end of the array
Assert.Throws<ArgumentException>(() => new TensorSpan<double>(new double[0], lengths: new IntPtr[] { 2 }, strides: new IntPtr[] { 1 }));
Assert.Throws<ArgumentException>(() => new TensorSpan<double>(new double[1], lengths: new IntPtr[] { 2 }, strides: new IntPtr[] { 1 }));
Assert.Throws<ArgumentException>(() => new TensorSpan<double>(new double[2], lengths: new IntPtr[] { 2 }, strides: new IntPtr[] { 2 }));

// Make sure basic T[] constructor works
int[] a = { 91, 92, -93, 94 };
scoped ReadOnlyTensorSpan<int> spanInt = new ReadOnlyTensorSpan<int>(a);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,11 @@ public static void TensorSpanSystemArrayConstructorTests()
[Fact]
public static void TensorSpanArrayConstructorTests()
{
// Make sure exception is thrown if lengths and strides would let you go past the end of the array
Assert.Throws<ArgumentException>(() => new TensorSpan<double>(new double[0], lengths: new IntPtr[] { 2 }, strides: new IntPtr[] { 1 }));
Assert.Throws<ArgumentException>(() => new TensorSpan<double>(new double[1], lengths: new IntPtr[] { 2 }, strides: new IntPtr[] { 1 }));
Assert.Throws<ArgumentException>(() => new TensorSpan<double>(new double[2], lengths: new IntPtr[] { 2 }, strides: new IntPtr[] { 2 }));

// Make sure basic T[] constructor works
int[] a = { 91, 92, -93, 94 };
scoped TensorSpan<int> spanInt = new TensorSpan<int>(a);
Expand Down