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

Throw informative errors when backends are not loaded #214

Merged
merged 4 commits into from
Apr 25, 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
1 change: 1 addition & 0 deletions DifferentiationInterface/src/DifferentiationInterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ include("utils/basis.jl")
include("utils/printing.jl")
include("utils/chunk.jl")
include("utils/check.jl")
include("utils/exceptions.jl")

include("first_order/pushforward.jl")
include("first_order/pullback.jl")
Expand Down
4 changes: 4 additions & 0 deletions DifferentiationInterface/src/first_order/pullback.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ function prepare_pullback_aux(f!, y, backend, x, dy, ::PullbackSlow)
return PushforwardPullbackExtras(pushforward_extras)
end

# Throw error if backend is missing
prepare_pullback_aux(f, backend, x, dy, ::PullbackFast) = throw(MissingBackendError(backend))
prepare_pullback_aux(f!, y, backend, x, dy, ::PullbackFast) = throw(MissingBackendError(backend))

## One argument

function value_and_pullback(
Expand Down
4 changes: 4 additions & 0 deletions DifferentiationInterface/src/first_order/pushforward.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ function prepare_pushforward_aux(f!, y, backend, x, dx, ::PushforwardSlow)
return PullbackPushforwardExtras(pullback_extras)
end

# Throw error if backend is missing
prepare_pushforward_aux(f, backend, x, dy, ::PushforwardFast) = throw(MissingBackendError(backend))
prepare_pushforward_aux(f!, y, backend, x, dy, ::PushforwardFast) = throw(MissingBackendError(backend))

## One argument

function value_and_pushforward(
Expand Down
20 changes: 20 additions & 0 deletions DifferentiationInterface/src/utils/exceptions.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
struct MissingBackendError <: Exception
backend::AbstractADType
end
function Base.showerror(io::IO, e::MissingBackendError)
println(io, "failed to use $(backend_string(e.backend)) backend.")
if !check_available(e.backend)
print(
io,
"""Backend package is not loaded. To fix, run

using $(backend_package_name(e.backend))
""",
)
else
print(
io,
"Please open an issue: https://github.com/gdalle/DifferentiationInterface.jl/issues/new",
)
end
end
29 changes: 16 additions & 13 deletions DifferentiationInterface/src/utils/printing.jl
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
backend_string_aux(b::AbstractADType) = string(b)
backend_package_name(b::AbstractADType) = strip(string(b), ['(', ')'])

backend_string_aux(::AutoChainRules) = "ChainRules"
backend_string_aux(::AutoDiffractor) = "Diffractor"
backend_string_aux(::AutoEnzyme) = "Enzyme"
backend_string_aux(::AutoFastDifferentiation) = "FastDifferentiation"
backend_string_aux(::AutoFiniteDiff) = "FiniteDiff"
backend_string_aux(::AutoFiniteDifferences) = "FiniteDifferences"
backend_string_aux(::AutoForwardDiff) = "ForwardDiff"
backend_string_aux(::AutoPolyesterForwardDiff) = "PolyesterForwardDiff"
backend_package_name(::AutoChainRules) = "ChainRules"
backend_package_name(::AutoDiffractor) = "Diffractor"
backend_package_name(::AutoEnzyme) = "Enzyme"
backend_package_name(::AutoFastDifferentiation) = "FastDifferentiation"
backend_package_name(::AutoFiniteDiff) = "FiniteDiff"
backend_package_name(::AutoFiniteDifferences) = "FiniteDifferences"
backend_package_name(::AutoForwardDiff) = "ForwardDiff"
backend_package_name(::AutoPolyesterForwardDiff) = "PolyesterForwardDiff"
backend_package_name(::AutoSymbolics) = "Symbolics"
backend_package_name(::AutoTapir) = "Tapir"
backend_package_name(::AutoTracker) = "Tracker"
backend_package_name(::AutoZygote) = "Zygote"
backend_package_name(::AutoReverseDiff) = "ReverseDiff"

backend_string_aux(b::AbstractADType) = backend_package_name(b)
backend_string_aux(b::AutoReverseDiff) = "ReverseDiff$(b.compile ? "{compiled}" : "")"
backend_string_aux(::AutoSymbolics) = "Symbolics"
backend_string_aux(::AutoTapir) = "Tapir"
backend_string_aux(::AutoTracker) = "Tracker"
backend_string_aux(::AutoZygote) = "Zygote"

function backend_string(backend::AbstractADType)
bs = backend_string_aux(backend)
Expand Down
3 changes: 3 additions & 0 deletions DifferentiationInterface/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ include("test_imports.jl")

Documenter.doctest(DifferentiationInterface)

@testset verbose = true "Exception handling" begin
include("test_exceptions.jl")
end
@testset verbose = true "First order" begin
include("first_order.jl")
end
Expand Down
33 changes: 33 additions & 0 deletions DifferentiationInterface/test/test_exceptions.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using DifferentiationInterface: MissingBackendError

"""
AutoBrokenForward <: ADTypes.AbstractADType

Available forward-mode backend with no pushforward implementation.
Used to test error messages.
"""
struct AutoBrokenForward <: AbstractADType end
ADTypes.mode(::AutoBrokenForward) = ADTypes.ForwardMode()
DifferentiationInterface.check_available(::AutoBrokenForward) = true

"""
AutoBrokenReverse <: ADTypes.AbstractADType

Available reverse-mode backend with no pullback implementation.
Used to test error messages.
"""
struct AutoBrokenReverse <: AbstractADType end
ADTypes.mode(::AutoBrokenReverse) = ADTypes.ReverseMode()
DifferentiationInterface.check_available(::AutoBrokenReverse) = true

## Test exceptions
@testset "MissingBackendError" begin
f(x::AbstractArray) = sum(abs2, x)
x = [1.0, 2.0, 3.0]

@test_throws MissingBackendError gradient(f, AutoBrokenForward(), x)
@test_throws MissingBackendError gradient(f, AutoBrokenReverse(), x)

@test_throws MissingBackendError hvp(f, AutoBrokenForward(), x, x)
@test_throws MissingBackendError hvp(f, AutoBrokenReverse(), x, x)
end
Loading