using System; using System.Buffers; using BenchmarkDotNet.Attributes; using BenchmarkDotNet.Engines; namespace ComputeSharp.Benchmark.Blas; /// /// A that benchmarks the APIs in the class, on both CPU and GPU. /// [SimpleJob(RunStrategy.Monitoring)] public class BlasBenchmark : 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 (GPU). /// private ReadOnlyBuffer? BufferX; /// /// The weights tensor (GPU). /// private ReadOnlyBuffer? BufferW; /// /// The bias tensor (GPU). /// private ReadOnlyBuffer? BufferB; /// /// The result tensor (GPU). /// 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() { // Creates a new random normalized float[] array of a given size float[] CreateRandomArray(int size) { float[] array = ArrayPool.Shared.Rent(size); foreach (ref float x in array.AsSpan()) { x = (float)Random.NextDouble(); } return array; } X = CreateRandomArray(C * N * M); W = CreateRandomArray(M * P); B = CreateRandomArray(P); Y = CreateRandomArray(C * N * P); BufferX = GraphicsDevice.Default.AllocateReadOnlyBuffer(X); BufferW = GraphicsDevice.Default.AllocateReadOnlyBuffer(W); BufferB = GraphicsDevice.Default.AllocateReadOnlyBuffer(B); BufferY = GraphicsDevice.Default.AllocateReadWriteBuffer(Y); Cpu(); GpuWithNoTemporaryBuffers(); GpuWithTemporaryBuffers(); } /// [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(); } /// /// Runs a fully connected forward operation on the CPU. /// [Benchmark(Baseline = true)] public void Cpu() { BlasHelpers.FullyConnectedForwardCpu(C, N, M, P, X!, W!, B!, Y!); } /// /// Runs a fully connected forward operation on the GPU. /// [Benchmark] public void GpuWithNoTemporaryBuffers() { BlasHelpers.FullyConnectedForwardGpu(GraphicsDevice.Default, 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 = GraphicsDevice.Default.AllocateReadOnlyBuffer(X!); using ReadOnlyBuffer w = GraphicsDevice.Default.AllocateReadOnlyBuffer(W!); using ReadOnlyBuffer b = GraphicsDevice.Default.AllocateReadOnlyBuffer(B!); using ReadWriteBuffer y = GraphicsDevice.Default.AllocateReadWriteBuffer(Y!.Length); BlasHelpers.FullyConnectedForwardGpu(GraphicsDevice.Default, C, N, M, P, x, w, b, y); y.CopyTo(Y); } }