From 59fd672f54d171577857fe75ed9223b91d7902d1 Mon Sep 17 00:00:00 2001 From: Vaibhav Thakkar Date: Mon, 28 Aug 2023 21:11:52 +0530 Subject: [PATCH] Add benchmark for comparing vector mode with existing methods --- benchmark/BenchmarkedFunctions.h | 8 ++ benchmark/CMakeLists.txt | 1 + benchmark/VectorModeComparison.cpp | 122 +++++++++++++++++++++++++++++ 3 files changed, 131 insertions(+) create mode 100644 benchmark/VectorModeComparison.cpp diff --git a/benchmark/BenchmarkedFunctions.h b/benchmark/BenchmarkedFunctions.h index 0c8a8ff1a..138ff6ca4 100644 --- a/benchmark/BenchmarkedFunctions.h +++ b/benchmark/BenchmarkedFunctions.h @@ -39,3 +39,11 @@ inline double product(double p[], int n) { } return prod; } + +///\returns the weighted sum of the elements in \p +inline double weightedSum(double p[], double w[], int n) { + double sum = 0; + for (int i = 0; i < n; i++) + sum += p[i] * w[i]; + return sum; +} \ No newline at end of file diff --git a/benchmark/CMakeLists.txt b/benchmark/CMakeLists.txt index d8553179e..2d22a6ea4 100644 --- a/benchmark/CMakeLists.txt +++ b/benchmark/CMakeLists.txt @@ -8,6 +8,7 @@ CB_ADD_GBENCHMARK(Simple Simple.cpp) CB_ADD_GBENCHMARK(AlgorithmicComplexity AlgorithmicComplexity.cpp) CB_ADD_GBENCHMARK(EnzymeCladComparison EnzymeCladComparison.cpp) CB_ADD_GBENCHMARK(MemoryComplexity MemoryComplexity.cpp) +CB_ADD_GBENCHMARK(VectorModeComparison VectorModeComparison.cpp) set (CLAD_BENCHMARK_DEPS clad) get_property(_benchmark_names DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY TESTS) diff --git a/benchmark/VectorModeComparison.cpp b/benchmark/VectorModeComparison.cpp new file mode 100644 index 000000000..3618b7d63 --- /dev/null +++ b/benchmark/VectorModeComparison.cpp @@ -0,0 +1,122 @@ +#include "benchmark/benchmark.h" + +#include "clad/Differentiator/Differentiator.h" + +#include "BenchmarkedFunctions.h" + +// Benchmark forward mode for weighted sum. +static void BM_ForwardModeWeightedSum(benchmark::State& state) { + auto dp0 = clad::differentiate(weightedSum, "p[0]"); + auto dp1 = clad::differentiate(weightedSum, "p[1]"); + auto dp2 = clad::differentiate(weightedSum, "p[2]"); + auto dp3 = clad::differentiate(weightedSum, "p[3]"); + auto dp4 = clad::differentiate(weightedSum, "p[4]"); + + auto dw0 = clad::differentiate(weightedSum, "w[0]"); + auto dw1 = clad::differentiate(weightedSum, "w[1]"); + auto dw2 = clad::differentiate(weightedSum, "w[2]"); + auto dw3 = clad::differentiate(weightedSum, "w[3]"); + auto dw4 = clad::differentiate(weightedSum, "w[4]"); + + constexpr int n = 5; + double inputs[n]; + double weights[n]; + for (int i = 0; i < n; ++i) { + inputs[i] = i + 1; + weights[i] = 1.0 / (double)(i + 1); + } + + double sum = 0; + for (auto _ : state) { + benchmark::DoNotOptimize( + sum += + dp0.execute(inputs, weights, n) + dp1.execute(inputs, weights, n) + + dp2.execute(inputs, weights, n) + dp3.execute(inputs, weights, n) + + dp4.execute(inputs, weights, n) + dw0.execute(inputs, weights, n) + + dw1.execute(inputs, weights, n) + dw2.execute(inputs, weights, n) + + dw3.execute(inputs, weights, n) + dw4.execute(inputs, weights, n)); + } +} +BENCHMARK(BM_ForwardModeWeightedSum); + +// Benchmark reverse mode for weighted sum. +static void BM_ReverseModeWeightedSum(benchmark::State& state) { + auto grad = clad::gradient(weightedSum, "p, w"); + constexpr int n = 5; + + double inputs[n]; + double weights[n]; + for (int i = 0; i < n; ++i) { + inputs[i] = i + 1; + weights[i] = 1.0 / (double)(i + 1); + } + + double dinp[n]; + double dweights[n]; + clad::array_ref dinp_ref(dinp, n); + clad::array_ref dweights_ref(dweights, n); + + double sum = 0; + for (auto _ : state) { + grad.execute(inputs, weights, n, dinp_ref, dweights_ref); + for (int i = 0; i < n; ++i) + sum += dinp[i] + dweights[i]; + } +} +BENCHMARK(BM_ReverseModeWeightedSum); + +// Benchmark enzyme's reverse mode for weighted sum. +static void BM_EnzymeReverseModeWeightedSum(benchmark::State& state) { + auto grad = clad::gradient(weightedSum, "p, w"); + constexpr int n = 5; + + double inputs[n]; + double weights[n]; + for (int i = 0; i < n; ++i) { + inputs[i] = i + 1; + weights[i] = 1.0 / (double)(i + 1); + } + + double dinp[n]; + double dweights[n]; + clad::array_ref dinp_ref(dinp, n); + clad::array_ref dweights_ref(dweights, n); + + double sum = 0; + for (auto _ : state) { + grad.execute(inputs, weights, n, dinp_ref, dweights_ref); + for (int i = 0; i < n; ++i) + sum += dinp[i] + dweights[i]; + } +} +BENCHMARK(BM_EnzymeReverseModeWeightedSum); + +// Benchmark vector forward mode for weighted sum. +static void BM_VectorForwardModeWeightedSum(benchmark::State& state) { + auto vm_grad = + clad::differentiate(weightedSum, "p, w"); + constexpr int n = 5; + + double inputs[n]; + double weights[n]; + for (int i = 0; i < n; ++i) { + inputs[i] = i + 1; + weights[i] = 1.0 / (double)(i + 1); + } + + double dinp[n]; + double dweights[n]; + clad::array_ref dinp_ref(dinp, n); + clad::array_ref dweights_ref(dweights, n); + + double sum = 0; + for (auto _ : state) { + vm_grad.execute(inputs, weights, n, dinp_ref, dweights_ref); + for (int i = 0; i < n; ++i) + sum += dinp[i] + dweights[i]; + } +} +BENCHMARK(BM_VectorForwardModeWeightedSum); + +// Define our main. +BENCHMARK_MAIN();