-
-
Notifications
You must be signed in to change notification settings - Fork 38
Add Enzyme reverse rules #110
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
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
480f434
Add Enzyme reverse rules
wsmoses b5762c7
fix
wsmoses 80bbf09
fixup
wsmoses 0978621
Add test project file
wsmoses 3fae4ca
gate per extension package
wsmoses 422fa47
Update test/runtests.jl
wsmoses 3ed66d2
Update test/runtests.jl
wsmoses c93b6e6
Update test/Project.toml
wsmoses 7a02b26
Update Project.toml
wsmoses 0d981f9
Add actual file
wsmoses f48b499
Update QuadGKEnzymeExt.jl
wsmoses e0f3256
Update ext/QuadGKEnzymeExt.jl
wsmoses 296a866
fixup
wsmoses da1454d
fixup
wsmoses f277f08
Bump minimum to 1.9
wsmoses a6e4e04
Update QuadGKEnzymeExt.jl
wsmoses 5a1f362
Update runtests.jl
wsmoses 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 |
---|---|---|
|
@@ -22,7 +22,7 @@ jobs: | |
fail-fast: false | ||
matrix: | ||
version: | ||
- '1.2' | ||
- '1.9' | ||
- '1' | ||
# - 'nightly' | ||
os: | ||
|
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 |
---|---|---|
@@ -0,0 +1,132 @@ | ||
|
||
module QuadGKEnzymeExt | ||
|
||
using QuadGK, Enzyme, LinearAlgebra | ||
|
||
function Enzyme.EnzymeRules.augmented_primal(config, ofunc::Const{typeof(quadgk)}, ::Type{RT}, f, segs::Annotation{T}...; kws...) where {RT, T} | ||
prims = map(x->x.val, segs) | ||
|
||
retres, segbuf = if f isa Const | ||
if EnzymeRules.needs_primal(config) | ||
quadgk(f.val, prims...; kws...), nothing | ||
else | ||
nothing | ||
end | ||
else | ||
I, E, segbuf = quadgk_segbuf(f.val, prims...; kws...) | ||
if EnzymeRules.needs_primal(config) | ||
(I, E), segbuf | ||
else | ||
nothing, segbuf | ||
end | ||
end | ||
|
||
dres = if !Enzyme.EnzymeRules.needs_shadow(config) | ||
nothing | ||
elseif EnzymeRules.width(config) == 1 | ||
zero.(res...) | ||
else | ||
ntuple(Val(EnzymeRules.width(config))) do i | ||
Base.@_inline_meta | ||
zero.(res...) | ||
end | ||
end | ||
|
||
cache = if RT <: Duplicated || RT <: DuplicatedNoNeed || RT <: BatchDuplicated || RT <: BatchDuplicatedNoNeed | ||
dres | ||
else | ||
nothing | ||
end | ||
cache2 = segbuf, cache | ||
|
||
return Enzyme.EnzymeRules.AugmentedReturn{ | ||
Enzyme.EnzymeRules.needs_primal(config) ? eltype(RT) : Nothing, | ||
Enzyme.EnzymeRules.needs_shadow(config) ? (Enzyme.EnzymeRules.width(config) == 1 ? eltype(RT) : NTuple{Enzyme.EnzymeRules.width(config), eltype(RT)}) : Nothing, | ||
typeof(cache2) | ||
}(retres, dres, cache2) | ||
end | ||
|
||
function call(f, x) | ||
f(x) | ||
end | ||
|
||
# Wrapper around a function f that allows it to act as a vector space, and hence be usable as | ||
# an integrand, where the vector operations act on the closed-over parameters of f that are | ||
# begin differentiated with respect to. In particular, if we have a closure f = x -> g(x, p), and we want | ||
# to differentiate with respect to p, then our reverse (vJp) rule needs an integrand given by the | ||
# Jacobian-vector product (pullback) vᵀ∂g/∂p. But Enzyme wraps this in a closure so that it is the | ||
# same "shape" as f, whereas to integrate it we need to be able to treat it as a vector space. | ||
# ClosureVector calls Enzyme.Compiler.recursive_add, which is an internal function that "unwraps" | ||
# the closure to access the internal state, which can then be added/subtracted/scaled. | ||
struct ClosureVector{F} | ||
f::F | ||
end | ||
|
||
@inline function guaranteed_nonactive(::Type{T}) where T | ||
rt = Enzyme.Compiler.active_reg_inner(T, (), nothing) | ||
return rt == Enzyme.Compiler.AnyState || rt == Enzyme.Compiler.DupState | ||
end | ||
|
||
function Base.:+(a::CV, b::CV) where {CV <: ClosureVector} | ||
Enzyme.Compiler.recursive_add(a, b, identity, guaranteed_nonactive)::CV | ||
end | ||
wsmoses marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
function Base.:-(a::CV, b::CV) where {CV <: ClosureVector} | ||
Enzyme.Compiler.recursive_add(a, b, x->-x, guaranteed_nonactive)::CV | ||
end | ||
|
||
function Base.:*(a::Number, b::CV) where {CV <: ClosureVector} | ||
# b + (a-1) * b = a * b | ||
Enzyme.Compiler.recursive_add(b, b, x->(a-1)*x, guaranteed_nonactive)::CV | ||
end | ||
|
||
function Base.:*(a::ClosureVector, b::Number) | ||
return b*a | ||
end | ||
|
||
function Enzyme.EnzymeRules.reverse(config, ofunc::Const{typeof(quadgk)}, dres::Active, cache, f::Union{Const, Active}, segs::Annotation{T}...; kws...) where {T} | ||
df = if f isa Const | ||
nothing | ||
else | ||
segbuf = cache[1] | ||
fwd, rev = Enzyme.autodiff_thunk(ReverseSplitNoPrimal, Const{typeof(call)}, Active, typeof(f), Const{T}) | ||
_df, _ = quadgk(map(x->x.val, segs)...; kws..., eval_segbuf=segbuf, maxevals=0, norm=f->0) do x | ||
tape, prim, shad = fwd(Const(call), f, Const(x)) | ||
drev = rev(Const(call), f, Const(x), dres.val[1], tape) | ||
return ClosureVector(drev[1][1]) | ||
end | ||
_df.f | ||
end | ||
dsegs1 = segs[1] isa Const ? nothing : -LinearAlgebra.dot(f.val(segs[1].val), dres.val[1]) | ||
dsegsn = segs[end] isa Const ? nothing : LinearAlgebra.dot(f.val(segs[end].val), dres.val[1]) | ||
return (df, # f | ||
dsegs1, | ||
ntuple(i -> nothing, Val(length(segs)-2))..., | ||
dsegsn) | ||
end | ||
|
||
function Enzyme.EnzymeRules.reverse(config, ofunc::Const{typeof(quadgk)}, dres::Type{<:Union{Duplicated, BatchDuplicated}}, cache, f::Union{Const, Active}, segs::Annotation{T}...; kws...) where {T} | ||
dres = cache[2] | ||
df = if f isa Const | ||
nothing | ||
else | ||
segbuf = cache[1] | ||
fwd, rev = Enzyme.autodiff_thunk(ReverseSplitNoPrimal, Const{typeof(call)}, Active, typeof(f), Const{T}) | ||
_df, _ = quadgk(map(x->x.val, segs)...; kws..., eval_segbuf=segbuf, maxevals=0, norm=f->0) do x | ||
tape, prim, shad = fwd(Const(call), f, Const(x)) | ||
shad .= dres | ||
drev = rev(Const(call), f, Const(x), tape) | ||
return ClosureVector(drev[1][1]) | ||
end | ||
_df.f | ||
end | ||
dsegs1 = segs[1] isa Const ? nothing : -LinearAlgebra.dot(f.val(segs[1].val), dres) | ||
dsegsn = segs[end] isa Const ? nothing : LinearAlgebra.dot(f.val(segs[end].val), dres) | ||
Enzyme.make_zero!(dres) | ||
return (df, # f | ||
dsegs1, | ||
ntuple(i -> nothing, Val(length(segs)-2))..., | ||
dsegsn) | ||
end | ||
|
||
end # module |
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[deps] | ||
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" | ||
Enzyme = "7da242da-08ed-463a-9acd-ee780be4f1d9" | ||
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" |
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.
Uh oh!
There was an error while loading. Please reload this page.