coin-or / CppAD

A C++ Algorithmic Differentiation Package: Home Page
https://cppad.readthedocs.io
Other
446 stars 94 forks source link

Exception thrown in val_graph optimizer #192

Closed metab0t closed 4 months ago

metab0t commented 4 months ago

I try to use val_graph optimizer in the following code:

#include "cppad/cppad.hpp"

int main(void)
{
    using CppAD::AD;
    using CppAD::ADFun;
    using CppAD::Independent;

    std::string opt_options_valgraph = "no_compare_op no_conditional_skip val_graph";
    std::string opt_options_novalgraph = "no_compare_op";
    std::string opt_options = opt_options_valgraph;

    size_t n = 2;
    size_t m = 2;
    size_t k = 1;
    CPPAD_TESTVECTOR(AD<double>) ax(n), ay(m), ap(k);
    Independent(ax, ap);
    ay[0] = ax[0];
    ay[1] = ap[0] * ax[1];
    ADFun<double> f;
    f.Dependent(ax, ay);

    ADFun<AD<double>, double> af = f.base2ad();

    CPPAD_TESTVECTOR(AD<double>) axp(n + k);
    Independent(axp);
    for (size_t j = 0; j < n; ++j)
        ax[j] = axp[j];
    for (size_t j = 0; j < k; ++j)
        ap[j] = axp[n + j];
    CPPAD_TESTVECTOR(AD<double>) az(m * n);
    af.new_dynamic(ap);
    az = af.Jacobian(ax);
    ADFun<double> g;
    g.Dependent(axp, az);
    g.optimize(opt_options);
}

Exception is thrown:

cppad-20240227 error from a known source:
vector: index greater than or equal vector size
Error detected by false result for
    i < length_
at line 542 in the file
    E:\codeworkspace\cpp_try\cppad_try\thirdparty\include\cppad/utility/vector.hpp
Assertion failed: false, file E:\codeworkspace\cpp_try\cppad_try\thirdparty\include\cppad/utility/error_handler.hpp, line 205

By looking at the stacktrace, the problem happens in https://github.com/coin-or/CppAD/blob/05881684eaf38444853cfcbbd947671fae6aae76/include/cppad/local/val_graph/renumber.hpp#L162

The elements in dep_vec_(length = 4) are very big numbers which seem uninitialized.

bradbell commented 4 months ago

See the heading 03-01 on https://cppad.readthedocs.io/latest/2024.html#mm-dd-03-01

The actual fix was in the change to fun2val.hpp in https://github.com/coin-or/CppAD/commit/c06562264d11f8a2a6d2a7f68dc6a69f08598a4d

If you are satisfied with this fix, please close this issue.

metab0t commented 4 months ago

Thanks for the fix! It does solve the problem. Just one extra curious problem @bradbell : what is the advantage of val_graph over cpp_graph for optimization? cpp_graph seems like a stack-based bytecode format of the execution graph already.

bradbell commented 4 months ago

The val_graph optimizer is a complete re-write of the optimizer. It treats dynamic parameter and variables the same and is much simpler in other ways. Unfortunately, the conversion from the representation that computes derivatives to the val_graph representation takes extra memory and time. On the other hand, it may be that the val_graph optimizer is better in some ways. The hope is that the optimizer can be written to be as simple as the val_graph optimizer, but not take the extra conversion time and memory.

Note that you can compare the speed of the resulting function (not counting the optimization) using val_graph or not.

There are examples and documentation for the val_graph representation in the developer build of the documentation. If you would like to know how to build this, open a discussion about the developer documentation.