Skip to content

Commit 838e444

Browse files
authored
Merge pull request #136 from JuliaDiff/ox/debugmode
Add Debug Mode
2 parents a4e078a + c6557aa commit 838e444

File tree

8 files changed

+53
-8
lines changed

8 files changed

+53
-8
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ChainRulesCore"
22
uuid = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
3-
version = "0.7.1"
3+
version = "0.7.2"
44

55
[deps]
66
MuladdMacro = "46d2c3a1-f734-5fdb-9937-b9b9aeba4221"

docs/make.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ DocMeta.setdocmeta!(
1111
Random.seed!(0) # frule doctest shows output
1212

1313
using ChainRulesCore
14-
# These rules are all actually defined in ChainRules.jl, but we redefine them here to
14+
# These rules are all actually defined in ChainRules.jl, but we redefine them here to
1515
# avoid the dependency.
1616
@scalar_rule(sin(x), cos(x)) # frule and rrule doctest
1717
@scalar_rule(sincos(x), @setup((sinx, cosx) = Ω), cosx, -sinx) # frule doctest
@@ -28,6 +28,7 @@ makedocs(
2828
"Introduction" => "index.md",
2929
"FAQ" => "FAQ.md",
3030
"Writing Good Rules" => "writing_good_rules.md",
31+
"Debug Mode" => "debug_mode.md",
3132
"API" => "api.md",
3233
],
3334
strict=true,

docs/src/api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ Private = false
3030
## Internal
3131
```@docs
3232
ChainRulesCore.AbstractDifferential
33+
ChainRulesCore.debug_mode
3334
```

docs/src/debug_mode.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Debug Mode
2+
3+
ChainRulesCore supports a *debug mode* which you can use while writing new rules.
4+
It provides better error messages.
5+
If you are developing some new rules, and you get a weird error message,
6+
it is worth enabling debug mode.
7+
8+
There is some overhead to having it enabled, so it is disabled by default.
9+
10+
To enable, redefine the [`ChainRulesCore.debug_mode`](@ref) function to return `true`.
11+
```julia
12+
ChainRulesCore.debug_mode() = true
13+
```

src/ChainRulesCore.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export Composite, DoesNotExist, InplaceableThunk, One, Thunk, Zero, AbstractZero
88
export NO_FIELDS
99

1010
include("compat.jl")
11+
include("debug_mode.jl")
1112

1213
include("differentials/abstract_differential.jl")
1314
include("differentials/abstract_zero.jl")

src/debug_mode.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"""
2+
debug_mode() -> Bool
3+
4+
Determines if ChainRulesCore is in `debug_mode`.
5+
Defaults to `false`, but if the user redefines it to return `true` then extra
6+
information will be shown when errors occur.
7+
8+
Enable via:
9+
```
10+
ChainRulesCore.debug_mode() = true
11+
```
12+
"""
13+
debug_mode() = false

src/differential_arithmetic.jl

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,15 @@ function Base.:+(a::Composite{P}, b::Composite{P}) where P
8686
return Composite{P, typeof(data)}(data)
8787
end
8888
function Base.:+(a::P, d::Composite{P}) where P
89-
try
90-
return construct(P, elementwise_add(backing(a), backing(d)))
91-
catch err
92-
throw(PrimalAdditionFailedException(a, d, err))
89+
net_backing = elementwise_add(backing(a), backing(d))
90+
if debug_mode()
91+
try
92+
return construct(P, net_backing)
93+
catch err
94+
throw(PrimalAdditionFailedException(a, d, err))
95+
end
96+
else
97+
return construct(P, net_backing)
9398
end
9499
end
95100
Base.:+(a::Composite{P}, b::P) where P = b + a

test/differentials/composite.jl

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,19 @@ end
128128
@testset "+ with Primals, with inner constructor" begin
129129
value = StructWithInvariant(10.0)
130130
diff = Composite{StructWithInvariant}(x=2.0, x2=6.0)
131-
@test_throws ChainRulesCore.PrimalAdditionFailedException (value + diff)
132-
@test_throws ChainRulesCore.PrimalAdditionFailedException (diff + value)
131+
132+
@testset "with and without debug mode" begin
133+
@assert ChainRulesCore.debug_mode() == false
134+
@test_throws MethodError (value + diff)
135+
@test_throws MethodError (diff + value)
136+
137+
ChainRulesCore.debug_mode() = true # enable debug mode
138+
@test_throws ChainRulesCore.PrimalAdditionFailedException (value + diff)
139+
@test_throws ChainRulesCore.PrimalAdditionFailedException (diff + value)
140+
ChainRulesCore.debug_mode() = false # disable it again
141+
end
142+
143+
133144

134145
# Now we define constuction for ChainRulesCore.jl's purposes:
135146
# It is going to determine the root quanity of the invarient

0 commit comments

Comments
 (0)