EnzymeAD / EnzymeAD/Enzyme

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

Open
#2,404 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
LLVM
Stars
1.7k
Forks
188
Avg merge
1d 22h
Merged PRs (30d)
26

Description

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
#include
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`

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.