CppADCodeGen icon indicating copy to clipboard operation
CppADCodeGen copied to clipboard

Hessian computation via CGAtomicGenericModel nested in a larger CppAD::ADFun

Open russelmann opened this issue 4 years ago • 1 comments

I am trying to use a cg::CGAtomicGenericModel to compute the Hessian of a CppAD::ADFun. For example in this schematic code:

...
CGAtomicGenericModel<double> atomic_model;
ADFun<double> fun;
...
std::vector<AD<double>> X, Y, Z;
Y = atomic_model(X, Y);
Z = Y * Y; // some nonlinear function
fun(X, Z);
...
fun.sparse_hes(x, w, hess_subset, hess_pattern, "cppad.general", hes_work);

This would result in the following error:

cppad-20190806 error from a known source:
Second-order reverse mode failed: py[2*i] (i=0...m) must be zero.
Error detected by false result for
    ret != 1
at line 552 in the file 
    /Users/rguseino/sdk/cppadcodegen/include/cppad/cg/model/functor_generic_model.hpp
Assertion failed: (false), function Default, file /Users/rguseino/sdk/cppad/include/cppad/utility/error_handler.hpp, line 206.

In case Z is linear in Y, the error is not reproduced. Does this mean that for now CGAtomicGenericModel is not supposed to be used in such scenario? Is there a workaround for this problem? P.S. This happens in my large project but, if necessary, I can write simple code to reproduce this behavior.

russelmann avatar Nov 28 '19 10:11 russelmann

Hello Ruslan,

This is not implemented yet. This would have to be done in ModelCSourceGen<Base>::generateReverseTwoSources(). There is an explicit check for this in the generated code.

joaoleal avatar Dec 01 '19 21:12 joaoleal