From 1efaf1c836d59bdc5c16ee471fdb9881a217797d Mon Sep 17 00:00:00 2001 From: Ben Date: Fri, 23 Oct 2020 15:44:21 -0400 Subject: [PATCH 1/2] Convert sum benchmark to use `var` --- benchmark/stan/driver.hpp | 12 ++++++++++-- benchmark/stan/log_sum_exp.cpp | 3 ++- benchmark/stan/matrix_product.cpp | 3 ++- benchmark/stan/normal_log_pdf.cpp | 3 ++- benchmark/stan/prod.cpp | 3 ++- benchmark/stan/prod_iter.cpp | 5 +++-- benchmark/stan/regression.cpp | 3 ++- benchmark/stan/stochastic_volatility.cpp | 3 ++- benchmark/stan/sum.cpp | 15 ++++++++++----- benchmark/stan/sum_iter.cpp | 3 ++- setup.sh | 2 +- 11 files changed, 38 insertions(+), 17 deletions(-) diff --git a/benchmark/stan/driver.hpp b/benchmark/stan/driver.hpp index c5de008..825758c 100644 --- a/benchmark/stan/driver.hpp +++ b/benchmark/stan/driver.hpp @@ -4,7 +4,7 @@ namespace adb { -template +template static void BM_stan(benchmark::State& state) { F f; @@ -17,8 +17,16 @@ static void BM_stan(benchmark::State& state) state.counters["N"] = x.size(); + int i = 0; for (auto _ : state) { - stan::math::gradient(f, x, fx, grad_fx); + V x_var(x); + stan::math::var fx_var = f(x_var); + + fx_var.grad(); + if(i == 0) + grad_fx = x_var.adj(); + stan::math::recover_memory(); + i++; } // sanity-check that output gradient is good diff --git a/benchmark/stan/log_sum_exp.cpp b/benchmark/stan/log_sum_exp.cpp index 5237d1f..6fe19d9 100644 --- a/benchmark/stan/log_sum_exp.cpp +++ b/benchmark/stan/log_sum_exp.cpp @@ -11,7 +11,8 @@ struct LogSumExpFunc: LogSumExpFuncBase } }; -BENCHMARK_TEMPLATE(BM_stan, LogSumExpFunc) +using matvar = Eigen::Matrix; + BENCHMARK_TEMPLATE(BM_stan, LogSumExpFunc, matvar) -> RangeMultiplier(2) -> Range(1, 1 << 14); } // namespace adb diff --git a/benchmark/stan/matrix_product.cpp b/benchmark/stan/matrix_product.cpp index 6b876d5..26f1613 100644 --- a/benchmark/stan/matrix_product.cpp +++ b/benchmark/stan/matrix_product.cpp @@ -15,7 +15,8 @@ struct MatrixProductFunc: MatrixProductFuncBase } }; -BENCHMARK_TEMPLATE(BM_stan, MatrixProductFunc) +using matvar = Eigen::Matrix; +BENCHMARK_TEMPLATE(BM_stan, MatrixProductFunc, matvar) -> RangeMultiplier(2) -> Range(1, 1 << 16); } // namespace adb diff --git a/benchmark/stan/normal_log_pdf.cpp b/benchmark/stan/normal_log_pdf.cpp index 1505e9d..cd1d422 100644 --- a/benchmark/stan/normal_log_pdf.cpp +++ b/benchmark/stan/normal_log_pdf.cpp @@ -13,7 +13,8 @@ struct NormalLogPdfFunc: NormalLogPdfFuncBase } }; -BENCHMARK_TEMPLATE(BM_stan, NormalLogPdfFunc) +using matvar = Eigen::Matrix; +BENCHMARK_TEMPLATE(BM_stan, NormalLogPdfFunc, matvar) -> RangeMultiplier(2) -> Range(1, 1 << 14); } // namespace adb diff --git a/benchmark/stan/prod.cpp b/benchmark/stan/prod.cpp index cf06403..0bfba47 100644 --- a/benchmark/stan/prod.cpp +++ b/benchmark/stan/prod.cpp @@ -6,7 +6,8 @@ namespace adb { struct ProdFunc: ProdFuncBase {}; -BENCHMARK_TEMPLATE(BM_stan, ProdFunc) +using matvar = Eigen::Matrix; +BENCHMARK_TEMPLATE(BM_stan, ProdFunc, matvar) -> RangeMultiplier(2) -> Range(1, 1 << 14); } // namespace adb diff --git a/benchmark/stan/prod_iter.cpp b/benchmark/stan/prod_iter.cpp index a35a7cf..4f6d5e5 100644 --- a/benchmark/stan/prod_iter.cpp +++ b/benchmark/stan/prod_iter.cpp @@ -6,7 +6,8 @@ namespace adb { struct ProdIterFunc: ProdIterFuncBase {}; -BENCHMARK_TEMPLATE(BM_stan, ProdIterFunc) - -> RangeMultiplier(2) -> Range(1, 1 << 14); +using matvar = Eigen::Matrix; +BENCHMARK_TEMPLATE(BM_stan, ProdIterFunc, matvar) + -> RangeMultiplier(2) -> Range(1, 1 << 14); } // namespace adb diff --git a/benchmark/stan/regression.cpp b/benchmark/stan/regression.cpp index 223c06a..c8fa101 100644 --- a/benchmark/stan/regression.cpp +++ b/benchmark/stan/regression.cpp @@ -20,7 +20,8 @@ struct RegressionFunc: RegressionFuncBase } }; -BENCHMARK_TEMPLATE(BM_stan, RegressionFunc) +using matvar = Eigen::Matrix; +BENCHMARK_TEMPLATE(BM_stan, RegressionFunc, matvar) -> RangeMultiplier(2) -> Range(1, 1 << 14); } // namespace adb diff --git a/benchmark/stan/stochastic_volatility.cpp b/benchmark/stan/stochastic_volatility.cpp index 8624232..0846658 100644 --- a/benchmark/stan/stochastic_volatility.cpp +++ b/benchmark/stan/stochastic_volatility.cpp @@ -64,7 +64,8 @@ struct StochasticVolatilityFunc: StochasticVolatilityFuncBase } }; -BENCHMARK_TEMPLATE(BM_stan, StochasticVolatilityFunc) +using matvar = Eigen::Matrix; + BENCHMARK_TEMPLATE(BM_stan, StochasticVolatilityFunc, matvar) -> RangeMultiplier(2) -> Range(1, 1 << 14); } // namespace adb diff --git a/benchmark/stan/sum.cpp b/benchmark/stan/sum.cpp index d5f6418..5e19a09 100644 --- a/benchmark/stan/sum.cpp +++ b/benchmark/stan/sum.cpp @@ -5,13 +5,18 @@ namespace adb { struct SumFunc: SumFuncBase { - stan::math::var operator()(const Eigen::Matrix& x) const - { - return stan::math::sum(x); - } + template + stan::math::var operator()(const T& x) const + { + return stan::math::sum(x); + } }; -BENCHMARK_TEMPLATE(BM_stan, SumFunc) +using varmat = stan::math::var_value; +using matvar = Eigen::Matrix; +BENCHMARK_TEMPLATE(BM_stan, SumFunc, varmat) + -> RangeMultiplier(2) -> Range(1, 1 << 14); +BENCHMARK_TEMPLATE(BM_stan, SumFunc, matvar) -> RangeMultiplier(2) -> Range(1, 1 << 14); } // namespace adb diff --git a/benchmark/stan/sum_iter.cpp b/benchmark/stan/sum_iter.cpp index 811de31..b04fdef 100644 --- a/benchmark/stan/sum_iter.cpp +++ b/benchmark/stan/sum_iter.cpp @@ -6,7 +6,8 @@ namespace adb { struct SumIterFunc: SumIterFuncBase {}; -BENCHMARK_TEMPLATE(BM_stan, SumIterFunc) +using matvar = Eigen::Matrix; +BENCHMARK_TEMPLATE(BM_stan, SumIterFunc, matvar) -> RangeMultiplier(2) -> Range(1, 1 << 14); } // namespace adb diff --git a/setup.sh b/setup.sh index 54788c6..d8df902 100755 --- a/setup.sh +++ b/setup.sh @@ -96,7 +96,7 @@ fi # setup STAN if [ ! -d "$stan" ]; then - git clone --depth 1 --branch v3.3.0 https://github.com/stan-dev/math.git $stan + git clone --depth 1 --branch v3.4.0-rc1 https://github.com/stan-dev/math.git $stan cd $stan echo "CXX=g++-10" > make/local make -j4 -f make/standalone math-libs From c7b99a7ec59bdcd2a96200788fd26ba1e142f2e7 Mon Sep 17 00:00:00 2001 From: Ben Date: Tue, 26 Jan 2021 09:38:19 -0500 Subject: [PATCH 2/2] Converting more benchmarks to varmat --- benchmark/stan/CMakeLists.txt | 4 ++-- benchmark/stan/log_sum_exp.cpp | 16 ++++++++++------ benchmark/stan/normal_log_pdf.cpp | 16 ++++++++++------ benchmark/stan/prod_iter.cpp | 17 ++++++++++++++++- benchmark/stan/regression.cpp | 29 ++++++++++++++++------------- benchmark/stan/sum_iter.cpp | 17 ++++++++++++++++- setup.sh | 2 +- 7 files changed, 71 insertions(+), 30 deletions(-) diff --git a/benchmark/stan/CMakeLists.txt b/benchmark/stan/CMakeLists.txt index 5a98d86..9b105d6 100644 --- a/benchmark/stan/CMakeLists.txt +++ b/benchmark/stan/CMakeLists.txt @@ -26,9 +26,9 @@ function(add_stan_executable name) ${PROJECT_SOURCE_DIR}/lib/stan-dev-math ${PROJECT_SOURCE_DIR}/lib/stan-dev-math/lib/tbb_2019_U8/include ${PROJECT_SOURCE_DIR}/lib/stan-dev-math/lib/boost_1.72.0 - ${PROJECT_SOURCE_DIR}/lib/stan-dev-math/lib/sundials_5.2.0/include) + ${PROJECT_SOURCE_DIR}/lib/stan-dev-math/lib/sundials_5.6.1/include + ${PROJECT_SOURCE_DIR}/lib/stan-dev-math/lib/eigen_3.3.9) target_link_libraries(${exec} - Eigen3::Eigen FastAD::FastAD ${TBB_LIB} ${TBBMALLOC_LIB} diff --git a/benchmark/stan/log_sum_exp.cpp b/benchmark/stan/log_sum_exp.cpp index 6fe19d9..d2eb762 100644 --- a/benchmark/stan/log_sum_exp.cpp +++ b/benchmark/stan/log_sum_exp.cpp @@ -5,14 +5,18 @@ namespace adb { struct LogSumExpFunc: LogSumExpFuncBase { - stan::math::var operator()(const Eigen::Matrix& x) const - { - return stan::math::log_sum_exp(x); - } + template + auto operator()(const T& x) const + { + return stan::math::log_sum_exp(x); + } }; +using varmat = stan::math::var_value; using matvar = Eigen::Matrix; - BENCHMARK_TEMPLATE(BM_stan, LogSumExpFunc, matvar) - -> RangeMultiplier(2) -> Range(1, 1 << 14); +BENCHMARK_TEMPLATE(BM_stan, LogSumExpFunc, matvar) + -> RangeMultiplier(2) -> Range(1, 1 << 14); +BENCHMARK_TEMPLATE(BM_stan, LogSumExpFunc, varmat) + -> RangeMultiplier(2) -> Range(1, 1 << 14); } // namespace adb diff --git a/benchmark/stan/normal_log_pdf.cpp b/benchmark/stan/normal_log_pdf.cpp index cd1d422..42826bf 100644 --- a/benchmark/stan/normal_log_pdf.cpp +++ b/benchmark/stan/normal_log_pdf.cpp @@ -5,16 +5,20 @@ namespace adb { struct NormalLogPdfFunc: NormalLogPdfFuncBase { - auto operator()(const Eigen::Matrix& x) const - { - stan::math::var mu = mu_; - stan::math::var sigma = sigma_; - return stan::math::normal_lpdf(x, mu, sigma); - } + template + auto operator()(const T& x) const + { + stan::math::var mu = mu_; + stan::math::var sigma = sigma_; + return stan::math::normal_lpdf(x, mu, sigma); + } }; +using varmat = stan::math::var_value; using matvar = Eigen::Matrix; BENCHMARK_TEMPLATE(BM_stan, NormalLogPdfFunc, matvar) -> RangeMultiplier(2) -> Range(1, 1 << 14); +BENCHMARK_TEMPLATE(BM_stan, NormalLogPdfFunc, varmat) + -> RangeMultiplier(2) -> Range(1, 1 << 14); } // namespace adb diff --git a/benchmark/stan/prod_iter.cpp b/benchmark/stan/prod_iter.cpp index 4f6d5e5..8d8cb2b 100644 --- a/benchmark/stan/prod_iter.cpp +++ b/benchmark/stan/prod_iter.cpp @@ -4,10 +4,25 @@ namespace adb { struct ProdIterFunc: ProdIterFuncBase -{}; +{ + template + stan::math::var operator()(const T& x) const { + using namespace stan::math; + stan::math::var res = 1.0; + for(size_t i = 0; i < x.size(); i++) { + res *= x.coeffRef(i); + } + + return res; + } +}; + +using varmat = stan::math::var_value; using matvar = Eigen::Matrix; BENCHMARK_TEMPLATE(BM_stan, ProdIterFunc, matvar) -> RangeMultiplier(2) -> Range(1, 1 << 14); +BENCHMARK_TEMPLATE(BM_stan, ProdIterFunc, varmat) + -> RangeMultiplier(2) -> Range(1, 1 << 14); } // namespace adb diff --git a/benchmark/stan/regression.cpp b/benchmark/stan/regression.cpp index c8fa101..ff44a3c 100644 --- a/benchmark/stan/regression.cpp +++ b/benchmark/stan/regression.cpp @@ -5,23 +5,26 @@ namespace adb { struct RegressionFunc: RegressionFuncBase { - auto operator()(const Eigen::Matrix& x) const - { - using namespace stan::math; - using vec_t = Eigen::Matrix; - size_t N = (x.size() - 2); - Eigen::Map w(x.data(), N); - auto& b = x(N); - auto& sigma = x(N + 1); - return normal_lpdf(y, multiply(X, w) + b * vec_t::Ones(1000), sigma) + - normal_lpdf(w, 0., 1.) + - normal_lpdf(b, 0., 1.) - - uniform_lpdf(sigma, 0.1, 10.); - } + template + auto operator()(const T& x) const + { + using namespace stan::math; + size_t N = (x.size() - 2); + const auto& w = x.head(N); + const auto& b = x(N); + const auto& sigma = x(N + 1); + return normal_lpdf(y, add(multiply(X, w), b), sigma) + + normal_lpdf(w, 0., 1.) + + normal_lpdf(b, 0., 1.) - + uniform_lpdf(sigma, 0.1, 10.); + } }; +using varmat = stan::math::var_value; using matvar = Eigen::Matrix; BENCHMARK_TEMPLATE(BM_stan, RegressionFunc, matvar) -> RangeMultiplier(2) -> Range(1, 1 << 14); +BENCHMARK_TEMPLATE(BM_stan, RegressionFunc, varmat) + -> RangeMultiplier(2) -> Range(1, 1 << 14); } // namespace adb diff --git a/benchmark/stan/sum_iter.cpp b/benchmark/stan/sum_iter.cpp index b04fdef..48e9642 100644 --- a/benchmark/stan/sum_iter.cpp +++ b/benchmark/stan/sum_iter.cpp @@ -4,10 +4,25 @@ namespace adb { struct SumIterFunc: SumIterFuncBase -{}; +{ + template + stan::math::var operator()(const T& x) const { + using namespace stan::math; + stan::math::var res = 0.0; + for(size_t i = 0; i < x.size(); i++) { + res += x.coeffRef(i); + } + + return res; + } +}; + +using varmat = stan::math::var_value; using matvar = Eigen::Matrix; BENCHMARK_TEMPLATE(BM_stan, SumIterFunc, matvar) -> RangeMultiplier(2) -> Range(1, 1 << 14); +BENCHMARK_TEMPLATE(BM_stan, SumIterFunc, varmat) + -> RangeMultiplier(2) -> Range(1, 1 << 14); } // namespace adb diff --git a/setup.sh b/setup.sh index d8df902..6d979d4 100755 --- a/setup.sh +++ b/setup.sh @@ -96,7 +96,7 @@ fi # setup STAN if [ ! -d "$stan" ]; then - git clone --depth 1 --branch v3.4.0-rc1 https://github.com/stan-dev/math.git $stan + git clone --depth 1 --branch v4.0.0 https://github.com/stan-dev/math.git $stan cd $stan echo "CXX=g++-10" > make/local make -j4 -f make/standalone math-libs