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

Leverage function_annotation for AutoEnzyme #407

Merged
merged 17 commits into from
Aug 10, 2024
Merged

Leverage function_annotation for AutoEnzyme #407

merged 17 commits into from
Aug 10, 2024

Conversation

gdalle
Copy link
Member

@gdalle gdalle commented Aug 8, 2024

DI extensions

Enzyme: adapt to SciML/ADTypes.jl#77 with the new function_annotation kwarg:

  • Pass get_f_and_df(f, backend) to every operator with the following behavior with the following behavior:
    • if function_annotation <: Nothing, pass f
    • if function_annotation <: Const, pass Const(f)
    • if function_annotation <: Duplicated, pass Duplicated(f, make_zero(f))
    • BatchDuplicated on f not necessary yet but it will be once I implement batched operators
  • Restrict the use of high-level API like Enzyme.gradient and Enzyme.jacobian to cases where f is either not annotated or Const: function_annotation <: Union{Nothing,Const}
  • Emulate the new default behavior of autodiff when calling autodiff_thunk:
    • force Const(f) if f is not annotated
    • make sure that Enzyme throws an error if something is written inside of f with EnzymeCore.set_err_if_func_written

DI tests

Enzyme:

  • Test that function_annotation=Nothing and function_annotation=Const work on normal scenarios
  • Test that function_annotation=Duplicated works on closurized scenarios
  • In SecondOrder tests, for Enzyme.Reverse over ForwardDiff, mark f as Const because the analysis is too conservative

@wsmoses would you add any other annotation? We'll worry about performance later (e.g. zero-ing in place instead of re-creating make_zero(f)).

Related:

@gdalle gdalle marked this pull request as draft August 8, 2024 06:39
@gdalle gdalle marked this pull request as ready for review August 8, 2024 06:40
@codecov-commenter
Copy link

codecov-commenter commented Aug 8, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.70%. Comparing base (0e5ecfd) to head (bf0dee3).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #407      +/-   ##
==========================================
+ Coverage   96.68%   96.70%   +0.01%     
==========================================
  Files         103      103              
  Lines        4981     5003      +22     
==========================================
+ Hits         4816     4838      +22     
  Misses        165      165              

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

@gdalle
Copy link
Member Author

gdalle commented Aug 8, 2024

Some of the failures I see here are for autodiff_thunk, which apparently needs a specific activity and cannot get away with just receiving f. Any suggestions @wsmoses?

@gdalle gdalle linked an issue Aug 8, 2024 that may be closed by this pull request
@gdalle gdalle added the backend Related to one or more autodiff backends label Aug 8, 2024
@wsmoses
Copy link

wsmoses commented Aug 8, 2024

@gdalle it's probably worth testing that an error is thrown for closure scenaios that write (and nor for ones that don't)

@gdalle
Copy link
Member Author

gdalle commented Aug 9, 2024

@wsmoses I think this is ready, care to take a second look?

@gdalle it's probably worth testing that an error is thrown for closure scenaios that write (and nor for ones that don't)

At the moment I can't do that easily because my function test_differentiation runs a test set, so I can't use @test_throws on it. The exceptions would just show up as failed tests within the global test suite.

@gdalle
Copy link
Member Author

gdalle commented Aug 10, 2024

I'm gonna merge this because the new version of Enzyme causes errors in my existing tests without it (see e.g. the current state of #329). We can hash out the details later

@gdalle gdalle merged commit 4af9a02 into main Aug 10, 2024
53 checks passed
@gdalle gdalle deleted the gd/annot branch August 10, 2024 15:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend Related to one or more autodiff backends
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make Enzyme dispatches compatible with closures
3 participants