I pushed a script called tmp_test.cpp in the latest commit to the branch feature/issue-202-vectorize-all-binary. It’s basically a sandbox to compare the 2nd derivatives of applying binary_foo (the vectorized function) on fvar<var>, int and on fvar<var>, std::vector<int> for the problematic case mentioned above of x = 0, y = -4 and the 2nd derivative involving fvar<var>.d_.grad().
Using the script, I noticed the following. The 2nd derivative of applying binary_foo on fvar<var>, int is -Inf. I think this is the correct value because that’s what I got when playing around with the mix pow test. Then, for clarity, I’m going to include a summary of the vectorization code:
std::vector<int> y_vector(5, -4);
std::vector<fvar<var> > result_v(y_vector.size());
for (size_t i = 0; i < std::vector<int>.size(); ++i) {
result_v = binary_foo(x, y_vector[i]);
}
-
If I take the derivative within the
forloop with respect tox.d_, the derivative is-
-Inffor i = 0 -
nanotherwise
-
-
If I refresh the memory using
recover_memory_nested()within the loop or rebuild the fvar, then the derivative is-Inffor all i. However, if I do this, it doesn’t seem like I can take the derivative with respect to my original fvar x because the 2nd derivative of the first element ofresult_vis0. -
If I use
recover_memory()within the for loop (as seen in the script), the 2nd derivative of the first element ofresult_visInf. -
If I leave the vectorization code as is, I get
nanfor my derivative.
All of this makes me think the issue is that I’m not handling memory correctly in my attempt to broadcast. Is there a good example in the math library for how to do this? I’ve been looking at probability functions and the add function in prim/mat/fun, but I’m wondering if there’s another function that I can look at. The script can be found in test/unit/math/mix/mat/vectorize and is commented.