Merge pull request #24 from Sergio0694/feature_benchmark

Added fully connected layer benchmark
このコミットが含まれているのは:
Sergio Pedri authored and GitHub committed 2019-08-17 16:41:08 +02:00
コミット 75d4ee01d3
18ファイル変更
+249 -17

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

差分なしでファイル名を変更。
+82
ファイルの表示
@@ -0,0 +1,82 @@
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using ComputeSharp.Graphics.Buffers;
namespace ComputeSharp.Benchmark
{
/// <summary>
/// A <see langword="class"/> that contains primitives to run certain operations of a neural network
/// </summary>
internal static class Dnn
{
/// <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)
{
Parallel.For(0, c, 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++)
{
for (int j = 0; j < p; j++)
{
float result = 0f;
for (int k = 0; k < m; k++)
{
result += Unsafe.Add(ref rx, i * m + k) * Unsafe.Add(ref rw, k * p + j);
}
Unsafe.Add(ref ry, i * p + j) = result + Unsafe.Add(ref rb, j);
}
}
});
}
/// <summary>
/// Executes the forward pass on a fully connected layer on the GPU
/// </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 FullyConnectedForwardGpu(int c, int n, int m, int p, float[] x, float[] w, float[] b, float[] y)
{
using ReadOnlyBuffer<float> x_gpu = Gpu.Default.AllocateReadOnlyBuffer(x);
using ReadOnlyBuffer<float> w_gpu = Gpu.Default.AllocateReadOnlyBuffer(w);
using ReadOnlyBuffer<float> b_gpu = Gpu.Default.AllocateReadOnlyBuffer(b);
using ReadWriteBuffer<float> y_gpu = Gpu.Default.AllocateReadWriteBuffer(y);
Gpu.Default.For(c, n, p, id =>
{
float result = 0f;
for (int k = 0; k < m; k++)
{
result += x_gpu[(uint)(id.X * n * p + id.Y * m + k)] * w_gpu[(uint)(k * p + id.Z)];
}
y_gpu[(uint)(id.X * n * p + id.Y * p + id.Z)] = result + b_gpu[id.Z];
});
y_gpu.GetData(y);
}
}
}
+95
ファイルの表示
@@ -0,0 +1,95 @@
using System;
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
namespace ComputeSharp.Benchmark
{
/// <summary>
/// A <see langword="class"/> that benchmarks a fully connected layer forward pass on both CPU and GPU
/// </summary>
public class FullyConnectedLayerBenchmark
{
/// <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 readonly float[] X;
/// <summary>
/// The weights tensor
/// </summary>
private readonly float[] W;
/// <summary>
/// The bias tensor
/// </summary>
private readonly float[] B;
/// <summary>
/// The result tensor
/// </summary>
private readonly float[] Y;
/// <summary>
/// A <see cref="System.Random"/> instance to initialize the tensors
/// </summary>
private static readonly Random Random = new Random();
/// <summary>
/// Creates a new <see cref="FullyConnectedLayerBenchmark"/> instance
/// </summary>
public FullyConnectedLayerBenchmark()
{
X = CreateRandomArray(C * N * M);
W = CreateRandomArray(M * P);
B = CreateRandomArray(P);
Y = CreateRandomArray(C * N * P);
}
/// <summary>
/// Creates a new <see langword="float"/> array of the specified size
/// </summary>
/// <param name="size">The size of the new random array to create</param>
[Pure]
private static float[] CreateRandomArray(int size)
{
float[] array = new float[size];
ref float r = ref array[0];
for (int i = 0; i < size; i++)
{
Unsafe.Add(ref r, i) = (float)Random.NextDouble();
}
return array;
}
/// <summary>
/// Runs a fully connected forward operation on the CPU
/// </summary>
public void Cpu() => Dnn.FullyConnectedForwardCpu(C, N, M, P, X, W, B, Y);
/// <summary>
/// Runs a fully connected forward operation on the GPU
/// </summary>
public void Gpu() => Dnn.FullyConnectedForwardGpu(C, N, M, P, X, W, B, Y);
}
}
+33
ファイルの表示
@@ -0,0 +1,33 @@
using System;
using System.Diagnostics;
namespace ComputeSharp.Benchmark
{
class Program
{
static void Main()
{
var benchmark = new FullyConnectedLayerBenchmark();
benchmark.Gpu(); // Warmup
Stopwatch timer = Stopwatch.StartNew();
TimeSpan cpuTime = TimeSpan.Zero, gpuTime = TimeSpan.Zero;
for (int i = 0; i < 10; i++)
{
benchmark.Cpu();
cpuTime += timer.Elapsed;
Console.WriteLine($"CPU: {timer.Elapsed:g}");
timer.Restart();
benchmark.Gpu();
gpuTime += timer.Elapsed;
Console.WriteLine($"GPU: {timer.Elapsed:g}");
timer.Restart();
}
Console.WriteLine("======== AVERAGE ========");
Console.WriteLine($"CPU: {cpuTime / 10:g}");
Console.WriteLine($"GPU: {gpuTime / 10:g}");
}
}
}
+12
ファイルの表示
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\ComputeSharp\ComputeSharp.csproj" />
</ItemGroup>
</Project>
@@ -1,9 +1,8 @@
using System;
using System.Linq;
using ComputeSharp;
using ComputeSharp.Graphics.Buffers;
namespace NetCoreComputeShaderSample
namespace ComputeSharp.Sample
{
public class Program
{
+1 -1
ファイルの表示
@@ -2,5 +2,5 @@
[assembly: InternalsVisibleTo("ComputeSharp")]
[assembly: InternalsVisibleTo("ComputeSharp.Shaders")]
[assembly: InternalsVisibleTo("ComputeSharp.NetCore.Tests.Internals")]
[assembly: InternalsVisibleTo("ComputeSharp.Tests.Internals")]
+14 -3
ファイルの表示
@@ -9,7 +9,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.Shaders", "Com
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp", "ComputeSharp\ComputeSharp.csproj", "{314EB353-E7E3-4146-8111-685CC83EC1DE}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetCoreComputeShaderSample", "..\samples\NetCoreComputeShaderSample\NetCoreComputeShaderSample.csproj", "{7A48A85D-8866-4DED-A404-653ED09BF396}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.Sample", "..\samples\ComputeSharp.Sample\ComputeSharp.Sample.csproj", "{7A48A85D-8866-4DED-A404-653ED09BF396}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "_SolutionItems", "_SolutionItems", "{1A9C4108-F61A-4ABE-B5CF-F524521EAE2D}"
ProjectSection(SolutionItems) = preProject
@@ -20,9 +20,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Samples", "Samples", "{0ED8
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{F8EFBB27-4EE2-4463-A75B-7EFDFB55D0F7}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.NetCore.Tests", "..\tests\ComputeSharp.NetCore.Tests\ComputeSharp.NetCore.Tests.csproj", "{E91F4D24-922A-4921-A8D7-9C45A143C45D}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.Tests", "..\tests\ComputeSharp.Tests\ComputeSharp.Tests.csproj", "{E91F4D24-922A-4921-A8D7-9C45A143C45D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ComputeSharp.NetCore.Tests.Internals", "..\tests\ComputeSharp.NetCore.Tests.Internals\ComputeSharp.NetCore.Tests.Internals.csproj", "{E889E6D6-A814-4950-AAC1-68F1ECFC5BD6}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.Tests.Internals", "..\tests\ComputeSharp.Tests.Internals\ComputeSharp.Tests.Internals.csproj", "{E889E6D6-A814-4950-AAC1-68F1ECFC5BD6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ComputeSharp.Benchmark", "..\samples\ComputeSharp.Benchmark\ComputeSharp.Benchmark.csproj", "{34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -80,6 +82,14 @@ Global
{E889E6D6-A814-4950-AAC1-68F1ECFC5BD6}.Release|Any CPU.Build.0 = Release|Any CPU
{E889E6D6-A814-4950-AAC1-68F1ECFC5BD6}.Release|x64.ActiveCfg = Release|Any CPU
{E889E6D6-A814-4950-AAC1-68F1ECFC5BD6}.Release|x64.Build.0 = Release|Any CPU
{34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF}.Debug|x64.ActiveCfg = Debug|Any CPU
{34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF}.Debug|x64.Build.0 = Debug|Any CPU
{34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF}.Release|Any CPU.Build.0 = Release|Any CPU
{34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF}.Release|x64.ActiveCfg = Release|Any CPU
{34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@@ -88,6 +98,7 @@ Global
{7A48A85D-8866-4DED-A404-653ED09BF396} = {0ED8F632-5E17-46BE-8CC3-B14A82D4AEB1}
{E91F4D24-922A-4921-A8D7-9C45A143C45D} = {F8EFBB27-4EE2-4463-A75B-7EFDFB55D0F7}
{E889E6D6-A814-4950-AAC1-68F1ECFC5BD6} = {F8EFBB27-4EE2-4463-A75B-7EFDFB55D0F7}
{34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF} = {0ED8F632-5E17-46BE-8CC3-B14A82D4AEB1}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {4664C5E3-0340-4E22-BCFD-98AAEDF5F2DC}
@@ -5,7 +5,7 @@ using ComputeSharp.Graphics.Buffers;
using ComputeSharp.Graphics.Buffers.Extensions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace ComputeSharp.NetCore.Tests.Internals
namespace ComputeSharp.Tests.Internals
{
[TestClass]
[TestCategory("Buffers")]
@@ -1,10 +1,10 @@
using System;
using System.Linq;
using ComputeSharp.Graphics.Buffers;
using ComputeSharp.NetCore.Tests.Extensions;
using ComputeSharp.Tests.Extensions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace ComputeSharp.NetCore.Tests
namespace ComputeSharp.Tests
{
[TestClass]
[TestCategory("Buffers")]
差分なしでファイル名を変更。
@@ -2,7 +2,7 @@
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
namespace ComputeSharp.NetCore.Tests.Extensions
namespace ComputeSharp.Tests.Extensions
{
/// <summary>
/// An helper <see langword="class"/> with methods to process fixed-size buffers
@@ -2,10 +2,10 @@
using System.Linq;
using ComputeSharp.Graphics;
using ComputeSharp.Graphics.Buffers;
using ComputeSharp.NetCore.Tests.Extensions;
using ComputeSharp.Tests.Extensions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace ComputeSharp.NetCore.Tests
namespace ComputeSharp.Tests
{
[TestClass]
[TestCategory("Initialization")]
@@ -1,7 +1,7 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace ComputeSharp.NetCore.Tests
namespace ComputeSharp.Tests
{
[TestClass]
[TestCategory("InvalidShader")]
差分なしでファイル名を変更。
@@ -2,10 +2,10 @@
using System.Linq;
using System.Numerics;
using ComputeSharp.Graphics.Buffers;
using ComputeSharp.NetCore.Tests.Extensions;
using ComputeSharp.Tests.Extensions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace ComputeSharp.NetCore.Tests
namespace ComputeSharp.Tests
{
[TestClass]
[TestCategory("ShaderClosure")]
@@ -1,10 +1,10 @@
using System;
using System.Linq;
using ComputeSharp.Graphics.Buffers;
using ComputeSharp.NetCore.Tests.Extensions;
using ComputeSharp.Tests.Extensions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace ComputeSharp.NetCore.Tests
namespace ComputeSharp.Tests
{
[TestClass]
[TestCategory("ShaderDispatch")]