-
-
Notifications
You must be signed in to change notification settings - Fork 75
Mooncake, LinearSolve integration - 1 #791
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
19 commits
Select commit
Hold shift + click to select a range
b514238
Mooncake with LinearSolve
AstitvaAggarwal ca7d681
Merge branch 'main' of https://github.com/SciML/LinearSolve.jl
AstitvaAggarwal babf81e
rm enzyme from adjoint
AstitvaAggarwal 2c80e2d
Mooncake for adjoint tests
AstitvaAggarwal b47d733
Mooncake compat
AstitvaAggarwal 384ea2d
all pass for adjoint tests except Complex Nums
AstitvaAggarwal 03391e6
Mooncake on lts only
AstitvaAggarwal 4b5aeba
lts, 1.11.x
AstitvaAggarwal 83c2604
.
AstitvaAggarwal 5e6ca62
Update runtests.jl
AstitvaAggarwal a77be18
Merge branch 'SciML:main' into dev
AstitvaAggarwal c951d85
remove stale import from klu module
AstitvaAggarwal 5b1d3bd
see why CI fails..;
AstitvaAggarwal 29f7769
.
AstitvaAggarwal 8564944
NoPre fails unrelated to PR imo
AstitvaAggarwal 934a279
Update runtests.jl
ChrisRackauckas c548ee4
Rename test/adjoint_mooncake.jl to test/nopre/mooncake.jl
ChrisRackauckas def891b
Update runtests.jl
ChrisRackauckas 0140dc2
Update Project.toml
ChrisRackauckas 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 |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| module LinearSolveMooncakeExt | ||
|
|
||
| using Mooncake | ||
| using Mooncake: @from_chainrules, MinimalCtx, ReverseMode, NoRData, increment!! | ||
| using LinearSolve: LinearSolve, SciMLLinearSolveAlgorithm, init, solve!, LinearProblem, | ||
| LinearCache, AbstractKrylovSubspaceMethod, DefaultLinearSolver, | ||
| defaultalg_adjoint_eval, solve | ||
| using LinearSolve.LinearAlgebra | ||
| using SciMLBase | ||
|
|
||
| @from_chainrules MinimalCtx Tuple{typeof(SciMLBase.solve), LinearProblem, Nothing} true ReverseMode | ||
| @from_chainrules MinimalCtx Tuple{ | ||
| typeof(SciMLBase.solve), LinearProblem, SciMLLinearSolveAlgorithm} true ReverseMode | ||
| @from_chainrules MinimalCtx Tuple{ | ||
| Type{<:LinearProblem}, AbstractMatrix, AbstractVector, SciMLBase.NullParameters} true ReverseMode | ||
|
|
||
| function Mooncake.increment_and_get_rdata!(f, r::NoRData, t::LinearProblem) | ||
| f.data.A .+= t.A | ||
| f.data.b .+= t.b | ||
|
|
||
| return NoRData() | ||
| end | ||
|
|
||
| function Mooncake.to_cr_tangent(x::Mooncake.PossiblyUninitTangent{T}) where {T} | ||
| if Mooncake.is_init(x) | ||
| return Mooncake.to_cr_tangent(x.tangent) | ||
| else | ||
| error("Trying to convert uninitialized tangent to ChainRules tangent.") | ||
| end | ||
| end | ||
|
|
||
| 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
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,155 @@ | ||
| using ForwardDiff | ||
| using LinearSolve, LinearAlgebra, Test | ||
| using FiniteDiff, RecursiveFactorization | ||
| using LazyArrays: BroadcastArray | ||
| using Mooncake | ||
|
|
||
| # first test | ||
| n = 4 | ||
| A = rand(n, n); | ||
| b1 = rand(n); | ||
|
|
||
| function f(A, b1; alg = LUFactorization()) | ||
| prob = LinearProblem(A, b1) | ||
|
|
||
| sol1 = solve(prob, alg) | ||
|
|
||
| s1 = sol1.u | ||
| norm(s1) | ||
| end | ||
|
|
||
| f_primal = f(A, b1) # Uses BLAS | ||
|
|
||
| cache = prepare_gradient_cache(f, (copy(A), copy(b1))...) | ||
| value, gradient = Mooncake.value_and_gradient!!(cache, f, (copy(A), copy(b1))...) | ||
|
|
||
| dA2 = ForwardDiff.gradient(x -> f(x, eltype(x).(b1)), copy(A)) | ||
| db12 = ForwardDiff.gradient(x -> f(eltype(x).(A), x), copy(b1)) | ||
|
|
||
| # Mooncake | ||
| @test value ≈ f_primal | ||
| @test gradient[2] ≈ dA2 | ||
| @test gradient[3] ≈ db12 | ||
|
|
||
| # Second test | ||
| A = rand(n, n); | ||
| b1 = rand(n); | ||
|
|
||
| _ff = (x, | ||
| y) -> f(x, | ||
| y; | ||
| alg = LinearSolve.DefaultLinearSolver(LinearSolve.DefaultAlgorithmChoice.LUFactorization)) | ||
| f_primal = _ff(copy(A), copy(b1)) | ||
|
|
||
| cache = prepare_gradient_cache(_ff, (copy(A), copy(b1))...) | ||
| value, gradient = Mooncake.value_and_gradient!!(cache, _ff, (copy(A), copy(b1))...) | ||
|
|
||
| dA2 = ForwardDiff.gradient(x -> f(x, eltype(x).(b1)), copy(A)) | ||
| db12 = ForwardDiff.gradient(x -> f(eltype(x).(A), x), copy(b1)) | ||
|
|
||
| # Mooncake | ||
| @test value ≈ f_primal | ||
| @test gradient[2] ≈ dA2 | ||
| @test gradient[3] ≈ db12 | ||
|
|
||
| # third test | ||
| # Test complex numbers | ||
| A = rand(n, n) + 1im * rand(n, n); | ||
| b1 = rand(n) + 1im * rand(n); | ||
|
|
||
| function f3(A, b1, b2; alg = KrylovJL_GMRES()) | ||
| prob = LinearProblem(A, b1) | ||
| sol1 = solve(prob, alg) | ||
| prob = LinearProblem(A, b2) | ||
| sol2 = solve(prob, alg) | ||
| norm(sol1.u .+ sol2.u) | ||
| end | ||
|
|
||
| # Mooncake needs atomic Complex Number tangents instead of NamedTuples. | ||
| # cache = Mooncake.prepare_gradient_cache(f3, (copy(A), copy(b1), copy(b1))...) | ||
| # results = Mooncake.value_and_gradient!!(cache, f3, (copy(A), copy(b1), copy(b1))...) | ||
|
|
||
| # dA2 = FiniteDiff.finite_difference_gradient( | ||
| # x -> f3(x, eltype(x).(b1), eltype(x).(b1)), copy(A)) | ||
| # db12 = FiniteDiff.finite_difference_gradient( | ||
| # x -> f3(eltype(x).(A), x, eltype(x).(b1)), copy(b1)) | ||
| # db22 = FiniteDiff.finite_difference_gradient( | ||
| # x -> f3(eltype(x).(A), eltype(x).(b1), x), copy(b1)) | ||
|
|
||
| # @test f3(A, b1, b1) ≈ results[1] | ||
| # @test dA2 ≈ results[2][2] | ||
| # @test db12 ≈ results[2][3] | ||
| # @test db22 ≈ results[2][4] | ||
|
|
||
| # fourth test | ||
| A = rand(n, n); | ||
| b1 = rand(n); | ||
|
|
||
| function f4(A, b1, b2; alg = LUFactorization()) | ||
| prob = LinearProblem(A, b1) | ||
| sol1 = solve(prob, alg; sensealg = LinearSolveAdjoint(; linsolve = KrylovJL_LSMR())) | ||
| prob = LinearProblem(A, b2) | ||
| sol2 = solve(prob, alg; sensealg = LinearSolveAdjoint(; linsolve = KrylovJL_GMRES())) | ||
| norm(sol1.u .+ sol2.u) | ||
| end | ||
|
|
||
| cache = Mooncake.prepare_gradient_cache(f4, (copy(A), copy(b1), copy(b1))...) | ||
| results = Mooncake.value_and_gradient!!(cache, f4, (copy(A), copy(b1), copy(b1))...) | ||
|
|
||
| dA2 = ForwardDiff.gradient(x -> f4(x, eltype(x).(b1), eltype(x).(b1)), copy(A)) | ||
| db12 = ForwardDiff.gradient(x -> f4(eltype(x).(A), x, eltype(x).(b1)), copy(b1)) | ||
| db22 = ForwardDiff.gradient(x -> f4(eltype(x).(A), eltype(x).(b1), x), copy(b1)) | ||
|
|
||
| @test f4(A, b1, b1) ≈ results[1] | ||
| @test dA2 ≈ results[2][2] | ||
| @test db12 ≈ results[2][3] | ||
| @test db22 ≈ results[2][4] | ||
|
|
||
| # fifth test | ||
| A = rand(n, n); | ||
| b1 = rand(n); | ||
| for alg in ( | ||
| LUFactorization(), | ||
| RFLUFactorization(), | ||
| KrylovJL_GMRES() | ||
| ) | ||
| @show alg | ||
| function fb(b) | ||
| prob = LinearProblem(A, b) | ||
|
|
||
| sol1 = solve(prob, alg) | ||
|
|
||
| sum(sol1.u) | ||
| end | ||
| fb(b1) | ||
|
|
||
| fd_jac = FiniteDiff.finite_difference_jacobian(fb, b1) |> vec | ||
| @show fd_jac | ||
|
|
||
| cache = Mooncake.prepare_gradient_cache(fb, copy(b1)) | ||
| results = Mooncake.value_and_gradient!!(cache, fb, copy(b1)) | ||
| @show results | ||
|
|
||
| @test results[1] ≈ fb(b1) | ||
| @test results[2][2] ≈ fd_jac rtol = 1e-5 | ||
|
|
||
| function fA(A) | ||
| prob = LinearProblem(A, b1) | ||
|
|
||
| sol1 = solve(prob, alg) | ||
|
|
||
| sum(sol1.u) | ||
| end | ||
| fA(A) | ||
|
|
||
| fd_jac = FiniteDiff.finite_difference_jacobian(fA, A) |> vec | ||
| @show fd_jac | ||
|
|
||
| cache = Mooncake.prepare_gradient_cache(fA, copy(A)) | ||
| results = Mooncake.value_and_gradient!!(cache, fA, copy(A)) | ||
| @show results | ||
| mooncake_gradient = results[2][2] |> vec | ||
|
|
||
| @test results[1] ≈ fA(A) | ||
| @test mooncake_gradient ≈ fd_jac rtol = 1e-5 | ||
| 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
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.
what's this about?
Uh oh!
There was an error while loading. Please reload this page.
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 was getting ExplicitImports, Aqua errors in all
/ Tests - NoPresince the import were not being technically directly used. In the same file, they are used as Base.propertynames etc. so I removed the imports.Uh oh!
There was an error while loading. Please reload this page.
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.
after this we get the errors : https://github.com/SciML/LinearSolve.jl/actions/runs/18461238570/job/52593055519?pr=791. (dont seem PR related)