Added GPU forward pass
このコミットが含まれているのは:
1 親
245a8fecb3
コミット
0caff1f479
2ファイル変更
+42
-3
検索条件に一致するファイルはありません
@@ -9,4 +9,8 @@
|
||||
<PackageReference Include="BenchmarkDotNet" Version="0.11.5" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\ComputeSharp\ComputeSharp.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -1,5 +1,6 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Threading.Tasks;
|
||||
using ComputeSharp.Graphics.Buffers;
|
||||
|
||||
namespace ComputeSharp.Benchmark
|
||||
{
|
||||
@@ -19,11 +20,10 @@ namespace ComputeSharp.Benchmark
|
||||
/// <param name="w">The weights tensor</param>
|
||||
/// <param name="b">The bias tensor</param>
|
||||
/// <param name="y">The result tensor</param>
|
||||
public static void FullyConnectedForward(int c, int n, int m, int p, float[] x, float[] w, float[] b, float[] y)
|
||||
public static void FullyConnectedForwardCpu(int c, int n, int m, int p, float[] x, float[] w, float[] b, float[] y)
|
||||
{
|
||||
void Kernel(int s)
|
||||
{
|
||||
int offset = s * n * m;
|
||||
ref float rx = ref x[s * n * m];
|
||||
ref float rw = ref w[0];
|
||||
ref float rb = ref b[0];
|
||||
@@ -40,12 +40,47 @@ namespace ComputeSharp.Benchmark
|
||||
result += Unsafe.Add(ref rx, i * m + k) * Unsafe.Add(ref rw, k * p + j);
|
||||
}
|
||||
|
||||
Unsafe.Add(ref ry, i * m + j) = result + Unsafe.Add(ref rb, j);
|
||||
Unsafe.Add(ref ry, i * p + 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="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);
|
||||
|
||||
void Kernel(ThreadIds 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];
|
||||
}
|
||||
|
||||
Gpu.Default.For(c, n, p, Kernel);
|
||||
|
||||
y_gpu.GetData(y);
|
||||
}
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする