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

Enzyme Implementation #116

Merged
merged 17 commits into from
Nov 22, 2024
Merged

Enzyme Implementation #116

merged 17 commits into from
Nov 22, 2024

Conversation

SCiarella
Copy link
Collaborator

@SCiarella SCiarella commented Nov 11, 2024

This branch implements AD using Enzyme.jl.

In particular, we can now compute $$\dfrac{\partial f(u,t)}{\partial u}$$ where $$f(u,t)$$ is the right hand side of INS, projected in order to be compatible with SciML. Notice that at the moment there is no learnable closure in the loop, and the derivative is calculated only for $u$.


Main result

In src/sciml.jl there are now create_right_hand_side (out of place) and right_hand_side! (in place).
The in-place version is ~30% faster and uses only ~40% of the memory compared to the out-of-place, as one can see from test/enzyme_integration. This discrepancy becomes even more drastic when $$f$$ is used in SciML.solve(), however this has not been tested yet.

To do AD, Zygote requires out-of-place operations, so I have compared the pullback speed of Zygote+out vs Enzyme+in . test/enzyme_integration reports that Enzyme is ~30% faster and uses ~40% of the memory.


Observations

In test/chainrules_enzyme I compare and test the pullback rules of all the functions that are also tested in chainrules.
However Enzyme has a significant limitation which is the fact that it can not differentiate functions that return vectors. To overcome this problem I introduced enzyme_wrap() that executes the function in a local scope and returns nothing. In this way Enzyme works and produces the same gradients as Zygote with better performance.

But still, due to this limitation one can not really reuse the rules defined for the single functions because it is not a good idea to wrap all the pieces in the right hand side, so the pullback rule for the full rhs has been defined separately.


Todo before merge

  • add docstrings
  • remove the @info in the tests that compare the time
  • check the consistency of the code

Bugs fixed

src/operators.jl Line 841 applybodyforce! : If I am not mistaken, in case of static force, it has to be applied only once outside the loop. Please check if this is correct.

@SCiarella SCiarella mentioned this pull request Nov 11, 2024
5 tasks
@agdestein
Copy link
Owner

Great! It would also be good to have an example of computing gradients with Enzyme, something like this:

https://github.com/agdestein/IncompressibleNavierStokes.jl/blob/main/docs/src/manual/differentiability.md

add docpages

clean deps

make @info optional in tests
@agdestein
Copy link
Owner

I also added a citation file, you can add yourself there.

Copy link

codecov bot commented Nov 21, 2024

Codecov Report

Attention: Patch coverage is 92.15686% with 4 lines in your changes missing coverage. Please review.

Project coverage is 59.39%. Comparing base (5ed9a60) to head (6a582de).
Report is 18 commits behind head on main.

Files with missing lines Patch % Lines
src/operators.jl 83.33% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #116      +/-   ##
==========================================
+ Coverage   57.39%   59.39%   +2.00%     
==========================================
  Files          29       30       +1     
  Lines        3666     3699      +33     
==========================================
+ Hits         2104     2197      +93     
+ Misses       1562     1502      -60     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.


🚨 Try these New Features:

@agdestein agdestein merged commit 7745807 into agdestein:main Nov 22, 2024
6 checks passed
@SCiarella SCiarella mentioned this pull request Nov 22, 2024
1 task
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.

2 participants