Skip to content

Commit 5e0684d

Browse files
committed
Ensure the relevant interfaces are implemented on the TensorSpan and ReadOnlyTensorSpan types
1 parent da9d4b0 commit 5e0684d

File tree

13 files changed

+495
-672
lines changed

13 files changed

+495
-672
lines changed

src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.net9.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ public readonly ref partial struct ReadOnlyTensorDimensionSpan<T>
1919
void System.IDisposable.Dispose() { }
2020
}
2121
}
22+
public readonly ref partial struct ReadOnlyTensorSpan<T> : System.Numerics.Tensors.IReadOnlyTensor, System.Numerics.Tensors.IReadOnlyTensor<System.Numerics.Tensors.TensorSpan<T>, T>
23+
{
24+
object? System.Numerics.Tensors.IReadOnlyTensor.this[params scoped System.ReadOnlySpan<NIndex> indexes] { get { throw null; } }
25+
object? System.Numerics.Tensors.IReadOnlyTensor.this[params scoped System.ReadOnlySpan<nint> indexes] { get { throw null; } }
26+
System.Numerics.Tensors.ReadOnlyTensorSpan<T> System.Numerics.Tensors.IReadOnlyTensor<System.Numerics.Tensors.ReadOnlyTensorSpan<T>, T>.AsReadOnlyTensorSpan() { throw null; }
27+
System.Numerics.Tensors.ReadOnlyTensorSpan<T> System.Numerics.Tensors.IReadOnlyTensor<System.Numerics.Tensors.ReadOnlyTensorSpan<T>, T>.AsReadOnlyTensorSpan(params scoped System.ReadOnlySpan<nint> startIndexes) { throw null; }
28+
System.Numerics.Tensors.ReadOnlyTensorSpan<T> System.Numerics.Tensors.IReadOnlyTensor<System.Numerics.Tensors.ReadOnlyTensorSpan<T>, T>.AsReadOnlyTensorSpan(params scoped System.ReadOnlySpan<NIndex> startIndexes) { throw null; }
29+
System.Numerics.Tensors.ReadOnlyTensorSpan<T> System.Numerics.Tensors.IReadOnlyTensor<System.Numerics.Tensors.ReadOnlyTensorSpan<T>, T>.AsReadOnlyTensorSpan(params scoped System.ReadOnlySpan<NRange> ranges) { throw null; }
30+
System.Numerics.Tensors.ReadOnlyTensorSpan<T> System.Numerics.Tensors.IReadOnlyTensor<System.Numerics.Tensors.ReadOnlyTensorSpan<T>, T>.ToDenseTensor() { throw null; }
31+
}
2232
public readonly ref partial struct TensorDimensionSpan<T>
2333
{
2434
public ref partial struct Enumerator : System.Collections.Generic.IEnumerator<System.Numerics.Tensors.TensorSpan<T>>, System.Collections.IEnumerator, System.IDisposable
@@ -27,4 +37,19 @@ public readonly ref partial struct TensorDimensionSpan<T>
2737
void System.IDisposable.Dispose() { }
2838
}
2939
}
40+
public readonly ref partial struct TensorSpan<T> : System.Numerics.Tensors.IReadOnlyTensor, System.Numerics.Tensors.IReadOnlyTensor<System.Numerics.Tensors.TensorSpan<T>, T>, System.Numerics.Tensors.ITensor, System.Numerics.Tensors.ITensor<System.Numerics.Tensors.TensorSpan<T>, T>
41+
{
42+
bool System.Numerics.Tensors.ITensor.IsReadOnly { get { throw null; } }
43+
object? System.Numerics.Tensors.ITensor.this[params scoped System.ReadOnlySpan<System.Buffers.NIndex> indexes] { get { throw null; } set { } }
44+
object? System.Numerics.Tensors.ITensor.this[params scoped System.ReadOnlySpan<nint> indexes] { get { throw null; } set { } }
45+
void System.Numerics.Tensors.ITensor.Fill(object value) { }
46+
static System.Numerics.Tensors.TensorSpan<T> ITensor<TensorSpan<T>, T>.Create(scoped System.ReadOnlySpan<nint> lengths, bool pinned) { throw null; }
47+
static System.Numerics.Tensors.TensorSpan<T> ITensor<TensorSpan<T>, T>.Create(scoped System.ReadOnlySpan<nint> lengths, scoped System.ReadOnlySpan<nint> strides, bool pinned) { throw null; }
48+
static System.Numerics.Tensors.TensorSpan<T> ITensor<TensorSpan<T>, T>.CreateUninitialized(scoped System.ReadOnlySpan<nint> lengths, bool pinned) { throw null; }
49+
static System.Numerics.Tensors.TensorSpan<T> ITensor<TensorSpan<T>, T>.CreateUninitialized(scoped System.ReadOnlySpan<nint> lengths, scoped System.ReadOnlySpan<nint> strides, bool pinned) { throw null; }
50+
System.Numerics.Tensors.TensorSpan<T> System.Numerics.Tensors.ITensor<System.Numerics.Tensors.TensorSpan<T>, T>.AsTensorSpan() { throw null; }
51+
System.Numerics.Tensors.TensorSpan<T> System.Numerics.Tensors.ITensor<System.Numerics.Tensors.TensorSpan<T>, T>.AsTensorSpan(params scoped System.ReadOnlySpan<nint> startIndexes) { throw null; }
52+
System.Numerics.Tensors.TensorSpan<T> System.Numerics.Tensors.ITensor<System.Numerics.Tensors.TensorSpan<T>, T>.AsTensorSpan(params scoped System.ReadOnlySpan<System.Buffers.NIndex> startIndexes) { throw null; }
53+
System.Numerics.Tensors.TensorSpan<T> System.Numerics.Tensors.ITensor<System.Numerics.Tensors.TensorSpan<T>, T>.AsTensorSpan(params scoped System.ReadOnlySpan<System.Buffers.NRange> ranges) { throw null; }
54+
}
3055
}

