-
-
Notifications
You must be signed in to change notification settings - Fork 193
Let prim functions return expressions #2190
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
Conversation
…4.1 (tags/RELEASE_600/final)
…4.1 (tags/RELEASE_600/final)
…4.1 (tags/RELEASE_600/final)
…4.1 (tags/RELEASE_600/final)
… into return_expressions2
Weird include error on poisson posted below
It looks like this is a clang specific error, but calling T_partials_return logp = stan::math::sum(multiply_log(n_val, lambda_val)); also gives an error that it can't find the correct sum function (it want's the
and that should be
I'm pretty sure I did that so my b. I fixed it over here, though I think just doing the above could work. https://github.com/stan-dev/math/compare/fix/include-stuff-for-eigen-exprs |
Thanks, that seems to work |
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.
Bunch o' comments and questions. Nothing big. Code looks good.
template <typename T, require_t<std::is_rvalue_reference<T&&>>* = nullptr, | ||
std::enable_if_t< | ||
static_cast<bool>(Eigen::internal::traits<std::decay_t<T>>::Flags& | ||
Eigen::NestByRefBit)>* = nullptr> |
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.
What is a NestByRefBit
? This should probably go in the docs. I didn't find it searching Google
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.
Eigen is stuck in C++03, in which rvalue references and perfect forwarding do not exist. So when constructing expressions, they can not know which arguments are rvalues and might go out of scope before the expression is evaled. Instead they decide which types to store by reference and which by value depending on the argument type. Lightweight objects, such as operations do not set NestByRefBit
and are stored by value. Heavy objects, such as matrices set NestByRefBit
and are stored by reference.
@t4c1 two more questions |
Jenkins Console Log Machine informationProductName: Mac OS X ProductVersion: 10.11.6 BuildVersion: 15G22010CPU: G++: Clang: |
Alright, expressions are tested and should work so I merged But @rok-cesnovar @SteveBronder @andrjohns @wds15 if you see something weird, say something. This is a pretty big new feature! |
@t4c1 @bbbales2 @SteveBronder Could be irrelevant (as I'm using unsupported development combination), but I wanted to report it anyway. I know that
|
Thanks for the report. Is this using the old stan to c++ compiler? Did you use what rstan uses but just swapped out the math? |
Yeah thanks for checking this. For clarification, is this breaking the current rstan develop branch? Or a custom rstan branch with a manually updated math? |
This breaks a custom PS: I'd like to push my changes in PRs but need time to assure backward compatibility. |
The problem here is that this math version will only work properly for all cases with the latest stanc3 version. That will be available to rstan in the next release and hopefully in the next few weeks on Github. Before that happens you need to revert this PR on your fork. |
Sounds good. Thanks @rok-cesnovar! |
The problem is that user defined functions need to be generated in such a way that they are able to support input Eigen expressions. And stanc2, which is whar cran rstan uses, does not have that support. |
I see. Looking forward to seeing |
@rok-cesnovar I confirm that modifying See hsbadr/rstan@114552e. |
That is great to hear! Thanks. |
@rok-cesnovar Where can I follow the active development of |
I believe its this: https://github.com/stan-dev/rstan/tree/try_2.25 and its currently blocked by stan-dev/stanc3#762 |
Thanks @rok-cesnovar! That helps. |
Partially revert stan-dev#2190. Signed-off-by: Hamada S. Badr <[email protected]>
Introduce `USE_STANC3` macro to branch changes specific to Stan 2.26 that cannot be supported by the older version of `stanc` transpiler. Partially reverts stan-dev#2190. Signed-off-by: Hamada S. Badr <[email protected]>
Summary
I went over all prim functions and modified them to return Eigen expressions, where it makes sense to do so.
I also added a new helper functionto_forwarding_ref
, which is used, where we need to store equivalent ofto_ref
in aholder
. It works the same asto_ref
, except it does perfect forwarding when output does not need to be converted to plain type. In other words it can return rvalue reference to input if the input does not need to be converted.Improved
make_holder
, so that holder is not used if the given functor returns plain type or if all arguments are of types, which Eigen passes around by value in expressions.Tests
All changes should be covered by expression testing framework. I am hoping nothing goes up in flame.
Side Effects
Like ... this whole PR? We never had exposed functions returning expressions.
Release notes
Prim functions now return expressions where that makes sense.
Checklist
Math issue Generalize matrix function signatures #1470
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 test-math-dependencies
)make doxygen
)make cpplint
)the code is written in idiomatic C++ and changes are documented in the doxygen
the new changes are tested