using System.Runtime.CompilerServices; using System.Threading.Tasks; namespace ComputeSharp.Benchmark.Blas; /// /// A that contains primitives to run certain operations of a neural network. /// internal static partial class BlasHelpers { /// /// 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 FullyConnectedForwardCpu(int c, int n, int m, int p, float[] x, float[] w, float[] b, float[] y) { void Kernel(int s) { 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++) { int x_offset = i * m; int y_offset = i * p; for (int j = 0; j < p; j++) { float result = 0f; for (int k = 0; k < m; k++) { result += Unsafe.Add(ref rx, x_offset + k) * Unsafe.Add(ref rw, (k * p) + j); } Unsafe.Add(ref ry, y_offset + 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 device to run the computation on. /// 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. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void FullyConnectedForwardGpu( GraphicsDevice device, int c, int n, int m, int p, ReadOnlyBuffer x, ReadOnlyBuffer w, ReadOnlyBuffer b, ReadWriteBuffer y) { device.For(c, n, p, new FullyConnectedForwardKernel(n, m, p, x, w, b, y)); } /// /// Kernel for . /// [AutoConstructor] public readonly partial struct FullyConnectedForwardKernel : IComputeShader { private readonly int n; private readonly int m; private readonly int p; private readonly ReadOnlyBuffer x; private readonly ReadOnlyBuffer w; private readonly ReadOnlyBuffer b; private readonly ReadWriteBuffer y; /// public void Execute() { int x_offset = (ThreadIds.X * this.n * this.p) + (ThreadIds.Y * this.m); float result = 0f; for (int k = 0; k < this.m; k++) { result += this.x[x_offset + k] * this.w[(k * this.p) + ThreadIds.Z]; } this.y[(ThreadIds.X * this.n * this.p) + (ThreadIds.Y * this.p) + ThreadIds.Z] = result + this.b[ThreadIds.Z]; } } }