3
3
4
4
using System ;
5
5
using System . Collections . Generic ;
6
- using System . Linq ;
7
6
using System . Runtime . CompilerServices ;
8
7
using System . Runtime . InteropServices ;
9
8
using System . Text ;
@@ -3533,7 +3532,8 @@ public static T StdDev<T>(in ReadOnlyTensorSpan<T> x)
3533
3532
/// <param name="tensor">The <see cref="TensorSpan{T}"/> you want to represent as a string.</param>
3534
3533
/// <param name="maximumLengths">Maximum Length of each dimension</param>
3535
3534
/// <returns>A <see cref="string"/> representation of the <paramref name="tensor"/></returns>
3536
- public static string ToString < T > ( this in TensorSpan < T > tensor , params ReadOnlySpan < nint > maximumLengths ) => ( ( ReadOnlyTensorSpan < T > ) tensor ) . ToString ( maximumLengths ) ;
3535
+ public static string ToString < T > ( this in TensorSpan < T > tensor , params ReadOnlySpan < nint > maximumLengths ) =>
3536
+ ( ( ReadOnlyTensorSpan < T > ) tensor ) . ToString ( maximumLengths ) ;
3537
3537
3538
3538
/// <summary>
3539
3539
/// Creates a <see cref="string"/> representation of the <see cref="ReadOnlyTensorSpan{T}"/>."/>
@@ -3543,11 +3543,16 @@ public static T StdDev<T>(in ReadOnlyTensorSpan<T> x)
3543
3543
/// <param name="maximumLengths">Maximum Length of each dimension</param>
3544
3544
public static string ToString < T > ( this in ReadOnlyTensorSpan < T > tensor , params ReadOnlySpan < nint > maximumLengths )
3545
3545
{
3546
+ StringBuilder sb = new ( ) ;
3547
+ ToString ( in tensor , sb , maximumLengths ) ;
3548
+ return sb . ToString ( ) ;
3549
+ }
3546
3550
3551
+ internal static void ToString < T > ( this in ReadOnlyTensorSpan < T > tensor , StringBuilder sb , params ReadOnlySpan < nint > maximumLengths )
3552
+ {
3547
3553
if ( maximumLengths . Length != tensor . Rank )
3548
3554
ThrowHelper . ThrowArgument_DimensionsNotSame ( nameof ( tensor ) ) ;
3549
3555
3550
- var sb = new StringBuilder ( ) ;
3551
3556
scoped Span < nint > curIndexes ;
3552
3557
nint [ ] ? curIndexesArray ;
3553
3558
if ( tensor . Rank > 6 )
@@ -3579,8 +3584,6 @@ public static string ToString<T>(this in ReadOnlyTensorSpan<T> tensor, params Re
3579
3584
3580
3585
if ( curIndexesArray != null )
3581
3586
ArrayPool < nint > . Shared . Return ( curIndexesArray ) ;
3582
-
3583
- return sb . ToString ( ) ;
3584
3587
}
3585
3588
3586
3589
/// <summary>
@@ -3602,11 +3605,15 @@ public static Tensor<T> Transpose<T>(Tensor<T> tensor)
3602
3605
{
3603
3606
if ( tensor . Lengths . Length < 2 )
3604
3607
ThrowHelper . ThrowArgument_TransposeTooFewDimensions ( ) ;
3605
- int [ ] dimension = Enumerable . Range ( 0 , tensor . Rank ) . ToArray ( ) ;
3608
+
3609
+ Span < int > dimension = tensor . Rank <= TensorShape . MaxInlineRank ? stackalloc int [ tensor . Rank ] : new int [ tensor . Rank ] ;
3610
+ TensorSpanHelpers . FillRange ( dimension ) ;
3611
+
3606
3612
int temp = dimension [ tensor . Rank - 1 ] ;
3607
3613
dimension [ tensor . Rank - 1 ] = dimension [ tensor . Rank - 2 ] ;
3608
3614
dimension [ tensor . Rank - 2 ] = temp ;
3609
- return PermuteDimensions ( tensor , dimension . AsSpan ( ) ) ;
3615
+
3616
+ return PermuteDimensions ( tensor , dimension ) ;
3610
3617
}
3611
3618
#endregion
3612
3619
@@ -3666,15 +3673,28 @@ public static Tensor<T> Unsqueeze<T>(this Tensor<T> tensor, int dimension)
3666
3673
if ( dimension < 0 )
3667
3674
dimension = tensor . Rank - dimension ;
3668
3675
3669
- List < nint > tempLengths = tensor . _lengths . ToList ( ) ;
3670
- tempLengths . Insert ( dimension , 1 ) ;
3671
- nint [ ] lengths = [ .. tempLengths ] ;
3672
- List < nint > tempStrides = tensor . Strides . ToArray ( ) . ToList ( ) ;
3676
+ Span < nint > lengths = tensor . _lengths . Length + 1 <= TensorShape . MaxInlineRank ?
3677
+ stackalloc nint [ tensor . _lengths . Length + 1 ] :
3678
+ new nint [ tensor . _lengths . Length + 1 ] ;
3679
+ tensor . _lengths . AsSpan ( 0 , dimension ) . CopyTo ( lengths ) ;
3680
+ tensor . _lengths . AsSpan ( dimension ) . CopyTo ( lengths . Slice ( dimension + 1 ) ) ;
3681
+ lengths [ dimension ] = 1 ;
3682
+
3683
+ Span < nint > strides = tensor . Strides . Length + 1 <= TensorShape . MaxInlineRank ?
3684
+ stackalloc nint [ tensor . Strides . Length + 1 ] :
3685
+ new nint [ tensor . Strides . Length + 1 ] ;
3673
3686
if ( dimension == tensor . Rank )
3674
- tempStrides . Add ( tensor . Strides [ dimension - 1 ] ) ;
3687
+ {
3688
+ tensor . Strides . CopyTo ( strides ) ;
3689
+ strides [ dimension ] = tensor . Strides [ dimension - 1 ] ;
3690
+ }
3675
3691
else
3676
- tempStrides . Insert ( dimension , tensor . Strides [ dimension ] * tensor . Lengths [ dimension ] ) ;
3677
- nint [ ] strides = [ .. tempStrides ] ;
3692
+ {
3693
+ tensor . Strides . Slice ( 0 , dimension ) . CopyTo ( strides ) ;
3694
+ tensor . Strides . Slice ( dimension ) . CopyTo ( strides . Slice ( dimension + 1 ) ) ;
3695
+ strides [ dimension ] = tensor . Strides [ dimension ] * tensor . Lengths [ dimension ] ;
3696
+ }
3697
+
3678
3698
return new Tensor < T > ( tensor . _values , lengths , strides ) ;
3679
3699
}
3680
3700
@@ -3690,15 +3710,28 @@ public static TensorSpan<T> Unsqueeze<T>(in this TensorSpan<T> tensor, int dimen
3690
3710
if ( dimension < 0 )
3691
3711
dimension = tensor . Rank - dimension ;
3692
3712
3693
- List < nint > tempLengths = tensor . Lengths . ToArray ( ) . ToList ( ) ;
3694
- tempLengths . Insert ( dimension , 1 ) ;
3695
- nint [ ] lengths = [ .. tempLengths ] ;
3696
- List < nint > tempStrides = tensor . Strides . ToArray ( ) . ToList ( ) ;
3713
+ Span < nint > lengths = tensor . Lengths . Length + 1 <= TensorShape . MaxInlineRank ?
3714
+ stackalloc nint [ tensor . Lengths . Length + 1 ] :
3715
+ new nint [ tensor . Lengths . Length + 1 ] ;
3716
+ tensor . Lengths . Slice ( 0 , dimension ) . CopyTo ( lengths ) ;
3717
+ tensor . Lengths . Slice ( dimension ) . CopyTo ( lengths . Slice ( dimension + 1 ) ) ;
3718
+ lengths [ dimension ] = 1 ;
3719
+
3720
+ Span < nint > strides = tensor . Strides . Length + 1 <= TensorShape . MaxInlineRank ?
3721
+ stackalloc nint [ tensor . Strides . Length + 1 ] :
3722
+ new nint [ tensor . Strides . Length + 1 ] ;
3697
3723
if ( dimension == tensor . Rank )
3698
- tempStrides . Add ( tensor . Strides [ dimension - 1 ] ) ;
3724
+ {
3725
+ tensor . Strides . CopyTo ( strides ) ;
3726
+ strides [ dimension ] = tensor . Strides [ dimension - 1 ] ;
3727
+ }
3699
3728
else
3700
- tempStrides . Insert ( dimension , tensor . Strides [ dimension ] * tensor . Lengths [ dimension ] ) ;
3701
- nint [ ] strides = [ .. tempStrides ] ;
3729
+ {
3730
+ tensor . Strides . Slice ( 0 , dimension ) . CopyTo ( strides ) ;
3731
+ tensor . Strides . Slice ( dimension ) . CopyTo ( strides . Slice ( dimension + 1 ) ) ;
3732
+ strides [ dimension ] = tensor . Strides [ dimension ] * tensor . Lengths [ dimension ] ;
3733
+ }
3734
+
3702
3735
return new TensorSpan < T > ( ref tensor . _reference , lengths , strides , tensor . _shape . _memoryLength ) ;
3703
3736
}
3704
3737
@@ -3714,15 +3747,28 @@ public static ReadOnlyTensorSpan<T> Unsqueeze<T>(in this ReadOnlyTensorSpan<T> t
3714
3747
if ( dimension < 0 )
3715
3748
dimension = tensor . Rank - dimension ;
3716
3749
3717
- List < nint > tempLengths = tensor . Lengths . ToArray ( ) . ToList ( ) ;
3718
- tempLengths . Insert ( dimension , 1 ) ;
3719
- nint [ ] lengths = [ .. tempLengths ] ;
3720
- List < nint > tempStrides = tensor . Strides . ToArray ( ) . ToList ( ) ;
3750
+ Span < nint > lengths = tensor . Lengths . Length + 1 <= TensorShape . MaxInlineRank ?
3751
+ stackalloc nint [ tensor . Lengths . Length + 1 ] :
3752
+ new nint [ tensor . Lengths . Length + 1 ] ;
3753
+ tensor . Lengths . Slice ( 0 , dimension ) . CopyTo ( lengths ) ;
3754
+ tensor . Lengths . Slice ( dimension ) . CopyTo ( lengths . Slice ( dimension + 1 ) ) ;
3755
+ lengths [ dimension ] = 1 ;
3756
+
3757
+ Span < nint > strides = tensor . Strides . Length + 1 <= TensorShape . MaxInlineRank ?
3758
+ stackalloc nint [ tensor . Strides . Length + 1 ] :
3759
+ new nint [ tensor . Strides . Length + 1 ] ;
3721
3760
if ( dimension == tensor . Rank )
3722
- tempStrides . Add ( tensor . Strides [ dimension - 1 ] ) ;
3761
+ {
3762
+ tensor . Strides . CopyTo ( strides ) ;
3763
+ strides [ dimension ] = tensor . Strides [ dimension - 1 ] ;
3764
+ }
3723
3765
else
3724
- tempStrides . Insert ( dimension , tensor . Strides [ dimension ] * tensor . Lengths [ dimension ] ) ;
3725
- nint [ ] strides = [ .. tempStrides ] ;
3766
+ {
3767
+ tensor . Strides . Slice ( 0 , dimension ) . CopyTo ( strides ) ;
3768
+ tensor . Strides . Slice ( dimension ) . CopyTo ( strides . Slice ( dimension + 1 ) ) ;
3769
+ strides [ dimension ] = tensor . Strides [ dimension ] * tensor . Lengths [ dimension ] ;
3770
+ }
3771
+
3726
3772
return new ReadOnlyTensorSpan < T > ( ref tensor . _reference , lengths , strides , tensor . _shape . _memoryLength ) ;
3727
3773
}
3728
3774
#endregion
0 commit comments