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

Add missing scalar prim signatures #2612

Merged
merged 35 commits into from
Jan 6, 2022
Merged

Conversation

rok-cesnovar
Copy link
Member

@rok-cesnovar rok-cesnovar commented Nov 6, 2021

Summary

Adds missing scalar implementations for max, min, fmod, atan2 and pow to fix #2611.

Additionally, this adds a test that generates unit tests on the fly. These tests check that all supported stanc3 scalar signatures (excluding those that return complex numbers and _rng functions) compile in the stan::math namespace. Additionally, it also checks that nothing was placed on any var stack (no inputs get promoted to vars).

Example of a test that is generated:

TEST(PrimSigsTests, pow4786) {
  /*
  pow(real, int) => real
   */
  double real0 = 0.4;
  int int1 = 1;
  auto result2 = stan::math::eval(stan::math::pow(real0, int1));
  auto result3 = stan::math::eval(stan::math::pow(real0, int1));
  EXPECT_STAN_EQ(result2, result3);
  EXPECT_EQ(stan::math::ChainableStack::instance_->var_stack_.size(), 0);
  EXPECT_EQ(stan::math::ChainableStack::instance_->var_nochain_stack_.size(), 0);
  EXPECT_EQ(stan::math::ChainableStack::instance_->var_alloc_stack_.size(), 0);
}

The functions are called twice because I wanted to make sure I included the result in an EXPECT test so it does not get optimized out. This might not be needed, but I just wanted to be safe.

The entire test file that is generated is here: prim_sig_test.txt

This test would be run on Github Actions.

Tests

Adds a new signature test that can be run via ./runTests.py test/scalar_prim_sig. This generates a unit test file that checks that all primitive scalar functions compile and that none of them gets promoted to vars.

Side Effects

/

Release notes

Added implementations of max, min, fmod, atan2 and pow for scalar inputs to the stan::math namespace.

Checklist

@bob-carpenter
Copy link
Member

I can review this.

The current failure is a bit worrisome---it looks like it just failed outright in the LLVM judging from the plea to submit a bug report:

clang: error: unable to execute command: Killed
clang: error: clang frontend command failed due to signal (use -v to see invocation)
clang version 6.0.0-1ubuntu2~16.04.1 (tags/RELEASE_600/final)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
clang: note: diagnostic msg: PLEASE submit a bug report to http://llvm.org/bugs/ and include the crash backtrace, preprocessed source, and associated run script.
clang: note: diagnostic msg: 
********************

PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
clang: note: diagnostic msg: /tmp/exp_mod_normal_lpdf_test-2506fe.cpp
clang: note: diagnostic msg: /tmp/exp_mod_normal_lpdf_test-2506fe.sh
clang: note: diagnostic msg: 

********************

@rok-cesnovar
Copy link
Member Author

Yeah, the tests were already passing but failed upstream because stan-dev/stan#3080 was required. Restarted the tests, should be ready for review.

Copy link
Member

@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.

A bunch of picky doc comments and some more substantive testing questions. My main concern is that all the promotion of integers is tested as that's been an ongoing problem for us with the standard library functions. Not something complicated, just integer tests for all arguments. So if it's binary, that's four cases of {double, int} x {double, int}.

@hsbadr
Copy link
Member

hsbadr commented Nov 10, 2021

@rok-cesnovar JFYI. This breaks the R packages that depend on rstan with, for instance, pow() ambiguity. I'm using the experimental branch of rstan with the nightly version of stanc3.

@rok-cesnovar
Copy link
Member Author

Yeah, I know (it also fails upstream for the exact same reason - the fact that some std functions live in the global namespace).

This branch will be merged together accompanied by a change in stanc3 that will add explicit stan::math:: namespace to all generated Stan Math.
See stan-dev/stanc3#1011 and stan-dev/stanc3#997

Also thanks for checking it. I love that we have up-to-date rstan now!

@WardBrian
Copy link
Member

For the benefit of anyone who is interested primarily in the Math development without following stanc3 super closely, this PR is motivated by an issue that can lead to programs that stanc3 compiles but the c++ compiler can't. For example,

parameters {
  real alpha;
  real beta;
}
transformed parameters {
  real beta_function = beta(alpha, beta)
}

We allow users to define a variable called beta, but without the explicit namespacing this PR allows the C++ compilers fail to compile the function call of beta.

@rok-cesnovar
Copy link
Member Author

This is now ready for re-review. Ignore the test failure as it fails upstream (we will need some coordination to merge this and stan-dev/stanc3#1011 together).

@rok-cesnovar
Copy link
Member Author

Please ignore the review request.

Testing with the Stanc3 changes revealed that we also have to add Stan Math support for standard library functions that support std::complex inputs (arg, abs, norm, conj, proj, polar, exp, log, log10, pow, sqrt and trigonometric functions).

The implementations should be simple, just calling the std:: function.

@rok-cesnovar
Copy link
Member Author

Added stan::math namespaced functions for std::complex<T> inputs, where T is not var for the following: arg, conj, norm, pow, proj.

Also added tests for complex inputs for other complex functions: abs, exp, log, log10, sqrt and trigonometric.

All models now compile with stan-dev/stanc3#1011 and this branch so this is ready for review.

@WardBrian
Copy link
Member

@bob-carpenter - could you take another look at this?

Copy link
Member

@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. thanks for dealing with this huge pr.

@bob-carpenter
Copy link
Member

This should be OK to merge when tests pass.

@stan-buildbot
Copy link
Contributor


Name Old Result New Result Ratio Performance change( 1 - new / old )
gp_pois_regr/gp_pois_regr.stan 3.59 3.57 1.01 0.6% faster
low_dim_corr_gauss/low_dim_corr_gauss.stan 0.02 0.02 0.93 -7.02% slower
eight_schools/eight_schools.stan 0.09 0.09 0.99 -0.83% slower
gp_regr/gp_regr.stan 0.14 0.14 1.02 1.52% faster
irt_2pl/irt_2pl.stan 5.79 5.85 0.99 -0.99% slower
performance.compilation 93.66 91.01 1.03 2.83% faster
low_dim_gauss_mix_collapse/low_dim_gauss_mix_collapse.stan 8.04 8.07 1.0 -0.32% slower
pkpd/one_comp_mm_elim_abs.stan 32.1 31.68 1.01 1.32% faster
sir/sir.stan 119.52 121.5 0.98 -1.66% slower
gp_regr/gen_gp_data.stan 0.04 0.03 1.01 1.08% faster
low_dim_gauss_mix/low_dim_gauss_mix.stan 3.02 3.01 1.0 0.25% faster
pkpd/sim_one_comp_mm_elim_abs.stan 0.39 0.42 0.94 -5.87% slower
arK/arK.stan 2.05 2.06 0.99 -0.62% slower
arma/arma.stan 0.23 0.23 1.0 -0.2% slower
garch/garch.stan 0.57 0.57 1.0 0.23% faster
Mean result: 0.994183784227

Jenkins Console Log
Blue Ocean
Commit hash: 943ccd3


Machine information ProductName: Mac OS X ProductVersion: 10.11.6 BuildVersion: 15G22010

CPU:
Intel(R) Xeon(R) CPU E5-1680 v2 @ 3.00GHz

G++:
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin15.6.0
Thread model: posix

Clang:
Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin15.6.0
Thread model: posix

@rok-cesnovar rok-cesnovar merged commit 4940a43 into develop Jan 6, 2022
@rok-cesnovar rok-cesnovar deleted the missing_prim_scalar_sigs branch January 6, 2022 14:33
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.

Add stub functions for certain std:: functions
6 participants