-
Notifications
You must be signed in to change notification settings - Fork 122
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
Incorect values for Jacobian when one of the outputs is built with intermediate steps #527
Milestone
Comments
same problem as #473? |
PetroZarytskyi
added a commit
to PetroZarytskyi/clad
that referenced
this issue
Nov 14, 2024
This was referenced Nov 14, 2024
PetroZarytskyi
added a commit
to PetroZarytskyi/clad
that referenced
this issue
Nov 19, 2024
PetroZarytskyi
added a commit
to PetroZarytskyi/clad
that referenced
this issue
Nov 19, 2024
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, vgvassilev#1057, vgvassilev#480, vgvassilev#527
PetroZarytskyi
added a commit
to PetroZarytskyi/clad
that referenced
this issue
Nov 19, 2024
PetroZarytskyi
added a commit
to PetroZarytskyi/clad
that referenced
this issue
Nov 19, 2024
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
added a commit
to PetroZarytskyi/clad
that referenced
this issue
Nov 19, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Code to reproduce:
Both versions will output:
[64, 190, 0, 0]
as the Jacobian result gets summed column wise.However, if the intermediate step is not present, then the corect result is computed:
[10, 90, 54, 100]
:In case of
test1
andtest2
thejacobianMatrix
indexing is at fault:In the case of
test3
the Jacobian is generated as:The text was updated successfully, but these errors were encountered: