Refactored code to use file scoped using declarations

このコミットが含まれているのは:
Sergio Pedri committed 2021-10-30 15:11:43 +02:00
1 親 52ec5a79b0
コミット d5e3114f7c
275ファイル変更
+91970 -92271

検索条件に一致するファイルはありません

+138 -139
ファイルの表示
@@ -3,160 +3,159 @@ using System.Buffers;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Engines;
namespace ComputeSharp.Benchmark.Blas
namespace ComputeSharp.Benchmark.Blas;
/// <summary>
/// A <see langword="class"/> that benchmarks the APIs in the <see cref="BlasHelpers"/> class, on both CPU and GPU.
/// </summary>
[SimpleJob(RunStrategy.Monitoring)]
public class BlasBenchmark : IDisposable
{
/// <summary>
/// A <see langword="class"/> that benchmarks the APIs in the <see cref="BlasHelpers"/> class, on both CPU and GPU.
/// The number of samples.
/// </summary>
[SimpleJob(RunStrategy.Monitoring)]
public class BlasBenchmark : IDisposable
private const int C = 128;
/// <summary>
/// The nummber of rows in the <see cref="X"/> matrix.
/// </summary>
private const int N = 512;
/// <summary>
/// The number of columns in the <see cref="X"/> matrix (same as the number of rows in the <see cref="W"/> matrix)
/// </summary>
private const int M = 512;
/// <summary>
/// The number of columns in the <see cref="W"/> matrix.
/// </summary>
private const int P = 256;
/// <summary>
/// The input tensor.
/// </summary>
private float[]? X;
/// <summary>
/// The weights tensor.
/// </summary>
private float[]? W;
/// <summary>
/// The bias tensor.
/// </summary>
private float[]? B;
/// <summary>
/// The result tensor.
/// </summary>
private float[]? Y;
/// <summary>
/// The input tensor (GPU).
/// </summary>
private ReadOnlyBuffer<float>? BufferX;
/// <summary>
/// The weights tensor (GPU).
/// </summary>
private ReadOnlyBuffer<float>? BufferW;
/// <summary>
/// The bias tensor (GPU).
/// </summary>
private ReadOnlyBuffer<float>? BufferB;
/// <summary>
/// The result tensor (GPU).
/// </summary>
private ReadWriteBuffer<float>? BufferY;
/// <summary>
/// A <see cref="System.Random"/> instance to initialize the tensors.
/// </summary>
private readonly Random Random = new Random();
/// <summary>
/// Initial setup for a benchmarking session.
/// </summary>
[GlobalSetup]
public void Setup()
{
/// <summary>
/// The number of samples.
/// </summary>
private const int C = 128;
/// <summary>
/// The nummber of rows in the <see cref="X"/> matrix.
/// </summary>
private const int N = 512;
/// <summary>
/// The number of columns in the <see cref="X"/> matrix (same as the number of rows in the <see cref="W"/> matrix)
/// </summary>
private const int M = 512;
/// <summary>
/// The number of columns in the <see cref="W"/> matrix.
/// </summary>
private const int P = 256;
/// <summary>
/// The input tensor.
/// </summary>
private float[]? X;
/// <summary>
/// The weights tensor.
/// </summary>
private float[]? W;
/// <summary>
/// The bias tensor.
/// </summary>
private float[]? B;
/// <summary>
/// The result tensor.
/// </summary>
private float[]? Y;
/// <summary>
/// The input tensor (GPU).
/// </summary>
private ReadOnlyBuffer<float>? BufferX;
/// <summary>
/// The weights tensor (GPU).
/// </summary>
private ReadOnlyBuffer<float>? BufferW;
/// <summary>
/// The bias tensor (GPU).
/// </summary>
private ReadOnlyBuffer<float>? BufferB;
/// <summary>
/// The result tensor (GPU).
/// </summary>
private ReadWriteBuffer<float>? BufferY;
/// <summary>
/// A <see cref="System.Random"/> instance to initialize the tensors.
/// </summary>
private readonly Random Random = new Random();
/// <summary>
/// Initial setup for a benchmarking session.
/// </summary>
[GlobalSetup]
public void Setup()
// Creates a new random normalized float[] array of a given size
float[] CreateRandomArray(int size)
{
// Creates a new random normalized float[] array of a given size
float[] CreateRandomArray(int size)
float[] array = ArrayPool<float>.Shared.Rent(size);
foreach (ref float x in array.AsSpan())
{
float[] array = ArrayPool<float>.Shared.Rent(size);
foreach (ref float x in array.AsSpan())
{
x = (float)Random.NextDouble();
}
return array;
x = (float)Random.NextDouble();
}
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);
Cpu();
GpuWithNoTemporaryBuffers();
GpuWithTemporaryBuffers();
return array;
}
/// <inheritdoc/>
[GlobalCleanup]
public void Dispose()
{
ArrayPool<float>.Shared.Return(X!);
ArrayPool<float>.Shared.Return(W!);
ArrayPool<float>.Shared.Return(B!);
ArrayPool<float>.Shared.Return(Y!);
X = CreateRandomArray(C * N * M);
W = CreateRandomArray(M * P);
B = CreateRandomArray(P);
Y = CreateRandomArray(C * N * P);
BufferX!.Dispose();
BufferW!.Dispose();
BufferB!.Dispose();
BufferY!.Dispose();
}
BufferX = Gpu.Default.AllocateReadOnlyBuffer(X);
BufferW = Gpu.Default.AllocateReadOnlyBuffer(W);
BufferB = Gpu.Default.AllocateReadOnlyBuffer(B);
BufferY = Gpu.Default.AllocateReadWriteBuffer(Y);
/// <summary>
/// Runs a fully connected forward operation on the CPU.
/// </summary>
[Benchmark(Baseline = true)]
public void Cpu()
{
BlasHelpers.FullyConnectedForwardCpu(C, N, M, P, X!, W!, B!, Y!);
}
Cpu();
GpuWithNoTemporaryBuffers();
GpuWithTemporaryBuffers();
}
/// <summary>
/// Runs a fully connected forward operation on the GPU.
/// </summary>
[Benchmark]
public void GpuWithNoTemporaryBuffers()
{
BlasHelpers.FullyConnectedForwardGpu(Gpu.Default, C, N, M, P, BufferX!, BufferW!, BufferB!, BufferY!);
}
/// <inheritdoc/>
[GlobalCleanup]
public void Dispose()
{
ArrayPool<float>.Shared.Return(X!);
ArrayPool<float>.Shared.Return(W!);
ArrayPool<float>.Shared.Return(B!);
ArrayPool<float>.Shared.Return(Y!);
/// <summary>
/// Runs a fully connected forward operation on the GPU, creating temporary GPU buffers to perform the operations.
/// </summary>
[Benchmark]
public void GpuWithTemporaryBuffers()
{
using ReadOnlyBuffer<float> x = Gpu.Default.AllocateReadOnlyBuffer(X!);
using ReadOnlyBuffer<float> w = Gpu.Default.AllocateReadOnlyBuffer(W!);
using ReadOnlyBuffer<float> b = Gpu.Default.AllocateReadOnlyBuffer(B!);
using ReadWriteBuffer<float> y = Gpu.Default.AllocateReadWriteBuffer<float>(Y!.Length);
BufferX!.Dispose();
BufferW!.Dispose();
BufferB!.Dispose();
BufferY!.Dispose();
}
BlasHelpers.FullyConnectedForwardGpu(Gpu.Default, C, N, M, P, x, w, b, y);
/// <summary>
/// Runs a fully connected forward operation on the CPU.
/// </summary>
[Benchmark(Baseline = true)]
public void Cpu()
{
BlasHelpers.FullyConnectedForwardCpu(C, N, M, P, X!, W!, B!, Y!);
}
y.CopyTo(Y);
}
/// <summary>
/// Runs a fully connected forward operation on the GPU.
/// </summary>
[Benchmark]
public void GpuWithNoTemporaryBuffers()
{
BlasHelpers.FullyConnectedForwardGpu(Gpu.Default, C, N, M, P, BufferX!, BufferW!, BufferB!, BufferY!);
}
/// <summary>
/// Runs a fully connected forward operation on the GPU, creating temporary GPU buffers to perform the operations.
/// </summary>
[Benchmark]
public void GpuWithTemporaryBuffers()
{
using ReadOnlyBuffer<float> x = Gpu.Default.AllocateReadOnlyBuffer(X!);
using ReadOnlyBuffer<float> w = Gpu.Default.AllocateReadOnlyBuffer(W!);
using ReadOnlyBuffer<float> b = Gpu.Default.AllocateReadOnlyBuffer(B!);
using ReadWriteBuffer<float> y = Gpu.Default.AllocateReadWriteBuffer<float>(Y!.Length);
BlasHelpers.FullyConnectedForwardGpu(Gpu.Default, C, N, M, P, x, w, b, y);
y.CopyTo(Y);
}
}
+83 -84
ファイルの表示
@@ -1,109 +1,108 @@
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
namespace ComputeSharp.Benchmark.Blas
namespace ComputeSharp.Benchmark.Blas;
/// <summary>
/// A <see langword="class"/> that contains primitives to run certain operations of a neural network.
/// </summary>
internal static partial class BlasHelpers
{
/// <summary>
/// A <see langword="class"/> that contains primitives to run certain operations of a neural network.
/// Executes the forward pass on a fully connected layer on the CPU.
/// </summary>
internal static partial class BlasHelpers
/// <param name="c">The number of samples in the input tensor.</param>
/// <param name="n">The number of rows in the input matrix.</param>
/// <param name="m">The number of columns in the input matrix.</param>
/// <param name="p">The number of columns in the output matrix.</param>
/// <param name="x">The input tensor.</param>
/// <param name="w">The weights tensor.</param>
/// <param name="b">The bias tensor.</param>
/// <param name="y">The result tensor.</param>
public static void FullyConnectedForwardCpu(int c, int n, int m, int p, float[] x, float[] w, float[] b, float[] y)
{
/// <summary>
/// Executes the forward pass on a fully connected layer on the CPU.
/// </summary>
/// <param name="c">The number of samples in the input tensor.</param>
/// <param name="n">The number of rows in the input matrix.</param>
/// <param name="m">The number of columns in the input matrix.</param>
/// <param name="p">The number of columns in the output matrix.</param>
/// <param name="x">The input tensor.</param>
/// <param name="w">The weights tensor.</param>
/// <param name="b">The bias tensor.</param>
/// <param name="y">The result tensor.</param>
public static void FullyConnectedForwardCpu(int c, int n, int m, int p, float[] x, float[] w, float[] b, float[] y)
void Kernel(int s)
{
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++)
{
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];
int x_offset = i * m;
int y_offset = i * p;
for (int i = 0; i < n; i++)
for (int j = 0; j < p; j++)
{
int x_offset = i * m;
int y_offset = i * p;
float result = 0f;
for (int j = 0; j < p; j++)
for (int k = 0; k < m; k++)
{
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);
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);
}
/// <summary>
/// Executes the forward pass on a fully connected layer on the GPU.
/// </summary>
/// <param name="device">The device to run the computation on.</param>
/// <param name="c">The number of samples in the input tensor.</param>
/// <param name="n">The number of rows in the input matrix.</param>
/// <param name="m">The number of columns in the input matrix.</param>
/// <param name="p">The number of columns in the output matrix.</param>
/// <param name="x">The input tensor.</param>
/// <param name="w">The weights tensor.</param>
/// <param name="b">The bias tensor.</param>
/// <param name="y">The result tensor.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void FullyConnectedForwardGpu(
GraphicsDevice device,
int c,
int n,
int m,
int p,
ReadOnlyBuffer<float> x,
ReadOnlyBuffer<float> w,
ReadOnlyBuffer<float> b,
ReadWriteBuffer<float> y)
{
device.For(c, n, p, new FullyConnectedForwardKernel(n, m, p, x, w, b, y));
}
Parallel.For(0, c, Kernel);
}
/// <summary>
/// Kernel for <see cref="FullyConnectedForwardGpu"/>.
/// </summary>
[AutoConstructor]
public readonly partial struct FullyConnectedForwardKernel : IComputeShader
{
public readonly int n;
public readonly int m;
public readonly int p;
public readonly ReadOnlyBuffer<float> x;
public readonly ReadOnlyBuffer<float> w;
public readonly ReadOnlyBuffer<float> b;
public readonly ReadWriteBuffer<float> y;
/// <summary>
/// Executes the forward pass on a fully connected layer on the GPU.
/// </summary>
/// <param name="device">The device to run the computation on.</param>
/// <param name="c">The number of samples in the input tensor.</param>
/// <param name="n">The number of rows in the input matrix.</param>
/// <param name="m">The number of columns in the input matrix.</param>
/// <param name="p">The number of columns in the output matrix.</param>
/// <param name="x">The input tensor.</param>
/// <param name="w">The weights tensor.</param>
/// <param name="b">The bias tensor.</param>
/// <param name="y">The result tensor.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void FullyConnectedForwardGpu(
GraphicsDevice device,
int c,
int n,
int m,
int p,
ReadOnlyBuffer<float> x,
ReadOnlyBuffer<float> w,
ReadOnlyBuffer<float> b,
ReadWriteBuffer<float> y)
{
device.For(c, n, p, new FullyConnectedForwardKernel(n, m, p, x, w, b, y));
}
/// <inheritdoc/>
public void Execute()
/// <summary>
/// Kernel for <see cref="FullyConnectedForwardGpu"/>.
/// </summary>
[AutoConstructor]
public readonly partial struct FullyConnectedForwardKernel : IComputeShader
{
public readonly int n;
public readonly int m;
public readonly int p;
public readonly ReadOnlyBuffer<float> x;
public readonly ReadOnlyBuffer<float> w;
public readonly ReadOnlyBuffer<float> b;
public readonly ReadWriteBuffer<float> y;
/// <inheritdoc/>
public void Execute()
{
int x_offset = ThreadIds.X * n * p + ThreadIds.Y * m;
float result = 0f;
for (int k = 0; k < m; k++)
{
int x_offset = ThreadIds.X * n * p + ThreadIds.Y * m;
float result = 0f;
for (int k = 0; k < m; k++)
{
result += x[x_offset + k] * w[k * p + ThreadIds.Z];
}
y[ThreadIds.X * n * p + ThreadIds.Y * p + ThreadIds.Z] = result + b[ThreadIds.Z];
result += x[x_offset + k] * w[k * p + ThreadIds.Z];
}
y[ThreadIds.X * n * p + ThreadIds.Y * p + ThreadIds.Z] = result + b[ThreadIds.Z];
}
}
}
+65 -66
ファイルの表示
@@ -10,82 +10,81 @@ using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Processing.Processors.Convolution;
using ImageSharpRgba32 = SixLabors.ImageSharp.PixelFormats.Rgba32;
namespace ComputeSharp.Benchmark.Imaging
namespace ComputeSharp.Benchmark.Imaging;
/// <summary>
/// A <see langword="class"/> that benchmarks the image processors against the reference in ImageSharp.
/// </summary>
[SimpleJob(RunStrategy.Monitoring)]
[CategoriesColumn]
[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory)]
public class ImagingBenchmark : IDisposable
{
/// <summary>
/// A <see langword="class"/> that benchmarks the image processors against the reference in ImageSharp.
/// The loaded image.
/// </summary>
[SimpleJob(RunStrategy.Monitoring)]
[CategoriesColumn]
[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory)]
public class ImagingBenchmark : IDisposable
private Image<ImageSharpRgba32>? image;
/// <summary>
/// Initial setup for a benchmarking session.
/// </summary>
[GlobalSetup]
public void Setup()
{
/// <summary>
/// The loaded image.
/// </summary>
private Image<ImageSharpRgba32>? image;
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg");
/// <summary>
/// Initial setup for a benchmarking session.
/// </summary>
[GlobalSetup]
public void Setup()
{
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg");
this.image = Image.Load<ImageSharpRgba32>(path);
this.image = Image.Load<ImageSharpRgba32>(path);
Bokeh_Cpu();
Bokeh_Gpu();
Gaussian_Cpu();
Gaussian_Gpu();
}
Bokeh_Cpu();
Bokeh_Gpu();
Gaussian_Cpu();
Gaussian_Gpu();
}
/// <inheritdoc/>
[GlobalCleanup]
public void Dispose()
{
this.image!.Dispose();
}
/// <inheritdoc/>
[GlobalCleanup]
public void Dispose()
{
this.image!.Dispose();
}
/// <summary>
/// Runs a bokeh blur effect on the CPU.
/// </summary>
[Benchmark(Baseline = true)]
[BenchmarkCategory("BOKEH")]
public void Bokeh_Cpu()
{
this.image!.Mutate(c => c.ApplyProcessor(new BokehBlurProcessor(80, 2, 3)));
}
/// <summary>
/// Runs a bokeh blur effect on the CPU.
/// </summary>
[Benchmark(Baseline = true)]
[BenchmarkCategory("BOKEH")]
public void Bokeh_Cpu()
{
this.image!.Mutate(c => c.ApplyProcessor(new BokehBlurProcessor(80, 2, 3)));
}
/// <summary>
/// Runs a bokeh blur effect on the GPU.
/// </summary>
[Benchmark]
[BenchmarkCategory("BOKEH")]
public void Bokeh_Gpu()
{
this.image!.Mutate(c => c.ApplyProcessor(new HlslBokehBlurProcessor(80, 2)));
}
/// <summary>
/// Runs a bokeh blur effect on the GPU.
/// </summary>
[Benchmark]
[BenchmarkCategory("BOKEH")]
public void Bokeh_Gpu()
{
this.image!.Mutate(c => c.ApplyProcessor(new HlslBokehBlurProcessor(80, 2)));
}
/// <summary>
/// Runs a gaussian blur effect on the CPU.
/// </summary>
[Benchmark(Baseline = true)]
[BenchmarkCategory("GAUSSIAN")]
public void Gaussian_Cpu()
{
this.image!.Mutate(c => c.ApplyProcessor(new GaussianBlurProcessor(80)));
}
/// <summary>
/// Runs a gaussian blur effect on the CPU.
/// </summary>
[Benchmark(Baseline = true)]
[BenchmarkCategory("GAUSSIAN")]
public void Gaussian_Cpu()
{
this.image!.Mutate(c => c.ApplyProcessor(new GaussianBlurProcessor(80)));
}
/// <summary>
/// Runs a gaussian blur effect on the GPU.
/// </summary>
[Benchmark]
[BenchmarkCategory("GAUSSIAN")]
public void Gaussian_Gpu()
{
this.image!.Mutate(c => c.ApplyProcessor(new HlslGaussianBlurProcessor(80)));
}
/// <summary>
/// Runs a gaussian blur effect on the GPU.
/// </summary>
[Benchmark]
[BenchmarkCategory("GAUSSIAN")]
public void Gaussian_Gpu()
{
this.image!.Mutate(c => c.ApplyProcessor(new HlslGaussianBlurProcessor(80)));
}
}