-
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
Bug: passing around C-style array parameters crashes Clad #762
Comments
That is probably related to #735. |
This issue is related to the initialization of derivatives of pointer arguments. The error is removed when we also include auto grad = clad::gradient(wrapper, "params, obs");
double param = 2.0;
double obs = 2.0;
double _d_params = 0.0;
double _d_obs = 0.0;
grad.execute(¶m, &obs, &_d_params, &_d_obs); |
Hi @vaithak, thanks for the reply! That would not solve the problem in my use case I think, because doesn't differentiating also with respect to Ans does Clad really have to know the size of obs if it only contains constants? I mean if I don't use Is there a workaround the problem without generating a different gradient? Thanks again! |
Hi @guitargeek, I agree with you that having a gradient of |
Ah okay, I see. It's not only about function parameters, this constant array can be anywhere. Here I put it in the global scope: #include "clad/Differentiator/Differentiator.h"
#include <iostream>
double obs[]{1.0};
inline double foo(double *params, double *obs)
{
return params[0] * obs[0];
}
double wrapper(double* params)
{
// return params[0] * obs[0]; // works!
return foo(params, obs); // doesn't work!
}
int main()
{
auto grad = clad::gradient(wrapper, "params");
double param = 2.0;
double result = 0.0;
grad.execute(¶m, &result);
std::cout << result << std::endl;
} We need a way to push constant data into nested functions, even if it's an ugly workaround :D I almost found a way: #include "clad/Differentiator/Differentiator.h"
#include <iostream>
#include <vector>
inline double foo(double *params, double *obs)
{
return params[0] * obs[0];
}
double wrapper(double * params, double* obs)
{
double obsCopy[2];
for(std::size_t i = 0; i < 2; ++i) { obsCopy[i] = obs[i]; }
// return params[0] * obs[0]; // works!
//return foo(params, obsCopy); // works
return foo(params, obsCopy + 1); // doesn't work!
}
int main()
{
auto grad = clad::gradient(wrapper, "params");
double param = 2.0;
std::vector<double> obs{2.0, 3.0};
double result = 0.0;
grad.execute(¶m, obs.data(), &result);
std::cout << result << std::endl;
} So close :) After copying the array to something with know size, I can use it! But only with a pointer to the beginning. I can't do pointer arithmetic :( The usecase is to inject hundreds of constant arrays like this into the function. The function signature would be way to long if they would all be separate, that's why we works with one concatenated auxiliary array that is passed to the function, and then we use offsetting. Is supporting the pointer arithmetic maybe more realistic? edit: what we currently to is to write 100s of for-loops that extract the subarrays. I think that this might be a factor in the long gradient jitting time for RooFit models, which is why I was trying to find a better solution. |
Pointer arithmetic one should be easy to fix, let me try this one out. I will update you once I have a fix for that. |
required for vgvassilev#762
Hi @guitargeek, the workaround should work after this: #764 |
Don't we have a facility to specify wrt to which parameter we should differentiate against?
That went out of date at the time I clicked on it :(
Probably worth creating a new issue? |
Awesome! The workaround with the single array declaration and then offsetting works not, and it reduces jitting time for the gradient of the ATLAS models by a factor 5! Thanks a lot, @vaithak |
@guitargeek, glad to hear. Can we close this issue now? |
I think we can close this one, I have already created a new issue for tracking the request for dealing with differentiation with partial arguments in a function call: #765. |
Using array parameters that are passed to the differentiated function as function arguments crashes Clad.
This is very important to support for the RooFit usecase, where we want to forward arrays of constant values like this, also passing arrays with offsets like
obs + 2
.Reproducer:
The text was updated successfully, but these errors were encountered: