forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changed TensorSpan and ReadOnlyTensorSpan layout for better performan…
…ce. (dotnet#103244) * TensorShape * fixed TensorShape issue * removed extra inline buffer type. Fixed tests. Added large dimension testing * removed typo * adding unsaved files * changes to use const for stack alloc comparison * changes from pr comments
- Loading branch information
1 parent
396e87e
commit 50458c6
Showing
11 changed files
with
475 additions
and
296 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 51 additions & 78 deletions
129
...braries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/ReadOnlyTensorSpan.cs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
191 changes: 95 additions & 96 deletions
191
...libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorExtensions.cs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorShape.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Diagnostics; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace System.Numerics.Tensors | ||
{ | ||
internal readonly struct TensorShape | ||
{ | ||
// Used to determine when we need to allocate a metadata array | ||
public const int MaxInlineArraySize = 5; | ||
|
||
// Used to determine when we can stack alloc for indexing vs when we need to allocate | ||
public const int MaxInlineRank = 8; | ||
|
||
internal readonly nint[]? _metadata; // 8 bytes | ||
|
||
internal readonly nint _memoryLength; // 8 bytes | ||
internal readonly int _rank; // 4 bytes | ||
|
||
private readonly NintBuffer _lengths; | ||
private readonly NintBuffer _strides; | ||
|
||
internal TensorShape(nint memoryLength, ReadOnlySpan<nint> lengths, ReadOnlySpan<nint> strides) | ||
{ | ||
_memoryLength = memoryLength; | ||
_rank = lengths.Length; | ||
if (lengths.Length > MaxInlineArraySize) | ||
{ | ||
_metadata = new nint[lengths.Length + strides.Length]; | ||
lengths.CopyTo(MemoryMarshal.CreateSpan(ref _metadata[0], lengths.Length)); | ||
strides.CopyTo(MemoryMarshal.CreateSpan(ref _metadata[lengths.Length], strides.Length)); | ||
} | ||
else | ||
{ | ||
lengths.CopyTo(_lengths); | ||
strides.CopyTo(_strides); | ||
} | ||
} | ||
|
||
[InlineArray(MaxInlineArraySize)] // 5x8 bytes (40) | ||
private struct NintBuffer | ||
{ | ||
public nint e0; | ||
} | ||
|
||
[UnscopedRef] | ||
public ReadOnlySpan<nint> Lengths => (_metadata is null) | ||
? ((ReadOnlySpan<nint>)_lengths).Slice(0, _rank) | ||
: MemoryMarshal.CreateReadOnlySpan(ref MemoryMarshal.GetArrayDataReference(_metadata), _rank); | ||
|
||
[UnscopedRef] | ||
public ReadOnlySpan<nint> Strides => (_metadata is null) | ||
? ((ReadOnlySpan<nint>)_strides).Slice(0, _rank) | ||
: MemoryMarshal.CreateReadOnlySpan(ref MemoryMarshal.GetArrayDataReference(_metadata), _rank * 2).Slice(_rank); | ||
|
||
public nint FlattenedLength => TensorSpanHelpers.CalculateTotalLength(Lengths); | ||
|
||
public bool IsEmpty => FlattenedLength == 0; | ||
} | ||
} |
Oops, something went wrong.