Skip to content

Commit

Permalink
Add Symbol -> AbstractADType mapping (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
gdalle committed Jun 17, 2024
1 parent 602ce68 commit 72f806d
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 1 deletion.
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)

2 comments on commit 72f806d

@gdalle
Copy link
Collaborator Author

@gdalle gdalle commented on 72f806d Jun 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/109228

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.4.0 -m "<description of version>" 72f806da70689bfa459264faba838ba80de860d7
git push origin v1.4.0

Please sign in to comment.