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

Added all missing xml docs for Tensors #106084

Merged
merged 5 commits into from
Aug 13, 2024
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 @@ -198,8 +198,8 @@
<data name="ThrowArgument_SetSliceNoRange" xml:space="preserve">
<value>When no ranges are specified the values tensor must be equal in size as the input tensor.</value>
</data>
<data name="ThrowArgument_ShapesNotBroadcastCompatible" xml:space="preserve">
<value>Shapes are not broadcast compatible.</value>
<data name="ThrowArgument_LengthsNotBroadcastCompatible" xml:space="preserve">
<value>Lengths are not broadcast compatible.</value>
</data>
<data name="ThrowArgument_SplitNotSplitEvenly" xml:space="preserve">
<value>The number of splits must perfectly divide the dimension.</value>
Expand Down Expand Up @@ -228,4 +228,4 @@
<data name="ThrowArgument_StackShapesNotSame" xml:space="preserve">
<value>All tensors must have the same shape.</value>
</data>
</root>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
<IsPackable>true</IsPackable>
<PackageDescription>Provides support for operating over tensors.</PackageDescription>
<GenAPIExcludeApiList>ReferenceAssemblyExclusions.txt</GenAPIExcludeApiList>
<ApiCompatValidateAssemblies>true</ApiCompatValidateAssemblies>
michaelgsharp marked this conversation as resolved.
Show resolved Hide resolved
<!-- SYSLIB5001: Tensor<T> and related APIs in System.Numerics.Tensors are experimental in .NET 9 -->
<NoWarn>$(NoWarn);SYSLIB5001</NoWarn>
</PropertyGroup>
Expand Down
23 changes: 15 additions & 8 deletions src/libraries/System.Numerics.Tensors/src/System/NIndex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace System.Buffers
{
private readonly nint _value;

/// <summary>Construct an NIndex using a value and indicating if the NIndex is from the start or from the end.</summary>
/// <summary>Construct an <see cref="NIndex"/> using a value and indicating if the <see cref="NIndex"/> is from the start or from the end.</summary>
/// <param name="value">The index value. it has to be zero or positive number.</param>
/// <param name="fromEnd">Indicating if the index is from the start or from the end.</param>
/// <remarks>
Expand Down Expand Up @@ -59,13 +59,13 @@ private NIndex(nint value)
_value = value;
}

/// <summary>Create an NIndex pointing at first element.</summary>
/// <summary>Create an <see cref="NIndex"/> pointing at first element.</summary>
public static NIndex Start => new NIndex((nint)0);

/// <summary>Create an NIndex pointing at beyond last element.</summary>
/// <summary>Create an <see cref="NIndex"/> pointing at beyond last element.</summary>
public static NIndex End => new NIndex((nint)~0);

/// <summary>Create an NIndex from the start at the position indicated by the value.</summary>
/// <summary>Create an <see cref="NIndex"/> from the start at the position indicated by the value.</summary>
/// <param name="value">The index value from the start.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NIndex FromStart(nint value)
Expand All @@ -91,12 +91,19 @@ public static NIndex FromEnd(nint value)
return new NIndex(~value);
}

#pragma warning disable 1591 // TODO: Document this API. https://github.com/dotnet/runtime/issues/105981
/// <summary>
/// Converts the <see cref="NIndex"/> to an <see cref="Index"/>.
/// </summary>
/// <returns>The converted Index.</returns>
public Index ToIndex() => checked((Index)this);

/// <summary>
/// Converts the <see cref="NIndex"/> to an <see cref="Index"/> without doing bounds checks.
/// </summary>
/// <returns>The converted Index.</returns>
public Index ToIndexUnchecked() => (Index)this;
#pragma warning restore 1591

/// <summary>Returns the NIndex value.</summary>
/// <summary>Returns the <see cref="NIndex"/> value.</summary>
public nint Value
{
get
Expand All @@ -108,7 +115,7 @@ public nint Value
}
}

/// <summary>Indicates whether the NIndex is from the start or the end.</summary>
/// <summary>Indicates whether the <see cref="NIndex"/> is from the start or the end.</summary>
public bool IsFromEnd => _value < 0;

