Skip to content

Commit 1f78b26

Browse files
oschulzgdalle
andauthored
Add NoAutoDiff (#126)
* Add NoAutoDiff * Add custom NoAutoDiffAvailable exception * Bump package version * Rename NoAutoDiffAvailable to NoAutoDiffSelected * Rename NoAutoDiffSelected to NoAutoDiffSelectedError * Apply suggestions from code review --------- Co-authored-by: Guillaume Dalle <22795598+gdalle@users.noreply.github.com>
1 parent 3ba3ecd commit 1f78b26

File tree

7 files changed

+54
-2
lines changed

7 files changed

+54
-2
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ADTypes"
22
uuid = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
33
authors = ["Vaibhav Dixit <vaibhavyashdixit@gmail.com>, Guillaume Dalle and contributors"]
4-
version = "1.17.0"
4+
version = "1.18.0"
55

66
[deps]
77
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"

docs/src/index.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,13 @@ ADTypes.symmetric_coloring
9292
ADTypes.NoColoringAlgorithm
9393
```
9494

95+
## No automatic differentiation
96+
97+
```@docs
98+
NoAutoDiff
99+
NoAutoDiffSelectedError
100+
```
101+
95102
## Modes
96103

97104
```@docs

src/ADTypes.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ export AutoChainRules,
4545
AutoTapir,
4646
AutoTaylorDiff,
4747
AutoTracker,
48-
AutoZygote
48+
AutoZygote,
49+
NoAutoDiff,
50+
NoAutoDiffSelectedError
4951
@public AbstractMode
5052
@public ForwardMode, ReverseMode, ForwardOrReverseMode, SymbolicMode
5153
@public mode

src/dense.jl

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,3 +515,35 @@ Defined by [ADTypes.jl](https://github.com/SciML/ADTypes.jl).
515515
struct AutoZygote <: AbstractADType end
516516

517517
mode(::AutoZygote) = ReverseMode()
518+
519+
"""
520+
NoAutoDiff
521+
522+
Struct used to select no automatic differentiation.
523+
524+
Defined by [ADTypes.jl](https://github.com/SciML/ADTypes.jl).
525+
526+
# Constructors
527+
528+
NoAutoDiff()
529+
"""
530+
struct NoAutoDiff <: AbstractADType end
531+
532+
"""
533+
NoAutoDiffSelectedError <: Exception
534+
535+
Signifies that code tried to use automatic differentiation, but [`NoAutoDiff`](@ref) was specified.
536+
537+
# Constructor
538+
539+
NoAutoDiffSelectedError(msg::String)
540+
"""
541+
struct NoAutoDiffSelectedError <: Exception
542+
msg::String
543+
end
544+
545+
NoAutoDiffSelectedError() = NoAutoDiffSelectedError("Automatic differentiation can not be used with NoAutoDiff()")
546+
547+
function mode(::NoAutoDiff)
548+
throw(NoAutoDiffSelectedError())
549+
end

src/symbols.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""
22
ADTypes.Auto(package::Symbol)
3+
ADTypes.Auto(nothing)::NoAutoDiff
34
45
A shortcut that converts an AD package name into an instance of [`AbstractADType`](@ref), with all parameters set to their default values.
56
@@ -27,3 +28,5 @@ for backend in (:ChainRules, :Diffractor, :Enzyme, :FastDifferentiation,
2728
@eval Auto(::Val{$(QuoteNode(backend))}, args...; kws...) = $(Symbol(:Auto, backend))(
2829
args...; kws...)
2930
end
31+
32+
Auto(::Nothing) = NoAutoDiff()

test/dense.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,3 +226,10 @@ end
226226
@test ad isa AutoZygote
227227
@test mode(ad) isa ReverseMode
228228
end
229+
230+
@testset "NoAutoDiff" begin
231+
ad = NoAutoDiff()
232+
@test ad isa AbstractADType
233+
@test ad isa NoAutoDiff
234+
@test_throws NoAutoDiffSelectedError mode(ad)
235+
end

test/symbols.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ using Test
1515
@test ADTypes.Auto(:Tapir) isa AutoTapir
1616
@test ADTypes.Auto(:Tracker) isa AutoTracker
1717
@test ADTypes.Auto(:Zygote) isa AutoZygote
18+
@test ADTypes.Auto(nothing) isa NoAutoDiff
1819

1920
@test_throws MethodError ADTypes.Auto(:ThisPackageDoesNotExist)
2021
@test_throws UndefKeywordError ADTypes.Auto(:ChainRules)

0 commit comments

Comments
 (0)