-
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
crash on jacobian with array inputs #472
Milestone
Comments
Hi @FROL256, This works for me. Can you give a standalone example which crashes? I use cat array.cpp
#include "clad/Differentiator/Differentiator.h"
void h(float a[3], float b, float output[]) {
output[0] = a[0] * a[0] * a[0];
output[1] = a[0] * a[0] * a[0] + b * b * b;
output[2] = 2 * (a[0] + b);
}
int main() {
auto dh = clad::gradient(h);
dh.dump();
return 0;
} vvassilev@vv-nuc ~/workspace/builds/clad $ /usr/lib/llvm-12/bin/clang -x c++ -std=c++11 -Xclang -add-plugin -Xclang clad -Xclang -plugin-arg-clad -Xclang -fdump-derived-fn -Xclang -load -Xclang /home/vvassilev/workspace/builds/clad/lib/clad.so array.cpp -I/home/vvassilev/workspace/sources/clad/test/Hessian/../../include -lstdc++ -lm
void h_grad(float a[3], float b, float output[], clad::array_ref<float> _d_a, clad::array_ref<float> _d_b, clad::array_ref<float> _d_output) {
float _t0;
float _t1;
float _t2;
float _t3;
float _t4;
float _t5;
float _t6;
float _t7;
float _t8;
float _t9;
float _t10;
float _t11;
float _t12;
_t2 = a[0];
_t1 = a[0];
_t3 = _t2 * _t1;
_t0 = a[0];
output[0] = _t3 * _t0;
_t6 = a[0];
_t5 = a[0];
_t7 = _t6 * _t5;
_t4 = a[0];
_t10 = b;
_t9 = b;
_t11 = _t10 * _t9;
_t8 = b;
output[1] = _t7 * _t4 + _t11 * _t8;
_t12 = (a[0] + b);
output[2] = 2 * _t12;
{
float _r_d2 = _d_output[2];
float _r12 = _r_d2 * _t12;
float _r13 = 2 * _r_d2;
_d_a[0] += _r13;
* _d_b += _r13;
_d_output[2] -= _r_d2;
_d_output[2];
}
{
float _r_d1 = _d_output[1];
float _r4 = _r_d1 * _t4;
float _r5 = _r4 * _t5;
_d_a[0] += _r5;
float _r6 = _t6 * _r4;
_d_a[0] += _r6;
float _r7 = _t7 * _r_d1;
_d_a[0] += _r7;
float _r8 = _r_d1 * _t8;
float _r9 = _r8 * _t9;
* _d_b += _r9;
float _r10 = _t10 * _r8;
* _d_b += _r10;
float _r11 = _t11 * _r_d1;
* _d_b += _r11;
_d_output[1] -= _r_d1;
_d_output[1];
}
{
float _r_d0 = _d_output[0];
float _r0 = _r_d0 * _t0;
float _r1 = _r0 * _t1;
_d_a[0] += _r1;
float _r2 = _t2 * _r0;
_d_a[0] += _r2;
float _r3 = _t3 * _r_d0;
_d_a[0] += _r3;
_d_output[0] -= _r_d0;
_d_output[0];
}
}
vvassilev@vv-nuc ~/workspace/builds/clad $ ./a.out
The code is:
void h_grad(float a[3], float b, float output[], clad::array_ref<float> _d_a, clad::array_ref<float> _d_b, clad::array_ref<float> _d_output) {
float _t0;
float _t1;
float _t2;
float _t3;
float _t4;
float _t5;
float _t6;
float _t7;
float _t8;
float _t9;
float _t10;
float _t11;
float _t12;
_t2 = a[0];
_t1 = a[0];
_t3 = _t2 * _t1;
_t0 = a[0];
output[0] = _t3 * _t0;
_t6 = a[0];
_t5 = a[0];
_t7 = _t6 * _t5;
_t4 = a[0];
_t10 = b;
_t9 = b;
_t11 = _t10 * _t9;
_t8 = b;
output[1] = _t7 * _t4 + _t11 * _t8;
_t12 = (a[0] + b);
output[2] = 2 * _t12;
{
float _r_d2 = _d_output[2];
float _r12 = _r_d2 * _t12;
float _r13 = 2 * _r_d2;
_d_a[0] += _r13;
* _d_b += _r13;
_d_output[2] -= _r_d2;
_d_output[2];
}
{
float _r_d1 = _d_output[1];
float _r4 = _r_d1 * _t4;
float _r5 = _r4 * _t5;
_d_a[0] += _r5;
float _r6 = _t6 * _r4;
_d_a[0] += _r6;
float _r7 = _t7 * _r_d1;
_d_a[0] += _r7;
float _r8 = _r_d1 * _t8;
float _r9 = _r8 * _t9;
* _d_b += _r9;
float _r10 = _t10 * _r8;
* _d_b += _r10;
float _r11 = _t11 * _r_d1;
* _d_b += _r11;
_d_output[1] -= _r_d1;
_d_output[1];
}
{
float _r_d0 = _d_output[0];
float _r0 = _r_d0 * _t0;
float _r1 = _r0 * _t1;
_d_a[0] += _r1;
float _r2 = _t2 * _r0;
_d_a[0] += _r2;
float _r3 = _t3 * _r_d0;
_d_a[0] += _r3;
_d_output[0] -= _r_d0;
_d_output[0];
}
} |
I can reproduce the crash if I change @Nirhar, can you take a look what is wrong here? |
Nirhar
added a commit
to Nirhar/clad
that referenced
this issue
Aug 3, 2022
…bian Mode Now One must be able to find the Jacobian of functions with Constant Arrays in the parameter list. For example, a function of the form: ```cpp void func(double arr[3], double x, double y, double* output){ output[0]=arr[2]*x*y; . . . output[n-1]=arr[0]*arr[1]*arr[2]; } ``` will generate a Jacobian of size n x 5. Corresponding tests for the same have been written. Closes vgvassilev#472
Nirhar
added a commit
to Nirhar/clad
that referenced
this issue
Aug 12, 2022
…bian Mode Now One must be able to find the Jacobian of functions with Constant Arrays in the parameter list. For example, a function of the form: ```cpp void func(double arr[3], double x, double y, double* output){ output[0]=arr[2]*x*y; . . . output[n-1]=arr[0]*arr[1]*arr[2]; } ``` will generate a Jacobian of size n x 5. Corresponding tests for the same have been written. Previously this feature was not implememnted because it was necessary to know the size of an array to correctly determine the size of the output jacobian matrix. This has now been achieved for constant arrays, as we know the array sizes for them and hence we can precisely locate where each jacobian entry for a array parameter is located. This is achieved with the help of m_IndependentVarsSize, which stores the number of actual parameters that each function parameter corresponds to. Prior to this commit a std::map<ValueDecl, Expr> was used to map variable declarations to their corresponding jacobian expression, so that we can lookup the latter during the reversemode computations quickly. While this is fine for primitive variables(because each variable will only correspond to one jacobian expression), an array declaration will correspond to multiple jacobian expressions, depending on which index of the array is being referred to. Since the ValueDecl can only refer to the name of the array and not name+index, we must update the map to use the name of the array + index as the key. This can be done easily by using a string representation of the ValueDecl name with the index as a suffix as the key of the map. Hence variables like m_ExprVariables, m_VectorOutputString have been introduced to map array declarations, indexed by position in array to their corresponding jacobian expressions. Closes vgvassilev#472
Nirhar
added a commit
to Nirhar/clad
that referenced
this issue
Aug 12, 2022
…bian Mode Now One must be able to find the Jacobian of functions with Constant Arrays in the parameter list. For example, a function of the form: ```cpp void func(double arr[3], double x, double y, double* output){ output[0]=arr[2]*x*y; . . . output[n-1]=arr[0]*arr[1]*arr[2]; } ``` will generate a Jacobian of size n x 5. Corresponding tests for the same have been written. Previously this feature was not implememnted because it was necessary to know the size of an array to correctly determine the size of the output jacobian matrix. This has now been achieved for constant arrays, as we know the array sizes for them and hence we can precisely locate where each jacobian entry for a array parameter is located. This is achieved with the help of m_IndependentVarsSize, which stores the number of actual parameters that each function parameter corresponds to. Prior to this commit a std::map<ValueDecl, Expr> was used to map variable declarations to their corresponding jacobian expression, so that we can lookup the latter during the reversemode computations quickly. While this is fine for primitive variables(because each variable will only correspond to one jacobian expression), an array declaration will correspond to multiple jacobian expressions, depending on which index of the array is being referred to. Since the ValueDecl can only refer to the name of the array and not name+index, we must update the map to use the name of the array + index as the key. This can be done easily by using a string representation of the ValueDecl name with the index as a suffix as the key of the map. Hence variables like m_ExprVariables, m_VectorOutputString have been introduced to map array declarations, indexed by position in array to their corresponding jacobian expressions. Closes vgvassilev#472
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
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
vgvassilev
pushed a commit
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 #472, Fixes #1057, Fixes #480, Fixes #527
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello! I took an example of jacobian from main page (h). If i make the first parameter array, clad crashes :(
Input:
Error:
PS: reproduces both with current version in master and release v0.9.
Thank you very much! :)
The text was updated successfully, but these errors were encountered: