Skip to content
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

Customize backend for conditions #87

Merged
merged 3 commits into from
Aug 5, 2023
Merged

Customize backend for conditions #87

merged 3 commits into from
Aug 5, 2023

Conversation

gdalle
Copy link
Member

@gdalle gdalle commented Aug 4, 2023

Add a conditions_backend kwarg to the ImplicitFunction constructor to decide which backend will be used for condition differentiation.
If conditions_backend is nothing, the same backend will be used as for the implicit function.

@gdalle gdalle linked an issue Aug 4, 2023 that may be closed by this pull request
@gdalle
Copy link
Member Author

gdalle commented Aug 4, 2023

This doesn't work with conditions_backend = AD.ZygoteBackend() or AD.ReverseDiffBackend(). Here's the error:

ArgumentError: Tuple contains 2 elements, must contain exactly 1 element
  Stacktrace:
    [1] only(x::Tuple{Float64, Float64})
      @ Base.Iterators ./iterators.jl:1531
    [2] (::AbstractDifferentiation.var"#14#16"{Vector{Float64}, ImplicitDifferentiation.var"#6#8"{Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, ImplicitFunction{var"#forward#8", var"#conditions#9", IterativeLinearSolver, AbstractDifferentiation.ReverseRuleConfigBackend{ZygoteRuleConfig{Zygote.Context{false}}}}, Vector{Float64}}, Tuple{Vector{Float64}}})(::Float64, ::Vararg{Float64})
      @ AbstractDifferentiation ~/.julia/packages/AbstractDifferentiation/eEkWP/src/AbstractDifferentiation.jl:172

I thought it might be linked to JuliaDiff/AbstractDifferentiation.jl#63 for Zygote but that may not explain the ReverseDiff aspect

@gdalle
Copy link
Member Author

gdalle commented Aug 4, 2023

Also doesn't work with AD.FiniteDifferencesBackend(), for a different reason

MethodError: no method matching (::ImplicitDifferentiation.var"#6#8"{Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, ImplicitFunction{var"#forward#8", var"#conditions#9", IterativeLinearSolver, AbstractDifferentiation.FiniteDifferencesBackend{FiniteDifferences.AdaptedFiniteDifferenceMethod{5, 1, FiniteDifferences.UnadaptedFiniteDifferenceMethod{7, 5}}}}, Vector{Float64}})(::Vector{Float64}, ::Vector{Float64})
  
  Closest candidates are:
    (::ImplicitDifferentiation.var"#6#8")(::Any)
     @ ImplicitDifferentiation ~/Work/GitHub/Julia/ImplicitDifferentiation.jl/src/operators.jl:11
  
  Stacktrace:
    [1] (::FiniteDifferences.var"#88#89"{ImplicitDifferentiation.var"#6#8"{Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, ImplicitFunction{var"#forward#8", var"#conditions#9", IterativeLinearSolver, AbstractDifferentiation.FiniteDifferencesBackend{FiniteDifferences.AdaptedFiniteDifferenceMethod{5, 1, FiniteDifferences.UnadaptedFiniteDifferenceMethod{7, 5}}}}, Vector{Float64}}})(xs::Tuple{Vector{Float64}, Vector{Float64}})
      @ FiniteDifferences ~/.julia/packages/FiniteDifferences/SDe8k/src/grad.jl:64
    [2] jvp(fdm::FiniteDifferences.AdaptedFiniteDifferenceMethod{5, 1, FiniteDifferences.UnadaptedFiniteDifferenceMethod{7, 5}}, f::FiniteDifferences.var"#88#89"{ImplicitDifferentiation.var"#6#8"{Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, ImplicitFunction{var"#forward#8", var"#conditions#9", IterativeLinearSolver, AbstractDifferentiation.FiniteDifferencesBackend{FiniteDifferences.AdaptedFiniteDifferenceMethod{5, 1, FiniteDifferences.UnadaptedFiniteDifferenceMethod{7, 5}}}}, Vector{Float64}}}, ::Tuple{Tuple{Vector{Float64}, Vector{Float64}}, Tuple{Float64, Float64}})
      @ FiniteDifferences ~/.julia/packages/FiniteDifferences/SDe8k/src/grad.jl:59

@codecov
Copy link

codecov bot commented Aug 4, 2023

Codecov Report

Patch coverage: 84.61% and project coverage change: +0.17% 🎉

Comparison is base (9d34d8b) 96.27% compared to head (de5876e) 96.44%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #87      +/-   ##
==========================================
+ Coverage   96.27%   96.44%   +0.17%     
==========================================
  Files           9        9              
  Lines         161      169       +8     
==========================================
+ Hits          155      163       +8     
  Misses          6        6              
Files Changed Coverage Δ
src/implicit_function.jl 70.00% <33.33%> (ø)
ext/ImplicitDifferentiationChainRulesExt.jl 100.00% <100.00%> (ø)
ext/ImplicitDifferentiationForwardDiffExt.jl 100.00% <100.00%> (ø)

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@gdalle gdalle merged commit 450d8aa into main Aug 5, 2023
@gdalle gdalle deleted the backend_cond branch August 9, 2023 22:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Reverse mode AD with forward mode backend for conditions (and vice versa)
1 participant