/// <summary>Calculate the offset from the start using the giving collection length.</summary>
Expand Down
38 changes: 29 additions & 9 deletions src/libraries/System.Numerics.Tensors/src/System/NRange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,23 @@ namespace System.Buffers
[Experimental(Experimentals.TensorTDiagId, UrlFormat = Experimentals.SharedUrlFormat)]
public readonly struct NRange : IEquatable<NRange>
{
/// <summary>Represent the inclusive start NIndex of the NRange.</summary>
/// <summary>Represents the inclusive start NIndex of the NRange.</summary>
public NIndex Start { get; }

/// <summary>Represent the exclusive end NIndex of the NRange.</summary>
/// <summary>Represents the exclusive end NIndex of the NRange.</summary>
public NIndex End { get; }

/// <summary>Construct an NRange object using the start and end NIndexes.</summary>
/// <param name="start">Represent the inclusive start NIndex of the NRange.</param>
/// <param name="end">Represent the exclusive end NIndex of the NRange.</param>
/// <summary>Constructs an <see cref="NRange"/> object using the start and end <see cref="NIndex"/>.</summary>
/// <param name="start">Represent the inclusive start <see cref="NIndex"/> of the <see cref="NRange"/>.</param>
/// <param name="end">Represent the exclusive end <see cref="NIndex"/> of the <see cref="NRange"/>.</param>
public NRange(NIndex start, NIndex end)
{
Start = start;
End = end;
}

/// <summary>
/// Construct a <see cref="NRange"/> object using a <see cref="Range"/>.
/// Constructs an <see cref="NRange"/> object using a <see cref="Range"/>.
/// </summary>
/// <param name="range">The <see cref="Range"/> to use.</param>
public NRange(Range range)
Expand All @@ -43,7 +43,7 @@ public NRange(Range range)
End = range.End;
}

/// <summary>Indicates whether the current NRange object is equal to another object of the same type.</summary>
/// <summary>Indicates whether the current <see cref="NRange"/> object is equal to another object of the same type.</summary>
/// <param name="value">An object to compare with this object</param>
public override bool Equals([NotNullWhen(true)] object? value) =>
value is NRange r &&
Expand Down Expand Up @@ -121,14 +121,34 @@ public override string ToString()

private static void ThrowArgumentOutOfRangeException() => throw new ArgumentOutOfRangeException("length");

#pragma warning disable 1591 // TODO: Document this API. https://github.com/dotnet/runtime/issues/105981
/// <summary>
/// Implicitly converts a <see cref="Range"/> to an <see cref="NRange"/>.
/// </summary>
/// <param name="range"></param>
public static implicit operator NRange(Range range) => new NRange(range.Start, range.End);

/// <summary>
/// Explicitly converts an <see cref="NRange"/> to a <see cref="Range"/> without doing bounds checks.
/// </summary>
/// <param name="value"><see cref="NRange"/> to convert.</param>
public static explicit operator Range(NRange value) => new Range((Index)value.Start, (Index)value.End);

/// <summary>
/// Explicitly converts an <see cref="NRange"/> to a <see cref="Range"/>.
/// </summary>
/// <param name="value"><see cref="NRange"/> to convert.</param>
public static explicit operator checked Range(NRange value) => new Range(checked((Index)value.Start), checked((Index)value.End));

/// <summary>
/// Converts a <see cref="NRange"/> to a <see cref="Range"/>.
/// </summary>
/// <returns>The converted Range.</returns>
public Range ToRange() => new Range(checked((Index)Start), checked((Index)End));

/// <summary>
/// Converts a <see cref="NRange"/> to a <see cref="Range"/> wihout doing bounds checks.
/// </summary>
/// <returns>The converted Range.</returns>
public Range ToRangeUnchecked() => new Range((Index)Start, (Index)End);
#pragma warning restore 1591
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,152 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;

#pragma warning disable 1591 // TODO: Document this API. https://github.com/dotnet/runtime/issues/105981

