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 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
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 <vaibhavyashdixit@gmail.com>, Guillaume Dalle and contributors",
]
version = "1.3.0"
version = "1.4.0"

[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
29 changes: 29 additions & 0 deletions src/symbols.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""
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, args...; kws...) = Auto(Val(package), args...; kws...)

for backend in (:ChainRules, :Diffractor, :Enzyme, :FastDifferentiation,
:FiniteDiff, :FiniteDifferences, :ForwardDiff, :PolyesterForwardDiff,
:ReverseDiff, :Symbolics, :Tapir, :Tracker, :Zygote)
@eval Auto(::Val{$(QuoteNode(backend))}, args...; kws...) = $(Symbol(:Auto, backend))(args...; kws...)
end

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
20 changes: 20 additions & 0 deletions test/symbols.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using ADTypes
using Test

@test ADTypes.Auto(:ChainRules, 1) isa AutoChainRules{Int64}
@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(:FiniteDifferences, 1.0) isa AutoFiniteDifferences{Float64}
@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 MethodError ADTypes.Auto(:ThisPackageDoesNotExist)
@test_throws UndefKeywordError ADTypes.Auto(:ChainRules)
@test_throws UndefKeywordError ADTypes.Auto(:FiniteDifferences)
Loading