Skip to content

fix returned expressions #1644

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 13 commits into from
Jan 29, 2020

Conversation

t4c1
Copy link
Contributor

@t4c1 t4c1 commented Jan 27, 2020

Summary

PR #1471 generalized some functions to return Eigen expressions. In #1643 it was found that the change prevents compilation of some models. This PR fixes the problem by making those functions return Eigen::Matrix again.

Tests

No tests, as this kind of problem is hard to test in general. But now I know this is an issue It will be simple not to repeat it.

Side Effects

Maybe the code will be a bit slower, but the difference should be less than the speedup introduced in #1471, if it is present at all.

Checklist

@t4c1 t4c1 changed the title fixes returned expressions fix returned expressions Jan 27, 2020
@t4c1
Copy link
Contributor Author

t4c1 commented Jan 27, 2020

@avehtari Can you check that using this branch fixes the problem?

@avehtari
Copy link
Member

Seems to work.
bin/stanc --version gives stanc acfb4612 (Unix) and cmdstanr using that compiles and runs the longest example in that issue.

@rok-cesnovar
Copy link
Member

rok-cesnovar commented Jan 27, 2020

@serban-nicusor-toptal just tagging you that the merge commit of this PR should be cherry-picked to master and released as Stan Math 3.1.1

This has to first pass tests. Let me know if you need any help regarding the patch release.

@bob-carpenter
Copy link
Member

bob-carpenter commented Jan 27, 2020

The code that failed to compile should be added as a test. An alternative is to add the problematic model to the good models in the stan-dev/stan repo.

@avehtari
Copy link
Member

The code that failed to compile should be added as a test. An alternative is to add the problematic model to the good models in the stan-dev/stan repo.

Additional info: That model is our (Aki, Juho Piironen) recommended reference code for logistic regression with regularized horseshoe prior. This will be also included in posteriordb as with some data sets, we get very nice examples of high dimensional multimodal distributions for which dynamic HMC in Stan works well.

@t4c1
Copy link
Contributor Author

t4c1 commented Jan 27, 2020

The code that failed to compile should be added as a test. An alternative is to add the problematic model to the good models in the stan-dev/stan repo.

A test like this will only test a single combination of nested function calls a(b(...)). The problem could potentially happen again for any combination of functions a and b from stan math, where b can return and a can accept Eigen::Matrix. If we want to make tests in a way that will prevent this from happening again all combinations should be tested. And I would prefere not to write these tests manually ...

However not testing that is not that large problem, since avoiding this is easy once one knows it could be an issue. Also once all functions are generalized to accept any Eigen expressions this issue can not happen again.

@bob-carpenter
Copy link
Member

bob-carpenter commented Jan 27, 2020 via email

@SteveBronder
Copy link
Collaborator

Having no testing for this because we can't test everything seems like not a great answer

@rok-cesnovar
Copy link
Member

rok-cesnovar commented Jan 28, 2020

That model is our (Aki, Juho Piironen) recommended reference code for logistic regression with regularized horseshoe prior.

I will make a Stan PR to add this to the good-models. Do I have your permission to add it @avehtari? If yes, I will add you both as copyright holder for the PR. Do you want to add any description as a commented-out line in the stan model?

@avehtari
Copy link
Member

You can add the code.

Useful comment would be

The regularized horseshoe prior code from Appendix C.1 of Piironen and Vehtari (2017). Sparsity information and regularization in the horseshoe and other shrinkage priors. In Electronic Journal of Statistics, 11(2):5018-5051. https://projecteuclid.org/euclid.ejs/1513306866

as this paper and the appendix explains what that model is.

As the code is very direct implementation of math in that paper, it's likely that someone else writing the code just using the equations would get the same result and thus there is no creativity and it should not be copyrightable. I think that comment should be sufficient, but I and Juho don't mind if you want to have us as the copyright holders in the PR.

@rok-cesnovar
Copy link
Member

Added Aki's and Juho's model to Stan and it fails on current math develop and passes with this PR as Math submodule. Once this PR is green we can proceed then.

failing:
https://jenkins.mc-stan.org/blue/organizations/jenkins/Stan/detail/PR-2882/3/pipeline
passing:
https://jenkins.mc-stan.org/blue/organizations/jenkins/Stan/detail/PR-2882/4/pipeline/
parameters of the passing: https://jenkins.mc-stan.org/job/Stan/job/PR-2882/4/parameters/

@bob-carpenter
Copy link
Member

bob-carpenter commented Jan 28, 2020 via email

@SteveBronder
Copy link
Collaborator

Should we just revert #1471 and #1558, do a hotfix release as 1.22.1 and make revert revert PRs for both of those? We need to do a 1.22.1 for cmdstan anyway

I think we went around this the opposite way we should have. We should have the stan repo first accept generic eigen expressions, then do this in math

@@ -41,7 +41,7 @@ template <typename T, require_t<std::is_arithmetic<scalar_type_t<T>>>...>
inline auto log_softmax(const T& x) {
return apply_vector_unary<T>::apply(x, [&](const auto& v) {
check_nonzero_size("log_softmax", "v", v);
return (v.array() - log_sum_exp(v)).matrix();
return (v.array() - log_sum_exp(v)).matrix().eval();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Rather than adding .eval() to each of these functions, you could instead modify the apply_vector_unary header to eval the eigen returns:

  template <typename F>
  static inline auto apply(const T& x, const F& f) {
    return f(x).eval();
  }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I went this way, so we don't need to first eval and than copy matrices in std::vector overloads that assign result of apply_vector_unary::apply() instead of returning it.

@rok-cesnovar
Copy link
Member

Should we just revert #1471 and #1558, do a hotfix release as 1.22.1 and make revert revert PRs for both of those? We need to do a 1.22.1 for cmdstan anyway

We need to do 3.1.1 with either this PR or revert of #1471. I am fine either way. The main benefit of #1471 will be seen after we have everything using eigen expressions anyways I think.

@t4c1
Copy link
Contributor Author

t4c1 commented Jan 29, 2020

Actually the main benefit of #1471 is using Eigens's implementations of exp that is way faster than std implementation.

@rok-cesnovar
Copy link
Member

Reverting #1471 might however be an issue due to the flatten.

@andrjohns andrjohns mentioned this pull request Jan 29, 2020
5 tasks
@SteveBronder
Copy link
Collaborator

Why do the test values need changed?

@SteveBronder
Copy link
Collaborator

I'll try the revert tonight

@t4c1
Copy link
Contributor Author

t4c1 commented Jan 29, 2020

Why do the test values need changed?

At first I just copy-pasted same values for all functions. But it turns out for some functions they were out of range where functions are defined.

@bob-carpenter
Copy link
Member

bob-carpenter commented Jan 29, 2020 via email

@rok-cesnovar
Copy link
Member

I'll try the revert tonight

We either add the merge commit of this PR to master or the revert merge commit PR to master and that becomes Math 3.1.1. Not sure its worth another set of Jenkins tests.

I am also not sure what you mean by

We should have the stan repo first accept generic eigen expressions

There is no issue in the Stan repo. Can you explain?

@SteveBronder
Copy link
Collaborator

We should have the stan repo first accept generic eigen expressions

There is no issue in the Stan repo. Can you explain?

In #1628 We hit some errors upstream in the Stan repo related to passing arbitrary eigen expressions.

I'm working on the indexing functions right now but we should check what else we would want to make sure takes in Eigen matrices. We'll probably need to do this .eval() on all the returns first while we get the signatures lined up. Then have one repo where we remove all the .evals()

@SteveBronder
Copy link
Collaborator

If all checks are passing and this fixes all the upstream issues then let's approve and merge

@rok-cesnovar
Copy link
Member

We'll probably need to do this .eval() on all the returns first while we get the signatures lined up. Then have one repo where we remove all the .evals()

Yep, I see no other way of doing this. We also need to come up with a "eigen expression mother model" that would make sure all functions signatures that accept eigen matrices, then work with eigen expressions.

If all checks are passing and this fixes all the upstream issues then let's approve and merge

It does yes, and I have reviewed changes here and they look fine. I will however go through #1471 and this PR manually to double check no function from the first PR now returns eigen expressions. Then I will merge and that is where @serban-nicusor-toptal comes in :)

Copy link
Member

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

@rok-cesnovar rok-cesnovar merged commit 987ffae into stan-dev:develop Jan 29, 2020
rok-cesnovar added a commit that referenced this pull request Jan 29, 2020
fix returned expressions
# Conflicts:
#	stan/math/prim/fun/log_softmax.hpp
@t4c1 t4c1 mentioned this pull request Feb 19, 2020
5 tasks
@t4c1 t4c1 deleted the fix_returned_expressions branch November 30, 2020 09:25
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.

7 participants