-
-
Notifications
You must be signed in to change notification settings - Fork 16
feat: add chunksize parameter to AutoEnzyme
#124
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
Open
gdalle
wants to merge
4
commits into
main
Choose a base branch
from
gd/enzyme_params
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
name = "ADTypes" | ||
uuid = "47edcb42-4c32-4615-8424-f2b9edc5f35b" | ||
authors = ["Vaibhav Dixit <[email protected]>, Guillaume Dalle and contributors"] | ||
version = "1.17.0" | ||
version = "1.18.0" | ||
|
||
[deps] | ||
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,46 +39,65 @@ struct AutoDiffractor <: AbstractADType end | |
mode(::AutoDiffractor) = ForwardOrReverseMode() | ||
|
||
""" | ||
AutoEnzyme{M,A} | ||
AutoEnzyme{M,A,C} | ||
|
||
Struct used to select the [Enzyme.jl](https://github.com/EnzymeAD/Enzyme.jl) backend for automatic differentiation. | ||
|
||
Defined by [ADTypes.jl](https://github.com/SciML/ADTypes.jl). | ||
|
||
# Constructors | ||
|
||
AutoEnzyme(; mode::M=nothing, function_annotation::Type{A}=Nothing) | ||
AutoEnzyme(; | ||
mode::Union{EnzymeCore.Mode,Nothing}=nothing, | ||
function_annotation::Type{<:Union{EnzymeCore.Annotation,Nothing}}=Nothing, | ||
chunksize::Union{Int,Float64,Nothing}=nothing, | ||
) | ||
|
||
# Type parameters | ||
- `mode::M` determines the autodiff mode (forward or reverse). It can be: | ||
|
||
- `A` determines how the function `f` to differentiate is passed to Enzyme. It can be: | ||
+ a mode object from EnzymeCore.jl, like `EnzymeCore.Forward` or `EnzymeCore.Reverse` (possibly modified with additional settings like runtime activity) | ||
+ `nothing` to choose the best mode automatically | ||
|
||
+ a subtype of `EnzymeCore.Annotation` (like `EnzymeCore.Const` or `EnzymeCore.Duplicated`) to enforce a given annotation | ||
+ `Nothing` to simply pass `f` and let Enzyme choose the most appropriate annotation | ||
- `A=function_annotation` determines how the function `f` to differentiate is passed to Enzyme. It can be: | ||
|
||
# Fields | ||
+ a subtype of `EnzymeCore.Annotation` (like `EnzymeCore.Const` or `EnzymeCore.Duplicated`) to enforce a given annotation | ||
|
||
- `mode::M` determines the autodiff mode (forward or reverse). It can be: | ||
+ `Nothing` (the type, not the object) to simply pass `f` and let Enzyme choose the most appropriate annotation | ||
- `C=chunksize` determines the number of derivatives evaluated simultaneously when computing operators like a Jacobian or a forward-mode gradient. It can be: | ||
|
||
+ an object subtyping `EnzymeCore.Mode` (like `EnzymeCore.Forward` or `EnzymeCore.Reverse`) if a specific mode is required | ||
+ `nothing` to choose the best mode automatically | ||
+ a positive `Int` to fix a constant chunk size | ||
+ `Inf` to pick the maximum chunk size, corresponding to the array length | ||
+ `nothing` to choose a good chunk size automatically | ||
""" | ||
struct AutoEnzyme{M, A} <: AbstractADType | ||
struct AutoEnzyme{M, A, C} <: AbstractADType | ||
mode::M | ||
|
||
function AutoEnzyme{M, A, C}(mode::M) where {M, A, C} | ||
@assert C isa Union{Nothing, Int, Float64} | ||
if C isa Int | ||
@assert C > 0 | ||
elseif C isa Float64 | ||
@assert C == Inf | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should give a better error message here |
||
end | ||
return new{M, A, C}(mode) | ||
end | ||
end | ||
|
||
function AutoEnzyme(; | ||
mode::M = nothing, function_annotation::Type{A} = Nothing) where {M, A} | ||
return AutoEnzyme{M, A}(mode) | ||
mode::M = nothing, | ||
function_annotation::Type{A} = Nothing, | ||
chunksize::Union{Nothing, Int, Float64} = nothing | ||
) where {M, A} | ||
return AutoEnzyme{M, A, chunksize}(mode) | ||
end | ||
|
||
mode(::AutoEnzyme) = ForwardOrReverseMode() # specialized in the extension | ||
|
||
function Base.show(io::IO, backend::AutoEnzyme{M, A}) where {M, A} | ||
function Base.show(io::IO, backend::AutoEnzyme{M, A, C}) where {M, A, C} | ||
print(io, AutoEnzyme, "(") | ||
!isnothing(backend.mode) && print(io, "mode=", repr(backend.mode; context = io)) | ||
!isnothing(backend.mode) && !(A <: Nothing) && print(io, ", ") | ||
!(A <: Nothing) && print(io, "function_annotation=", repr(A; context = io)) | ||
!isnothing(backend.mode) && print(io, "mode=", repr(backend.mode; context = io), ", ") | ||
!(A <: Nothing) && print(io, "function_annotation=", repr(A; context = io), ", ") | ||
!(C === nothing) && print(io, "chunksize=", repr(C; context = io)) | ||
print(io, ")") | ||
end | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I kind of wonder if a chunk size of 0 here would be a good way to represent maximum chunk size
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I get where you're coming from but I have two objections:
chunksize=Inf
makes much more sense even to the uninformed readerThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reading that, I still don't understand why a zero chunksize is semantically meaningful?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you denote by$N$ the dimension, $C$ the chunk size, $N_C$ the number of chunks, you have $N = N_C \cdot C$ (plus a remainder possibly). For $N = 0$ , you can either pick $C = 0$ or $N_C = 0$ . None of those means a lot to be honest, but different backends have different conventions, and ForwardDiff picks a zero chunk size in the zero-length case by default (JuliaDiff/DifferentiationInterface.jl#835 (comment)) while Enzyme doesn't. That's why I'd rather steer clear of this whole mess.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see where you're coming from vis-a-vis forward diff making a different design choice, but I'm not sure that is most critical here.
Alternatively, I kind of wonder, if it would be best to make an EnzymeCore.MaxChunk (which equally can be used by the Enzyme.gradient/jacobian wrappers), which would be the alternate here like there is for EnzymeCore.Mode
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm gonna veto the zero but if you want to add the max chunk setting to EnzymeCore that's fine by me too, your call
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi Billy, just following up on this, do you want to add that setting to EnzymeCore?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah I think thats the right move. if you have cycles before I feel free to open a PR on
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See EnzymeAD/Enzyme.jl#2659