-
-
Notifications
You must be signed in to change notification settings - Fork 193
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
fix returned expressions #1644
Conversation
@avehtari Can you check that using this branch fixes the problem? |
Seems to work. |
@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. |
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. |
A test like this will only test a single combination of nested function calls 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. |
I understand that it's hard to test in full generality. Rather than waiting for an automated general solution, I'd like to see the tests added for the cases being fixed to at least ensure the problem is fixed and there are no regressions.
|
Having no testing for this because we can't test everything seems like not a great answer |
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? |
You can add the code. Useful comment would be
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. |
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: |
On Jan 28, 2020, at 2:28 PM, Rok Češnovar ***@***.***> wrote:
Added Aki's and Juho's model to Stan and it fails on current math develop and passes with this PR as Math submodule.
Thanks!
|
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(); |
There was a problem hiding this comment.
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();
}
There was a problem hiding this comment.
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.
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. |
Actually the main benefit of #1471 is using Eigens's implementations of exp that is way faster than std implementation. |
Reverting #1471 might however be an issue due to the flatten. |
Why do the test values need changed? |
I'll try the revert tonight |
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. |
One possible fix for this kind of thing is define a function that composes a variable transform with the function being tested so that it can take unconstrained input values to test.
… On Jan 29, 2020, at 9:59 AM, Tadej Ciglarič ***@***.***> wrote:
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.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
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
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 |
If all checks are passing and this fixes all the upstream issues then let's approve and merge |
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.
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 :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good.
fix returned expressions # Conflicts: # stan/math/prim/fun/log_softmax.hpp
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
Math issue Stan Math functions should not return Eigen expressions #1643
Copyright holder: Tadej Ciglarič
The copyright holder is typically you or your assignee, such as a university or company. By submitting this pull request, the copyright holder is agreeing to the license the submitted work under the following licenses:
- Code: BSD 3-clause (https://opensource.org/licenses/BSD-3-Clause)
- Documentation: CC-BY 4.0 (https://creativecommons.org/licenses/by/4.0/)
the basic tests are passing
./runTests.py test/unit
)make test-headers
)make doxygen
)make cpplint
)the code is written in idiomatic C++ and changes are documented in the doxygen
the new changes are tested