Enzyme icon indicating copy to clipboard operation
Enzyme copied to clipboard

Inconsistent Undefined Gradient for `abs(x)` at `x = 0` Compared to Other AD Tools

Open shadow-orange41 opened this issue 1 year ago • 0 comments

When differentiating abs(x) at x = 0, Enzyme produces a gradient of 1, while other AD tools, such as CppAD, CodiPack, and Adept, return 0. I understand that the gradient at x = 0 is mathematically undefined, but this inconsistency can lead to confusion and errors when comparing gradient outputs across different AD frameworks.

Also, I noticed that using the abs() function (instead of std::abs()) leads to a gradient of 0 at x = 0, even though it generates a compile-time warning. This behavior differs from the result when using std::abs(). The following example demonstrates this:

#include <stdio.h>
#include <iostream>
extern double __enzyme_autodiff(void*, double);
double abs_std_func(double x) {
    return std::abs(x);
}

double abs_func(double x) {
  return abs(x);
}

int main() {
    double x = 0.0;
    double dx_std = __enzyme_autodiff((void*) abs_std_func, x);
    double dx_enzyme = __enzyme_autodiff((void*) abs_func, x);
    std::cout << "x = " << x << ", dx_std_abs = " << dx_std << ", dx_abs = " << dx_enzyme << std::endl;
    return 0;
}

Output: x = 0, dx_std = 1, dx_enzyme = 0

shadow-orange41 avatar Aug 05 '25 13:08 shadow-orange41