src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,13 @@ public partial interface IReadOnlyTensor
6767
int Rank { get; }
6868
[System.Diagnostics.CodeAnalysis.UnscopedRefAttribute]
6969
System.ReadOnlySpan<nint> Strides { get; }
70-
System.Buffers.MemoryHandle GetPinnedHandle();
7170
}
72-
public partial interface IReadOnlyTensor<TSelf, T> : System.Collections.Generic.IEnumerable<T>, System.Collections.IEnumerable, System.Numerics.Tensors.IReadOnlyTensor where TSelf : System.Numerics.Tensors.IReadOnlyTensor<TSelf, T>
71+
public partial interface IReadOnlyTensor<TSelf, T> : System.Numerics.Tensors.IReadOnlyTensor
72+
#if NET9_0_OR_GREATER
73+
where TSelf : System.Numerics.Tensors.IReadOnlyTensor<TSelf, T>, allows ref struct
74+
#else
75+
where TSelf : System.Numerics.Tensors.IReadOnlyTensor<TSelf, T>
76+
#endif
7377
{
7478
static abstract TSelf Empty { get; }
7579
new ref readonly T this[params scoped System.ReadOnlySpan<System.Buffers.NIndex> indexes] { get; }
@@ -98,7 +102,12 @@ public partial interface ITensor : System.Numerics.Tensors.IReadOnlyTensor
98102
void Clear();
99103
void Fill(object value);
100104
}
101-
public partial interface ITensor<TSelf, T> : System.Collections.Generic.IEnumerable<T>, System.Collections.IEnumerable, System.Numerics.Tensors.IReadOnlyTensor, System.Numerics.Tensors.IReadOnlyTensor<TSelf, T>, System.Numerics.Tensors.ITensor where TSelf : System.Numerics.Tensors.ITensor<TSelf, T>
105+
public partial interface ITensor<TSelf, T> : System.Numerics.Tensors.IReadOnlyTensor, System.Numerics.Tensors.IReadOnlyTensor<TSelf, T>, System.Numerics.Tensors.ITensor
106+
#if NET9_0_OR_GREATER
107+
where TSelf : System.Numerics.Tensors.ITensor<TSelf, T>, allows ref struct
108+
#else
109+
where TSelf : System.Numerics.Tensors.ITensor<TSelf, T>
110+
#endif
102111
{
103112
new ref T this[params scoped System.ReadOnlySpan<System.Buffers.NIndex> indexes] { get; }
104113
new TSelf this[params scoped System.ReadOnlySpan<System.Buffers.NRange> ranges] { get; set; }
@@ -120,6 +129,7 @@ public readonly ref partial struct ReadOnlyTensorDimensionSpan<T>
120129
private readonly object _dummy;
121130
private readonly int _dummyPrimitive;
122131
public System.Numerics.Tensors.ReadOnlyTensorSpan<T> this[nint index] { get { throw null; } }
132+
public bool IsDense { get { throw null; } }
123133
public nint Length { get { throw null; } }
124134
public System.Numerics.Tensors.ReadOnlyTensorDimensionSpan<T>.Enumerator GetEnumerator() { throw null; }
125135
public ref partial struct Enumerator
@@ -155,6 +165,7 @@ public readonly ref partial struct ReadOnlyTensorSpan<T>
155165
public bool HasAnyDenseDimensions { get { throw null; } }
156166
public bool IsDense { get { throw null; } }
157167
public bool IsEmpty { get { throw null; } }
168+
public bool IsPinned { get { throw null; } }
158169
public ref readonly T this[params scoped System.ReadOnlySpan<System.Buffers.NIndex> indexes] { get { throw null; } }
159170
public System.Numerics.Tensors.ReadOnlyTensorSpan<T> this[params scoped System.ReadOnlySpan<System.Buffers.NRange> ranges] { get { throw null; } }
160171
public ref readonly T this[params scoped System.ReadOnlySpan<nint> indexes] { get { throw null; } }
@@ -198,7 +209,7 @@ public void FlattenTo(scoped System.Span<T> destination) { }
198209
readonly object? System.Collections.IEnumerator.Current { get { throw null; } }
199210
public bool MoveNext() { throw null; }
200211
public void Reset() { }
201-
void System.IDisposable.Dispose() { }
212+
readonly void System.IDisposable.Dispose() { }
202213
}
203214
}
204215
public static partial class Tensor
@@ -570,7 +581,9 @@ public readonly ref partial struct TensorDimensionSpan<T>
570581
private readonly object _dummy;
571582
private readonly int _dummyPrimitive;
572583
public System.Numerics.Tensors.TensorSpan<T> this[nint index] { get { throw null; } }
584+
public bool IsDense { get { throw null; } }
573585
public nint Length { get { throw null; } }
586+
public static implicit operator System.Numerics.Tensors.ReadOnlyTensorDimensionSpan<T>(scoped in System.Numerics.Tensors.TensorDimensionSpan<T> tensorDimension) { throw null; }
574587
public System.Numerics.Tensors.TensorDimensionSpan<T>.Enumerator GetEnumerator() { throw null; }
575588
public ref partial struct Enumerator
576589
{
@@ -839,6 +852,7 @@ public readonly ref partial struct TensorSpan<T>
839852
public bool HasAnyDenseDimensions { get { throw null; } }
840853
public bool IsDense { get { throw null; } }
841854
public bool IsEmpty { get { throw null; } }
855+
public bool IsPinned { get { throw null; } }
842856
public ref T this[params scoped System.ReadOnlySpan<System.Buffers.NIndex> indexes] { get { throw null; } }
843857
public System.Numerics.Tensors.TensorSpan<T> this[params scoped System.ReadOnlySpan<System.Buffers.NRange> ranges] { get { throw null; } set { } }
844858
public ref T this[params scoped System.ReadOnlySpan<nint> indexes] { get { throw null; } }
@@ -888,10 +902,10 @@ public void FlattenTo(scoped System.Span<T> destination) { }
888902
readonly object? System.Collections.IEnumerator.Current { get { throw null; } }
889903
public bool MoveNext() { throw null; }
890904
public void Reset() { }
891-
void System.IDisposable.Dispose() { }
905+
readonly void System.IDisposable.Dispose() { }
892906
}
893907
}
894-
public sealed partial class Tensor<T> : System.Collections.Generic.IEnumerable<T>, System.Collections.IEnumerable, System.Numerics.Tensors.IReadOnlyTensor, System.Numerics.Tensors.IReadOnlyTensor<System.Numerics.Tensors.Tensor<T>, T>, System.Numerics.Tensors.ITensor, System.Numerics.Tensors.ITensor<System.Numerics.Tensors.Tensor<T>, T>
908+
public sealed partial class Tensor<T> : System.Collections.IEnumerable, System.Collections.Generic.IEnumerable<T>, System.Numerics.Tensors.IReadOnlyTensor, System.Numerics.Tensors.IReadOnlyTensor<System.Numerics.Tensors.Tensor<T>, T>, System.Numerics.Tensors.ITensor, System.Numerics.Tensors.ITensor<System.Numerics.Tensors.Tensor<T>, T>
895909
{
896910
internal Tensor() { }
897911
public static System.Numerics.Tensors.Tensor<T> Empty { get { throw null; } }

src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/IReadOnlyTensor.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,5 @@ public interface IReadOnlyTensor
5656
/// <summary>Gets the stride of each dimension in the tensor.</summary>
5757
[UnscopedRef]
5858
ReadOnlySpan<nint> Strides { get; }
59-
60-
/// <summary>Pins and gets a <see cref="MemoryHandle"/> to the backing memory.</summary>
61-
/// <returns><see cref="MemoryHandle"/></returns>
62-
MemoryHandle GetPinnedHandle();
6359
}
6460
}

