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

fix: replace MissingBackendError with error hint #656

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))

Check warning on line 1 in DifferentiationInterface/src/utils/printing.jl

View check run for this annotation

Codecov / codecov/patch

DifferentiationInterface/src/utils/printing.jl#L1

Added line #L1 was not covered by tests

function package_name(::Type{B}) where {B<:AbstractADType}
s = string(B)

Check warning on line 4 in DifferentiationInterface/src/utils/printing.jl

View check run for this annotation

Codecov / codecov/patch

DifferentiationInterface/src/utils/printing.jl#L3-L4

Added lines #L3 - L4 were not covered by tests
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

Check warning on line 9 in DifferentiationInterface/src/utils/printing.jl

View check run for this annotation

Codecov / codecov/patch

DifferentiationInterface/src/utils/printing.jl#L7-L9

Added lines #L7 - L9 were not covered by tests
else
return s[begin:(k - 1)]

Check warning on line 11 in DifferentiationInterface/src/utils/printing.jl

View check run for this annotation

Codecov / codecov/patch

DifferentiationInterface/src/utils/printing.jl#L11

Added line #L11 was not covered by tests
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)

Check warning on line 17 in DifferentiationInterface/src/utils/printing.jl

View check run for this annotation

Codecov / codecov/patch

DifferentiationInterface/src/utils/printing.jl#L15-L17

Added lines #L15 - L17 were not covered by tests
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)

Check warning on line 21 in DifferentiationInterface/src/utils/printing.jl

View check run for this annotation

Codecov / codecov/patch

DifferentiationInterface/src/utils/printing.jl#L21

Added line #L21 was not covered by tests

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.

Loading