-
-
Notifications
You must be signed in to change notification settings - Fork 187
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
Clang tidy cleanup and using std algorithms #1373
Changes from 14 commits
b9f545b
4064902
23706ed
bade075
8a2dff8
2407b7a
8bb5458
0c31b37
d68129f
f9275d1
06c465a
bdadd8e
8656836
d716461
3266349
90be53e
c9fef6c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,10 +2,12 @@ | |
#define STAN_MATH_PRIM_ARR_FUN_LOG_SUM_EXP_HPP | ||
|
||
#include <stan/math/prim/meta.hpp> | ||
#include <cmath> | ||
#include <cstdlib> | ||
#include <algorithm> | ||
#include <numeric> | ||
#include <limits> | ||
#include <vector> | ||
#include <cmath> | ||
#include <cstdlib> | ||
|
||
namespace stan { | ||
namespace math { | ||
|
@@ -27,21 +29,11 @@ inline double log_sum_exp(const std::vector<double>& x) { | |
using std::exp; | ||
using std::log; | ||
using std::numeric_limits; | ||
double max = -numeric_limits<double>::infinity(); | ||
for (double xx : x) { | ||
if (xx > max) { | ||
max = xx; | ||
} | ||
} | ||
|
||
double sum = 0.0; | ||
for (size_t ii = 0; ii < x.size(); ii++) { | ||
if (x[ii] != -numeric_limits<double>::infinity()) { | ||
sum += exp(x[ii] - max); | ||
} | ||
} | ||
|
||
return max + log(sum); | ||
double max_val = *std::max_element(x.begin(), x.end()); | ||
double sum = std::accumulate( | ||
x.begin(), x.end(), 0.0, | ||
[&max_val](auto& acc, auto&& x_i) { return acc + exp(x_i - max_val); }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will this generate code that's as efficient as before? It will come down to how efficiently it can compile that closure. How do we test? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just did this on godbolt, lhs is the code (bottom is current (labled editor 1) and top is the new one (editor 2) middle is the output from the new stuff and far right is the output from the current stuff. You can highlight certain instructions and it usually pops up a little 'heres what this does'. You can click 'add' in the top right to get a diff view of the two outputs, though it usually looks wonky at O3. You can click and drag any of the tabs for each little block to move stuff. If you right click the highlighted code on the lhs it should have an options to take you to where that line is happening in whichever of the bottom two outputs, though it's not always exact. I like to look at -O0 to see where stuff is then looking at -O3. About lines 40-60'ish is where the loop and exp calculation happen. The code is super similar, the lambda version removes a compare and a few moves. But those are mostly because we don't do the if statement in there anymore. I can look tmrw at just removing that check there with the old version. godbolt is pretty neat! I learned last night you can also get a real graph of the call graph! There's a way to make a PR on their repo so we can get Stan up there, would like to find time for that in the next week or so There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another cool internet benchmark thing! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the rules for capture are like argument passing, so that primitives like |
||
return max_val + log(sum); | ||
} | ||
|
||
} // namespace math | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,7 +34,7 @@ class accumulator { | |
/** | ||
* Destroy an accumulator. | ||
*/ | ||
~accumulator() {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason for defining the accumulator destructor as empty here? tmk this still calls the destructor for all the accumulates members There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one is OK to leave as default---as is, it's not virtual and breaks the rule of 3(5). |
||
~accumulator() = default; | ||
|
||
/** | ||
* Add the specified arithmetic type value to the buffer after | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -191,8 +191,8 @@ gp_exp_quad_cov(const std::vector<T_x> &x, const T_sigma &sigma, | |
return cov; | ||
} | ||
|
||
for (size_t n = 0; n < x_size; ++n) { | ||
check_not_nan("gp_exp_quad_cov", "x", x[n]); | ||
for (auto &&x_i : x) { | ||
check_not_nan("gp_exp_quad_cov", "x", x_i); | ||
} | ||
|
||
cov = internal::gp_exp_quad_cov(x, square(sigma), | ||
|
@@ -275,11 +275,11 @@ gp_exp_quad_cov(const std::vector<T_x1> &x1, const std::vector<T_x2> &x2, | |
return cov; | ||
} | ||
|
||
for (size_t i = 0; i < x1_size; ++i) { | ||
check_not_nan(function_name, "x1", x1[i]); | ||
for (auto &&x1_i : x1) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As is, I think these can be const. These should be using a vectorized Another alternative would be a for-each loop, which doesn't actually simplify things here, especiallyw ith explicit capture of the function name.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Agree this should use a vectorized check_nan, but the vectorized version of After Andrew and I sort out the more generic templating discussion in #1425 then I'm going to come back to these check functions and clean them up so we can do that. |
||
check_not_nan(function_name, "x1", x1_i); | ||
} | ||
for (size_t i = 0; i < x2_size; ++i) { | ||
check_not_nan(function_name, "x2", x2[i]); | ||
for (auto &&x2_i : x2) { | ||
check_not_nan(function_name, "x2", x2_i); | ||
} | ||
|
||
cov = internal::gp_exp_quad_cov(x1, x2, square(sigma), | ||
|
@@ -325,11 +325,11 @@ gp_exp_quad_cov(const std::vector<Eigen::Matrix<T_x1, -1, 1>> &x1, | |
} | ||
|
||
const char *function_name = "gp_exp_quad_cov"; | ||
for (size_t i = 0; i < x1_size; ++i) { | ||
check_not_nan(function_name, "x1", x1[i]); | ||
for (auto &&x1_i : x1) { | ||
check_not_nan(function_name, "x1", x1_i); | ||
} | ||
for (size_t i = 0; i < x2_size; ++i) { | ||
check_not_nan(function_name, "x2", x2[i]); | ||
for (auto &&x2_i : x2) { | ||
check_not_nan(function_name, "x2", x2_i); | ||
} | ||
check_positive_finite(function_name, "magnitude", sigma); | ||
check_positive_finite(function_name, "length scale", length_scale); | ||
|
@@ -369,8 +369,8 @@ inline Eigen::MatrixXd gp_exp_quad_cov(const std::vector<double> &x, | |
} | ||
const auto total_size = x_size + cov.size(); | ||
if (total_size < opencl_context.tuning_opts().gp_exp_quad_cov_simple) { | ||
for (size_t n = 0; n < x_size; ++n) { | ||
check_not_nan("gp_exp_quad_cov", "x", x[n]); | ||
for (auto x_i : x) { | ||
check_not_nan("gp_exp_quad_cov", "x", x_i); | ||
} | ||
|
||
cov = internal::gp_exp_quad_cov(x, square(sigma), | ||
|
@@ -415,8 +415,8 @@ inline Eigen::MatrixXd gp_exp_quad_cov(const std::vector<Eigen::VectorXd> &x, | |
const size_t inner_x1_size = x[0].size(); | ||
const auto total_size = x_size * inner_x1_size + cov.size(); | ||
if (total_size < opencl_context.tuning_opts().gp_exp_quad_cov_complex) { | ||
for (size_t i = 0; i < x_size; ++i) { | ||
check_not_nan("gp_exp_quad_cov", "x", x[i]); | ||
for (auto &&x_i : x) { | ||
check_not_nan("gp_exp_quad_cov", "x", x_i); | ||
} | ||
cov = internal::gp_exp_quad_cov(x, square(sigma), | ||
-0.5 / square(length_scale)); | ||
|
@@ -503,11 +503,11 @@ inline typename Eigen::MatrixXd gp_exp_quad_cov(const std::vector<double> &x1, | |
} | ||
const auto total_size = x1.size() + x2.size() + cov.size(); | ||
if (total_size < opencl_context.tuning_opts().gp_exp_quad_cov_simple) { | ||
for (size_t i = 0; i < x1.size(); ++i) { | ||
check_not_nan(function_name, "x1", x1[i]); | ||
for (auto x_i : x1) { | ||
check_not_nan(function_name, "x1", x_i); | ||
} | ||
for (size_t i = 0; i < x2.size(); ++i) { | ||
check_not_nan(function_name, "x2", x2[i]); | ||
for (auto x_i : x2) { | ||
check_not_nan(function_name, "x2", x_i); | ||
} | ||
|
||
cov = internal::gp_exp_quad_cov(x1, x2, square(sigma), | ||
|
@@ -560,11 +560,11 @@ inline typename Eigen::MatrixXd gp_exp_quad_cov( | |
const auto total_size | ||
= x1_size * x1_inner_size + x2_size * x2_inner_size + cov.size(); | ||
if (total_size < opencl_context.tuning_opts().gp_exp_quad_cov_complex) { | ||
for (size_t i = 0; i < x1.size(); ++i) { | ||
check_not_nan(function_name, "x1", x1[i]); | ||
for (auto &&x_i : x1) { | ||
check_not_nan(function_name, "x1", x_i); | ||
} | ||
for (size_t i = 0; i < x2.size(); ++i) { | ||
check_not_nan(function_name, "x2", x2[i]); | ||
for (auto &&x_i : x2) { | ||
check_not_nan(function_name, "x2", x_i); | ||
} | ||
|
||
cov = internal::gp_exp_quad_cov(x1, x2, square(sigma), | ||
|
@@ -621,11 +621,11 @@ inline typename Eigen::MatrixXd gp_exp_quad_cov( | |
const auto total_size | ||
= x1_size * x1_inner_size + x2_size * x2_inner_size + l_size + cov.size(); | ||
if (total_size < opencl_context.tuning_opts().gp_exp_quad_cov_complex) { | ||
for (size_t i = 0; i < x1_size; ++i) { | ||
check_not_nan(function_name, "x1", x1[i]); | ||
for (auto &&x1_i : x1) { | ||
check_not_nan(function_name, "x1", x1_i); | ||
} | ||
for (size_t i = 0; i < x2_size; ++i) { | ||
check_not_nan(function_name, "x1", x2[i]); | ||
for (auto &&x2_i : x2) { | ||
check_not_nan(function_name, "x1", x2_i); | ||
} | ||
cov = internal::gp_exp_quad_cov(divide_columns(x1, length_scale), | ||
divide_columns(x2, length_scale), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,8 +4,10 @@ | |
#include <stan/math/rev/core.hpp> | ||
#include <stan/math/rev/scal/fun/calculate_chain.hpp> | ||
#include <stan/math/prim/arr/fun/log_sum_exp.hpp> | ||
#include <vector> | ||
#include <algorithm> | ||
#include <limits> | ||
#include <numeric> | ||
#include <vector> | ||
|
||
namespace stan { | ||
namespace math { | ||
|
@@ -15,19 +17,12 @@ inline double log_sum_exp_as_double(const std::vector<var>& x) { | |
using std::exp; | ||
using std::log; | ||
using std::numeric_limits; | ||
double max = -numeric_limits<double>::infinity(); | ||
for (size_t i = 0; i < x.size(); ++i) { | ||
if (x[i] > max) { | ||
max = x[i].val(); | ||
} | ||
} | ||
double sum = 0.0; | ||
for (size_t i = 0; i < x.size(); ++i) { | ||
if (x[i] != -numeric_limits<double>::infinity()) { | ||
sum += exp(x[i].val() - max); | ||
} | ||
} | ||
return max + log(sum); | ||
double max_val = std::max_element(x.begin(), x.end())->val(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [optional] There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ack, it's so close! I think for a v v clean version of this we need a vectorized I put a comment above |
||
double sum = std::accumulate(x.begin(), x.end(), 0.0, | ||
[&max_val](auto& acc, auto&& x_i) { | ||
return acc + exp(x_i.val() - max_val); | ||
}); | ||
return max_val + log(sum); | ||
} | ||
|
||
class log_sum_exp_vector_vari : public op_vector_vari { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is very neat!