Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert sum benchmark to use var<mat> #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions benchmark/stan/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
12 changes: 10 additions & 2 deletions benchmark/stan/driver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace adb {

template <class F>
template <class F, class V>
static void BM_stan(benchmark::State& state)
{
F f;
Expand All @@ -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
Expand Down
17 changes: 11 additions & 6 deletions benchmark/stan/log_sum_exp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@ namespace adb {

struct LogSumExpFunc: LogSumExpFuncBase
{
stan::math::var operator()(const Eigen::Matrix<stan::math::var, Eigen::Dynamic, 1>& x) const
{
return stan::math::log_sum_exp(x);
}
template <typename T>
auto operator()(const T& x) const
{
return stan::math::log_sum_exp(x);
}
};

BENCHMARK_TEMPLATE(BM_stan, LogSumExpFunc)
-> RangeMultiplier(2) -> Range(1, 1 << 14);
using varmat = stan::math::var_value<Eigen::VectorXd>;
using matvar = Eigen::Matrix<stan::math::var, Eigen::Dynamic, 1>;
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
3 changes: 2 additions & 1 deletion benchmark/stan/matrix_product.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ struct MatrixProductFunc: MatrixProductFuncBase
}
};

BENCHMARK_TEMPLATE(BM_stan, MatrixProductFunc)
using matvar = Eigen::Matrix<stan::math::var, Eigen::Dynamic, 1>;
BENCHMARK_TEMPLATE(BM_stan, MatrixProductFunc, matvar)
-> RangeMultiplier(2) -> Range(1, 1 << 16);

} // namespace adb
19 changes: 12 additions & 7 deletions benchmark/stan/normal_log_pdf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@ namespace adb {

struct NormalLogPdfFunc: NormalLogPdfFuncBase
{
auto operator()(const Eigen::Matrix<stan::math::var, Eigen::Dynamic, 1>& x) const
{
stan::math::var mu = mu_;
stan::math::var sigma = sigma_;
return stan::math::normal_lpdf(x, mu, sigma);
}
template <typename T>
auto operator()(const T& x) const
{
stan::math::var mu = mu_;
stan::math::var sigma = sigma_;
return stan::math::normal_lpdf(x, mu, sigma);
}
};

BENCHMARK_TEMPLATE(BM_stan, NormalLogPdfFunc)
using varmat = stan::math::var_value<Eigen::VectorXd>;
using matvar = Eigen::Matrix<stan::math::var, Eigen::Dynamic, 1>;
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
3 changes: 2 additions & 1 deletion benchmark/stan/prod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ namespace adb {
struct ProdFunc: ProdFuncBase
{};

BENCHMARK_TEMPLATE(BM_stan, ProdFunc)
using matvar = Eigen::Matrix<stan::math::var, Eigen::Dynamic, 1>;
BENCHMARK_TEMPLATE(BM_stan, ProdFunc, matvar)
-> RangeMultiplier(2) -> Range(1, 1 << 14);

} // namespace adb
22 changes: 19 additions & 3 deletions benchmark/stan/prod_iter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,25 @@
namespace adb {

struct ProdIterFunc: ProdIterFuncBase
{};
{
template <class T>
stan::math::var operator()(const T& x) const {
using namespace stan::math;

BENCHMARK_TEMPLATE(BM_stan, ProdIterFunc)
-> RangeMultiplier(2) -> Range(1, 1 << 14);
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<Eigen::VectorXd>;
using matvar = Eigen::Matrix<stan::math::var, Eigen::Dynamic, 1>;
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
32 changes: 18 additions & 14 deletions benchmark/stan/regression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,26 @@ namespace adb {

struct RegressionFunc: RegressionFuncBase
{
auto operator()(const Eigen::Matrix<stan::math::var, Eigen::Dynamic, 1>& x) const
{
using namespace stan::math;
using vec_t = Eigen::Matrix<var, Eigen::Dynamic, 1>;
size_t N = (x.size() - 2);
Eigen::Map<const vec_t> 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 <typename T>
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.);
}
};

BENCHMARK_TEMPLATE(BM_stan, RegressionFunc)
using varmat = stan::math::var_value<Eigen::VectorXd>;
using matvar = Eigen::Matrix<stan::math::var, Eigen::Dynamic, 1>;
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
3 changes: 2 additions & 1 deletion benchmark/stan/stochastic_volatility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ struct StochasticVolatilityFunc: StochasticVolatilityFuncBase
}
};

BENCHMARK_TEMPLATE(BM_stan, StochasticVolatilityFunc)
using matvar = Eigen::Matrix<stan::math::var, Eigen::Dynamic, 1>;
BENCHMARK_TEMPLATE(BM_stan, StochasticVolatilityFunc, matvar)
-> RangeMultiplier(2) -> Range(1, 1 << 14);

} // namespace adb
Expand Down
15 changes: 10 additions & 5 deletions benchmark/stan/sum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@ namespace adb {

struct SumFunc: SumFuncBase
{
stan::math::var operator()(const Eigen::Matrix<stan::math::var, Eigen::Dynamic, 1>& x) const
{
return stan::math::sum(x);
}
template <typename T>
stan::math::var operator()(const T& x) const
{
return stan::math::sum(x);
}
};

BENCHMARK_TEMPLATE(BM_stan, SumFunc)
using varmat = stan::math::var_value<Eigen::VectorXd>;
using matvar = Eigen::Matrix<stan::math::var, Eigen::Dynamic, 1>;
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
20 changes: 18 additions & 2 deletions benchmark/stan/sum_iter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,25 @@
namespace adb {

struct SumIterFunc: SumIterFuncBase
{};
{
template <class T>
stan::math::var operator()(const T& x) const {
using namespace stan::math;

BENCHMARK_TEMPLATE(BM_stan, SumIterFunc)
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<Eigen::VectorXd>;
using matvar = Eigen::Matrix<stan::math::var, Eigen::Dynamic, 1>;
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
2 changes: 1 addition & 1 deletion setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 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
Expand Down