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

adds accumulators for var and fwd #2535

Merged
merged 35 commits into from
Jul 27, 2021
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
96da34b
adds accumulators for var and fwd
SteveBronder Jul 13, 2021
9417de4
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Jul 13, 2021
848e17e
go back to using a std::vector and add stuff for matrix_cl
SteveBronder Jul 14, 2021
85058a2
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Jul 14, 2021
8cf71ee
remove first importing /prim/ from the rev.hpp
SteveBronder Jul 15, 2021
874079d
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Jul 15, 2021
c712684
fix for opencl
SteveBronder Jul 15, 2021
379dc72
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Jul 15, 2021
63b11e5
Fix test headers
SteveBronder Jul 16, 2021
3124877
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Jul 16, 2021
c0bd208
revert
SteveBronder Jul 16, 2021
2034f76
add back accumulator specializations
SteveBronder Jul 16, 2021
2b18ba3
Merge branch 'feature/varmat-accumulator' of github.com:stan-dev/math…
SteveBronder Jul 16, 2021
554fc93
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Jul 16, 2021
0d86b22
put back prim headers and remove arena_alloctor from var impl
SteveBronder Jul 16, 2021
0065321
put back prim headers and remove arena_alloctor from var impl
SteveBronder Jul 16, 2021
3e99bbb
put back prim headers and remove arena_alloctor from var impl
SteveBronder Jul 16, 2021
f1e7001
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Jul 16, 2021
544f023
add back fvar accumulator specialization
SteveBronder Jul 16, 2021
c55dc7d
remove arena_allocator from std vector in var accumulator
SteveBronder Jul 16, 2021
d18c38a
fixup sum to use as_array_or_scalar()
SteveBronder Jul 16, 2021
e619889
fix headers
SteveBronder Jul 16, 2021
c9a62d6
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Jul 16, 2021
a4a24c1
test removing specializations
SteveBronder Jul 20, 2021
8e76a68
fix include order
t4c1 Jul 21, 2021
a754293
optimizations
t4c1 Jul 21, 2021
1d9f92f
update accumulator buffer #
SteveBronder Jul 21, 2021
c9ba6de
Merge commit '7eee48cc1830a5d9c8b7173c98c3996344c76c70' into HEAD
yashikno Jul 21, 2021
9fcae77
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Jul 21, 2021
620346d
Fixed values for accumulator test
SteveBronder Jul 22, 2021
f13f4ca
update some test headers and remove using fvar<var<T>> for accumulato…
SteveBronder Jul 22, 2021
86e4970
add back fvar accumulator
SteveBronder Jul 22, 2021
97508a7
add back buf_ for prim and fwd
SteveBronder Jul 22, 2021
027ce0e
change include orders for mix
SteveBronder Jul 22, 2021
5b1992e
remove fvar specialization
SteveBronder Jul 23, 2021
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
12 changes: 4 additions & 8 deletions stan/math/fwd/fun/accumulator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
#define STAN_MATH_FWD_FUN_ACCUMULATOR_HPP

#include <stan/math/prim/fun/Eigen.hpp>
#include <stan/math/prim/fun/accumulator.hpp>
#include <stan/math/fwd/core.hpp>
#include <stan/math/fwd/meta.hpp>
#include <stan/math/fwd/fun/sum.hpp>
#include <stan/math/prim/fun/accumulator.hpp>
#include <vector>
#include <type_traits>

