|
| 1 | +# Here we determine the preferred AD backend. We have a predefined list of ADs and then |
| 2 | +# we select the first one that is avialable and would work with the problem. |
| 3 | + |
| 4 | +# Ordering is important here. We want to select the first one that is compatible with the |
| 5 | +# problem. |
| 6 | +const ReverseADs = [ |
| 7 | + ADTypes.AutoEnzyme(; mode = EnzymeCore.Reverse), |
| 8 | + ADTypes.AutoZygote(), |
| 9 | + ADTypes.AutoTracker(), |
| 10 | + ADTypes.AutoReverseDiff(), |
| 11 | + ADTypes.AutoFiniteDiff() |
| 12 | +] |
| 13 | + |
| 14 | +const ForwardADs = [ |
| 15 | + ADTypes.AutoEnzyme(; mode = EnzymeCore.Forward), |
| 16 | + ADTypes.AutoPolyesterForwardDiff(), |
| 17 | + ADTypes.AutoForwardDiff(), |
| 18 | + ADTypes.AutoFiniteDiff() |
| 19 | +] |
| 20 | + |
| 21 | +# TODO: Handle Sparsity |
| 22 | + |
| 23 | +function select_forward_mode_autodiff( |
| 24 | + prob::AbstractNonlinearProblem, ad::AbstractADType; warn_check_mode::Bool = true) |
| 25 | + if warn_check_mode && !(ADTypes.mode(ad) isa ADTypes.ForwardMode) |
| 26 | + @warn "The chosen AD backend $(ad) is not a forward mode AD. Use with caution." |
| 27 | + end |
| 28 | + if incompatible_backend_and_problem(prob, ad) |
| 29 | + adₙ = select_forward_mode_autodiff(prob, nothing; warn_check_mode) |
| 30 | + @warn "The chosen AD backend `$(ad)` does not support the chosen problem. After \ |
| 31 | + running autodiff selection detected `$(adₙ)` as a potential forward mode \ |
| 32 | + backend." |
| 33 | + return adₙ |
| 34 | + end |
| 35 | + return ad |
| 36 | +end |
| 37 | + |
| 38 | +function select_forward_mode_autodiff(prob::AbstractNonlinearProblem, ::Nothing; |
| 39 | + warn_check_mode::Bool = true) |
| 40 | + idx = findfirst(!Base.Fix1(incompatible_backend_and_problem, prob), ForwardADs) |
| 41 | + idx !== nothing && return ForwardADs[idx] |
| 42 | + throw(ArgumentError("No forward mode AD backend is compatible with the chosen problem. \ |
| 43 | + This could be because no forward mode autodiff backend is loaded \ |
| 44 | + or the loaded backends don't support the problem.")) |
| 45 | +end |
| 46 | + |
| 47 | +function select_reverse_mode_autodiff( |
| 48 | + prob::AbstractNonlinearProblem, ad::AbstractADType; warn_check_mode::Bool = true) |
| 49 | + if warn_check_mode && !(ADTypes.mode(ad) isa ADTypes.ReverseMode) |
| 50 | + if !is_finite_differences_backend(ad) |
| 51 | + @warn "The chosen AD backend $(ad) is not a reverse mode AD. Use with caution." |
| 52 | + else |
| 53 | + @warn "The chosen AD backend $(ad) is a finite differences backend. This might \ |
| 54 | + be slow and inaccurate. Use with caution." |
| 55 | + end |
| 56 | + end |
| 57 | + if incompatible_backend_and_problem(prob, ad) |
| 58 | + adₙ = select_reverse_mode_autodiff(prob, nothing; warn_check_mode) |
| 59 | + @warn "The chosen AD backend `$(ad)` does not support the chosen problem. After \ |
| 60 | + running autodiff selection detected `$(adₙ)` as a potential reverse mode \ |
| 61 | + backend." |
| 62 | + return adₙ |
| 63 | + end |
| 64 | + return ad |
| 65 | +end |
| 66 | + |
| 67 | +function select_reverse_mode_autodiff(prob::AbstractNonlinearProblem, ::Nothing; |
| 68 | + warn_check_mode::Bool = true) |
| 69 | + idx = findfirst(!Base.Fix1(incompatible_backend_and_problem, prob), ReverseADs) |
| 70 | + idx !== nothing && return ReverseADs[idx] |
| 71 | + throw(ArgumentError("No reverse mode AD backend is compatible with the chosen problem. \ |
| 72 | + This could be because no reverse mode autodiff backend is loaded \ |
| 73 | + or the loaded backends don't support the problem.")) |
| 74 | +end |
| 75 | + |
| 76 | +function select_jacobian_autodiff(prob::AbstractNonlinearProblem, ad::AbstractADType) |
| 77 | + if incompatible_backend_and_problem(prob, ad) |
| 78 | + adₙ = select_jacobian_autodiff(prob, nothing) |
| 79 | + @warn "The chosen AD backend `$(ad)` does not support the chosen problem. After \ |
| 80 | + running autodiff selection detected `$(adₙ)` as a potential jacobian \ |
| 81 | + backend." |
| 82 | + return adₙ |
| 83 | + end |
| 84 | + return ad |
| 85 | +end |
| 86 | + |
| 87 | +function select_jacobian_autodiff(prob::AbstractNonlinearProblem, ::Nothing) |
| 88 | + idx = findfirst(!Base.Fix1(incompatible_backend_and_problem, prob), ForwardADs) |
| 89 | + idx !== nothing && !is_finite_differences_backend(ForwardADs[idx]) && |
| 90 | + return ForwardADs[idx] |
| 91 | + idx = findfirst(!Base.Fix1(incompatible_backend_and_problem, prob), ReverseADs) |
| 92 | + idx !== nothing && return ReverseADs[idx] |
| 93 | + throw(ArgumentError("No jacobian AD backend is compatible with the chosen problem. \ |
| 94 | + This could be because no jacobian autodiff backend is loaded \ |
| 95 | + or the loaded backends don't support the problem.")) |
| 96 | +end |
| 97 | + |
| 98 | +function incompatible_backend_and_problem( |
| 99 | + prob::AbstractNonlinearProblem, ad::AbstractADType) |
| 100 | + !DI.check_available(ad) && return true |
| 101 | + SciMLBase.isinplace(prob) && !DI.check_inplace(ad) && return true |
| 102 | + return additional_incompatible_backend_check(prob, ad) |
| 103 | +end |
| 104 | + |
| 105 | +additional_incompatible_backend_check(::AbstractNonlinearProblem, ::AbstractADType) = false |
| 106 | + |
| 107 | +is_finite_differences_backend(ad::AbstractADType) = false |
| 108 | +is_finite_differences_backend(::ADTypes.AutoFiniteDiff) = true |
| 109 | +is_finite_differences_backend(::ADTypes.AutoFiniteDifferences) = true |
0 commit comments