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

Add Symbol -> AbstractADType mapping #62

Merged
merged 4 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ uuid = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
authors = [
"Vaibhav Dixit <[email protected]>, Guillaume Dalle and contributors",
]
version = "1.3.0"
version = "1.3.1"
gdalle marked this conversation as resolved.
Show resolved Hide resolved

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand Down
6 changes: 6 additions & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,9 @@ ADTypes.ForwardOrReverseMode
ADTypes.ReverseMode
ADTypes.SymbolicMode
```

## Miscellaneous

```@docs
ADTypes.Auto
```
1 change: 1 addition & 0 deletions src/ADTypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ include("mode.jl")
include("dense.jl")
include("sparse.jl")
include("legacy.jl")
include("symbols.jl")

if !isdefined(Base, :get_extension)
include("../ext/ADTypesChainRulesCoreExt.jl")
Expand Down
42 changes: 42 additions & 0 deletions src/symbols.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""
ADTypes.Auto(package::Symbol)

A shortcut that converts an AD package name into an instance of [`AbstractADType`](@ref), with all parameters set to their default values.

!!! warning

This function is type-unstable by design and might lead to suboptimal performance.
In most cases, you should never need it: use the individual backend types directly.

# Example

```jldoctest
import ADTypes
backend = ADTypes.Auto(:Zygote)

# output

ADTypes.AutoZygote()
```
"""
Auto(package::Symbol) = Auto(Val(package))
gdalle marked this conversation as resolved.
Show resolved Hide resolved

Auto(::Val{:Diffractor}) = AutoDiffractor()
Auto(::Val{:Enzyme}) = AutoEnzyme()
Auto(::Val{:FastDifferentiation}) = AutoFastDifferentiation()
Auto(::Val{:FiniteDiff}) = AutoFiniteDiff()
Auto(::Val{:ForwardDiff}) = AutoForwardDiff()
Auto(::Val{:PolyesterForwardDiff}) = AutoPolyesterForwardDiff()
Auto(::Val{:ReverseDiff}) = AutoReverseDiff()
Auto(::Val{:Symbolics}) = AutoSymbolics()
Auto(::Val{:Tapir}) = AutoTapir()
Auto(::Val{:Tracker}) = AutoTracker()
Auto(::Val{:Zygote}) = AutoZygote()
gdalle marked this conversation as resolved.
Show resolved Hide resolved

function Auto(::Val{:ChainRules})
throw(ArgumentError("ChainRules backend has mandatory arguments"))
end

function Auto(::Val{:FiniteDifferences})
throw(ArgumentError("FiniteDifferences backend has mandatory arguments"))
end
gdalle marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 3 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ end
@testset "Sparse" begin
include("sparse.jl")
end
@testset "Symbols" begin
include("symbols.jl")
end
@testset "Legacy" begin
include("legacy.jl")
end
Expand Down
17 changes: 17 additions & 0 deletions test/symbols.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using ADTypes
using Test

@test ADTypes.Auto(:Diffractor) isa AutoDiffractor
@test ADTypes.Auto(:Enzyme) isa AutoEnzyme
@test ADTypes.Auto(:FastDifferentiation) isa AutoFastDifferentiation
@test ADTypes.Auto(:FiniteDiff) isa AutoFiniteDiff
@test ADTypes.Auto(:ForwardDiff) isa AutoForwardDiff
@test ADTypes.Auto(:PolyesterForwardDiff) isa AutoPolyesterForwardDiff
@test ADTypes.Auto(:ReverseDiff) isa AutoReverseDiff
@test ADTypes.Auto(:Symbolics) isa AutoSymbolics
@test ADTypes.Auto(:Tapir) isa AutoTapir
@test ADTypes.Auto(:Tracker) isa AutoTracker
@test ADTypes.Auto(:Zygote) isa AutoZygote

@test_throws ArgumentError ADTypes.Auto(:ChainRules)
@test_throws ArgumentError ADTypes.Auto(:FiniteDifferences)
gdalle marked this conversation as resolved.
Show resolved Hide resolved
Loading