diff --git a/Project.toml b/Project.toml index 240038e..6860184 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ADTypes" uuid = "47edcb42-4c32-4615-8424-f2b9edc5f35b" authors = ["Vaibhav Dixit , Guillaume Dalle and contributors"] -version = "1.17.0" +version = "1.18.0" [deps] ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" diff --git a/docs/src/index.md b/docs/src/index.md index eda25fa..7ae4eed 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -92,6 +92,13 @@ ADTypes.symmetric_coloring ADTypes.NoColoringAlgorithm ``` +## No automatic differentiation + +```@docs +NoAutoDiff +NoAutoDiffSelectedError +``` + ## Modes ```@docs diff --git a/src/ADTypes.jl b/src/ADTypes.jl index bbc31c3..379b02f 100644 --- a/src/ADTypes.jl +++ b/src/ADTypes.jl @@ -45,7 +45,9 @@ export AutoChainRules, AutoTapir, AutoTaylorDiff, AutoTracker, - AutoZygote + AutoZygote, + NoAutoDiff, + NoAutoDiffSelectedError @public AbstractMode @public ForwardMode, ReverseMode, ForwardOrReverseMode, SymbolicMode @public mode diff --git a/src/dense.jl b/src/dense.jl index 38072e6..0a3534d 100644 --- a/src/dense.jl +++ b/src/dense.jl @@ -515,3 +515,35 @@ Defined by [ADTypes.jl](https://github.com/SciML/ADTypes.jl). struct AutoZygote <: AbstractADType end mode(::AutoZygote) = ReverseMode() + +""" + NoAutoDiff + +Struct used to select no automatic differentiation. + +Defined by [ADTypes.jl](https://github.com/SciML/ADTypes.jl). + +# Constructors + + NoAutoDiff() +""" +struct NoAutoDiff <: AbstractADType end + +""" + NoAutoDiffSelectedError <: Exception + +Signifies that code tried to use automatic differentiation, but [`NoAutoDiff`](@ref) was specified. + +# Constructor + + NoAutoDiffSelectedError(msg::String) +""" +struct NoAutoDiffSelectedError <: Exception + msg::String +end + +NoAutoDiffSelectedError() = NoAutoDiffSelectedError("Automatic differentiation can not be used with NoAutoDiff()") + +function mode(::NoAutoDiff) + throw(NoAutoDiffSelectedError()) +end diff --git a/src/symbols.jl b/src/symbols.jl index a7c0f5e..58199f3 100644 --- a/src/symbols.jl +++ b/src/symbols.jl @@ -1,5 +1,6 @@ """ ADTypes.Auto(package::Symbol) + ADTypes.Auto(nothing)::NoAutoDiff A shortcut that converts an AD package name into an instance of [`AbstractADType`](@ref), with all parameters set to their default values. @@ -27,3 +28,5 @@ for backend in (:ChainRules, :Diffractor, :Enzyme, :FastDifferentiation, @eval Auto(::Val{$(QuoteNode(backend))}, args...; kws...) = $(Symbol(:Auto, backend))( args...; kws...) end + +Auto(::Nothing) = NoAutoDiff() diff --git a/test/dense.jl b/test/dense.jl index 307403d..28f3aa4 100644 --- a/test/dense.jl +++ b/test/dense.jl @@ -226,3 +226,10 @@ end @test ad isa AutoZygote @test mode(ad) isa ReverseMode end + +@testset "NoAutoDiff" begin + ad = NoAutoDiff() + @test ad isa AbstractADType + @test ad isa NoAutoDiff + @test_throws NoAutoDiffSelectedError mode(ad) +end diff --git a/test/symbols.jl b/test/symbols.jl index 7c6ca9f..3ebf0e2 100644 --- a/test/symbols.jl +++ b/test/symbols.jl @@ -15,6 +15,7 @@ using Test @test ADTypes.Auto(:Tapir) isa AutoTapir @test ADTypes.Auto(:Tracker) isa AutoTracker @test ADTypes.Auto(:Zygote) isa AutoZygote +@test ADTypes.Auto(nothing) isa NoAutoDiff @test_throws MethodError ADTypes.Auto(:ThisPackageDoesNotExist) @test_throws UndefKeywordError ADTypes.Auto(:ChainRules)