Someone has made a PR to merge their autodiff library into Boost Math
It appears to be forward-mode only and relying on C++17 constructs. But it is perhaps worth commenting on the implementation and / or considering whether Stan Math could, at some point, use that for forward mode and drop the partial implementation that we have had for a long time.
My impression was that we could rely on Boost’s autodiff to get a basic forward mode implementation working, take advantage of the fact that they are unit testing it, and then incrementally add functions and analytic derivatives as needed. For example, Boost Math doesn’t have log_sum_exp but we can call a templated log_sum_exp(autodiff_fvar& x, autodiff_fvar&y) and it should produce a valid autodiff_fvar as the output. We could specialize it to do the derivative analytically like we do with vars but that mostly just saves RAM.
Our forward-mode autodiff is very well tested through the C++ math library level. And mostly through our non-matrix functions. So I’m still not sure what this gains us other than having it be someone else’s library rather than ours. We’re still going to want to test all of our functions.
It depends on the function and the order. With something like fvar<var>, you want to make sure to call the var version of log_sum_exp internally. Then you get the usual efficiency (time and space both) from implementing analytic gradients. For example,
Writing a custom gradient function for \frac{\exp(x)}{\exp(x) + \exp(y)} would save a lot of evals, especially if we share evals for \frac{\partial}{\partial x} and \frac{\partial}{\partial y}.
Every time we save memory, we also save time in propagating gradients.
That is what I am saying could reduce our development burden. We shouldn’t need to test things like log(autodiff_fvar& x) unless we specialize its gradient. And this would allow us to have a complete-ish forward mode autodiff that algorithms use while we remove bottlenecks incrementally by specializing the gradients.
And merged. Presumably we have a few months before Boost 1.71 is released to make more comments on the implementation and see if we want to use it for fwd mode in Stan.
Stan has all the C++ library functions and almost all of our other unary functions well tested.
What’s not well tested is the matrix library functions and all of our compound functions. I’m not sure exactly where @syclik is at in terms of testing requirements for forward mode. Can we just write templated code, test it on double and declare it done for forward mode?
Yup. That’s all we need if we’re not writing template specializations for forward mode. If someone does write template specializations, then that needs to be tested. All our basic functions are tested pretty well.
Then our existing higher-order autodiff is probably close to being fully tested, then. I think everywhere we have specialized autodiff code for forward mode, we have pretty good tests.
Yes. I agree. It’s close! We don’t have that many template specializations for forward or mix. And if we find that those are problematic, we can write tests, remove the implementation, and reimplement later.
The compiler errors we saw once upon a time… they might not happen with the updated windows compiler. It’s just something we need to check. The unit tests are active again on Windows, so anything that’s explicitly tested works (as well as the tests specify).
I think @bgoodri is implying it would save us testing, but I don’t see how it would save us testing given that we need to make sure everything works with our rev mode.