-
-
Notifications
You must be signed in to change notification settings - Fork 188
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
Let value_of and value_of_rec return expressions #1872
Let value_of and value_of_rec return expressions #1872
Conversation
Jenkins Console Log Machine informationProductName: Mac OS X ProductVersion: 10.11.6 BuildVersion: 15G22010CPU: G++: Clang: |
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.
Couple queries, mostly around return types with forwarding references. Otherwise looking pretty good!
…4.1 (tags/RELEASE_600/final)
Jenkins Console Log Machine informationProductName: Mac OS X ProductVersion: 10.11.6 BuildVersion: 15G22010CPU: G++: Clang: |
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.
LGTM!
@t4c1 Is there a design doc or anything on these expressions? Or if there isn't one, do you mind starting one? I don't mind using these things, and I don't doubt that they can be useful, but I had a run-in with this pull request where I was banking on The line in question is here. In that case I'm trying to save a copy of all the arguments in a tuple for the reverse pass. An expression won't work, cause the original memory will go out of scope. I guess vaguely questions I have for this are:
|
I'll leave the rest of the question to tadej, but this should fix your code:
Adding |
Oooo, right, and then the expression would copy. That makes sense thanks. |
There is no design doc. Since Eigen was used by Stan Math for a long time I assumed people were somewhat familiar with it. I guess I was wrong and design doc really would be helpful. Anyway I will try to answer your questions here:
|
Ha! Tricked you.
Yuck. Thanks for the explanation though. I saw some function calls like this the other day and wanted to change them to
Oh I see. Yes this would be good thing to talk about in a design doc, especially the rollout, since this leads to accidental breaks.
Oooh, so I suppose part of this will include some stanc3 analysis to make sure this doesn't happen? |
I intended this to be transparent to Stan language and compiler - it all stays within Math. But if compiler can be somehow used to check that, I am alll for it. |
Well since this could cause slowdowns if we accidentally use expressions more than once, we kinda have to push this up to the language level too to avoid getting silently bit by it. |
I really do not understand how do you propose to do it. AFAIK stanc parses stan lang not C++. And this changes happen in C++. |
Just to clarifiy: matrix A[5,5] = D*F + 2*C*(D+4);
matrix B[5,5] = A*A + A*A; This isn't problematic as Anyhow, yes. Lets make a doc. |
Right. Doc PR is up here: stan-dev/design-docs#23 |
Actually not to reduce copies. To reduce number of times matrices are loaded from RAM to CPU. |
Yeah I meant the "copy" from RAM to the CPU. I didn't realize there was a doc already. Thanks. |
Ooooh, okay, I see. Yeah that was what I was worried about. As long as
Well it uses the C++ and makes assumptions about the C++. I vaguely remember at one point discussions about:
Like should that be a This would mean leaning on C++ auto for types is a no-go, but not sure if that problem still exists or whatever, but they are connected. |
Summary
value_of
andvalue_of_rec
now can now return Eigen expressions. So if their result is only used in a single expression it needs not be saved in memory. We can do that even before all functions are generalized as these two functions are not exposed to stan lang.value_of_rec
now also uses perfect forwarding.All functions using eigher of these two functions and acceptiong the result into a variable of type auto can only use such a variable once. Otherwise they would evaluate the expression multiple times. If they need to use it multiple times they can accept the result into
Eigen::Ref
. A helper fucntionto_ref
has been added to simplify that. All existing cases that would evaluate result of one of these two functions have been fixed usingto_ref
.Tests
Added tests for
value_of
andvalue_of_rec
used on expressions.Side Effects
See second paragraph of the Summary.
Release notes
value_of
andvalue_of_rec
now can now return Eigen expressions.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