Skip to content

Commit 3c6e5dc

Browse files
authored
fix: replace MissingBackendError with error hint (#656)
1 parent 881fc3f commit 3c6e5dc

7 files changed

Lines changed: 29 additions & 102 deletions

File tree

DifferentiationInterface/src/DifferentiationInterface.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ include("utils/traits.jl")
4141
include("utils/basis.jl")
4242
include("utils/batchsize.jl")
4343
include("utils/check.jl")
44-
include("utils/exceptions.jl")
4544
include("utils/printing.jl")
4645
include("utils/context.jl")
4746
include("utils/linalg.jl")
@@ -123,4 +122,6 @@ export AutoSparse
123122

124123
@public inner, outer
125124

125+
include("init.jl")
126+
126127
end # module

DifferentiationInterface/src/first_order/pullback.jl

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -145,18 +145,6 @@ function _prepare_pullback_aux(
145145
return PushforwardPullbackPrep(pushforward_prep)
146146
end
147147

148-
function _prepare_pullback_aux(
149-
::PullbackFast, f, backend::AbstractADType, x, ty::NTuple, contexts::Vararg{Context}
150-
)
151-
throw(MissingBackendError(backend))
152-
end
153-
154-
function _prepare_pullback_aux(
155-
::PullbackFast, f!, y, backend::AbstractADType, x, ty::NTuple, contexts::Vararg{Context}
156-
)
157-
throw(MissingBackendError(backend))
158-
end
159-
160148
## One argument
161149

162150
function _pullback_via_pushforward(

DifferentiationInterface/src/first_order/pushforward.jl

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -146,24 +146,6 @@ function _prepare_pushforward_aux(
146146
return PullbackPushforwardPrep(pullback_prep)
147147
end
148148

149-
function _prepare_pushforward_aux(
150-
::PushforwardFast, f, backend::AbstractADType, x, tx::NTuple, contexts::Vararg{Context}
151-
)
152-
throw(MissingBackendError(backend))
153-
end
154-
155-
function _prepare_pushforward_aux(
156-
::PushforwardFast,
157-
f!,
158-
y,
159-
backend::AbstractADType,
160-
x,
161-
tx::NTuple,
162-
contexts::Vararg{Context},
163-
)
164-
throw(MissingBackendError(backend))
165-
end
166-
167149
## One argument
168150

169151
function _pushforward_via_pullback(
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function __init__()
2+
Base.Experimental.register_error_hint(MethodError) do io, exc, argtypes, kwargs
3+
if exc.f in (_prepare_pushforward_aux, _prepare_pullback_aux)
4+
B = first(T for T in argtypes if T <: AbstractADType)
5+
printstyled(
6+
io,
7+
"\n\nThe autodiff backend package you want to use may not be loaded. Please run the following command and try again:";
8+
bold=true,
9+
)
10+
printstyled(io, "\n\n\timport $(package_name(B))"; color=:cyan, bold=true)
11+
end
12+
end
13+
end

DifferentiationInterface/src/utils/exceptions.jl

Lines changed: 0 additions & 21 deletions
This file was deleted.

DifferentiationInterface/src/utils/printing.jl

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
1-
function package_name(b::AbstractADType)
2-
s = string(b)
1+
package_name(b::AbstractADType) = package_name(typeof(b))
2+
3+
function package_name(::Type{B}) where {B<:AbstractADType}
4+
s = string(B)
35
s = chopprefix(s, "ADTypes.")
46
s = chopprefix(s, "Auto")
5-
k = findfirst('(', s)
6-
isnothing(k) && throw(ArgumentError("Cannot parse backend into package"))
7-
return s[begin:(k - 1)]
7+
k = findfirst('{', s)
8+
if isnothing(k)
9+
return s
10+
else
11+
return s[begin:(k - 1)]
12+
end
813
end
914

10-
function package_name(b::SecondOrder)
11-
p1 = package_name(outer(b))
12-
p2 = package_name(inner(b))
15+
function package_name(::Type{SecondOrder{O,I}}) where {O,I}
16+
p1 = package_name(O)
17+
p2 = package_name(I)
1318
return p1 == p2 ? p1 : "$p1, $p2"
1419
end
1520

16-
package_name(b::AutoSparse) = package_name(dense_ad(b))
21+
package_name(::Type{<:AutoSparse{D}}) where {D} = package_name(D)
1722

1823
function document_preparation(operator_name::AbstractString; same_point=false)
1924
if same_point

DifferentiationInterface/test/Core/Internals/exceptions.jl

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)