Skip to content

Commit

Permalink
Add benchmark for hessians
Browse files Browse the repository at this point in the history
  • Loading branch information
vaithak committed Jun 20, 2024
1 parent 7237986 commit edf7593
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ if (CLAD_ENABLE_ENZYME_BACKEND)
endif(CLAD_ENABLE_ENZYME_BACKEND)
CB_ADD_GBENCHMARK(VectorModeComparison VectorModeComparison.cpp)
CB_ADD_GBENCHMARK(MemoryComplexity MemoryComplexity.cpp)
CB_ADD_GBENCHMARK(Hessians Hessians.cpp)

set (CLAD_BENCHMARK_DEPS clad)
get_property(_benchmark_names DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY TESTS)
Expand Down
47 changes: 47 additions & 0 deletions benchmark/Hessians.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include "benchmark/benchmark.h"

#include "clad/Differentiator/Differentiator.h"

#include "BenchmarkedFunctions.h"

// Benchmark Hessian diagonal sum computation, by computing the
// entire computation.
static void BM_HessianCompleteComputation(benchmark::State& state) {
auto dfdx2 = clad::hessian(weightedSum, "p[0:1],w[0:1]");
double p[] = {1, 2};
double w[] = {3, 4};
unsigned long long sum = 0;
double hessianMatrix[16] = {};
for (unsigned i = 0; i < 16; i++)
hessianMatrix[i] = 0.0;
for (auto _ : state) {
dfdx2.execute(p, w, 3, hessianMatrix);
for (int i = 0; i < 4; i++)
// Sum the diagonal of the Hessian matrix.
benchmark::DoNotOptimize(sum += hessianMatrix[i * 4 + i]);
}
}
BENCHMARK(BM_HessianCompleteComputation);

// Benchmark Hessian diagonal sum computation, by computing only
// the diagonal elements.
static void BM_HessianDiagonalComputation(benchmark::State& state) {
auto dfdx2 =
clad::hessian<clad::opts::diagonal_only>(weightedSum, "p[0:1],w[0:1]");
double p[] = {1, 2};
double w[] = {3, 4};
unsigned long long sum = 0;
double diagonalHessian[4] = {};
for (unsigned i = 0; i < 4; i++)
diagonalHessian[i] = 0.0;
for (auto _ : state) {
dfdx2.execute(p, w, 3, diagonalHessian);
for (int i = 0; i < 4; i++)
// Sum the diagonal of the Hessian matrix.
benchmark::DoNotOptimize(sum += diagonalHessian[i]);
}
}
BENCHMARK(BM_HessianDiagonalComputation);

// Define our main.
BENCHMARK_MAIN();

0 comments on commit edf7593

Please sign in to comment.