From 245a8fecb362c11b3ca35f96c152e93b7d6934fa Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 17 Aug 2019 16:05:04 +0200 Subject: [PATCH] Added CPU forward pass --- samples/ComputeSharp.Benchmark/Dnn.cs | 51 +++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 samples/ComputeSharp.Benchmark/Dnn.cs diff --git a/samples/ComputeSharp.Benchmark/Dnn.cs b/samples/ComputeSharp.Benchmark/Dnn.cs new file mode 100644 index 00000000..3e9f812b --- /dev/null +++ b/samples/ComputeSharp.Benchmark/Dnn.cs @@ -0,0 +1,51 @@ +using System.Runtime.CompilerServices; +using System.Threading.Tasks; + +namespace ComputeSharp.Benchmark +{ + /// + /// A that contains primitives to run certain operations of a neural network + /// + internal static class Dnn + { + /// + /// Executes the forward pass on a fully connected layer on the CPU + /// + /// The number of samples in the input tensor + /// The number of rows in the input matrix + /// The number of columns in the input matrix + /// The number of columns in the output matrix + /// The input tensor + /// The weights tensor + /// The bias tensor + /// The result tensor + public static void FullyConnectedForward(int c, int n, int m, int p, float[] x, float[] w, float[] b, float[] y) + { + void Kernel(int s) + { + int offset = s * n * m; + ref float rx = ref x[s * n * m]; + ref float rw = ref w[0]; + ref float rb = ref b[0]; + ref float ry = ref y[s * n * p]; + + for (int i = 0; i < n; i++) + { + for (int j = 0; j < p; j++) + { + float result = 0f; + + for (int k = 0; k < m; k++) + { + result += Unsafe.Add(ref rx, i * m + k) * Unsafe.Add(ref rw, k * p + j); + } + + Unsafe.Add(ref ry, i * m + j) = result + Unsafe.Add(ref rb, j); + } + } + } + + Parallel.For(0, c, Kernel); + } + } +}