using System; using System.Buffers; using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; using BenchmarkDotNet.Attributes; namespace ComputeSharp.Benchmark { /// /// A that benchmarks the APIs in the class, on both CPU and GPU /// public class DnnBenchmark : IDisposable { /// /// The number of samples /// private const int C = 128; /// /// The nummber of rows in the matrix /// private const int N = 512; /// /// The number of columns in the matrix (same as the number of rows in the matrix) /// private const int M = 512; /// /// The number of columns in the matrix /// private const int P = 256; /// /// The input tensor /// private float[] X; /// /// The weights tensor /// private float[] W; /// /// The bias tensor /// private float[] B; /// /// The result tensor /// private float[] Y; /// /// The input tensor /// private ReadOnlyBuffer BufferX; /// /// The weights tensor /// private ReadOnlyBuffer BufferW; /// /// The bias tensor /// private ReadOnlyBuffer BufferB; /// /// The result tensor /// private ReadWriteBuffer BufferY; /// /// A instance to initialize the tensors /// private readonly Random Random = new Random(); /// /// Initial setup for a benchmarking session /// [GlobalSetup] public void Setup() { X = CreateRandomArray(C * N * M); W = CreateRandomArray(M * P); B = CreateRandomArray(P); Y = CreateRandomArray(C * N * P); BufferX = Gpu.Default.AllocateReadOnlyBuffer(X); BufferW = Gpu.Default.AllocateReadOnlyBuffer(W); BufferB = Gpu.Default.AllocateReadOnlyBuffer(B); BufferY = Gpu.Default.AllocateReadWriteBuffer(Y); } /// /// Clears the memory from the current session /// [GlobalCleanup] public void Dispose() { ArrayPool.Shared.Return(X); ArrayPool.Shared.Return(W); ArrayPool.Shared.Return(B); ArrayPool.Shared.Return(Y); BufferX.Dispose(); BufferW.Dispose(); BufferB.Dispose(); BufferY.Dispose(); } /// /// Creates a new array of the specified size /// /// The size of the new random array to create [Pure] private float[] CreateRandomArray(int size) { float[] array = ArrayPool.Shared.Rent(size); ref float r = ref array[0]; for (int i = 0; i < size; i++) { Unsafe.Add(ref r, i) = (float)Random.NextDouble(); } return array; } /// /// Runs a fully connected forward operation on the CPU /// [Benchmark(Baseline = true)] public void Cpu() => Dnn.FullyConnectedForwardCpu(C, N, M, P, X, W, B, Y); /// /// Runs a fully connected forward operation on the GPU /// [Benchmark] public void GpuWithNoTemporaryBuffers() => Dnn.FullyConnectedForwardGpu(C, N, M, P, BufferX, BufferW, BufferB, BufferY); /// /// Runs a fully connected forward operation on the GPU, creating temporary GPU buffers to perform the operations /// [Benchmark] public void GpuWithTemporaryBuffers() { using ReadOnlyBuffer x = Gpu.Default.AllocateReadOnlyBuffer(X); using ReadOnlyBuffer w = Gpu.Default.AllocateReadOnlyBuffer(W); using ReadOnlyBuffer b = Gpu.Default.AllocateReadOnlyBuffer(B); using ReadWriteBuffer y = Gpu.Default.AllocateReadWriteBuffer(Y); Dnn.FullyConnectedForwardGpu(C, N, M, P, x, w, b, y); y.GetData(Y); } } }