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

Standalone GQ: Output NaNs when exception is caught #3173

Merged
merged 2 commits into from
Mar 20, 2023

Conversation

WardBrian
Copy link
Member

Submission Checklist

  • Run unit tests: ./runTests.py src/test/unit
  • Run cpplint: make cpplint
  • Declare copyright holder and open-source license: see below

Summary

Closes stan-dev/cmdstan#1007. Since stan-dev/stanc3#1165 we have made sure that write_array always populates the array with NaNs, so writing it out is safe even after an exception.

Intended Effect

Standalone generate quantities behavior now matches that of sampling (nans output if write_array fails) and the number of rows in the output of standalone generated quantities is now always the number of rows in the input fitted_params.

How to Verify

The example from stan-dev/cmdstan#1007 works well

Side Effects

Documentation

Copyright and Licensing

Please list the copyright holder for the work you are submitting (this will be you or your assignee, such as a university or company):

Simons Foundation

By submitting this pull request, the copyright holder is agreeing to license the submitted work under the following licenses:

Copy link
Contributor

@bob-carpenter bob-carpenter left a comment

Choose a reason for hiding this comment

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

Looks good other than scope of a logger in one file.

@@ -81,7 +81,6 @@ class gq_writer {
if (ss.str().length() > 0)
logger_.info(ss);
logger_.info(e.what());
return;
Copy link
Contributor

Choose a reason for hiding this comment

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

If this isn't going to return, then the following logger_ call outside the catch duplicates the ss message. The full context is this:

    try {
      model.write_array(rng, const_cast<std::vector<double>&>(draw), params_i,
                        values, false, true, &ss);
    } catch (const std::exception& e) {
      if (ss.str().length() > 0)
        logger_.info(ss);
      logger_.info(e.what());
    }
    if (ss.str().length() > 0)
      logger_.info(ss);

The conditional logging after the catch should be moved into the try block to avoid duplicating logger_.info(ss).

This is also making me think the logger itself should be ignoring empty string messages rather than duplicating this everywhere.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch.

Ignoring empty strings has a (nearly 5 year old!) issue open: #2577

// model test_gq.stan generates 4 values, 3 commas
EXPECT_EQ(count_matches(",", sample_ss.str()), 3);

EXPECT_EQ(count_matches("nan", sample_ss.str()), 4);
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for adding the test.

Copy link
Contributor

@bob-carpenter bob-carpenter left a comment

Choose a reason for hiding this comment

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

Thanks for the update.

@bob-carpenter
Copy link
Contributor

Thanks, and feel free to merge when tests finish.

@WardBrian WardBrian merged commit 068dd7d into develop Mar 20, 2023
@WardBrian WardBrian deleted the fix/standalone-gq-exception-behavior branch March 20, 2023 21:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make generate quantities fill failed draws with Nan
2 participants