Skip to content
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 @@ -853,12 +853,22 @@ public static TensorShape Create<T>(T[]? array)
if (array is not null)
{
int linearLength = array.Length;
nint stride = 1;

TensorFlags flags = TensorFlags.IsDense | TensorFlags.HasAnyDenseDimensions;

if (linearLength <= 1)
{
stride = 0;
flags |= TensorFlags.IsBroadcast;
}

return new TensorShape(
flattenedLength: linearLength,
linearLength: linearLength,
lengths: [linearLength],
strides: [1],
TensorFlags.IsDense | TensorFlags.HasAnyDenseDimensions
strides: [stride],
flags
);
}
return default;
Expand Down Expand Up @@ -908,14 +918,22 @@ public static TensorShape Create<T>(ref readonly T reference, nint linearLength,
{
if (!Unsafe.IsNullRef(in reference))
{
nint stride = 1;

TensorFlags flags = pinned ? TensorFlags.IsPinned : TensorFlags.None;
flags |= TensorFlags.IsDense | TensorFlags.HasAnyDenseDimensions;

if (linearLength <= 1)
{
stride = 0;
flags |= TensorFlags.IsBroadcast;
}

return new TensorShape(
flattenedLength: linearLength,
linearLength: linearLength,
lengths: [linearLength],
strides: [1],
strides: [stride],
flags
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ public static void ReadOnlyTensorSpanArrayConstructorTests()
Assert.Equal(1, spanInt.Rank);
Assert.Equal(0, spanInt.Lengths[0]);
Assert.Equal(0, spanInt.FlattenedLength);
Assert.Equal(1, spanInt.Strides[0]);
Assert.Equal(0, spanInt.Strides[0]);
// Make sure it still throws on index 0
Assert.Throws<IndexOutOfRangeException>(() => {
var spanInt = new ReadOnlyTensorSpan<int>(b);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ public static void TensorSpanArrayConstructorTests()
Assert.Equal(1, spanInt.Rank);
Assert.Equal(0, spanInt.Lengths[0]);
Assert.Equal(0, spanInt.FlattenedLength);
Assert.Equal(1, spanInt.Strides[0]);
Assert.Equal(0, spanInt.Strides[0]);
// Make sure it still throws on index 0
Assert.Throws<IndexOutOfRangeException>(() => {
var spanInt = new TensorSpan<int>(b);
Expand Down
71 changes: 71 additions & 0 deletions src/libraries/System.Numerics.Tensors/tests/TensorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,77 @@ public static void TensorFactoryCreateTests()
});
}

[Fact]
public static void TensorCreateSingleElementTests()
{
// Tensor.Create with a single-element array should have stride 0
Tensor<double> src = Tensor.Create([1.0]);
Assert.Equal(1, src.Rank);
Assert.Equal(1, src.Lengths[0]);
Assert.Equal(0, src.Strides[0]);
Assert.Equal(1, src.FlattenedLength);
Assert.Equal(1.0, src[0]);

// CreateFromShapeUninitialized without strides should work
Tensor<double> dst = Tensor.CreateFromShapeUninitialized<double>(src.Lengths);
Assert.Equal(1, dst.Rank);
Assert.Equal(1, dst.Lengths[0]);
Assert.Equal(0, dst.Strides[0]);
Assert.Equal(1, dst.FlattenedLength);

// CopyTo should succeed
src.CopyTo(dst);
Assert.Equal(1.0, dst[0]);

// CreateFromShapeUninitialized with explicit strides should work
dst = Tensor.CreateFromShapeUninitialized<double>(src.Lengths, src.Strides);
Assert.Equal(1, dst.Rank);
Assert.Equal(1, dst.Lengths[0]);
Assert.Equal(0, dst.Strides[0]);
Assert.Equal(1, dst.FlattenedLength);

src.CopyTo(dst);
Assert.Equal(1.0, dst[0]);

// CreateFromShape without strides should also work
dst = Tensor.CreateFromShape<double>(src.Lengths);
Assert.Equal(1, dst.Rank);
Assert.Equal(1, dst.Lengths[0]);
Assert.Equal(0, dst.Strides[0]);
Assert.Equal(1, dst.FlattenedLength);

src.CopyTo(dst);
Assert.Equal(1.0, dst[0]);

// CreateFromShape with explicit strides should also work
dst = Tensor.CreateFromShape<double>(src.Lengths, src.Strides);
Assert.Equal(1, dst.Rank);
Assert.Equal(1, dst.Lengths[0]);
Assert.Equal(0, dst.Strides[0]);
Assert.Equal(1, dst.FlattenedLength);

src.CopyTo(dst);
Assert.Equal(1.0, dst[0]);

// TensorSpan from single-element span should also have stride 0
Span<double> span = [42.0];
TensorSpan<double> tensorSpan = new TensorSpan<double>(span);
Assert.Equal(1, tensorSpan.Rank);
Assert.Equal(1, tensorSpan.Lengths[0]);
Assert.Equal(0, tensorSpan.Strides[0]);
Assert.Equal(1, tensorSpan.FlattenedLength);
Assert.Equal(42.0, tensorSpan[0]);

// ReadOnlyTensorSpan from single-element span should also have stride 0
ReadOnlySpan<double> roSpan = [42.0];
ReadOnlyTensorSpan<double> roTensorSpan = new ReadOnlyTensorSpan<double>(roSpan);
Assert.Equal(1, roTensorSpan.Rank);
Assert.Equal(1, roTensorSpan.Lengths[0]);
Assert.Equal(0, roTensorSpan.Strides[0]);
Assert.Equal(1, roTensorSpan.FlattenedLength);
Assert.Equal(42.0, roTensorSpan[0]);
}

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