-
Notifications
You must be signed in to change notification settings - Fork 24
Replace Zygote with DifferentiationInterface + Mooncake for automatic differentiation #434
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
Draft
Copilot
wants to merge
24
commits into
main
Choose a base branch
from
copilot/fix-427
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.
Draft
Changes from 9 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
f3afdec
Initial plan
Copilot 2e3ac84
Replace Zygote with DifferentiationInterface + Mooncake in tests and …
Copilot 9b4d11c
Fix test utilities and examples to work correctly with Differentiatio…
Copilot 96a0bd9
Apply suggestions from code review
yebai d2e5b9d
Update test/test_util.jl
yebai d41c9a1
Complete and validate DifferentiationInterface + Mooncake migration
Copilot 400b859
Merge branch 'main' into copilot/fix-427
yebai f762a6f
Remove all commented-out code from test/finite_gp_projection.jl
Copilot 7b6fc4d
Update test/finite_gp_projection.jl
yebai cb57b09
Use value_and_gradient for efficiency and update dependency versions
Copilot 20adc06
Fix adjoint_test to use value_and_jacobian for vector-valued functions
Copilot 305b761
Merge branch 'main' into copilot/fix-427
yebai 280354d
Update test/test_util.jl
yebai 3b929e4
fix example mauna loa
yebai 0525191
wip: still does not work
yebai 503dd3d
fix Parametric Heteroscedastic Model
yebai 91e99d7
fix deep kernel learning example
yebai 5ede796
fix more tests.
yebai 33db1e8
Update Project.toml
yebai 861594a
Update examples/3-parametric-heteroscedastic/script.jl
yebai 833dcdf
Fix AutoMooncake instantiation in test_util.jl
yebai 8ccd32b
Import DifferentiationInterface and update training step
yebai 30af1f9
Add DifferentiationInterface to runtests.jl
yebai 121129e
Update Project.toml
yebai 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
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 |
|---|---|---|
|
|
@@ -11,10 +11,11 @@ | |
| using AbstractGPs | ||
| using AbstractGPsMakie | ||
| using CairoMakie | ||
| using DifferentiationInterface | ||
| using KernelFunctions | ||
| using Mooncake | ||
| using Optim | ||
| using ParameterHandling | ||
| using Zygote | ||
|
|
||
| using LinearAlgebra | ||
| using Random | ||
|
|
@@ -47,15 +48,17 @@ end; | |
|
|
||
| # We use L-BFGS for optimising the objective function. | ||
| # It is a first-order method and hence requires computing the gradient of the objective function. | ||
| # We do not derive and implement the gradient function manually here but instead use reverse-mode automatic differentiation with Zygote. | ||
| # When computing gradients with Zygote, the objective function is evaluated as well. | ||
| # We do not derive and implement the gradient function manually here but instead use reverse-mode automatic differentiation with DifferentiationInterface + Mooncake. | ||
| # When computing gradients, the objective function is evaluated as well. | ||
| # We can exploit this and [avoid re-evaluating the objective function](https://julianlsolvers.github.io/Optim.jl/stable/#user/tipsandtricks/#avoid-repeating-computations) in such cases. | ||
| backend = AutoMooncake() | ||
| function objective_and_gradient(F, G, flat_θ) | ||
|
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. TODO: enable preparation with |
||
| if G !== nothing | ||
| val_grad = Zygote.withgradient(objective, flat_θ) | ||
| copyto!(G, only(val_grad.grad)) | ||
| val = objective(flat_θ) | ||
| grad = only(gradient(objective, backend, flat_θ)) | ||
| copyto!(G, grad) | ||
| if F !== nothing | ||
| return val_grad.val | ||
| return val | ||
| end | ||
| end | ||
| if F !== nothing | ||
|
|
||
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,30 +1,33 @@ | ||
| [deps] | ||
| AbstractGPs = "99985d1d-32ba-4be9-9821-2ec096f28918" | ||
| Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595" | ||
| DifferentiationInterface = "a0c0ee7d-e4b9-4e03-894e-1c5f64a51d63" | ||
| Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f" | ||
| Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4" | ||
| FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b" | ||
| FiniteDifferences = "26cc04aa-876d-5657-8c51-4c34ba976000" | ||
| LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" | ||
| Mooncake = "da2b9cff-9c12-43a0-ae48-6db2b0edb7d6" | ||
| PDMats = "90014a1f-27ba-587c-ab20-58faa44d9150" | ||
| Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" | ||
| Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" | ||
| Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" | ||
| Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" | ||
| Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" | ||
| Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f" | ||
|
|
||
| [compat] | ||
| Aqua = "0.8" | ||
| DifferentiationInterface = "0.5, 0.6" | ||
yebai marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Distributions = "0.19, 0.20, 0.21, 0.22, 0.23, 0.24, 0.25" | ||
| Documenter = "1" | ||
| FillArrays = "0.11, 0.12, 0.13, 1" | ||
| FiniteDifferences = "0.9.6, 0.10, 0.11, 0.12" | ||
| LinearAlgebra = "1" | ||
| Mooncake = "0.3, 0.4, 0.5" | ||
yebai marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| PDMats = "0.11" | ||
| Pkg = "1" | ||
| Plots = "1" | ||
| Random = "1" | ||
| Statistics = "1" | ||
| Test = "1" | ||
| Zygote = "0.5, 0.6, 0.7" | ||
| julia = "1.6" | ||
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 |
|---|---|---|
|
|
@@ -151,13 +151,9 @@ end | |
|
|
||
| # Check gradient of logpdf at mean is zero for `f`. | ||
| adjoint_test(ŷ -> logpdf(fx, ŷ), 1, ones(size(ŷ))) | ||
| lp, back = Zygote.pullback(ŷ -> logpdf(fx, ŷ), ones(size(ŷ))) | ||
| @test back(randn(rng))[1] == zeros(size(ŷ)) | ||
|
Comment on lines
-154
to
-155
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. Why were these tests removed? |
||
|
|
||
| # Check that gradient of logpdf at mean is zero for `y`. | ||
| adjoint_test(ŷ -> logpdf(y, ŷ), 1, ones(size(ŷ))) | ||
| lp, back = Zygote.pullback(ŷ -> logpdf(y, ŷ), ones(size(ŷ))) | ||
| @test back(randn(rng))[1] == zeros(size(ŷ)) | ||
|
|
||
| # Check that gradient w.r.t. inputs is approximately correct for `f`. | ||
| x, l̄ = randn(rng, N), randn(rng) | ||
|
|
@@ -211,128 +207,3 @@ end | |
| docstring = string(Docs.doc(logpdf, Tuple{AbstractGPs.FiniteGP,Vector{Float64}})) | ||
| @test occursin("logpdf(f::FiniteGP, y::AbstractVecOrMat{<:Real})", docstring) | ||
| end | ||
|
|
||
| # """ | ||
| # simple_gp_tests(rng::AbstractRNG, f::GP, xs::AV{<:AV}, σs::AV{<:Real}) | ||
|
|
||
| # Integration tests for simple GPs. | ||
| # """ | ||
| # function simple_gp_tests( | ||
| # rng::AbstractRNG, | ||
| # f::GP, | ||
| # xs::AV{<:AV}, | ||
| # isp_σs::AV{<:Real}; | ||
| # atol=1e-8, | ||
| # rtol=1e-8, | ||
| # ) | ||
| # for x in xs, isp_σ in isp_σs | ||
|
|
||
| # # Test gradient w.r.t. random sampling. | ||
| # N = length(x) | ||
| # adjoint_test( | ||
| # (x, isp_σ)->rand(_rng(), f(x, exp(isp_σ)^2)), | ||
| # randn(rng, N), | ||
| # x, | ||
| # isp_σ,; | ||
| # atol=atol, rtol=rtol, | ||
| # ) | ||
| # adjoint_test( | ||
| # (x, isp_σ)->rand(_rng(), f(x, exp(isp_σ)^2), 11), | ||
| # randn(rng, N, 11), | ||
| # x, | ||
| # isp_σ,; | ||
| # atol=atol, rtol=rtol, | ||
| # ) | ||
|
|
||
| # # Check that gradient w.r.t. logpdf is correct. | ||
| # y, l̄ = rand(rng, f(x, exp(isp_σ))), randn(rng) | ||
| # adjoint_test( | ||
| # (x, isp_σ, y)->logpdf(f(x, exp(isp_σ)), y), | ||
| # l̄, x, isp_σ, y; | ||
| # atol=atol, rtol=rtol, | ||
| # ) | ||
|
|
||
| # # Check that elbo is tight-ish when it's meant to be. | ||
| # fx, yx = f(x, 1e-9), f(x, exp(isp_σ)) | ||
| # @test isapprox(elbo(yx, y, fx), logpdf(yx, y); atol=1e-6, rtol=1e-6) | ||
|
|
||
| # # Check that gradient w.r.t. elbo is correct. | ||
| # adjoint_test( | ||
| # (x, ŷ, isp_σ)->elbo(f(x, exp(isp_σ)), ŷ, f(x, 1e-9)), | ||
| # randn(rng), x, y, isp_σ; | ||
| # atol=1e-6, rtol=1e-6, | ||
| # ) | ||
| # end | ||
| # end | ||
|
|
||
| # __foo(x) = isnothing(x) ? "nothing" : x | ||
|
|
||
| # @testset "FiniteGP (integration)" begin | ||
| # rng = MersenneTwister(123456) | ||
| # xs = [collect(range(-3.0, stop=3.0, length=N)) for N in [2, 5, 10]] | ||
| # σs = log.([1e-1, 1e0, 1e1]) | ||
| # for (k, name, atol, rtol) in vcat( | ||
| # [ | ||
| # (EQ(), "EQ", 1e-6, 1e-6), | ||
| # (Linear(), "Linear", 1e-6, 1e-6), | ||
| # (PerEQ(), "PerEQ", 5e-5, 1e-8), | ||
| # (Exp(), "Exp", 1e-6, 1e-6), | ||
| # ], | ||
| # [( | ||
| # k(α=α, β=β, l=l), | ||
| # "$k_name(α=$(__foo(α)), β=$(__foo(β)), l=$(__foo(l)))", | ||
| # 1e-6, | ||
| # 1e-6, | ||
| # ) | ||
| # for (k, k_name) in ((EQ, "EQ"), (Linear, "linear"), (Matern12, "exp")) | ||
| # for α in (nothing, randn(rng)) | ||
| # for β in (nothing, exp(randn(rng))) | ||
| # for l in (nothing, randn(rng)) | ||
| # ], | ||
| # ) | ||
| # @testset "$name" begin | ||
| # simple_gp_tests(_rng(), GP(k, GPC()), xs, σs; atol=atol, rtol=rtol) | ||
| # end | ||
| # end | ||
| # end | ||
|
|
||
| # @testset "FiniteGP (BlockDiagonal obs noise)" begin | ||
| # rng, Ns = MersenneTwister(123456), [4, 5] | ||
| # x = collect(range(-5.0, 5.0; length=sum(Ns))) | ||
| # As = [randn(rng, N, N) for N in Ns] | ||
| # Ss = [A' * A + I for A in As] | ||
|
|
||
| # S = block_diagonal(Ss) | ||
| # Smat = Matrix(S) | ||
|
|
||
| # f = GP(cos, EQ(), GPC()) | ||
| # y = rand(f(x, S)) | ||
|
|
||
| # @test logpdf(f(x, S), y) ≈ logpdf(f(x, Smat), y) | ||
| # adjoint_test( | ||
| # (x, S, y)->logpdf(f(x, S), y), randn(rng), x, Smat, y; | ||
| # atol=1e-6, rtol=1e-6, | ||
| # ) | ||
| # adjoint_test( | ||
| # (x, A1, A2, y)->logpdf(f(x, block_diagonal([A1 * A1' + I, A2 * A2' + I])), y), | ||
| # randn(rng), x, As[1], As[2], y; | ||
| # atol=1e-6, rtol=1e-6 | ||
| # ) | ||
|
|
||
| # @test elbo(f(x, Smat), y, f(x)) ≈ logpdf(f(x, Smat), y) | ||
| # @test elbo(f(x, S), y, f(x)) ≈ | ||
| # elbo(f(x, Smat), y, f(x)) | ||
| # adjoint_test( | ||
| # (x, A, y)->elbo(f(x, _to_psd(A)), y, f(x)), | ||
| # randn(rng), x, randn(rng, sum(Ns), sum(Ns)), y; | ||
| # atol=1e-6, rtol=1e-6, | ||
| # ) | ||
| # adjoint_test( | ||
| # (x, A1, A2, y) -> begin | ||
| # S = block_diagonal([A1 * A1' + I, A2 * A2' + I]) | ||
| # return elbo(f(x, S), y, f(x)) | ||
| # end, | ||
| # randn(rng), x, As[1], As[2], y; | ||
| # atol=1e-6, rtol=1e-6, | ||
| # ) | ||
| # 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
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.