Skip to content

Commit

Permalink
fix: replace MissingBackendError with error hint (#656)
Browse files Browse the repository at this point in the history
  • Loading branch information
gdalle authored Dec 5, 2024
1 parent 881fc3f commit 3c6e5dc
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 102 deletions.
3 changes: 2 additions & 1 deletion DifferentiationInterface/src/DifferentiationInterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ include("utils/traits.jl")
include("utils/basis.jl")
include("utils/batchsize.jl")
include("utils/check.jl")
include("utils/exceptions.jl")
include("utils/printing.jl")
include("utils/context.jl")
include("utils/linalg.jl")
Expand Down Expand Up @@ -123,4 +122,6 @@ export AutoSparse

@public inner, outer

include("init.jl")

end # module
12 changes: 0 additions & 12 deletions DifferentiationInterface/src/first_order/pullback.jl
Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,6 @@ function _prepare_pullback_aux(
return PushforwardPullbackPrep(pushforward_prep)
end

function _prepare_pullback_aux(
::PullbackFast, f, backend::AbstractADType, x, ty::NTuple, contexts::Vararg{Context}
)
throw(MissingBackendError(backend))
end

function _prepare_pullback_aux(
::PullbackFast, f!, y, backend::AbstractADType, x, ty::NTuple, contexts::Vararg{Context}
)
throw(MissingBackendError(backend))
end

## One argument

function _pullback_via_pushforward(
Expand Down
18 changes: 0 additions & 18 deletions DifferentiationInterface/src/first_order/pushforward.jl
Original file line number Diff line number Diff line change
Expand Up @@ -146,24 +146,6 @@ function _prepare_pushforward_aux(
return PullbackPushforwardPrep(pullback_prep)
end

function _prepare_pushforward_aux(
::PushforwardFast, f, backend::AbstractADType, x, tx::NTuple, contexts::Vararg{Context}
)
throw(MissingBackendError(backend))
end

function _prepare_pushforward_aux(
::PushforwardFast,
f!,
y,
backend::AbstractADType,
x,
tx::NTuple,
contexts::Vararg{Context},
)
throw(MissingBackendError(backend))
end

## One argument

function _pushforward_via_pullback(
Expand Down
13 changes: 13 additions & 0 deletions DifferentiationInterface/src/init.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function __init__()
Base.Experimental.register_error_hint(MethodError) do io, exc, argtypes, kwargs
if exc.f in (_prepare_pushforward_aux, _prepare_pullback_aux)
B = first(T for T in argtypes if T <: AbstractADType)
printstyled(
io,
"\n\nThe autodiff backend package you want to use may not be loaded. Please run the following command and try again:";
bold=true,
)
printstyled(io, "\n\n\timport $(package_name(B))"; color=:cyan, bold=true)
end
end
end
21 changes: 0 additions & 21 deletions DifferentiationInterface/src/utils/exceptions.jl

This file was deleted.

23 changes: 14 additions & 9 deletions DifferentiationInterface/src/utils/printing.jl
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
function package_name(b::AbstractADType)
s = string(b)
package_name(b::AbstractADType) = package_name(typeof(b))

function package_name(::Type{B}) where {B<:AbstractADType}
s = string(B)
s = chopprefix(s, "ADTypes.")
s = chopprefix(s, "Auto")
k = findfirst('(', s)
isnothing(k) && throw(ArgumentError("Cannot parse backend into package"))
return s[begin:(k - 1)]
k = findfirst('{', s)
if isnothing(k)
return s
else
return s[begin:(k - 1)]
end
end

function package_name(b::SecondOrder)
p1 = package_name(outer(b))
p2 = package_name(inner(b))
function package_name(::Type{SecondOrder{O,I}}) where {O,I}
p1 = package_name(O)
p2 = package_name(I)
return p1 == p2 ? p1 : "$p1, $p2"
end

package_name(b::AutoSparse) = package_name(dense_ad(b))
package_name(::Type{<:AutoSparse{D}}) where {D} = package_name(D)

function document_preparation(operator_name::AbstractString; same_point=false)
if same_point
Expand Down
41 changes: 0 additions & 41 deletions DifferentiationInterface/test/Core/Internals/exceptions.jl

This file was deleted.

0 comments on commit 3c6e5dc

Please sign in to comment.