Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate code for Enzyme autodiff for functions with Pointer/array arguments #466

Merged
merged 2 commits into from
Aug 15, 2022

Conversation

Nirhar
Copy link
Contributor

@Nirhar Nirhar commented Jun 29, 2022

For functions of type
double myfunction(double* arr){...};
OR double myfunction(double arr[3]){...};

we generate derivative code of type:

        void d_myfunction(double* arr, clad::array_ref<double> _d_arr){
		double* d_arr = _d_arr.ptr();
		__enzyme_autodiff_myfunction(myfunction, arr, d_arr);
	}

Further on enzyme will handle the function differentiation

This Commit also Downloads and Installs Enzyme in the Clad Directory, if
the flag "-DENZYME=On" is passed to cmake during project configuration.

This Commit also contains tests for the above feature in test/Gradient/Enzyme.C

@Nirhar Nirhar marked this pull request as draft June 29, 2022 05:02
@Nirhar Nirhar force-pushed the enzyme_integration_2 branch from 8111f03 to 96dbc31 Compare June 29, 2022 08:34
@Nirhar
Copy link
Contributor Author

Nirhar commented Jun 29, 2022

@vgvassilev @parth-07
The clad code in this pull request throws the error

"cannot initialize a parameter of type 'double (double *)' with an lvalue of type 'double (double *)'"

during compilation of the below code:

#include "clad/Differentiator/Differentiator.h"
double f(double *arr) { return arr[0] * arr[1]; }
int main() {
    auto f_grad=clad::gradient<clad::opts::use_enzyme>(f);
}

Any idea how to resolve this?

@vgvassilev vgvassilev requested a review from parth-07 June 29, 2022 08:47
@parth-07
Copy link
Collaborator

@vgvassilev @parth-07 The clad code in this pull request throws the error

"cannot initialize a parameter of type 'double (double *)' with an lvalue of type 'double (double *)'"

during compilation of the below code:

#include "clad/Differentiator/Differentiator.h"
double f(double *arr) { return arr[0] * arr[1]; }
int main() {
    auto f_grad=clad::gradient<clad::opts::use_enzyme>(f);
}

Any idea how to resolve this?

Are you able to print generated derived function? If yes, can you please paste that in a comment here?

@Nirhar
Copy link
Contributor Author

Nirhar commented Jun 29, 2022

@parth-07 No, I am not able to print the generated function. The same error is thrown when I attempt to print it.

@parth-07
Copy link
Collaborator

@parth-07 No, I am not able to print the generated function. The same error is thrown when I attempt to print it.

Have you tried printing the derived function using the flag -fdump-derived-fn?

@parth-07
Copy link
Collaborator

parth-07 commented Jun 29, 2022

"cannot initialize a parameter of type 'double (double *)' with an lvalue of type 'double (double *)'"

