Skip to content
Open
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
13 changes: 13 additions & 0 deletions src/stan/callbacks/logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,19 @@ class logger {
*/
virtual void fatal(const std::stringstream& message) {}
};

inline void log_if_nonempty(logger& log, const std::stringstream& message) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that this hard codes the "info" level, it'd be nice if that's in its name, as in log_info_if_nonempty.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you could also add function doc, that'd be helpful.

if (message.str().length() > 0) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you reorder the two functions, this can be reduced to
log_if_nonempty(logger, msg.str()).

I also think the variable should be called logger rather than log.

log.info(message);
}
}

inline void log_if_nonempty(logger& log, const std::string& message) {
if (!message.empty()) {
log.info(message);
}
}

} // namespace callbacks
} // namespace stan

Expand Down
4 changes: 2 additions & 2 deletions src/stan/model/test_gradients.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ int test_gradients(const Model& model, std::vector<double>& params_r,
double lp = log_prob_grad<propto, jacobian_adjust_transform>(
model, params_r, params_i, grad, &msg);
if (msg.str().length() > 0) {
logger.info(msg);
log_if_nonempty(logger, msg);
parameter_writer(msg.str());
}

std::vector<double> grad_fd;
finite_diff_grad<false, true, Model>(model, interrupt, params_r, params_i,
grad_fd, epsilon, &msg);
if (msg.str().length() > 0) {
logger.info(msg);
log_if_nonempty(logger, msg);
parameter_writer(msg.str());
}

Expand Down
17 changes: 5 additions & 12 deletions src/stan/services/optimize/bfgs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,11 @@ int bfgs(Model& model, const stan::io::var_context& init,
model.write_array(rng, cont_vector, disc_vector, values, true, true,
&msg);
} catch (const std::exception& e) {
if (msg.str().length() > 0) {
logger.info(msg);
}
log_if_nonempty(logger, msg);
logger.error(e.what());
return error_codes::SOFTWARE;
}
if (msg.str().length() > 0)
logger.info(msg);
log_if_nonempty(logger, msg);

values.insert(values.begin(), {lp, static_cast<double>(ret)});
parameter_writer(values);
Expand Down Expand Up @@ -166,8 +163,7 @@ int bfgs(Model& model, const stan::io::var_context& init,
&msg);

// This if is here to match the pre-refactor behavior
if (msg.str().length() > 0)
logger.info(msg);
log_if_nonempty(logger, msg);

values.insert(values.begin(), {lp, static_cast<double>(ret)});
parameter_writer(values);
Expand All @@ -185,14 +181,11 @@ int bfgs(Model& model, const stan::io::var_context& init,
model.write_array(rng, cont_vector, disc_vector, values, true, true,
&msg);
} catch (const std::exception& e) {
if (msg.str().length() > 0) {
logger.info(msg);
}
log_if_nonempty(logger, msg);
logger.error(e.what());
return error_codes::SOFTWARE;
}
if (msg.str().length() > 0)
logger.info(msg);
log_if_nonempty(logger, msg);
values.insert(values.begin(), {lp, static_cast<double>(ret)});
parameter_writer(values);
}
Expand Down
13 changes: 4 additions & 9 deletions src/stan/services/optimize/lbfgs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ int lbfgs(Model& model, const stan::io::var_context& init,
std::vector<double> values;
std::stringstream msg;
model.write_array(rng, cont_vector, disc_vector, values, true, true, &msg);
if (msg.str().length() > 0)
logger.info(msg);
log_if_nonempty(logger, msg);

values.insert(values.begin(), {lp, static_cast<double>(ret)});
parameter_writer(values);
Expand Down Expand Up @@ -159,8 +158,7 @@ int lbfgs(Model& model, const stan::io::var_context& init,
std::stringstream msg;
model.write_array(rng, cont_vector, disc_vector, values, true, true,
&msg);
if (msg.str().length() > 0)
logger.info(msg);
log_if_nonempty(logger, msg);

values.insert(values.begin(), {lp, static_cast<double>(ret)});
parameter_writer(values);
Expand All @@ -178,14 +176,11 @@ int lbfgs(Model& model, const stan::io::var_context& init,
model.write_array(rng, cont_vector, disc_vector, values, true, true,
&msg);
} catch (const std::exception& e) {
if (msg.str().length() > 0) {
logger.info(msg);
}
log_if_nonempty(logger, msg);
logger.error(e.what());
return error_codes::SOFTWARE;
}
if (msg.str().length() > 0)
logger.info(msg);
log_if_nonempty(logger, msg);

values.insert(values.begin(), {lp, static_cast<double>(ret)});
parameter_writer(values);
Expand Down
6 changes: 2 additions & 4 deletions src/stan/services/sample/standalone_gqs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ int standalone_generate(const Model &model, const Eigen::MatrixXd &draws,
try {
model.unconstrain_array(row, unconstrained_params_r, &msg);
} catch (const std::exception &e) {
if (msg.str().length() > 0)
logger.error(msg);
log_if_nonempty(logger, msg);
logger.error(e.what());
return error_codes::DATAERR;
}
Expand Down Expand Up @@ -178,8 +177,7 @@ int standalone_generate(const Model &model, const int num_chains,
row = draws[slice_idx].row(i);
model.unconstrain_array(row, unconstrained_params_r, &msg);
} catch (const std::domain_error &e) {
if (msg.str().length() > 0)
logger.error(msg);
log_if_nonempty(logger, msg);
logger.error(e.what());
error_any = true;
return;
Expand Down
26 changes: 7 additions & 19 deletions src/stan/services/util/initialize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,15 @@ std::vector<double> initialize(Model& model, const InitContext& init, RNG& rng,
model.transform_inits(context, disc_vector, unconstrained, &msg);
}
} catch (std::domain_error& e) {
if (msg.str().length() > 0) {
logger.info(msg);
}
log_if_nonempty(logger, msg);
logger.warn("Rejecting initial value:");
logger.warn(
" Error evaluating the log probability"
" at the initial value.");
logger.warn(e.what());
continue;
} catch (std::exception& e) {
if (msg.str().length() > 0) {
logger.info(msg);
}
log_if_nonempty(logger, msg);
logger.error(
"Unrecoverable error evaluating the log probability"
" at the initial value.");
Expand All @@ -132,22 +128,17 @@ std::vector<double> initialize(Model& model, const InitContext& init, RNG& rng,
// the parameters.
log_prob = model.template log_prob<false, Jacobian>(unconstrained,
disc_vector, &msg);
if (msg.str().length() > 0) {
logger.info(msg);
}
log_if_nonempty(logger, msg);
} catch (std::domain_error& e) {
if (msg.str().length() > 0)
logger.info(msg);
log_if_nonempty(logger, msg);
logger.warn("Rejecting initial value:");
logger.warn(
" Error evaluating the log probability"
" at the initial value.");
logger.warn(e.what());
continue;
} catch (std::exception& e) {
if (msg.str().length() > 0) {
logger.info(msg);
}
log_if_nonempty(logger, msg);
logger.error(
"Unrecoverable error evaluating the log probability"
" at the initial value.");
Expand All @@ -172,9 +163,7 @@ std::vector<double> initialize(Model& model, const InitContext& init, RNG& rng,
log_prob = stan::model::log_prob_grad<true, Jacobian>(
model, unconstrained, disc_vector, gradient, &log_prob_msg);
} catch (const std::exception& e) {
if (log_prob_msg.str().length() > 0) {
logger.info(log_prob_msg);
}
log_if_nonempty(logger, log_prob_msg);
logger.error(e.what());
throw;
}
Expand All @@ -183,8 +172,7 @@ std::vector<double> initialize(Model& model, const InitContext& init, RNG& rng,
= std::chrono::duration_cast<std::chrono::microseconds>(end - start)
.count()
/ 1000000.0;
if (log_prob_msg.str().length() > 0)
logger.info(log_prob_msg);
log_if_nonempty(logger, log_prob_msg);

bool gradient_ok = std::isfinite(stan::math::sum(gradient));

Expand Down
3 changes: 1 addition & 2 deletions src/stan/variational/advi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -484,8 +484,7 @@ class advi {
std::stringstream msg;
model_.write_array(rng_, cont_vector, disc_vector, values, true, true,
&msg);
if (msg.str().length() > 0)
logger.info(msg);
log_if_nonempty(logger, msg);

// The first row of lp_, log_p, and log_g.
values.insert(values.begin(), {0, 0, 0});
Expand Down
Loading