Expand Down Expand Up @@ -37,7 +38,7 @@ class accumulator<fvar<T>> {
* @tparam S Type of argument
* @param x Value to add
*/
template <typename S, require_stan_scalar_t<S>* = nullptr>
template <typename S, typename = require_stan_scalar_t<S>>
inline void add(S x) {
buf_.push_back(x);
}
Expand All @@ -63,18 +64,13 @@ class accumulator<fvar<T>> {
* @tparam S Type of value to recursively add.
* @param xs Vector of entries to add
*/
template <typename S, require_container_t<S>* = nullptr>
template <typename S>
inline void add(const std::vector<S>& xs) {
for (size_t i = 0; i < xs.size(); ++i) {
this->add(xs[i]);
}
}

template <typename S, require_not_container_t<S>* = nullptr>
inline void add(const std::vector<S>& xs) {
buf_.push_back(stan::math::sum(xs));
}

/**
* Return the sum of the accumulated values.
*
Expand Down
8 changes: 4 additions & 4 deletions stan/math/mix.hpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#ifndef STAN_MATH_MIX_HPP
#define STAN_MATH_MIX_HPP

#ifdef STAN_OPENCL
#include <stan/math/opencl/rev.hpp>
#endif

#include <stan/math/mix/meta.hpp>
#include <stan/math/mix/fun.hpp>
#include <stan/math/mix/functor.hpp>

#ifdef STAN_OPENCL
#include <stan/math/opencl/rev.hpp>
#endif

#include <stan/math/rev/core.hpp>
#include <stan/math/rev/meta.hpp>
#include <stan/math/rev/fun.hpp>
Expand Down
34 changes: 9 additions & 25 deletions stan/math/prim/fun/accumulator.hpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
#ifndef STAN_MATH_PRIM_FUN_ACCUMULATOR_HPP
#define STAN_MATH_PRIM_FUN_ACCUMULATOR_HPP

#include <stan/math/prim/meta.hpp>
#include <stan/math/prim/fun/Eigen.hpp>
#include <stan/math/prim/meta.hpp>
#include <stan/math/prim/fun/sum.hpp>

#ifdef STAN_OPENCL
#include <stan/math/opencl/prim.hpp>
#endif

#include <vector>
#include <type_traits>

Expand Down Expand Up @@ -41,13 +36,14 @@ class accumulator {
* @tparam S Type of argument
* @param x Value to add
*/
template <typename S, typename = require_arithmetic_t<S>>
template <typename S, typename = require_stan_scalar_t<S>>
inline void add(S x) {
buf_.push_back(x);
}

/**
* Sum each entry of the matrix and then add the sum to the buffer.
* Add each entry in the specified matrix, vector, or row vector
* of values to the buffer.
*
* @tparam S type of the matrix
* @param m Matrix of values to add
Expand All @@ -60,24 +56,21 @@ class accumulator {
/**
* Recursively add each entry in the specified standard vector
* to the buffer. This will allow vectors of primitives,
* autodiff variables to be added.
* autodiff variables to be added; if the vector entries
* are collections, their elements are recursively added.
*
* @tparam S Type of value to recursively add.
* @param xs Vector of entries to add
*/
template <typename S, require_container_t<S>* = nullptr>
template <typename S>
inline void add(const std::vector<S>& xs) {
for (size_t i = 0; i < xs.size(); ++i) {
this->add(xs[i]);
}
}

template <typename S, require_not_container_t<S>* = nullptr>
inline void add(const std::vector<S>& xs) {
buf_.push_back(stan::math::sum(xs));
}

#ifdef STAN_OPENCL

/**
* Sum each entry and then push to the buffer.
* @tparam S A Type inheriting from `matrix_cl_base`
Expand All @@ -88,16 +81,7 @@ class accumulator {
inline void add(const S& xs) {
buf_.push_back(stan::math::sum(xs));
}
/**
* Sum each entry and then push to the buffer.
* @tparam S A Type inheriting from `matrix_cl_base`
* @param x An `std::vector` with inner type OpenCL matrix
*/
template <typename S,
require_all_kernel_expressions_and_none_scalar_t<S>* = nullptr>
inline void add(const std::vector<S>& xs) {
buf_.push_back(stan::math::sum(xs));
}

#endif

/**
Expand Down
2 changes: 1 addition & 1 deletion stan/math/prim/fun/sum.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ inline T sum(T&& m) {
* @param m Standard vector to sum.
* @return Sum of elements.
*/
template <typename T>
template <typename T, require_not_var_t<T>* = nullptr>
inline T sum(const std::vector<T>& m) {
return std::accumulate(m.begin(), m.end(), T{0});
}
Expand Down
5 changes: 0 additions & 5 deletions stan/math/rev/fun.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@

#include <stan/math/prim/fun/Eigen.hpp>

#include <stan/math/prim/err.hpp>
#include <stan/math/prim/fun.hpp>
#include <stan/math/prim/functor.hpp>
#include <stan/math/prim/meta.hpp>
t4c1 marked this conversation as resolved.
Show resolved Hide resolved

#include <stan/math/rev/fun/LDLT_factor.hpp>
#include <stan/math/rev/fun/Phi.hpp>
#include <stan/math/rev/fun/Phi_approx.hpp>
Expand Down
47 changes: 12 additions & 35 deletions stan/math/rev/fun/accumulator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,9 @@
#define STAN_MATH_REV_FUN_ACCUMULATOR_HPP

#include <stan/math/prim/fun/Eigen.hpp>
#include <stan/math/prim/fun/accumulator.hpp>
#include <stan/math/rev/meta.hpp>
#include <stan/math/rev/core.hpp>
#include <stan/math/rev/fun/sum.hpp>

#ifdef STAN_OPENCL
#include <stan/math/opencl/rev.hpp>
#endif

#include <stan/math/prim/fun/accumulator.hpp>
#include <vector>
#include <type_traits>

Expand All @@ -30,7 +24,7 @@ namespace math {
template <>
class accumulator<var> {
private:
std::vector<var> buf_;
std::vector<var, arena_allocator<var>> buf_;
SteveBronder marked this conversation as resolved.
Show resolved Hide resolved

public:
/**
Expand All @@ -43,7 +37,7 @@ class accumulator<var> {
* @tparam S Type of argument
* @param x Value to add
*/
template <typename S, require_stan_scalar_t<S>* = nullptr>
template <typename S, typename = require_stan_scalar_t<S>>
inline void add(S x) {
buf_.push_back(x);
}
Expand All @@ -66,26 +60,10 @@ class accumulator<var> {
* autodiff variables to be added; if the vector entries
* are collections, their elements are recursively added.
*
* @tparam S A standard vector holding non-container elements
* @tparam S Type of value to recursively add.
* @param xs Vector of entries to add
*/
template <typename S,
require_all_not_t<is_container<S>, is_var_matrix<S>>* = nullptr>
inline void add(const std::vector<S>& xs) {
buf_.push_back(stan::math::sum(xs));
}

/**
* Recursively add each entry in the specified standard vector
* to the buffer. This will allow vectors of primitives,
* autodiff variables to be added; if the vector entries
* are collections, their elements are recursively added.
*
* @tparam S A standard vector holding container elements
* @param xs Vector of entries to add
*/
template <typename S,
require_any_t<is_container<S>, is_var_matrix<S>>* = nullptr>
template <typename S>
inline void add(const std::vector<S>& xs) {
for (size_t i = 0; i < xs.size(); ++i) {
this->add(xs[i]);
Expand All @@ -96,33 +74,32 @@ class accumulator<var> {
/**
* Sum each entry and then push to the buffer.
* @tparam S A Type inheriting from `matrix_cl_base`
* @param x A `var_value` with inner type OpenCL matrix
* @param x An OpenCL matrix
*/
template <typename S,
require_all_kernel_expressions_and_none_scalar_t<S>* = nullptr>
inline void add(const var_value<S>& x) {
buf_.push_back(stan::math::sum(x));
inline void add(const var_value<S>& xs) {
buf_.push_back(stan::math::sum(xs));
}

/**
* Sum each entry and then push to the buffer.
* @tparam S A Type inheriting from `matrix_cl_base`
* @param x An `std::vector` holding a `var_value` with inner type OpenCL
* matrix
* @param x An OpenCL matrix
*/
template <typename S,
require_all_kernel_expressions_and_none_scalar_t<S>* = nullptr>
inline void add(const std::vector<var_value<S>>& xs) {
inline void add(const S& xs) {
buf_.push_back(stan::math::sum(xs));
}

#endif

/**
* Return the sum of the accumulated values.
*
* @return Sum of accumulated values.
*/
inline var sum() const { return stan::math::sum(buf_); }
inline var sum() const noexcept { return stan::math::sum(buf_); }
};

} // namespace math
Expand Down
14 changes: 11 additions & 3 deletions stan/math/rev/fun/sum.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,19 @@ class sum_v_vari : public vari {
* @param m Vector.
* @return Sum of vector entries.
*/
inline var sum(const std::vector<var>& m) {
if (m.empty()) {
template <typename Alloc>
inline var sum(const std::vector<var, Alloc>& m) {
if (unlikely(m.empty())) {
return 0.0;
} else {
auto arena_m = to_arena(m);
return make_callback_var(stan::math::sum(as_array_or_scalar(arena_m).val()),
[arena_m](auto& vi) mutable {
for (auto& x_i : arena_m) {
x_i.adj() += vi.adj();
}
});
}
return var(new sum_v_vari(m));
t4c1 marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down
1 change: 0 additions & 1 deletion test/unit/math/mix/fun/ldexp_test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <test/unit/math/test_ad.hpp>
#include <stan/math/prim.hpp>

TEST(mathMixScalFun, ldexp) {
auto f = [](const auto& x1) { return stan::math::ldexp(x1, 5); };
Expand Down
4 changes: 1 addition & 3 deletions test/unit/math/mix/meta/require_generics_test.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#include <stan/math/fwd.hpp>
#include <stan/math/rev.hpp>
#include <stan/math/prim.hpp>
#include <test/unit/math/test_ad.hpp>
#include <test/unit/math/require_util.hpp>
#include <gtest/gtest.h>
#include <type_traits>
Expand Down
1 change: 0 additions & 1 deletion test/unit/math/mix/prob/von_mises_cdf_test.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include <stan/math/prim.hpp>
#include <test/unit/math/test_ad.hpp>
#include <limits>

Expand Down
22 changes: 0 additions & 22 deletions test/unit/math/opencl/rev/accumulator_test.cpp

This file was deleted.

3 changes: 1 addition & 2 deletions test/unit/math/rev/core/gradable.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#ifndef TEST_UNIT_MATH_REV_CORE_GRADABLE_HPP
#define TEST_UNIT_MATH_REV_CORE_GRADABLE_HPP

#include <stan/math.hpp>
#include <stan/math/prim.hpp>
#include <stan/math/rev.hpp>
#include <stan/math/rev/fun/quad_form.hpp>
#include <test/unit/math/rev/fun/util.hpp>
#include <vector>
Expand Down
2 changes: 1 addition & 1 deletion test/unit/math/rev/fun/accumulator_test.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <stan/math/rev.hpp>
#include <gtest/gtest.h>
#include <test/unit/math/rev/util.hpp>
#include <gtest/gtest.h>
#include <vector>

// test sum of first n numbers for sum of a
Expand Down
2 changes: 1 addition & 1 deletion test/unit/math/rev/fun/eigenvalues_sym_test.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <stan/math/prim.hpp>
#include <stan/math/rev.hpp>
#include <stan/math/rev/fun/eigenvalues_sym.hpp>
#include <stan/math/rev/fun/sum.hpp>
#include <stan/math/rev/fun/log.hpp>
Expand Down
1 change: 1 addition & 0 deletions test/unit/math/rev/fun/eigenvectors_sym_test.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <stan/math/rev.hpp>
t4c1 marked this conversation as resolved.
Show resolved Hide resolved
#include <stan/math/prim.hpp>
#include <stan/math/rev/fun/eigenvectors_sym.hpp>
#include <stan/math/rev/fun/sum.hpp>
Expand Down
2 changes: 1 addition & 1 deletion test/unit/math/rev/fun/lbeta_test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <stan/math/prim.hpp>
#include <stan/math/rev.hpp>
#include <stan/math/prim.hpp>
#include <stan/math/rev/core/nested_rev_autodiff.hpp>
#include <test/unit/math/expect_near_rel.hpp>
#include <gtest/gtest.h>
Expand Down
2 changes: 1 addition & 1 deletion test/unit/math/rev/fun/promote_elements_test.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <stan/math/rev/core.hpp>
#include <stan/math/rev.hpp>
#include <stan/math/prim.hpp>
#include <boost/typeof/typeof.hpp>
#include <gtest/gtest.h>
Expand Down
1 change: 1 addition & 0 deletions test/unit/math/rev/fun/singular_values_test.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <stan/math/rev.hpp>
#include <stan/math/prim.hpp>
#include <stan/math/rev/fun/singular_values.hpp>
#include <stan/math/rev/fun/sum.hpp>
Expand Down