Skip to content

Commit

Permalink
changes from pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelgsharp committed Jun 20, 2024
1 parent 5053a67 commit 63bc01d
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 161 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ public ReadOnlyTensorSpan<T> this[params scoped ReadOnlySpan<NRange> ranges]
/// Gets a value indicating whether this <see cref="TensorSpan{T}"/> is empty.
/// </summary>
/// <value><see langword="true"/> if this span is empty; otherwise, <see langword="false"/>.</value>
public bool IsEmpty => _shape.FlattenedLength == 0;
public bool IsEmpty => _shape.IsEmpty;

/// <summary>
/// Gets the length of each dimension in this <see cref="TensorSpan{T}"/>.
Expand Down Expand Up @@ -522,7 +522,7 @@ public void CopyTo(scoped TensorSpan<T> destination)
{
scoped Span<nint> curIndexes;
nint[]? curIndexesArray;
if (Rank > TensorSpan.MaxRankForStackAlloc)
if (Rank > TensorShape.MaxInlineRank)
{
curIndexesArray = ArrayPool<nint>.Shared.Rent(Rank);
curIndexes = curIndexesArray;
Expand Down Expand Up @@ -569,7 +569,7 @@ public bool TryCopyTo(scoped TensorSpan<T> destination)
{
scoped Span<nint> curIndexes;
nint[]? curIndexesArray;
if (Rank > TensorSpan.MaxRankForStackAlloc)
if (Rank > TensorShape.MaxInlineRank)
{
curIndexesArray = ArrayPool<nint>.Shared.Rent(Rank);
curIndexes = curIndexesArray;
Expand Down Expand Up @@ -653,7 +653,7 @@ public ReadOnlyTensorSpan<T> Slice(params scoped ReadOnlySpan<NRange> ranges)

scoped Span<nint> lengths;
scoped Span<nint> offsets;
if (Rank > TensorSpan.MaxRankForStackAlloc)
if (Rank > TensorShape.MaxInlineRank)
{
lengths = stackalloc nint[Rank];
offsets = stackalloc nint[Rank];
Expand Down Expand Up @@ -692,7 +692,7 @@ public bool TryFlattenTo(scoped Span<T> destination)
{
scoped Span<nint> curIndexes;
nint[]? curIndexesArray;
if (Rank > TensorSpan.MaxRankForStackAlloc)
if (Rank > TensorShape.MaxInlineRank)
{
curIndexesArray = ArrayPool<nint>.Shared.Rent(Rank);
curIndexes = curIndexesArray;
Expand Down Expand Up @@ -733,7 +733,7 @@ public void FlattenTo(scoped Span<T> destination)

scoped Span<nint> curIndexes;
nint[]? curIndexesArray;
if (Rank > TensorSpan.MaxRankForStackAlloc)
if (Rank > TensorShape.MaxInlineRank)
{
curIndexesArray = ArrayPool<nint>.Shared.Rent(Rank);
curIndexes = curIndexesArray;
Expand All @@ -756,7 +756,7 @@ public void FlattenTo(scoped Span<T> destination)
{
TensorSpanHelpers.Memmove(destination.Slice(checked((int)copiedValues)), ref Unsafe.Add(ref _reference, TensorSpanHelpers.ComputeLinearIndex(curIndexes, Strides, Lengths)), Lengths[Rank - 1]);
}
TensorSpanHelpers.AdjustIndexes(Rank - 2, 1, curIndexes, _lengths);
TensorSpanHelpers.AdjustIndexes(Rank - 2, 1, curIndexes, _shape.Lengths);
copiedValues += Lengths[Rank - 1];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,16 +153,16 @@ public static Tensor<T> CreateUninitialized<T>(scoped ReadOnlySpan<nint> lengths

public static ref readonly TensorSpan<T> FillGaussianNormalDistribution<T>(in TensorSpan<T> destination) where T : IFloatingPoint<T>
{
Span<T> span = MemoryMarshal.CreateSpan<T>(ref destination._reference, (int)destination._flattenedLength);
Span<T> span = MemoryMarshal.CreateSpan<T>(ref destination._reference, (int)destination._shape._memoryLength);

GaussianDistribution<T>(span, destination._flattenedLength);
GaussianDistribution<T>(span, destination._shape._memoryLength);

return ref destination;
}

public static ref readonly TensorSpan<T> FillUniformDistribution<T>(in TensorSpan<T> destination) where T : IFloatingPoint<T>
{
Span<T> span = MemoryMarshal.CreateSpan<T>(ref destination._reference, (int)destination._flattenedLength);
Span<T> span = MemoryMarshal.CreateSpan<T>(ref destination._reference, (int)destination._shape._memoryLength);

for (int i = 0; i < span.Length; i++)
span[i] = T.CreateChecked(Random.Shared.NextDouble());
Expand Down
Loading

0 comments on commit 63bc01d

Please sign in to comment.