-
-
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
Feature/issue 2401 alg solver adjoint #2421
Feature/issue 2401 alg solver adjoint #2421
Conversation
…4.1 (tags/RELEASE_600/final)
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.
Took a quick scan and left a few comments. I'll need to look closer to see if there's any more room for cleanups/whatever. I think 1st just getting the algo running without segfaults and try a few benchmarks before we worry too much about that.
@jgaeb yoyo, I was looking at the algebra_solver to retool some of the internals for #2384 Basically the change that needs to happen is make this support variadic arguments in the same way that reduce_sum and the ode solvers currently do. I don't think we'll expose this to the Stan language initially -- just add all the internals. In terms of the algebra solver this would mean that the parameters can be a mix of anything (the solution would still be a vector). Since the internal solvers really only need gradients with respect to the solutions vector, I think this won't be too complicated (we don't need to wire all the variadic stuff into Kinsol). Since you are working directly on this stuff right now, want to meet and pair program this at some point? I don't want to refactor algebra_solver stuff and leave you out of the loop :D. |
Hi @bbbales2 — that would be great! I'll have a lot of free time next week, if that would work for you. (Also, this week is finals week, so I've been a little overwhelmed, but I'm hoping to get to all of your and @SteveBronder's comments next week / starting on Friday!) |
Next week is good. I'm free anytime other than Monday at 3PM and Thursday at 12PM (all eastern time zone). Probly plan for like a 2 hour block? We can probably hammer out quite a bit of stuff in that amount of time. Don't worry too much about fixing things here it they don't make sense. I can explain those things in person probably pretty easily. |
Cool! I sent you calendar invites for a couple of times to meet. Feel free just to cancel whichever one is less convenient. If neither of those is convenient for whatever reason, just let me know and I can try and suggest some other blocks that hopefully work a little better. |
Remove consts, and initialize objects that need to be destructed when they go out of scope in the arena.
…4.1 (tags/RELEASE_600/final)
I tried to make most of the changes @bbbales2 and @SteveBronder asked for. A couple I didn't yet just because I'm still a little confused, but hopefully once I get a chance to talk with @bbbales2 tomorrow, I'll have a better sense of what's going on and be able to make those changes. Previously some of the tests weren't passing because I was passing Now, the only tests that aren't passing are the (Also, I didn't realize that the code was being automatically reformatted, so I didn't pull before making these changes. I've added a couple commits to deal with that. When we actually get to the point of merging this pull request, I'm assuming I can just clean up the git history? Or should I plan on something else? Please let me know if I should be, e.g., making these changes in a scratch branch and then only merging and pushing periodically.) |
…4.1 (tags/RELEASE_600/final)
Don't worry about it. Our git history is super messy. The autoformatter does its thing and then you gotta remember to pull or you'll have to do a local merge. The only big downside is the annoyance of the merge. |
…aeb/math into feature/issue-2401-alg-solver-adjoint
…4.1 (tags/RELEASE_600/final)
@jgaeb want to do another call sometime this week and finish off the variadic stuff? |
@SteveBronder — it looks like the expression tests are still failing for the algebra solver, with the following error:
I'll see if I can figure out what's going on locally, but I won't push any changes so you can see the error for yourself. |
Ok, I think I tracked down the bug that was causing the expression tests to fail! I've got my fingers crossed that it builds this time 🤞 |
@SteveBronder So, the expression tests ran fine, but there was a problem with the
I think the way to solve this is to ensure that the plain_type_t<decltype(to_ref(value_of(x)))> x_ref = to_ref(value_of(x)); from auto& x_ref = to_ref(value_of(x)); It still seems to compile OK on my end, but the typename is pretty nasty, so I'm guessing there's a better way to do it. I just wanted to check that this is the idiomatic way of converting something (here the |
Hey sorry for the delay was finishing up a few little things, I will look at this tmrw morning! |
I think you might just need auto&& x_ref = to_ref(value_of(x)); or just a const auto& x_ref = to_ref(value_of(x)); if your not modifying x I know that looks really odd but the below does a good job of explaining it |
@SteveBronder — Unfortunately, the integration tests are still failing for sort of a weird reason. The algebraic functor generated in struct algebra_system_functor__ {
template <typename T0__, typename T1__, typename T2__>
Eigen::Matrix<stan::promote_args_t<stan::value_type_t<T0__>, stan::value_type_t<T1__>,
T2__>, -1, 1>
operator()(const T0__& x, const T1__& y, const std::vector<T2__>& dat,
const std::vector<int>& dat_int, std::ostream* pstream__) const
{ This gets passed around as auto f_wrt_x = [&args_vals_tuple, &f, msgs](const auto& x) {
return apply(
[&x, &f, msgs](const auto&... args) { return f(x, msgs, args...); },
args_vals_tuple);
}; all the args in Do you have any thoughts about the right way to deal with this? There must be a simpler way to deal with it than changing how the arguments are templated in the (On a separate note, using |
…ainable pointer for the arguments passed to the user defined function (UDF).
@jgaeb I think I sorted it out. We had to hard copy |
Something weird seems to be happening with Jenkins... the tests have run and then aborted in the middle like ten times since I last checked in yesterday. Any idea what's going on? |
Not sure, I tried restarting them but yeah something weird seems to be happening on jenkins. Let me restart it one more time to see what's going on |
Jenkins Console Log Machine informationProductName: Mac OS X ProductVersion: 10.11.6 BuildVersion: 15G22010CPU: G++: Clang: |
Jenkins Console Log Machine informationProductName: Mac OS X ProductVersion: 10.11.6 BuildVersion: 15G22010CPU: G++: Clang: |
@SteveBronder—Holy cow! It built! |
Alright! Lemme give this one more lookover tmrw then I think we'll be good! |
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.
Alrighty lgtm!!!
Awesome! Thanks for sticking with this, @SteveBronder, @bbbales2, and @charlesm93 (along with everybody else who helped out along the way). I really appreciate all the work you put in helping me through this, and realize it would have been much faster for somebody else to implement. I learned a ton diving into this, and am looking forward to continuing to contribute (hopefully in ways that require less of your time!) in the future! |
@jgaeb thanks and congrats on your first merged PR to the Stan Math repository! 🎉 🎉 |
Summary
Fixes Issue #2401. (See the discourse thread here.)
This pull request changes the implementation of auto diff in the Powell and Newton solvers to more efficiently compute cotangents by replacing matrix inversion with a smaller number of matrix solves.
(Parallel changes were made for the fixed point solver, but those will be put into a different PR.)
The new solution method was benchmarked against the old solution method across a variety of problem sizes. (See this comment.)
Tests
Additional tests have been added for the variadic interfaces, and
make_unsafe_chainable_ptr()
.Side Effects
In the course of making those changes, variadic interfaces (
algebra_solver_powell_impl
andalgebra_solver_newton_impl
) were added for both solvers.Release notes
Updated Powell and Newton solvers to use an adjoint method to propagate derivatives in reverse mode. Should result in modest speed-up. Added variadic interfaces (
algebra_solver_powell_impl
andalgebra_solver_newton_impl
).Checklist
Math issue Algebraic Solver Differentiation Speedup #2401
Copyright holder: Johann D. Gaebler
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