-
-
Notifications
You must be signed in to change notification settings - Fork 183
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 2 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 |
---|---|---|
|
@@ -28,10 +28,9 @@ inline double log_sum_exp(const std::vector<double>& x) { | |
using std::log; | ||
using std::numeric_limits; | ||
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); | ||
}); | ||
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); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -191,7 +191,7 @@ gp_exp_quad_cov(const std::vector<T_x> &x, const T_sigma &sigma, | |
return cov; | ||
} | ||
|
||
for (auto&& x_i : x) { | ||
for (auto &&x_i : x) { | ||
check_not_nan("gp_exp_quad_cov", "x", x_i); | ||
} | ||
|
||
|
@@ -275,10 +275,10 @@ gp_exp_quad_cov(const std::vector<T_x1> &x1, const std::vector<T_x2> &x2, | |
return cov; | ||
} | ||
|
||
for (auto&& x1_i : x1) { | ||
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 (auto&& x2_i : x2) { | ||
for (auto &&x2_i : x2) { | ||
check_not_nan(function_name, "x2", x2_i); | ||
} | ||
|
||
|
@@ -325,10 +325,10 @@ gp_exp_quad_cov(const std::vector<Eigen::Matrix<T_x1, -1, 1>> &x1, | |
} | ||
|
||
const char *function_name = "gp_exp_quad_cov"; | ||
for (auto&& x1_i : x1) { | ||
for (auto &&x1_i : x1) { | ||
check_not_nan(function_name, "x1", x1_i); | ||
} | ||
for (auto&& x2_i : x2) { | ||
for (auto &&x2_i : x2) { | ||
check_not_nan(function_name, "x2", x2_i); | ||
} | ||
check_positive_finite(function_name, "magnitude", sigma); | ||
|
@@ -415,7 +415,7 @@ 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 (auto&& x_i : x) { | ||
for (auto &&x_i : x) { | ||
check_not_nan("gp_exp_quad_cov", "x", x_i); | ||
} | ||
cov = internal::gp_exp_quad_cov(x, square(sigma), | ||
|
@@ -560,10 +560,10 @@ 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 (auto&& x_i : x1) { | ||
for (auto &&x_i : x1) { | ||
check_not_nan(function_name, "x1", x_i); | ||
} | ||
for (auto&& x_i : x2) { | ||
for (auto &&x_i : x2) { | ||
check_not_nan(function_name, "x2", x_i); | ||
} | ||
|
||
|
@@ -621,10 +621,10 @@ 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 (auto&& x1_i : x1) { | ||
for (auto &&x1_i : x1) { | ||
check_not_nan(function_name, "x1", x1_i); | ||
} | ||
for (auto&& x2_i : x2) { | ||
for (auto &&x2_i : x2) { | ||
check_not_nan(function_name, "x1", x2_i); | ||
} | ||
cov = internal::gp_exp_quad_cov(divide_columns(x1, length_scale), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,9 +18,9 @@ inline double log_sum_exp_as_double(const std::vector<var>& x) { | |
using std::numeric_limits; | ||
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); | ||
}); | ||
[&max_val](auto& acc, auto&& x_i) { | ||
return acc + exp(x_i.val() - max_val); | ||
}); | ||
return max_val + log(sum); | ||
} | ||
|
||
|
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!