clad
clad copied to clipboard
Add support for verifying Enzyme Gradients with Clad Gradients
This commit generates code that will verify the results of Enzyme Gradients with Clad Gradients. For example, if previously the following code was generated for differentiating with enzyme for a function:
void f1_grad_enzyme(double arr[2], clad::array_ref<double> _d_arr) {
double *d_arr = _d_arr.ptr();
__enzyme_autodiff_f1(f1, arr, d_arr);
}
The above code will be appended with checks to verify the calculated gradients. Thus the newly generated code would be:
void f1_grad_enzyme(double arr[2], clad::array_ref<double> _d_arr) {
double *d_arr = _d_arr.ptr();
__enzyme_autodiff_f1(f1, arr, d_arr);
double cladResult1[2];
f1_grad(arr, cladResult1);
EssentiallyEqualArrays(cladResult1, _d_arr.ptr(), 2UL);
}
EssentiallyEqualArrays
and EssentiallyEqual
are functions defined in Differentiator.h
Only functions with primitive type and ConstantArray type parameters can be verified in this manner.
To trigger this verification one must append the following flag to clang while compiling the function to be generated: -Xclang -plugin-arg-clad -Xclang -fcheck-enzyme-with-clad
This PR fails when I attempt to differentiate multiple functions with enzyme and verify it with clad. Then clad outputs the wrong gradients in some runs of the compiled code but not in others. Here is a failing example:
double f1(double arr[2]) { return arr[0] * arr[1]; }
double f2(double x, double y, double z){
return x * y * z;
}
int main(){
auto f1_grad = clad::gradient<clad::opts::use_enzyme>(f1);
double f1_v[2] = {3, 4};
double f1_g[2] = {0};
f1_grad.execute(f1_v, f1_g);
printf("d_x = %.2f, d_y = %.2f\n", f1_g[0], f1_g[1]);
auto f2_grad=clad::gradient<clad::opts::use_enzyme>(f2);
double f2_res[3];
double f2_x=3,f2_y=4,f2_z=5;
f2_grad.execute(f2_x,f2_y,f2_z,&f2_res[0],&f2_res[1],&f2_res[2]);
}
If each function is individually differentiated and verified(in different files), this error does not occur. Only when they are jointly differentiated the error occurs. The derivatives generated for the above functions are:
void f1_grad(double arr[2], clad::array_ref<double> _d_arr) {
double _t0;
double _t1;
_t1 = arr[0];
_t0 = arr[1];
double f1_return = _t1 * _t0;
goto _label0;
_label0:
{
double _r0 = 1 * _t0;
_d_arr[0] += _r0;
double _r1 = _t1 * 1;
_d_arr[1] += _r1;
}
}
void f1_grad_enzyme(double arr[2], clad::array_ref<double> _d_arr) {
double *d_arr = _d_arr.ptr();
__enzyme_autodiff_f1(f1, arr, d_arr);
//Verification part below
double cladResult1[2];
f1_grad(arr, cladResult1);
EssentiallyEqualArrays(cladResult1, _d_arr.ptr(), 2UL);
}
void f2_grad(double x, double y, double z, clad::array_ref<double> _d_x, clad::array_ref<double> _d_y, clad::array_ref<double> _d_z) {
double _t0;
double _t1;
double _t2;
double _t3;
_t2 = x;
_t1 = y;
_t3 = _t2 * _t1;
_t0 = z;
double f2_return = _t3 * _t0;
goto _label0;
_label0:
{
double _r0 = 1 * _t0;
double _r1 = _r0 * _t1;
* _d_x += _r1;
double _r2 = _t2 * _r0;
* _d_y += _r2;
double _r3 = _t3 * 1;
* _d_z += _r3;
}
}
void f2_grad_enzyme(double x, double y, double z, clad::array_ref<double> _d_x, clad::array_ref<double> _d_y, clad::array_ref<double> _d_z) {
clad::EnzymeGradient<3> grad = __enzyme_autodiff_f2(f2, x, y, z);
* _d_x = grad.d_arr[0U];
* _d_y = grad.d_arr[1U];
* _d_z = grad.d_arr[2U];
//Verification part below
double cladResult1;
double cladResult2;
double cladResult3;
f2_grad(x, y, z, &cladResult1, &cladResult2, &cladResult3);
EssentiallyEqual(cladResult1, * _d_x);
EssentiallyEqual(cladResult2, * _d_y);
EssentiallyEqual(cladResult3, * _d_z);
}
cc @vgvassilev @parth-07 for review and some help
The above problem has been fixed. It was simply that I was not initializing declared variables in some places
Codecov Report
Merging #488 (2d05fa0) into master (09e2ed0) will increase coverage by
0.10%
. The diff coverage is100.00%
.
:exclamation: Current head 2d05fa0 differs from pull request most recent head a882316. Consider uploading reports for the commit a882316 to get more accurate results
@@ Coverage Diff @@
## master #488 +/- ##
==========================================
+ Coverage 92.56% 92.67% +0.10%
==========================================
Files 37 37
Lines 5528 5610 +82
==========================================
+ Hits 5117 5199 +82
Misses 411 411
Impacted Files | Coverage Δ | |
---|---|---|
include/clad/Differentiator/DiffPlanner.h | 100.00% <ø> (ø) |
|
include/clad/Differentiator/ReverseModeVisitor.h | 98.70% <ø> (ø) |
|
include/clad/Differentiator/VisitorBase.h | 100.00% <ø> (ø) |
|
tools/DerivedFnInfo.h | 100.00% <ø> (ø) |
|
lib/Differentiator/ReverseModeVisitor.cpp | 96.16% <100.00%> (+0.14%) |
:arrow_up: |
lib/Differentiator/VisitorBase.cpp | 97.67% <100.00%> (+0.05%) |
:arrow_up: |
tools/ClangPlugin.cpp | 90.32% <100.00%> (+0.10%) |
:arrow_up: |
tools/ClangPlugin.h | 74.57% <100.00%> (+0.89%) |
:arrow_up: |
tools/DerivedFnInfo.cpp | 100.00% <100.00%> (ø) |
Impacted Files | Coverage Δ | |
---|---|---|
include/clad/Differentiator/DiffPlanner.h | 100.00% <ø> (ø) |
|
include/clad/Differentiator/ReverseModeVisitor.h | 98.70% <ø> (ø) |
|
include/clad/Differentiator/VisitorBase.h | 100.00% <ø> (ø) |
|
tools/DerivedFnInfo.h | 100.00% <ø> (ø) |
|
lib/Differentiator/ReverseModeVisitor.cpp | 96.16% <100.00%> (+0.14%) |
:arrow_up: |
lib/Differentiator/VisitorBase.cpp | 97.67% <100.00%> (+0.05%) |
:arrow_up: |
tools/ClangPlugin.cpp | 90.32% <100.00%> (+0.10%) |
:arrow_up: |
tools/ClangPlugin.h | 74.57% <100.00%> (+0.89%) |
:arrow_up: |
tools/DerivedFnInfo.cpp | 100.00% <100.00%> (ø) |