Skip to content

Commit

Permalink
Merge pull request #92 from JamesYang007/vectorized_beval
Browse files Browse the repository at this point in the history
Fully refactored with new vectorized beval method
  • Loading branch information
JamesYang007 authored Aug 29, 2020
2 parents 9712fdc + 42ffdea commit 0875f62
Show file tree
Hide file tree
Showing 71 changed files with 3,251 additions and 3,833 deletions.
30 changes: 11 additions & 19 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@ version: 2
jobs:
build:
docker:
- image: buildpack-deps:trusty
- image: buildpack-deps:bionic

steps:
- checkout
- run:
name: Install sudo
command: 'apt-get update && apt-get install -y sudo && rm -rf /var/lib/apt/lists/*'
- run:
name: Install ubuntu-toolchain-r/test and g++-7
name: Install ubuntu-toolchain-r/test, g++-8, and cmake
command: |
sudo apt-get update
sudo apt-get install -y software-properties-common
sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test
sudo apt update
sudo apt install g++-7 -y
sudo apt install g++-8 -y
sudo apt install cmake -y
- run:
name: Check g++ version
command: |
Expand All @@ -25,20 +26,6 @@ jobs:
sudo update-alternatives --config gcc
gcc --version
g++ --version
- run:
name: Install CMake
command: |
sudo apt-get update
version=3.9
build=2
mkdir ~/temp
cd ~/temp
wget https://cmake.org/files/v$version/cmake-$version.$build.tar.gz
tar -xzvf cmake-$version.$build.tar.gz
cd cmake-$version.$build/
./bootstrap
make -j$(nproc)
sudo make install
- run:
name: Install Ninja
command: |
Expand All @@ -60,7 +47,12 @@ jobs:
command: 'cmake --version; uname -a'
- run:
name: Compile
command: './clean-build.sh release'
command: |
mkdir -p build && cd build
mkdir -p release && cd release
rm -rf ./*
cmake ../../ -GNinja
cmake --build . -- -j4
- run:
name: Execute test suite
command: 'cd build/release ; ctest -j12'
command: 'cd build/release ; ctest -j6'
8 changes: 4 additions & 4 deletions benchmark/ad_benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ static void BM_test1_fastad(benchmark::State& state)
x.emplace_back(i / 100.);
}
std::vector<Var<double>> w(3);
auto expr = (w[0] = x[0] * x[1] - x[2] * sin(x[0]),
auto expr = ad::bind(
(w[0] = x[0] * x[1] - x[2] * sin(x[0]),
w[1] = x[1] * w[0] - cos(w[0]) +
ad::sum(x.begin(), x.end(), [](const auto& xi) {return xi;}),
w[2] = w[1] + ad::exp(w[1] - w[0]));
std::vector<double> tmp(expr.bind_size());
expr.bind(tmp.data());
w[2] = w[1] + ad::exp(w[1] - w[0]))
);

for (auto _ : state) {
autodiff(expr);
Expand Down
9 changes: 4 additions & 5 deletions benchmark/constant_eager_benchmark.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <fastad_bits/reverse/core/var.hpp>
#include <fastad_bits/reverse/core/math.hpp>
#include <fastad_bits/reverse/core/unary.hpp>
#include <fastad_bits/reverse/core/binary.hpp>
#include <fastad_bits/reverse/core/eval.hpp>
#include <fastad_bits/reverse/core/pow.hpp>
#include <fastad_bits/reverse/core/sum.hpp>
Expand Down Expand Up @@ -38,16 +39,14 @@ static void BM_normal_repeated_stddev(benchmark::State& state)
}

int i = 0;
auto expr = ad::sum(values.begin(), values.end(),
auto expr = ad::bind(ad::sum(values.begin(), values.end(),
[&](double v) {
auto&& expr = -ad::constant(0.5) *
ad::pow<2>((ad::constant(v) - w * ad::constant(values2[i])) / ad::constant(2.)) -
ad::log(ad::constant(2.));
++i;
return expr;
});
std::vector<double> tmp(expr.bind_size());
expr.bind(tmp.data());
}));

for (auto _ : state) {
ad::autodiff(expr);
Expand Down
38 changes: 12 additions & 26 deletions benchmark/normal_benchmark.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <fastad_bits/reverse/core/math.hpp>
#include <fastad_bits/reverse/core/var.hpp>
#include <fastad_bits/reverse/core/unary.hpp>
#include <fastad_bits/reverse/core/binary.hpp>
#include <fastad_bits/reverse/core/eval.hpp>
#include <fastad_bits/reverse/core/eq.hpp>
#include <fastad_bits/reverse/core/pow.hpp>
#include <fastad_bits/reverse/core/sum.hpp>
#include <fastad_bits/reverse/stat/normal.hpp>
Expand Down Expand Up @@ -45,18 +45,15 @@ BENCHMARK_DEFINE_F(normal_fixture, BM_normal_adj_log_pdf)(benchmark::State& stat

VarView<value_t, vec> x(nullptr, nullptr, size);
VarView<value_t, vec> mu(nullptr, nullptr, size);
VarView<value_t, selfadjmat> sigma(nullptr, nullptr, size, size);
VarView<value_t, mat> sigma(nullptr, nullptr, size, size);

size_t tot_size = x.size() + mu.size() + sigma.size();
std::vector<double> val(tot_size, 0);
std::vector<double> adj(tot_size, 0);

auto val_next = x.bind(val.data());
auto adj_next = x.bind_adj(adj.data());
val_next = mu.bind(val_next);
adj_next = mu.bind_adj(adj_next);
val_next = sigma.bind(val_next);
adj_next = sigma.bind_adj(adj_next);
auto ptr_pack = x.bind({val.data(), adj.data()});
ptr_pack = mu.bind(ptr_pack);
ptr_pack = sigma.bind(ptr_pack);

x.get().Random();
mu.get().Random();
Expand All @@ -79,32 +76,21 @@ BENCHMARK_DEFINE_F(normal_fixture, BM_normal_adj_log_pdf_flat)(benchmark::State&
VarView<value_t, vec> x(nullptr, nullptr, size);
VarView<value_t, vec> mu(nullptr, nullptr, size);

size_t tot_size = x.size() + mu.size() + (size * (size + 1))/2;
size_t tot_size = x.size() + mu.size() + (size * size);
std::vector<double> val(tot_size, 0);
std::vector<double> adj(tot_size, 0);

auto val_next = x.bind(val.data());
auto adj_next = x.bind_adj(adj.data());
val_next = mu.bind(val_next);
adj_next = mu.bind_adj(adj_next);
auto ptr_pack = x.bind({val.data(), adj.data()});
ptr_pack = mu.bind(ptr_pack);

Eigen::MatrixXd mat_vals(size, size);
VarView<value_t, selfadjmat> sigma(mat_vals.data(),
val_next,
adj_next,
size);
VarView<value_t, mat> sigma(ptr_pack.val,
ptr_pack.adj,
size, size);

x.get().Random();
mu.get().Random();
make_cov(sigma);

size_t k = 0;
for (size_t j = 0; j < sigma.cols(); ++j) {
for (size_t i = j; i < sigma.rows(); ++i, ++k) {
val_next[k] = sigma.get()(i,j);
}
}

auto expr = ad::bind(normal_adj_log_pdf(x, mu, sigma));

for (auto _ : state) {
Expand Down
10 changes: 5 additions & 5 deletions benchmark/prod_benchmark.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include <fastad_bits/reverse/core/var.hpp>
#include <fastad_bits/reverse/core/eq.hpp>
#include <fastad_bits/reverse/core/prod.hpp>
#include <fastad_bits/reverse/core/math.hpp>
#include <fastad_bits/reverse/core/unary.hpp>
#include <fastad_bits/reverse/core/binary.hpp>
#include <fastad_bits/reverse/core/eval.hpp>
#include <fastad_bits/reverse/core/pow.hpp>
#include <fastad_bits/reverse/core/prod.hpp>
#include <benchmark/benchmark.h>
#ifdef USE_ADEPT
#include <adept_arrays.h>
Expand Down Expand Up @@ -53,9 +55,7 @@ static void BM_prod_fastad(benchmark::State& state)
, [](const auto& x) {
return x * x;
});
auto expr = (w4 = prod_expr, w5 = w4 * w4 + ad::cos(w4));
std::vector<double> tmp(expr.bind_size());
expr.bind(tmp.data());
auto expr = ad::bind((w4 = prod_expr, w5 = w4 * w4 + ad::cos(w4)));

for (auto _ : state) {
autodiff(expr);
Expand Down
26 changes: 12 additions & 14 deletions benchmark/sum_benchmark.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <fastad_bits/reverse/core/math.hpp>
#include <fastad_bits/reverse/core/binary.hpp>
#include <fastad_bits/reverse/core/unary.hpp>
#include <fastad_bits/reverse/core/var.hpp>
#include <fastad_bits/reverse/core/eval.hpp>
#include <fastad_bits/reverse/core/eq.hpp>
Expand Down Expand Up @@ -51,9 +52,7 @@ static void BM_sumnode_fastad(benchmark::State& state)
}
Var<double> w4, w5;
auto sum_expr = ad::sum(vec.begin(), vec.end(), [](const auto& x) {return x * x;});
auto expr = (w4=sum_expr, w5 = w4 * w4 + ad::sin(w4));
std::vector<double> tmp(expr.bind_size());
expr.bind(tmp.data());
auto expr = ad::bind((w4=sum_expr, w5 = w4 * w4 + ad::sin(w4)));

for (auto _ : state) {
autodiff(expr);
Expand All @@ -78,16 +77,15 @@ static void BM_sumnode_fastad_large_vectorized(benchmark::State& state)
}

int i = 0;
auto expr = ad::sum(values.begin(), values.end(),
[&](double v) {
if (i % values2.size() == 0) i = 0;
auto&& expr = -ad::constant(0.5) *
ad::pow<2>((ad::constant(v) - w[0] * ad::constant(values2[i])) / w[1]);
++i;
return expr;
});
std::vector<double> tmp(expr.bind_size());
expr.bind(tmp.data());
auto expr = ad::bind(
ad::sum(values.begin(), values.end(),
[&](double v) {
if (i % values2.size() == 0) i = 0;
auto&& expr = -ad::constant(0.5) *
ad::pow<2>((ad::constant(v) - w[0] * ad::constant(values2[i])) / w[1]);
++i;
return expr;
}));

for (auto _ : state) {
ad::autodiff(expr);
Expand Down
13 changes: 9 additions & 4 deletions example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ void reverse_simple()
, w5 = exp(w4*w3)
);

std::vector<double> tmp(expr.bind_size());
expr.bind(tmp.data());
auto size_pack = expr.bind_cache_size();
std::vector<double> val_buf(size_pack(0));
std::vector<double> adj_buf(size_pack(1));
expr.bind_cache({val_buf.data(), adj_buf.data()});

autodiff(expr);

Expand All @@ -63,8 +65,11 @@ void reverse_vec()
, w[1] = w[0] + x[0] * x[1]
, w[2] = exp(w[1] * w[0])
);
std::vector<double> tmp(expr.bind_size());
expr.bind(tmp.data());

auto size_pack = expr.bind_cache_size();
std::vector<double> val_buf(size_pack(0));
std::vector<double> adj_buf(size_pack(1));
expr.bind_cache({val_buf.data(), adj_buf.data()});

autodiff(expr);

Expand Down
36 changes: 2 additions & 34 deletions include/fastad
Original file line number Diff line number Diff line change
@@ -1,36 +1,4 @@
// Export header file

#pragma once

#include "fastad_bits/forward/core/dualnum.hpp"
#include "fastad_bits/forward/core/forward.hpp"

#include "fastad_bits/reverse/core/binary.hpp"
#include "fastad_bits/reverse/core/bind.hpp"
#include "fastad_bits/reverse/core/constant.hpp"
#include "fastad_bits/reverse/core/dot.hpp"
#include "fastad_bits/reverse/core/eq.hpp"
#include "fastad_bits/reverse/core/eval.hpp"
#include "fastad_bits/reverse/core/expr_base.hpp"
#include "fastad_bits/reverse/core/for_each.hpp"
#include "fastad_bits/reverse/core/glue.hpp"
#include "fastad_bits/reverse/core/if_else.hpp"
#include "fastad_bits/reverse/core/math.hpp"
#include "fastad_bits/reverse/core/norm.hpp"
#include "fastad_bits/reverse/core/pow.hpp"
#include "fastad_bits/reverse/core/prod.hpp"
#include "fastad_bits/reverse/core/sqrt.hpp"
#include "fastad_bits/reverse/core/sum.hpp"
#include "fastad_bits/reverse/core/unary.hpp"
#include "fastad_bits/reverse/core/value_view.hpp"
#include "fastad_bits/reverse/core/var.hpp"
#include "fastad_bits/reverse/core/var_view.hpp"
//#include "fastad_bits/reverse/core/hessian.hpp"

#include "fastad_bits/util/shape_traits.hpp"
#include "fastad_bits/util/type_traits.hpp"

#include "fastad_bits/reverse/stat/normal.hpp"
#include "fastad_bits/reverse/stat/uniform.hpp"
#include "fastad_bits/reverse/stat/bernoulli.hpp"
#include "fastad_bits/reverse/stat/wishart.hpp"
#include "fastad_bits/forward.hpp"
#include "fastad_bits/reverse.hpp"
2 changes: 2 additions & 0 deletions include/fastad_bits/forward.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#pragma once
#include "forward/core.hpp"
3 changes: 3 additions & 0 deletions include/fastad_bits/forward/core.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once
#include "core/dualnum.hpp"
#include "core/forward.hpp"
4 changes: 4 additions & 0 deletions include/fastad_bits/reverse.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#pragma once

#include "reverse/core.hpp"
#include "reverse/stat.hpp"
20 changes: 20 additions & 0 deletions include/fastad_bits/reverse/core.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once
#include "fastad_bits/reverse/core/binary.hpp"
#include "fastad_bits/reverse/core/bind.hpp"
#include "fastad_bits/reverse/core/constant.hpp"
#include "fastad_bits/reverse/core/dot.hpp"
#include "fastad_bits/reverse/core/eq.hpp"
#include "fastad_bits/reverse/core/eval.hpp"
#include "fastad_bits/reverse/core/expr_base.hpp"
#include "fastad_bits/reverse/core/for_each.hpp"
#include "fastad_bits/reverse/core/glue.hpp"
#include "fastad_bits/reverse/core/if_else.hpp"
#include "fastad_bits/reverse/core/norm.hpp"
#include "fastad_bits/reverse/core/pow.hpp"
#include "fastad_bits/reverse/core/prod.hpp"
#include "fastad_bits/reverse/core/sum.hpp"
#include "fastad_bits/reverse/core/unary.hpp"
#include "fastad_bits/reverse/core/value_view.hpp"
#include "fastad_bits/reverse/core/var.hpp"
#include "fastad_bits/reverse/core/var_view.hpp"
//#include "fastad_bits/reverse/core/hessian.hpp"
Loading

0 comments on commit 0875f62

Please sign in to comment.