Hi,
I noticed that when specifying a custom forward derivative for a function, only the gradient is evaluated but not the function itself. Maybe I’m doing something wrong, so I wanted to check here rather than creating an issue right away.
A minimal example is the following:
#include <stdio.h>
int enzyme_const, enzyme_dup;
extern void __enzyme_fwddiff(void*, ...);
void square(double *y, double *x) {
*y = *x * *x;
}
void grad_square(double *y, double *dy, double *x, double *dx) {
*dy = 3 * *x; // Purposefully wrong
}
void* __enzyme_register_derivative_square[] = {
(void*)square,
(void*)grad_square,
};
int main() {
double x = 5.0;
double dx = 1.0;
double y = 0.0;
double dy = 0.0;
__enzyme_fwddiff((void*) square, enzyme_dup, &y, &dy, enzyme_dup, &x, &dx);
printf("f(%f) = %f, df(%f) = %f\n", x, y, x, dy);
}
which prints out
f(5.000000) = 0.000000, df(5.000000) = 15.000000
Without the custom derivative, the square
function itself is getting evaluated:
f(5.000000) = 25.000000, df(5.000000) = 10.000000
Is this expected behavior or a bug?