namespace System.Numerics.Tensors
{
/// <summary>
/// Represents a read-only tensor.
/// </summary>
/// <typeparam name="TSelf">The type that implements this interface.</typeparam>
/// <typeparam name="T">The element type.</typeparam>
[Experimental(Experimentals.TensorTDiagId, UrlFormat = Experimentals.SharedUrlFormat)]
public interface IReadOnlyTensor<TSelf, T> : IEnumerable<T>
where TSelf : IReadOnlyTensor<TSelf, T>
{
/// <summary>
/// Gets an empty tensor.
/// </summary>
static abstract TSelf? Empty { get; }

/// <summary>
/// Gets a value that indicates whether the collection is currently empty.
/// </summary>
bool IsEmpty { get; }

/// <summary>
/// Gets a value that indicates whether the underlying buffer is pinned.
/// </summary>
bool IsPinned { get; }

/// <summary>
/// Gets the number of elements in the tensor.
/// </summary>
nint FlattenedLength { get; }

/// <summary>
/// Gets the number of dimensions in the tensor.
/// </summary>
int Rank { get; }

/// <summary>
/// Gets the value at the specified indexes.
/// </summary>
/// <param name="indexes">The indexes to be used.</param>
T this[params scoped ReadOnlySpan<nint> indexes] { get; }

/// <summary>
/// Gets the value at the specified indexes.
/// </summary>
/// <param name="indexes">The indexes to be used.</param>
T this[params scoped ReadOnlySpan<NIndex> indexes] { get; }

/// <summary>
/// Gets the values at the specified ranges.
/// </summary>
/// <param name="ranges">The ranges to be used.</param>
TSelf this[params scoped ReadOnlySpan<NRange> ranges] { get; }

/// <summary>
/// Creates a read-only tensor span for the entire underlying buffer.
/// </summary>
/// <returns>The converted <see cref="ReadOnlyTensorSpan{T}"/>.</returns>
ReadOnlyTensorSpan<T> AsReadOnlyTensorSpan();

/// <summary>
/// Creates a read-only tensor span for the specified start indexes.
/// </summary>
/// <param name="start">The start locations to be used.</param>
/// <returns>The converted <see cref="ReadOnlyTensorSpan{T}"/>.</returns>
ReadOnlyTensorSpan<T> AsReadOnlyTensorSpan(params scoped ReadOnlySpan<nint> start);

/// <summary>
/// Creates a read-only tensor span for the specified start indexes.
/// </summary>
/// <param name="startIndex">The started indexes to be used.</param>
/// <returns>The converted <see cref="ReadOnlyTensorSpan{T}"/>.</returns>
ReadOnlyTensorSpan<T> AsReadOnlyTensorSpan(params scoped ReadOnlySpan<NIndex> startIndex);

/// <summary>
/// Creates a read-only tensor span for the specified ranges.
/// </summary>
/// <param name="range">The ranges to be used.</param>
/// <returns>The converted <see cref="ReadOnlyTensorSpan{T}"/>.</returns>
ReadOnlyTensorSpan<T> AsReadOnlyTensorSpan(params scoped ReadOnlySpan<NRange> range);

/// <summary>
/// Copies the tensor to the specified destination. The destination tensor must be equal to or larger than the source tensor.
/// </summary>
/// <param name="destination">The destination span where the data should be copied to.</param>
void CopyTo(scoped TensorSpan<T> destination);

/// <summary>
/// Flattens the tensor to the specified destination. The destination span must be equal to or larger than the number of elements in the source tensor.
/// </summary>
/// <param name="destination">The destination span where the data should be flattened to.</param>
void FlattenTo(scoped Span<T> destination);

/// <summary>
/// Gets the length of each dimension in the tensor.
/// </summary>
[UnscopedRef]
ReadOnlySpan<nint> Lengths { get; }

/// <summary>
/// Gets the stride of each dimension in the tensor.
/// </summary>
[UnscopedRef]
ReadOnlySpan<nint> Strides { get; }

/// <summary>
/// Returns a reference to the 0th element of the tensor. If the tensor is empty, returns <see langword="null"/>.
/// </summary>
michaelgsharp marked this conversation as resolved.
Show resolved Hide resolved
/// <remarks>
/// This method can be used for pinning and is required to support the use of the tensor within a fixed statement.
/// </remarks>
ref readonly T GetPinnableReference();

/// <summary>
/// Slices the tensor using the specified start indexes.
/// </summary>
/// <param name="start">The start locations to be used.</param>
/// <returns>The sliced tensor.</returns>
TSelf Slice(params scoped ReadOnlySpan<nint> start);

/// <summary>
/// Slices the tensor using the specified start indexes.
/// </summary>
/// <param name="startIndex">The start indexes to be used.</param>
/// <returns>The sliced tensor.</returns>
TSelf Slice(params scoped ReadOnlySpan<NIndex> startIndex);

/// <summary>
/// Slices the tensor using the specified ranges.
/// </summary>
/// <param name="range">The ranges to be used.</param>
/// <returns>The sliced tensor.</returns>
TSelf Slice(params scoped ReadOnlySpan<NRange> range);

/// <summary>
/// Tries to copy the tensor to the specified destination. The destination tensor must be equal to or larger than the source tensor.
/// </summary>
/// <param name="destination">The destination span where the data should be copied to.</param>
/// <returns><see langword="true" /> if the copy succeeded, <see langword="false" /> otherwise.</returns>
bool TryCopyTo(scoped TensorSpan<T> destination);

/// <summary>
/// Tries to flatten the tensor to the specified destination. The destination span must be equal to or larger than the number of elements in the source tensor.
/// </summary>
/// <param name="destination">The destination span where the data should be flattened to.</param>
/// <returns><see langword="true" /> if the flatten succeeded, <see langword="false" /> otherwise.</returns>
bool TryFlattenTo(scoped Span<T> destination);
}
}

#pragma warning restore 1591
Loading
Loading