Skip to content

Stop passing raw stream to log_prob_grad helper #1317

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

Merged
merged 2 commits into from
Mar 18, 2025
Merged
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
3 changes: 1 addition & 2 deletions src/cmdstan/command.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -449,8 +449,7 @@ int command(int argc, const char *argv[]) {
}
}
try {
services_log_prob_grad(model, jacobian, params_r_ind, sig_figs,
sample_writers[0].get_stream());
services_log_prob_grad(model, jacobian, params_r_ind, sample_writers[0]);
return_code = return_codes::OK;
} catch (const std::exception &e) {
msg << "Error during log_prob calculation:" << std::endl;
Expand Down
26 changes: 10 additions & 16 deletions src/cmdstan/command_helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <cmdstan/arguments/argument_parser.hpp>
#include <cmdstan/arguments/arg_sample.hpp>
#include <cmdstan/file.hpp>
#include <stan/callbacks/unique_stream_writer.hpp>
#include <stan/callbacks/json_writer.hpp>
#include <stan/callbacks/writer.hpp>
#include <stan/io/dump.hpp>
Expand Down Expand Up @@ -580,18 +579,14 @@ std::vector<std::vector<double>> get_uparams_r(
*/
void services_log_prob_grad(const stan::model::model_base &model, bool jacobian,
std::vector<std::vector<double>> &params_set,
int sig_figs, std::ostream &output_stream) {
// header row
output_stream << std::setprecision(sig_figs) << "lp__,";
std::vector<std::string> p_names;
stan::callbacks::writer &output) {
// header
std::vector<std::string> p_names{"lp__"};
model.unconstrained_param_names(p_names, false, false);
for (size_t i = 0; i < p_names.size(); ++i) {
output_stream << "g_" << p_names[i];
if (i == p_names.size() - 1)
output_stream << "\n";
else
output_stream << ",";
}
std::transform(p_names.begin() + 1, p_names.end(), p_names.begin() + 1,
[](std::string s) { return "g_" + s; });
output(p_names);

// data row(s)
std::vector<int> dummy_params_i;
double lp;
Expand All @@ -604,10 +599,9 @@ void services_log_prob_grad(const stan::model::model_base &model, bool jacobian,
lp = stan::model::log_prob_grad<true, false>(model, params,
dummy_params_i, gradients);
}
output_stream << lp << ",";
std::copy(gradients.begin(), gradients.end() - 1,
std::ostream_iterator<double>(output_stream, ","));
output_stream << gradients.back() << "\n";
// unfortunate: var.grad clears the vector, so need to insert lp afterwards
gradients.insert(gradients.begin(), lp);
output(gradients);
}
}

Expand Down