src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/IReadOnlyTensor_1.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Buffers;
5-
using System.Collections.Generic;
6-
using System.Diagnostics.CodeAnalysis;
75

86
namespace System.Numerics.Tensors
97
{
108
/// <summary>Represents a read-only tensor.</summary>
119
/// <typeparam name="TSelf">The type that implements this interface.</typeparam>
1210
/// <typeparam name="T">The element type.</typeparam>
13-
public interface IReadOnlyTensor<TSelf, T> : IReadOnlyTensor, IEnumerable<T>
11+
public interface IReadOnlyTensor<TSelf, T> : IReadOnlyTensor
12+
#if NET9_0_OR_GREATER
13+
where TSelf : IReadOnlyTensor<TSelf, T>, allows ref struct
14+
#else
1415
where TSelf : IReadOnlyTensor<TSelf, T>
16+
#endif
1517
{
1618
/// <summary>Gets an empty tensor.</summary>
1719
static abstract TSelf Empty { get; }

src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/ITensor_1.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Buffers;
5-
using System.Diagnostics.CodeAnalysis;
65

76
namespace System.Numerics.Tensors
87
{
98
/// <summary>Represents a tensor.</summary>
109
/// <typeparam name="TSelf">The type that implements this interface.</typeparam>
1110
/// <typeparam name="T">The element type.</typeparam>
1211
public interface ITensor<TSelf, T> : ITensor, IReadOnlyTensor<TSelf, T>
12+
#if NET9_0_OR_GREATER
13+
where TSelf : ITensor<TSelf, T>, allows ref struct
14+
#else
1315
where TSelf : ITensor<TSelf, T>
16+
#endif
1417
{
1518
// TODO: Determine if we can implement `IEqualityOperators<TSelf, T, bool>`.
1619
// It looks like C#/.NET currently hits limitations here as it believes TSelf and T could be the same type

src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/ReadOnlyTensorDimensionSpan_1.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Runtime.CompilerServices;
5+
6+
#if NET9_0_OR_GREATER
47
using System.Collections;
58
using System.Collections.Generic;
6-
using System.Runtime.CompilerServices;
7-
using System.Runtime.InteropServices;
9+
#endif
810

911
namespace System.Numerics.Tensors
1012
{
@@ -28,9 +30,12 @@ internal ReadOnlyTensorDimensionSpan(ReadOnlyTensorSpan<T> tensor, int dimension
2830
_tensor = tensor;
2931
_length = TensorPrimitives.Product(tensor.Lengths[..dimension]);
3032
_dimension = dimension;
31-
_sliceShape = TensorShape.Create((dimension != tensor.Rank) ? tensor.Lengths[dimension..] : [1], tensor.Strides[dimension..]);
33+
_sliceShape = TensorShape.Create((dimension != tensor.Rank) ? tensor.Lengths[dimension..] : [1], tensor.Strides[dimension..], tensor.IsPinned);
3234
}
3335

36+
/// <summary>Gets <c>true</c> if the slices that exist within the tracked dimension are dense; otherwise, <c>false</c>.</summary>
37+
public bool IsDense => _sliceShape.IsDense;
38+
3439
/// <summary>Gets the length of the tensor dimension span.</summary>
3540
public nint Length => _length;
3641

0 commit comments

Comments
 (0)