The error suggests that you are using an incorrect value category (https://en.cppreference.com/w/cpp/language/value_category) for the function argument of the call to __enzyme_autodiff_. Correctly generating clang::DeclRefExpr node may fix the issue. You can use, Sema::BuildDeclRefExpr or VisitorBase::BuildDeclRef for correctly generating it.

@Nirhar
Copy link
Contributor Author

Nirhar commented Jun 29, 2022

@parth-07 No, I am not able to print the generated function. The same error is thrown when I attempt to print it.

Have you tried printing the derived function using the flag -fdump-derived-fn?

Yes I tried that, the generated function does not contain the __enzyme_autodiff function call

@Nirhar
Copy link
Contributor Author

Nirhar commented Jun 29, 2022

"cannot initialize a parameter of type 'double (double *)' with an lvalue of type 'double (double *)'"

The error suggests that you are using an incorrect value category (https://en.cppreference.com/w/cpp/language/value_category) for the function argument of the call to __enzyme_autodiff_. Correctly generating clang::DeclRefExpr node may fix the issue. You can use, Sema::BuildDeclRefExpr or VisitorBase::BuildDeclRef for correctly generating it.

Okay let me try this out

@Nirhar Nirhar force-pushed the enzyme_integration_2 branch 2 times, most recently from 9062756 to cecd694 Compare July 5, 2022 05:05
@Nirhar
Copy link
Contributor Author

Nirhar commented Jul 5, 2022

This code runs on my machine, but here the tests throw the error fatal: could not create work tree dir 'Enzyme': Permission denied. How do I deal with this @vgvassilev @parth-07 ?

@Nirhar Nirhar requested a review from parth-07 July 5, 2022 05:14
@Nirhar Nirhar force-pushed the enzyme_integration_2 branch 2 times, most recently from d8d19ea to 4b17356 Compare July 5, 2022 10:53
@Nirhar
Copy link
Contributor Author

Nirhar commented Jul 5, 2022

Now I am getting the below error in the CI tests:

CMake Error: Could not open file for write in copy operation /usr/local/EnzymeAD/tmp/Enzyme-mkdirs.cmake.tmp

I am not getting this error on my machine. How do I resolve this @vgvassilev @parth-07?

@Nirhar Nirhar force-pushed the enzyme_integration_2 branch from 4b17356 to adcb7ac Compare July 5, 2022 11:01
@parth-07
Copy link
Collaborator

parth-07 commented Jul 5, 2022

Now I am getting the below error in the CI tests:

CMake Error: Could not open file for write in copy operation /usr/local/EnzymeAD/tmp/Enzyme-mkdirs.cmake.tmp

I am not getting this error on my machine. How do I resolve this @vgvassilev @parth-07?

It seems to be a permission issue. Can you try to install it in a non-root directory? And also, check if we are missing the right permissions somewhere.

@Nirhar Nirhar force-pushed the enzyme_integration_2 branch from adcb7ac to ab8b465 Compare July 5, 2022 14:38
@Nirhar
Copy link
Contributor Author

Nirhar commented Jul 5, 2022

Now I am getting the below error in the CI tests:

CMake Error: Could not open file for write in copy operation /usr/local/EnzymeAD/tmp/Enzyme-mkdirs.cmake.tmp

I am not getting this error on my machine. How do I resolve this @vgvassilev @parth-07?

It seems to be a permission issue. Can you try to install it in a non-root directory? And also, check if we are missing the right permissions somewhere.

Currently I am installing Enzyme in the "CMAKE_INSTALL_PREFIX". I think in the CI machines this is in some root directory. Where do I install it, is the build_dir a good place?

@parth-07
Copy link
Collaborator

parth-07 commented Jul 5, 2022

Now I am getting the below error in the CI tests:

CMake Error: Could not open file for write in copy operation /usr/local/EnzymeAD/tmp/Enzyme-mkdirs.cmake.tmp

I am not getting this error on my machine. How do I resolve this @vgvassilev @parth-07?

It seems to be a permission issue. Can you try to install it in a non-root directory? And also, check if we are missing the right permissions somewhere.

Currently I am installing Enzyme in the "CMAKE_INSTALL_PREFIX". I think in the CI machines this is in some root directory. Where do I install it, is the build_dir a good place?

You can just specify a non-root directory for the CMAKE_INSTALL_PREFIX option in the CMake configure command.

@parth-07
Copy link
Collaborator

parth-07 commented Jul 5, 2022

Where do I install it, is the build_dir a good place?

Any non-root directory should work for the debugging purpose right now. We would like things to work as expected when the user is installing in root locations as well. Thus the main goal is to find the underlying issue that's causing the CMake configuration to fail.

@Nirhar Nirhar force-pushed the enzyme_integration_2 branch from ab8b465 to 56c835a Compare July 5, 2022 17:26
@Nirhar
Copy link
Contributor Author

Nirhar commented Jul 5, 2022

Its mentioned in the Enzyme website that Enzyme has been tested against LLVM 7,8,9,10,11,12. I personally tested it against LLVM 13 and 14 and it works. Maybe the lower versions of LLVM/clang are causing a problem? As I have seen, most failures in the CI are because the Enzyme build has failed.

@Nirhar Nirhar requested a review from parth-07 July 5, 2022 17:47
@parth-07
Copy link
Collaborator

parth-07 commented Jul 5, 2022

Its mentioned in the Enzyme website that Enzyme has been tested against LLVM 7,8,9,10,11,12. I personally tested it against LLVM 13 and 14 and it works. Maybe the lower versions of LLVM/clang are causing a problem? As I have seen, most failures in the CI are because the Enzyme build has failed.

Can you test enzyme integration with Clad when lower versions of LLVM are used in your local system? If you are getting the same/similar errors in the local system as well, then it will be easier to debug.

@vgvassilev vgvassilev force-pushed the enzyme_integration_2 branch from d8b51ce to 59a8160 Compare August 12, 2022 06:09
@Nirhar Nirhar force-pushed the enzyme_integration_2 branch 5 times, most recently from 3ee6a23 to 96187a6 Compare August 12, 2022 06:43
@Nirhar Nirhar force-pushed the enzyme_integration_2 branch 2 times, most recently from 3b85706 to 3b0e400 Compare August 12, 2022 09:46
@Nirhar Nirhar force-pushed the enzyme_integration_2 branch from 3b0e400 to 95df090 Compare August 13, 2022 05:26
@Nirhar Nirhar force-pushed the enzyme_integration_2 branch 3 times, most recently from 99db221 to d93e774 Compare August 13, 2022 13:40
@Nirhar Nirhar force-pushed the enzyme_integration_2 branch from d93e774 to d8bc2f3 Compare August 13, 2022 15:04
@Nirhar Nirhar force-pushed the enzyme_integration_2 branch 4 times, most recently from 3823f51 to 97c6593 Compare August 14, 2022 16:54
@Nirhar Nirhar force-pushed the enzyme_integration_2 branch 2 times, most recently from a174e7c to f48f95e Compare August 15, 2022 18:08
…guments

For functions of type
	double myfunction(double* arr){...};
OR 	double myfunction(double arr[3]){...};

we generate derivative code of type:
	void d_myfunction(double* arr, clad::array_ref<double> _d_arr){
		double* d_arr = _d_arr.ptr();
		__enzyme_autodiff_myfunction(myfunction, arr, d_arr);
	}

Further on enzyme will handle the function differentiation

This Commit also Downloads and Installs Enzyme in the Clad Directory, if
the flag "-DENABLE_ENZYME_BACKEND=On" is passed to cmake during project configuration.

This commit also contains tests for the above feature in test/Gradient/Enzyme.C

This commit also adds the enzyme pass to the llvm optimisation pipeline so that
it can run at the right moment.
@Nirhar Nirhar force-pushed the enzyme_integration_2 branch from f48f95e to 3598e48 Compare August 15, 2022 18:30
Copy link
Owner

@vgvassilev vgvassilev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Great work @Nirhar!

The codecov coverage reports are mostly due to code using the new pass manager. Unfortunately enzyme has not yet moved to the new pass manager. It's registration happens via the old way. However, I'd like to keep the new way around and they move we will do little work to reuse the already put in place infrastructure.

@vgvassilev vgvassilev merged commit 828baf7 into vgvassilev:master Aug 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants