using System; using System.Buffers; using BenchmarkDotNet.Attributes; using BenchmarkDotNet.Engines; #pragma warning disable CA1063 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(); /// /// 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)this.random.NextDouble(); } return array; } this.x = CreateRandomArray(C * N * M); this.w = CreateRandomArray(M * P); this.b = CreateRandomArray(P); this.y = CreateRandomArray(C * N * P); this.bufferX = GraphicsDevice.GetDefault().AllocateReadOnlyBuffer(this.x); this.bufferW = GraphicsDevice.GetDefault().AllocateReadOnlyBuffer(this.w); this.bufferB = GraphicsDevice.GetDefault().AllocateReadOnlyBuffer(this.b); this.bufferY = GraphicsDevice.GetDefault().AllocateReadWriteBuffer(this.y); Cpu(); GpuWithNoTemporaryBuffers(); GpuWithTemporaryBuffers(); } /// [GlobalCleanup] public void Dispose() { ArrayPool.Shared.Return(this.x!); ArrayPool.Shared.Return(this.w!); ArrayPool.Shared.Return(this.b!); ArrayPool.Shared.Return(this.y!); this.bufferX!.Dispose(); this.bufferW!.Dispose(); this.bufferB!.Dispose(); this.bufferY!.Dispose(); } /// /// Runs a fully connected forward operation on the CPU. /// [Benchmark(Baseline = true)] public void Cpu() { BlasHelpers.FullyConnectedForwardCpu(C, N, M, P, this.x!, this.w!, this.b!, this.y!); } /// /// Runs a fully connected forward operation on the GPU. /// [Benchmark] public void GpuWithNoTemporaryBuffers() { BlasHelpers.FullyConnectedForwardGpu(GraphicsDevice.GetDefault(), C, N, M, P, this.bufferX!, this.bufferW!, this.bufferB!, this.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.GetDefault().AllocateReadOnlyBuffer(this.x!); using ReadOnlyBuffer w = GraphicsDevice.GetDefault().AllocateReadOnlyBuffer(this.w!); using ReadOnlyBuffer b = GraphicsDevice.GetDefault().AllocateReadOnlyBuffer(this.b!); using ReadWriteBuffer y = GraphicsDevice.GetDefault().AllocateReadWriteBuffer(this.y!.Length); BlasHelpers.FullyConnectedForwardGpu(GraphicsDevice.GetDefault(), C, N, M, P, x, w, b, y); y.CopyTo(this.y); } }