> On Apr 19, 2017, at 1:35 PM, Ben B <
bbba...@gmail.com> wrote:
>
> Good stuff! Looks like you've done your homework.
>
> I went through the file you posted and modified it:
https://gist.github.com/bbbales2/e351f1b3815f3cc875a36b39d5ef69ea
>
> (I can get rid of that gist after this thread is over if you don't want that floating around)
>
> There's a class in Stan, stored_gradient_vari, that does what precomputed_gradients_vari does, so you don't have to worry about that.
>
> 1. I'm not 100% sure, but I think you need to keep the templating to keep the linker happy. Not sure. The issue I had was that Stan wanted a copy of my function with the parameters instantiated as vars as well as a version with doubles. I think you need to define the function with all the templating, and then provide the two template specializations the linker wants. This is just for linking with an actual Stan model though. You can prototype this as just a Stan Math thing without having to deal with the templating at first.
It depends how you put your libs together.
If your function takes N arguments and you want to include it in Stan,
it needs to admit all 3^N combinations of integer/double/autodiff var.
You can do this with a single templated function, multiple templated
functions (either overloads or specializations), or with pure overloading.
Which is easiest will depend on the size of N and the nature of the function.
> 2. I changed around how you were doing this (
https://gist.github.com/bbbales2/e351f1b3815f3cc875a36b39d5ef69ea). The big thing is you want the input and output of your new autograd-friendly function to be the same shapes as if the autograd wasn't there. The derivatives hide inside the vars. You don't want to flatten that structure.
Exactly what you need to do.
> 3. (b) Operands is just a list of pointers to other varis, I think you did it right
>
> Hahaha, you've picked a good place to start learning C++ (this isn't trivial)!
No kidding on the non-trivial part. It tooks us years to
work up to this, which is why I tried to write it all out
in the autodiff paper. Wish we'd had that before we started (but
then I always write papers aimed at my younger self).
> I'd recommend that you build a little cpp test file separately to get this stuff working. Don't try to get it all linking up with Stan in one go. Just pretend you're adding another operator to the Stan Math library. Get that working first (by comparing with finite difference derivs) and then move on from there.
Absolutely the only sensible way to proceed. People always think writing
tests will slow them down, but the key thing to do is to design top down,
but code bottom up, testing each infrastructure layer very thoroughly so
that when you go a layer up, you can trust your foundation. Anything else
is too unpredictable and prone to lead to impossibly tangled debugging
situations.
- Bob