# Error not systematically caught by unit test

**URL:** <https://discourse.mc-stan.org/t/error-not-systematically-caught-by-unit-test/281>\
**Category:** Developers\
**Created:** [March 2, 2017, 3:20pm UTC](https://discourse.mc-stan.org/t/error-not-systematically-caught-by-unit-test/281 "2017-03-02T15:20:18Z")\
**Posts on this page:** 2\
**Page:** 2

<div class="post-metadata">

**Author:** ![charlesm93](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/charlesm93/32/4634_2.png) [@charlesm93](https://discourse.mc-stan.org/u/charlesm93)\
**Post date:** [March 24, 2017, 7:06pm UTC](https://discourse.mc-stan.org/t/error-not-systematically-caught-by-unit-test/281/21 "2017-03-24T19:06:40Z")

</div>

I’m happy to report Bob and I fixed the issue.

What caused the bug: you cannot call Jacobian() inside a chain method. One run of Auto-Diff interferes with the other. In particular, once memory is freed by the Jacobian, it messes with the memory allocated for chain() or whatever gradient computation may be going on.

As a reminder, this is problematic, because we calculate the adjoint of the parameters _y_ for the algebraic solver using Jacobians (as prescribed by the “implicit function theorem” technique, see [Implicit Function Theorem Math](http://discourse.mc-stan.org/t/implicit-function-theorem-math/264/4)).

The solution is to compute these Jacobians before the chain method gets called. As it is, we do this in the constructor of the vari. Jx\_y (jacobian of the solution with respect to the auxiliary variables) is stored as a member of vari, and can then be used inside the chain method.

We’ll probably need to address the special case where the user passes a null vector for y (i.e. no parameters involved, all fixed data).

For completion, I’ll mention we revised the structure of the algebra\_solver\_vari structure. I initially followed the example of quad\_form, but apparently there is some fancy memory work going on there we’ll want to revise eventually.

---

<div class="post-metadata">

**Author:** ![Bob\_Carpenter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bob_carpenter/32/9230_2.png) [@Bob\_Carpenter](https://discourse.mc-stan.org/u/Bob_Carpenter)\
**Post date:** [March 24, 2017, 10:21pm UTC](https://discourse.mc-stan.org/t/error-not-systematically-caught-by-unit-test/281/22 "2017-03-24T22:21:14Z")

</div>

Thanks for reporting back. Hope this lets me comment on a solved issue!

[Previous page](https://discourse.mc-stan.org/t/error-not-systematically-caught-by-unit-test/281.md?page=1)
