Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reimplement jacobians using the vectorized forward mode #1121

Merged
merged 4 commits into from
Nov 19, 2024

Commits on Nov 19, 2024

  1. Reimplement jacobians using the vectorized forward mode.

     Previously, jacobians were based on the non-vectorized reverse mode, which was mostly incapable of capturing multiple outputs. The implementation worked in a few particular cases. For example, it was not possible to differentiate function calls or declare variables inside the original function body.
     This PR implements jacobians using the vectorized forward mode. At the very least, this will solve the issues described above and give a way forward to solve other ones. This also means introducing features to the vectorized fwd mode will introduce the same features to jacobians.
     Let's take a look at the new signature of jacobians. Since the function to be differentiated is expected to have multiple outputs, we should expect the output in the form of array/pointer/reference parameters (just like before). And for every output parameter, we should generate a corresponding adjoint parameter for the user to acquire the results. Since there is no way to specify which parameters are used as output and which are not, adjoints are generated for all array/pointer/reference parameters. For example:
    
    ```
    void f(double a, double b, double* c)
     -->
    void f_jac(double a, double b, double* c, <matrix<double>* _d_c)
    ```
    
    or
    
    ```
    void f(double a, double b, double* c, double[7] t)
     -->
    void f_jac(double a, double b, double* c, double[7] t,
     array_ref<matrix<double>> _d_c, matrix<double>* _d_t)
    ```
    
    This signature is also similar to the old one. e.g.
    ```
    df.execute(a, b, c, result); // old behavior
    df.execute(a, b, c, &result); // new behavior
    ```
    However, the behavior differs for multiple output parameters, which the old jacobians did not support.
    
     Note: the same functionality can be achieved by using the vectorized reverse mode, which should probably be implemented at some point. However, the old code for jacobians is unlikely to be useful for that, and there is not much point in keeping it.
    
    Fixes vgvassilev#472, Fixes vgvassilev#1057, Fixes vgvassilev#480, Fixes vgvassilev#527
    PetroZarytskyi committed Nov 19, 2024
    Configuration menu
    Copy the full SHA
    6a18cb9 View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    8c8ea59 View commit details
    Browse the repository at this point in the history
  3. Configuration menu
    Copy the full SHA
    0fe5627 View commit details
    Browse the repository at this point in the history
  4. Configuration menu
    Copy the full SHA
    29ad5f6 View commit details
    Browse the repository at this point in the history