Skip to content

Commit aed63f2

Browse files
committed
Removed AsSpan<T>(this T[,]) extension method
1 parent 0ca805e commit aed63f2

File tree

5 files changed

+24
-16
lines changed

5 files changed

+24
-16
lines changed

NeuralNetwork.NET/APIs/Structs/Tensor.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,8 @@ public unsafe float[] ToArray(bool keepAlive = true)
281281
{
282282
if (Ptr == IntPtr.Zero) return new float[0, 0];
283283
float[,] result = new float[Entities, Length];
284-
new Span<float>(this, Size).CopyTo(result.AsSpan());
284+
fixed (float* p = result)
285+
new Span<float>(this, Size).CopyTo(new Span<float>(p, Size));
285286
if (!keepAlive) Free();
286287
return result;
287288
}

NeuralNetwork.NET/Extensions/ArrayExtensions.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Runtime.CompilerServices;
45
using System.Threading.Tasks;
56
using JetBrains.Annotations;
67

@@ -11,6 +12,19 @@ namespace NeuralNetworkNET.Extensions
1112
/// </summary>
1213
internal static class ArrayExtensions
1314
{
15+
/// <summary>
16+
/// Flattens a 2D array to a 1D array
17+
/// </summary>
18+
/// <typeparam name="T">The type of each element in the input matrix</typeparam>
19+
/// <param name="m">The input 2D array to flatten</param>
20+
[Pure]
21+
[CollectionAccess(CollectionAccessType.Read)]
22+
public static unsafe T[] Flatten<T>([NotNull] this T[,] m) where T : struct
23+
{
24+
fixed (void* p = &Unsafe.As<T, byte>(ref m[0, 0]))
25+
return new Span<T>(p, m.Length * Unsafe.SizeOf<T>()).ToArray();
26+
}
27+
1428
/// <summary>
1529
/// Merges the line pairs in the input collection into two 2D arrays
1630
/// </summary>

NeuralNetwork.NET/Extensions/DebugExtensions.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,14 @@ public static bool ContentEquals(in this Tensor m, in Tensor o, float absolute =
5555
/// <param name="o">The second matrix to test</param>
5656
/// <param name="absolute">The relative comparison threshold</param>
5757
/// <param name="relative">The relative comparison threshold</param>
58-
public static bool ContentEquals([CanBeNull] this float[,] m, [CanBeNull] float[,] o, float absolute = 1e-6f, float relative = 1e-6f)
58+
public static unsafe bool ContentEquals([CanBeNull] this float[,] m, [CanBeNull] float[,] o, float absolute = 1e-6f, float relative = 1e-6f)
5959
{
6060
if (m == null && o == null) return true;
6161
if (m == null || o == null) return false;
6262
if (m.GetLength(0) != o.GetLength(0) ||
6363
m.GetLength(1) != o.GetLength(1)) return false;
64-
return m.AsSpan().ContentEquals(o.AsSpan(), absolute, relative);
64+
fixed (float* pm = m, po = o)
65+
return new Span<float>(pm, m.Length).ContentEquals(new Span<float>(po, o.Length), absolute, relative);
6566
}
6667

6768
/// <summary>

NeuralNetwork.NET/Extensions/SpanExtensions.cs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,12 @@ public static unsafe void Fill<T>(this Span<T> span, [NotNull] Func<T> provider)
5151
/// <param name="w">The number of matrix columns</param>
5252
[Pure, NotNull]
5353
[CollectionAccess(CollectionAccessType.Read)]
54-
public static T[,] AsMatrix<T>(this Span<T> span, int h, int w) where T : struct
54+
public static unsafe T[,] AsMatrix<T>(this Span<T> span, int h, int w) where T : struct
5555
{
5656
if (h * w != span.Length) throw new ArgumentException("The input dimensions don't match the source vector size");
5757
T[,] m = new T[h, w];
58-
span.CopyTo(m.AsSpan());
58+
fixed (void* p = &Unsafe.As<T, byte>(ref m[0, 0]))
59+
span.CopyTo(new Span<T>(p, m.Length * Unsafe.SizeOf<T>()));
5960
return m;
6061
}
6162

@@ -94,15 +95,6 @@ public static T[] Copy<T>(this Span<T> span) where T : struct
9495
return result;
9596
}
9697

97-
/// <summary>
98-
/// Returns a new <see cref="Span{T}"/> instance from the input matrix
99-
/// </summary>
100-
/// <typeparam name="T">The type of the input matrix</typeparam>
101-
/// <param name="m">The input matrix</param>
102-
[Pure]
103-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
104-
public static Span<T> AsSpan<T>([NotNull] this T[,] m) where T : struct => Span<T>.DangerousCreate(m, ref m[0, 0], m.Length);
105-
10698
/// <summary>
10799
/// Extracts a single row from a given matrix
108100
/// </summary>

NeuralNetwork.NET/Networks/Implementations/ComputationGraphNetwork.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,8 +422,8 @@ private static unsafe bool TryExecuteMergeForward([NotNull] TensorMap<IComputati
422422
public override IReadOnlyList<(float[] Z, float[] A)> ExtractDeepFeatures(float[] x)
423423
{
424424
return (from pair in ExtractDeepFeatures(x.AsSpan().AsMatrix(1, x.Length))
425-
let z = pair.Z?.AsSpan().ToArray()
426-
let a = pair.A.AsSpan().ToArray()
425+
let z = pair.Z?.Flatten()
426+
let a = pair.A.Flatten()
427427
select (z, a)).ToArray();
428428
}
429429

0 commit comments

Comments
 (0)