You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
but when following the same pattern with a stateful lambda we get an error message:
auto f = [a = 1.0, b = 2.0](std::array<double,2> x) {
return x[0] * a + x[1] * x[1] * b * b;
};
std::array<double, 2> x{3.0, 4.0};
std::array<double, 2> dx{5.0, 6.0};
// error: function '__enzyme_fwddiff<// double, // int, // (lambda at <source>:29:12) *, // int, // std::array<double, 2> *, // std::array<double, 2> *// >' is used but not defined in this translation unit, // and cannot be defined in any other translation unit because its type does not have linkagedouble dy = __enzyme_fwddiff<double>((void*)functor_wrapper<decltype(f), std::array<double,2> >, enzyme_const, &f, enzyme_dup, &x, &dx);
Is there a limitation in enzyme that prevents differentiation of stateful lambdas? The error message seems misleading, it doesn't seem like a linkage issue, since all of this code exists in a single translation unit.
From a C++ point of view, I think of these two examples as practically equivalent, since the compiler implements lambda functions by turning them into functor objects behind the scenes (using cppinsights.io):
This is a purely C++ error, outside of Enzyme. Basically its saying that no possible code could definite the definition of enzyme_fwdddiff, since it uses a local variable, and the template is marked extern. However, the compiler actually can definite (as we do here). Marking the type as a non-local type (such as void* in my fix, or your functor in above) means that it's fine.
Thanks for the quick response-- is there any way to emit a descriptive error message here to help users diagnose this sort of problem and implement your fix?
Hi, I'm trying to figure how to differentiate functors/lambdas with Enzyme.
Functors seem to work after introducing a level of indirection to turn the
operator()
into a free function:but when following the same pattern with a stateful lambda we get an error message:
Is there a limitation in enzyme that prevents differentiation of stateful lambdas? The error message seems misleading, it doesn't seem like a linkage issue, since all of this code exists in a single translation unit.
From a C++ point of view, I think of these two examples as practically equivalent, since the compiler implements lambda functions by turning them into functor objects behind the scenes (using cppinsights.io):
Here's a complete example of the issue: https://fwd.gymni.ch/PlRQpl
The text was updated successfully, but these errors were encountered: