EnzymeAD / EnzymeAD/Enzyme

Enzyme Linker plugin doesn't recognize the custom gradient.

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

Description

Hi,
I am using the Enzyme linker plugin LLDEnzyme-12.so to test the custom gradient functionality. It seems that it can't recognize the custom gradient I defined. I am not sure if I am using it correctly. Below is the code.
By calling this, it gives the correct result.
```bash
make main
./main
```
This gives the wrong result.
```bash
make main2
./main2
```
Could you take a look? Thanks!

main.cpp
```cpp
#include

double __enzyme_autodiff(void*, double);

__attribute__((optnone))
void square_(const double* src, double* dest) {
*dest = *src * *src;
}

int augment = 0;
void* augment_square_(const double* src, const double *d_src, double* dest, double* d_dest) {
augment++;
// intentionally incorrect for debugging
*dest = 7.0;
*d_dest = 11.0;
return nullptr;
}

int gradient = 0;
void gradient_square_(const double* src, double *d_src, const double* dest, const double* d_dest, void* tape) {
gradient++;
// intentionally incorrect for debugging
*d_src = 13.0;
}

void* __enzyme_register_gradient_square[] = {
(void*)square_,
(void*)augment_square_,
(void*)gradient_square_,
};

double square(double x) {
double y;
square_(&x, &y);
return y;
}

double dsquare(double x) {
return __enzyme_autodiff((void*)square, x);
}

int main() {
double res = dsquare(3.0);
printf("res=%f augment=%d gradient=%d\n", res, augment, gradient);
`}`
```
Makefile
```Makefile
input.ll: main.cpp
clang main.cpp -S -emit-llvm -o input.ll -O2 -fno-vectorize -fno-slp-vectorize -fno-unroll-loops

output.ll: input.ll
opt input.ll -load=/usr/local/lib/LLVMEnzyme-12.so -enzyme -o output.ll -S

main: output.ll
clang output.ll -o main

main2: main.cpp
clang++ -fuse-ld=lld -flto -o main2 main.cpp -Wl,--lto-legacy-pass-manager -Wl,-mllvm=-load=/usr/local/lib/LLDEnzyme-12.so
```

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.