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

Add constant_function kwarg to AutoEnzyme #72

Merged
merged 3 commits into from
Jul 17, 2024

Conversation

gdalle
Copy link
Collaborator

@gdalle gdalle commented Jul 17, 2024

Checklist

  • Appropriate tests were added
  • Any code changes were done in a way that does not break public API
  • All documentation related to code changes were updated
  • The new code follows the
    contributor guidelines, in particular the SciML Style Guide and
    COLPRAC.
  • Any new documentation only uses public API

Additional context

This PR adds a keyword argument (and type parameter) to AutoEnzyme, which specified whether the function itself should be considered constant during differentiation. See SciML/SciMLBenchmarks.jl#988 (comment) for the related discussion with @wsmoses and @ChrisRackauckas

src/dense.jl Outdated Show resolved Hide resolved
src/dense.jl Outdated Show resolved Hide resolved
@ChrisRackauckas ChrisRackauckas merged commit 39da305 into main Jul 17, 2024
4 checks passed
@ChrisRackauckas ChrisRackauckas deleted the gd/constant_f_enzyme branch July 17, 2024 08:42
AutoEnzyme(; mode=nothing, constant_function::Bool=false)

The `constant_function` keyword argument (and type parameter) determines whether the function object itself should be considered constant or not during differentiation with Enzyme.jl.
For simple functions, `constant_function` should usually be set to `false`, but in the case of closures or callable structs which contain differentiated data that can be treated as constant, `constant_function` should be set to `true` for increased performance (more details below).
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should read be usually set to true. However if the closure has differentiable data loaded and stored into, it may need to be set false

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Woops I switched true and false

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

See #73


# Notes

If `constant_function = true` but the enclosed data is not truly constant, then Enzyme.jl will not compute the correct derivative values.
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should read “then Enzyme.jl will consider the load and store into cache as constant resulting in a derivative of 0”, this is still a correct derivative but subject to assumptions given to Enzyme

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I understand that you always want to stress that Enzyme computes the right thing, but this is a high-level docstring for beginners. I don't think we're implying that Enzyme is wrong here, just that someone might be using it wrong if they say constant_function=true

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I tried to be clearer in #73

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