From be9de0bf8b681bc0cf4d8e1a461901d85b4bc504 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 18 Aug 2019 23:00:06 +0200 Subject: [PATCH] Fixed benchmark checking --- .../FullyConnectedLayerBenchmark.cs | 15 +++++++++++++-- samples/ComputeSharp.Benchmark/Program.cs | 2 +- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/samples/ComputeSharp.Benchmark/FullyConnectedLayerBenchmark.cs b/samples/ComputeSharp.Benchmark/FullyConnectedLayerBenchmark.cs index bf862448..171c9ab1 100644 --- a/samples/ComputeSharp.Benchmark/FullyConnectedLayerBenchmark.cs +++ b/samples/ComputeSharp.Benchmark/FullyConnectedLayerBenchmark.cs @@ -149,9 +149,20 @@ namespace ComputeSharp.Benchmark // Compare the two results ref float r_cpu = ref y_cpu[0]; ref float r_gpu = ref Y[0]; - for (int i = 0; i < Y.Length; i++) - if (MathF.Abs(Unsafe.Add(ref r_cpu, i) - Unsafe.Add(ref r_gpu, i)) > 0.0001f) + + for (int i = 0; i < y_cpu.Length; i++) + { + float x_cpu = Unsafe.Add(ref r_cpu, i); + float x_gpu = Unsafe.Add(ref r_gpu, i); + float absolute = MathF.Abs(x_cpu - x_gpu); + float relative = 1e-1f * MathF.Max(MathF.Abs(x_cpu), MathF.Abs(x_gpu)); + + if (absolute >= 1e-2f && + absolute > MathF.Max(1e-2f, relative)) + { return false; + } + } return true; } diff --git a/samples/ComputeSharp.Benchmark/Program.cs b/samples/ComputeSharp.Benchmark/Program.cs index c7bd0d4f..09d064b0 100644 --- a/samples/ComputeSharp.Benchmark/Program.cs +++ b/samples/ComputeSharp.Benchmark/Program.cs @@ -10,7 +10,7 @@ namespace ComputeSharp.Benchmark using var benchmark = new FullyConnectedLayerBenchmark(); Console.WriteLine("======== WARMUP ========"); - if (benchmark.EnsureImplementationsMatch()) throw new InvalidOperationException("CPU and GPU code don't match"); + if (!benchmark.EnsureImplementationsMatch()) throw new InvalidOperationException("CPU and GPU code don't match"); Console.WriteLine(); Console.WriteLine("======== GPU with temporary buffer ========");