Skip to content

Commit

Permalink
Tensor validations, scope changes, and TensorPrimitives methods (dotn…
Browse files Browse the repository at this point in the history
…et#103005)

* Additional validations and TensorPrimitives forwarding

* TensorSpan extensions

* ref updates

* more testing

* test fixes and ref fixes
  • Loading branch information
michaelgsharp committed Jun 7, 2024
1 parent 2ea80d6 commit 204f0e1
Show file tree
Hide file tree
Showing 22 changed files with 6,925 additions and 1,213 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,10 @@
<data name="ThrowArgument_StrideLessThan0" xml:space="preserve">
<value>Strides cannot be less than 0.</value>
</data>
<data name="Argument_2DTensorRequired" xml:space="preserve">
<value>Must be a 2d Tensor.</value>
</data>
<data name="Argument_IncompatibleDimensions" xml:space="preserve">
<value>Incompatible dimensions for provided tensors. left.Lengths[1] == {0} while right.Lengths[1] == {1}.</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp'">
<Compile Include="System\Numerics\Tensors\netcore\Common\IReadOnlyTensor.cs" />
<Compile Include="System\Numerics\Tensors\netcore\TensorHelpers.cs" />
<Compile Include="System\Numerics\Tensors\netcore\TensorExtensions.cs" />
<Compile Include="System\Numerics\Tensors\netcore\Tensor.Factory.cs" />
<Compile Include="System\Numerics\Tensors\netcore\Tensor.cs" />
<Compile Include="System\Numerics\Tensors\netcore\ITensor.cs" />
<Compile Include="System\Numerics\Tensors\netcore\IReadOnlyTensor.cs" />
<Compile Include="System\Numerics\Tensors\netcore\TensorSpanDebugView.cs" />
<Compile Include="System\Numerics\Tensors\netcore\TensorSpanExtensions.cs" />
<Compile Include="System\Numerics\Tensors\netcore\ReadOnlyTensorSpan.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Buffers;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace System.Numerics.Tensors
{
Expand All @@ -20,8 +16,8 @@ public interface IReadOnlyTensor<TSelf, T> : IEnumerable<T>
nint FlattenedLength { get; }
int Rank { get; }

T this[params ReadOnlySpan<nint> indexes] { get; }
T this[params ReadOnlySpan<NIndex> indexes] { get; }
T this[params scoped ReadOnlySpan<nint> indexes] { get; }
T this[params scoped ReadOnlySpan<NIndex> indexes] { get; }
TSelf this[params scoped ReadOnlySpan<NRange> ranges] { get; }

ReadOnlyTensorSpan<T> AsReadOnlyTensorSpan();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Buffers;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace System.Numerics.Tensors
{
Expand All @@ -26,8 +21,8 @@ public interface ITensor<TSelf, T>

bool IsReadOnly { get; }

new T this[params ReadOnlySpan<nint> indexes] { get; set; }
new T this[params ReadOnlySpan<NIndex> indexes] { get; set; }
new T this[params scoped ReadOnlySpan<nint> indexes] { get; set; }
new T this[params scoped ReadOnlySpan<NIndex> indexes] { get; set; }
new TSelf this[params scoped ReadOnlySpan<NRange> ranges] { get; set; }

TensorSpan<T> AsTensorSpan();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;
using System.Runtime.Versioning;
using System.Text;
using static System.Runtime.InteropServices.JavaScript.JSType;
using EditorBrowsableAttribute = System.ComponentModel.EditorBrowsableAttribute;
using EditorBrowsableState = System.ComponentModel.EditorBrowsableState;

Expand Down Expand Up @@ -43,7 +41,7 @@ public readonly ref struct ReadOnlyTensorSpan<T>
/// <param name="array">The target array.</param>
/// <remarks>Returns default when <paramref name="array"/> is null.</remarks>
/// <exception cref="ArrayTypeMismatchException">Thrown when <paramref name="array"/> is covariant and array's type is not exactly T[].</exception>
public ReadOnlyTensorSpan(T[]? array) : this(array, 0, [], [])
public ReadOnlyTensorSpan(T[]? array) : this(array, 0, [array?.Length ?? 0], [])
{
}

Expand Down Expand Up @@ -81,6 +79,9 @@ public ReadOnlyTensorSpan(T[]? array, Index startIndex, scoped ReadOnlySpan<nint
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ReadOnlyTensorSpan(T[]? array, int start, scoped ReadOnlySpan<nint> lengths, scoped ReadOnlySpan<nint> strides)
{
if (lengths.IsEmpty && array != null)
lengths = [array.Length];

nint linearLength = TensorSpanHelpers.CalculateTotalLength(lengths);
if (array == null)
{
Expand All @@ -92,18 +93,20 @@ public ReadOnlyTensorSpan(T[]? array, int start, scoped ReadOnlySpan<nint> lengt
if (!typeof(T).IsValueType && array.GetType() != typeof(T[]))
ThrowHelper.ThrowArrayTypeMismatchException();

strides = strides.IsEmpty ? (ReadOnlySpan<nint>)TensorSpanHelpers.CalculateStrides(lengths) : strides;
nint maxElements = TensorSpanHelpers.ComputeMaxElementCount(strides, lengths);
strides = strides.IsEmpty ? (ReadOnlySpan<nint>)TensorSpanHelpers.CalculateStrides(lengths, linearLength) : strides;
TensorSpanHelpers.ValidateStrides(strides, lengths);
nint maxElements = TensorSpanHelpers.ComputeMaxLinearIndex(strides, lengths);

if (Environment.Is64BitProcess)
{
// See comment in Span<T>.Slice for how this works.
if ((ulong)(uint)start + (ulong)(uint)maxElements > (ulong)(uint)array.Length)
ThrowHelper.ThrowArgumentOutOfRangeException();
if ((ulong)(uint)start + (ulong)(uint)maxElements >= (ulong)(uint)array.Length && array.Length != 0)
ThrowHelper.ThrowArgument_InvalidStridesAndLengths();
}
else
{
if ((uint)start > (uint)array.Length || (uint)maxElements > (uint)(array.Length - start))
ThrowHelper.ThrowArgumentOutOfRangeException();
if (((uint)start > (uint)array.Length || (uint)maxElements >= (uint)(array.Length - start)) && array.Length != 0)
ThrowHelper.ThrowArgument_InvalidStridesAndLengths();
}

_flattenedLength = linearLength;
Expand All @@ -115,8 +118,8 @@ public ReadOnlyTensorSpan(T[]? array, int start, scoped ReadOnlySpan<nint> lengt
}

/// <summary>
/// Creates a new <see cref="ReadOnlyTensorSpan{T}"/> over the provided <see cref="Span{T}"/>. The new <see cref="ReadOnlyTensorSpan{T}"/> will
/// have a rank of 1 and a length equal to the length of the provided <see cref="Span{T}"/>.
/// Creates a new <see cref="ReadOnlyTensorSpan{T}"/> over the provided <see cref="ReadOnlySpan{T}"/>. The new <see cref="ReadOnlyTensorSpan{T}"/> will
/// have a rank of 1 and a length equal to the length of the provided <see cref="ReadOnlySpan{T}"/>.
/// </summary>
/// <param name="span">The target span.</param>
public ReadOnlyTensorSpan(ReadOnlySpan<T> span) : this(span, [span.Length], []) { }
Expand All @@ -130,18 +133,15 @@ public ReadOnlyTensorSpan(ReadOnlySpan<T> span) : this(span, [span.Length], [])
/// <param name="strides">The strides for each dimension. Will be automatically calculated if not provided.</param>
public ReadOnlyTensorSpan(ReadOnlySpan<T> span, scoped ReadOnlySpan<nint> lengths, scoped ReadOnlySpan<nint> strides)
{
if (lengths.IsEmpty)
lengths = [span.Length];

nint linearLength = TensorSpanHelpers.CalculateTotalLength(lengths);
if (span.IsEmpty)
{
if (linearLength != 0)
ThrowHelper.ThrowArgumentOutOfRangeException();
this = default;
return; // returns default
}

strides = strides.IsEmpty ? (ReadOnlySpan<nint>)TensorSpanHelpers.CalculateStrides(lengths) : strides;
nint maxElements = TensorSpanHelpers.ComputeMaxElementCount(strides, lengths);
if (maxElements >= span.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)
ThrowHelper.ThrowArgument_InvalidStridesAndLengths();

_flattenedLength = linearLength;
Expand All @@ -157,7 +157,7 @@ public ReadOnlyTensorSpan(ReadOnlySpan<T> span, scoped ReadOnlySpan<nint> length
/// have a rank of 1 and a length equal to the length of the provided <see cref="Array"/>.
/// </summary>
/// <param name="array">The target array.</param>
public ReadOnlyTensorSpan(Array? array) : this(array, ReadOnlySpan<int>.Empty, [], []) { }
public ReadOnlyTensorSpan(Array? array) : this(array, ReadOnlySpan<int>.Empty, array == null ? [0] : (from dim in Enumerable.Range(0, array.Rank) select (nint)array.GetLength(dim)).ToArray(), []) { }

/// <summary>
/// Creates a new <see cref="ReadOnlyTensorSpan{T}"/> over the provided <see cref="Array"/> using the specified start offsets, lengths, and strides.
Expand All @@ -169,9 +169,10 @@ public ReadOnlyTensorSpan(Array? array) : this(array, ReadOnlySpan<int>.Empty, [
/// <param name="strides">The strides for each dimension. Will be automatically calculated if not provided.</param>
public ReadOnlyTensorSpan(Array? array, scoped ReadOnlySpan<int> start, scoped ReadOnlySpan<nint> lengths, scoped ReadOnlySpan<nint> strides)
{
if (lengths.IsEmpty && array != null)
lengths = (from dim in Enumerable.Range(0, array.Rank) select (nint)array.GetLength(dim)).ToArray();

nint linearLength = TensorSpanHelpers.CalculateTotalLength(lengths);
strides = strides.IsEmpty ? (ReadOnlySpan<nint>)TensorSpanHelpers.CalculateStrides(lengths) : strides;
nint startOffset = TensorSpanHelpers.ComputeLinearIndex(start, strides, lengths);
if (array == null)
{
if (!start.IsEmpty || linearLength != 0)
Expand All @@ -182,16 +183,20 @@ public ReadOnlyTensorSpan(Array? array, scoped ReadOnlySpan<int> start, scoped R
if (!typeof(T).IsValueType && array.GetType() != typeof(T[]))
ThrowHelper.ThrowArrayTypeMismatchException();

nint maxElements = TensorSpanHelpers.ComputeMaxElementCount(strides, lengths);
strides = strides.IsEmpty ? (ReadOnlySpan<nint>)TensorSpanHelpers.CalculateStrides(lengths, linearLength) : strides;
TensorSpanHelpers.ValidateStrides(strides, lengths);

nint startOffset = TensorSpanHelpers.ComputeStartOffsetSystemArray(array, start);
nint maxElements = TensorSpanHelpers.ComputeMaxLinearIndex(strides, lengths);
if (Environment.Is64BitProcess)
{
// See comment in Span<T>.Slice for how this works.
if ((ulong)(uint)startOffset + (ulong)(uint)maxElements > (ulong)(uint)array.Length)
if ((ulong)(uint)startOffset + (ulong)(uint)maxElements >= (ulong)(uint)array.Length && array.Length != 0)
ThrowHelper.ThrowArgumentOutOfRangeException();
}
else
{
if ((uint)startOffset > (uint)array.Length || (uint)maxElements > (uint)(array.Length - startOffset))
if (((uint)startOffset > (uint)array.Length || (uint)maxElements >= (uint)(array.Length - startOffset)) && array.Length != 0)
ThrowHelper.ThrowArgumentOutOfRangeException();
}

Expand All @@ -213,9 +218,10 @@ public ReadOnlyTensorSpan(Array? array, scoped ReadOnlySpan<int> start, scoped R
/// <param name="strides">The strides for each dimension. Will be automatically calculated if not provided.</param>
public ReadOnlyTensorSpan(Array? array, scoped ReadOnlySpan<NIndex> startIndex, scoped ReadOnlySpan<nint> lengths, scoped ReadOnlySpan<nint> strides)
{
if (lengths.IsEmpty && array != null)
lengths = (from dim in Enumerable.Range(0, array.Rank) select (nint)array.GetLength(dim)).ToArray();

nint linearLength = TensorSpanHelpers.CalculateTotalLength(lengths);
strides = strides.IsEmpty ? (ReadOnlySpan<nint>)TensorSpanHelpers.CalculateStrides(lengths) : strides;
nint start = TensorSpanHelpers.ComputeLinearIndex(startIndex, strides, lengths);
if (array == null)
{
if (!startIndex.IsEmpty || linearLength != 0)
Expand All @@ -226,22 +232,26 @@ public ReadOnlyTensorSpan(Array? array, scoped ReadOnlySpan<NIndex> startIndex,
if (!typeof(T).IsValueType && array.GetType() != typeof(T[]))
ThrowHelper.ThrowArrayTypeMismatchException();

nint maxElements = TensorSpanHelpers.ComputeMaxElementCount(strides, lengths);
strides = strides.IsEmpty ? (ReadOnlySpan<nint>)TensorSpanHelpers.CalculateStrides(lengths, linearLength) : strides;
TensorSpanHelpers.ValidateStrides(strides, lengths);

nint startOffset = TensorSpanHelpers.ComputeStartOffsetSystemArray(array, startIndex);
nint maxElements = TensorSpanHelpers.ComputeMaxLinearIndex(strides, lengths);
if (Environment.Is64BitProcess)
{
// See comment in Span<T>.Slice for how this works.
if ((ulong)(uint)start + (ulong)(uint)maxElements > (ulong)(uint)array.Length)
if ((ulong)(uint)startOffset + (ulong)(uint)maxElements > (ulong)(uint)array.Length)
ThrowHelper.ThrowArgumentOutOfRangeException();
}
else
{
if ((uint)start > (uint)array.Length || (uint)maxElements > (uint)(array.Length - start))
if ((uint)startOffset > (uint)array.Length || (uint)maxElements >= (uint)(array.Length - startOffset))
ThrowHelper.ThrowArgumentOutOfRangeException();
}

_flattenedLength = linearLength;
_memoryLength = array.Length;
_reference = ref Unsafe.Add(ref Unsafe.As<byte, T>(ref MemoryMarshal.GetArrayDataReference(array)), (nint)(uint)start /* force zero-extension */);
_reference = ref Unsafe.Add(ref Unsafe.As<byte, T>(ref MemoryMarshal.GetArrayDataReference(array)), (nint)(uint)startOffset /* force zero-extension */);

_lengths = lengths.ToArray();
_strides = strides.ToArray();
Expand Down Expand Up @@ -276,13 +286,21 @@ public unsafe ReadOnlyTensorSpan(T* data, nint dataLength) : this(data, dataLeng
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe ReadOnlyTensorSpan(T* data, nint dataLength, scoped ReadOnlySpan<nint> lengths, scoped ReadOnlySpan<nint> strides)
{
nint linearLength = TensorSpanHelpers.CalculateTotalLength(lengths);
if (dataLength < 0)
ThrowHelper.ThrowArgumentOutOfRangeException();

if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
ThrowHelper.ThrowInvalidTypeWithPointersNotSupported(typeof(T));

strides = strides.IsEmpty ? (ReadOnlySpan<nint>)TensorSpanHelpers.CalculateStrides(lengths) : strides;
nint maxElements = TensorSpanHelpers.ComputeMaxElementCount(strides, lengths);
if (maxElements >= dataLength)
if (lengths.IsEmpty)
lengths = [dataLength];

nint linearLength = TensorSpanHelpers.CalculateTotalLength(lengths);

strides = strides.IsEmpty ? (ReadOnlySpan<nint>)TensorSpanHelpers.CalculateStrides(lengths, linearLength) : strides;
TensorSpanHelpers.ValidateStrides(strides, lengths);
nint maxElements = TensorSpanHelpers.ComputeMaxLinearIndex(strides, lengths);
if (maxElements >= dataLength && dataLength != 0)
ThrowHelper.ThrowArgument_InvalidStridesAndLengths();

_flattenedLength = linearLength;
Expand Down
Loading

0 comments on commit 204f0e1

Please sign in to comment.