diff --git a/samples/ComputeSharp.Benchmark/ComputeSharp.Benchmark.csproj b/samples/ComputeSharp.Benchmark/ComputeSharp.Benchmark.csproj
index 4f1afcc1..f93e3c60 100644
--- a/samples/ComputeSharp.Benchmark/ComputeSharp.Benchmark.csproj
+++ b/samples/ComputeSharp.Benchmark/ComputeSharp.Benchmark.csproj
@@ -9,4 +9,8 @@
+
+
+
+
diff --git a/samples/ComputeSharp.Benchmark/Dnn.cs b/samples/ComputeSharp.Benchmark/Dnn.cs
index 3e9f812b..4e5a6c8c 100644
--- a/samples/ComputeSharp.Benchmark/Dnn.cs
+++ b/samples/ComputeSharp.Benchmark/Dnn.cs
@@ -1,5 +1,6 @@
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
+using ComputeSharp.Graphics.Buffers;
namespace ComputeSharp.Benchmark
{
@@ -19,11 +20,10 @@ namespace ComputeSharp.Benchmark
/// 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)
+ public static void FullyConnectedForwardCpu(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];
@@ -40,12 +40,47 @@ namespace ComputeSharp.Benchmark
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);
+ Unsafe.Add(ref ry, i * p + j) = result + Unsafe.Add(ref rb, j);
}
}
}
Parallel.For(0, c, Kernel);
}
+
+ ///
+ /// Executes the forward pass on a fully connected layer on the GPU
+ ///
+ /// 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 FullyConnectedForwardGpu(int c, int n, int m, int p, float[] x, float[] w, float[] b, float[] y)
+ {
+ using ReadOnlyBuffer x_gpu = Gpu.Default.AllocateReadOnlyBuffer(x);
+ using ReadOnlyBuffer w_gpu = Gpu.Default.AllocateReadOnlyBuffer(w);
+ using ReadOnlyBuffer b_gpu = Gpu.Default.AllocateReadOnlyBuffer(b);
+ using ReadWriteBuffer y_gpu = Gpu.Default.AllocateReadWriteBuffer(y);
+
+ void Kernel(ThreadIds id)
+ {
+ float result = 0f;
+
+ for (int k = 0; k < m; k++)
+ {
+ result += x_gpu[(uint)(id.X * n * p + id.Y * m + k)] * w_gpu[(uint)(k * p + id.Z)];
+ }
+
+ y_gpu[(uint)(id.X * n * p + id.Y * p + id.Z)] = result + b_gpu[id.Z];
+ }
+
+ Gpu.Default.For(c, n, p, Kernel);
+
+ y_gpu.GetData(y);
+ }
}
}