-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/v0.2-backport' into v0.2-backport
- Loading branch information
Showing
10 changed files
with
256 additions
and
131 deletions.
There are no files selected for viewing
This file contains 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 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,42 @@ | ||
module AdvancedVIEnzymeExt | ||
|
||
if isdefined(Base, :get_extension) | ||
using AdvancedVI: AdvancedVI, ADTypes, DiffResults, Distributions | ||
using Enzyme: Enzyme | ||
else | ||
using ..AdvancedVI: AdvancedVI, ADTypes, DiffResults, Distributions | ||
using ..Enzyme: Enzyme | ||
end | ||
|
||
AdvancedVI.ADBackend(::Val{:enzyme}) = ADTypes.AutoEnzyme() | ||
function AdvancedVI.setadbackend(::Val{:enzyme}) | ||
Base.depwarn("`setadbackend` is deprecated. Please pass a `ADTypes.AbstractADType` as a keyword argument to the VI algorithm.", :setadbackend) | ||
AdvancedVI.ADBACKEND[] = :enzyme | ||
end | ||
|
||
function AdvancedVI.grad!( | ||
vo, | ||
alg::AdvancedVI.VariationalInference{<:ADTypes.AutoEnzyme}, | ||
q, | ||
model, | ||
θ::AbstractVector{<:Real}, | ||
out::DiffResults.MutableDiffResult, | ||
args... | ||
) | ||
f(θ) = | ||
if (q isa Distributions.Distribution) | ||
-vo(alg, AdvancedVI.update(q, θ), model, args...) | ||
else | ||
-vo(alg, q(θ), model, args...) | ||
end | ||
# Use `Enzyme.ReverseWithPrimal` once it is released: | ||
# https://github.com/EnzymeAD/Enzyme.jl/pull/598 | ||
y = f(θ) | ||
DiffResults.value!(out, y) | ||
dy = DiffResults.gradient(out) | ||
fill!(dy, 0) | ||
Enzyme.autodiff(Enzyme.ReverseWithPrimal, f, Enzyme.Active, Enzyme.Duplicated(θ, dy)) | ||
return out | ||
end | ||
|
||
end |
This file contains 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,13 @@ | ||
module AdvancedVIFluxExt | ||
|
||
if isdefined(Base, :get_extension) | ||
using AdvancedVI: AdvancedVI | ||
using Flux: Flux | ||
else | ||
using ..AdvancedVI: AdvancedVI | ||
using ..Flux: Flux | ||
end | ||
|
||
AdvancedVI.apply!(o::Flux.Optimise.AbstractOptimiser, x, Δ) = Flux.Optimise.apply!(o, x, Δ) | ||
|
||
end |
This file contains 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,40 @@ | ||
module AdvancedVIReverseDiffExt | ||
|
||
if isdefined(Base, :get_extension) | ||
using AdvancedVI: AdvancedVI, ADTypes, DiffResults, Distributions | ||
using ReverseDiff: ReverseDiff | ||
else | ||
using ..AdvancedVI: AdvancedVI, ADTypes, DiffResults, Distributions | ||
using ..ReverseDiff: ReverseDiff | ||
end | ||
|
||
AdvancedVI.ADBackend(::Val{:reversediff}) = ADTypes.AutoReverseDiff() | ||
|
||
function AdvancedVI.setadbackend(::Val{:reversediff}) | ||
Base.depwarn("`setadbackend` is deprecated. Please pass a `ADTypes.AbstractADType` as a keyword argument to the VI algorithm.", :setadbackend) | ||
AdvancedVI.ADBACKEND[] = :reversediff | ||
end | ||
|
||
tape(f, x) = ReverseDiff.GradientTape(f, x) | ||
|
||
function AdvancedVI.grad!( | ||
vo, | ||
alg::AdvancedVI.VariationalInference{<:ADTypes.AutoReverseDiff}, | ||
q, | ||
model, | ||
θ::AbstractVector{<:Real}, | ||
out::DiffResults.MutableDiffResult, | ||
args... | ||
) | ||
f(θ) = | ||
if (q isa Distributions.Distribution) | ||
-vo(alg, AdvancedVI.update(q, θ), model, args...) | ||
else | ||
-vo(alg, q(θ), model, args...) | ||
end | ||
tp = tape(f, θ) | ||
ReverseDiff.gradient!(out, tp, θ) | ||
return out | ||
end | ||
|
||
end |
This file contains 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,39 @@ | ||
module AdvancedVIZygoteExt | ||
|
||
if isdefined(Base, :get_extension) | ||
using AdvancedVI: AdvancedVI, ADTypes, DiffResults, Distributions | ||
using Zygote: Zygote | ||
else | ||
using ..AdvancedVI: AdvancedVI, ADTypes, DiffResults, Distributions | ||
using ..Zygote: Zygote | ||
end | ||
|
||
AdvancedVI.ADBackend(::Val{:zygote}) = ADTypes.AutoZygote() | ||
function AdvancedVI.setadbackend(::Val{:zygote}) | ||
Base.depwarn("`setadbackend` is deprecated. Please pass a `ADTypes.AbstractADType` as a keyword argument to the VI algorithm.", :setadbackend) | ||
AdvancedVI.ADBACKEND[] = :zygote | ||
end | ||
|
||
function AdvancedVI.grad!( | ||
vo, | ||
alg::AdvancedVI.VariationalInference{<:ADTypes.AutoZygote}, | ||
q, | ||
model, | ||
θ::AbstractVector{<:Real}, | ||
out::DiffResults.MutableDiffResult, | ||
args... | ||
) | ||
f(θ) = | ||
if (q isa Distributions.Distribution) | ||
-vo(alg, AdvancedVI.update(q, θ), model, args...) | ||
else | ||
-vo(alg, q(θ), model, args...) | ||
end | ||
y, back = Zygote.pullback(f, θ) | ||
dy = first(back(1.0)) | ||
DiffResults.value!(out, y) | ||
DiffResults.gradient!(out, dy) | ||
return out | ||
end | ||
|
||
end |
This file contains 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.