Skip to content

Commit 0597f61

Browse files
committed
Update fenced code block examples
1 parent 5116538 commit 0597f61

File tree

12 files changed

+84
-67
lines changed

12 files changed

+84
-67
lines changed

docs/src/FAQ.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ For example, in `access(xs, n) = xs[n]`, the derivative of `access` with respect
6363
When no custom `frule` or `rrule` exists, if you try to call one of those, it will return `nothing` by default.
6464
As a result, you may encounter errors like
6565

66-
```julia
66+
```plain
6767
MethodError: no method matching iterate(::Nothing)
6868
```
6969

docs/src/ad_author/opt_out.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ We provide two ways to know that a rule has been opted out of.
77
`@opt_out` defines a `frule` or `rrule` matching the signature that returns `nothing`.
88

99
If you are in a position to generate code, in response to values returned by function calls then you can do something like:
10-
```@julia
10+
```julia
1111
res = rrule(f, xs)
1212
if res === nothing
1313
y, pullback = perform_ad_via_decomposition(r, xs) # do AD without hitting the rrule

docs/src/design/changing_the_primal.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ What about using `sincos`?
6363
```@raw html
6464
<details open><summary>Example for `sin`</summary>
6565
```
66-
```julia
66+
```julia-repl
6767
julia> using BenchmarkTools
6868
6969
julia> @btime sin(x) setup=(x=rand());
@@ -76,7 +76,7 @@ julia> 3.838 + 4.795
7676
8.633
7777
```
7878
vs computing both together:
79-
```julia
79+
```julia-repl
8080
julia> @btime sincos(x) setup=(x=rand());
8181
6.028 ns (0 allocations: 0 bytes)
8282
```
@@ -96,7 +96,7 @@ So we can save time, if we can reuse that `exp(x)`.
9696
<details open><summary>Example for the logistic sigmoid</summary>
9797
```
9898
If we have to computing separately:
99-
```julia
99+
```julia-repl
100100
julia> @btime 1/(1+exp(x)) setup=(x=rand());
101101
5.622 ns (0 allocations: 0 bytes)
102102
@@ -108,7 +108,7 @@ julia> 5.622 + 6.036
108108
```
109109

110110
vs reusing `exp(x)`:
111-
```julia
111+
```julia-repl
112112
julia> @btime exp(x) setup=(x=rand());
113113
5.367 ns (0 allocations: 0 bytes)
114114

docs/src/design/many_tangents.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Structural tangents are derived from the structure of the input.
4545
Either automatically, as part of the AD, or manually, as part of a custom rule.
4646

4747
Consider the structure of `DateTime`:
48-
```julia
48+
```julia-repl
4949
julia> dump(now())
5050
DateTime
5151
instant: UTInstant{Millisecond}
@@ -83,15 +83,15 @@ Where there is no natural tangent type for the outermost type but there is for s
8383

8484
Consider if we had a representation of a country's GDP as output by some continuous time model like a Gaussian Process, where that representation is as a sequence of `TimeSample`s
8585
structured as follows:
86-
```julia
86+
```julia-repl
8787
julia> struct TimeSample
8888
time::DateTime
8989
value::Float64
9090
end
9191
```
9292

9393
We can look at its structure:
94-
```julia
94+
```julia-repl
9595
julia> dump(TimeSample(now(), 2.6e9))
9696
TimeSample
9797
time: DateTime

docs/src/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ end
191191
# output
192192
193193
```
194+
194195
```jldoctest index
195196
#### Find dfoo/dx via rrules
196197
#### First the forward pass, gathering up the pullbacks

docs/src/rule_author/example.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ end
3838
```
3939

4040
We can check this rule against a finite-differences approach using [`ChainRulesTestUtils`](https://github.com/JuliaDiff/ChainRulesTestUtils.jl):
41-
```julia
41+
```julia-repl
4242
julia> using ChainRulesTestUtils
43+
4344
julia> test_rrule(foo_mul, Foo(rand(3, 3), 3.0), rand(3, 3))
4445
Test Summary: | Pass Total
4546
test_rrule: foo_mul on Foo{Float64},Matrix{Float64} | 10 10

docs/src/rule_author/which_functions_need_rules.md

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ function addone(a::AbstractArray)
3434
end
3535
```
3636
complains that
37-
```julia
37+
```julia-repl
3838
julia> using Zygote
39+
3940
julia> gradient(addone, a)
4041
ERROR: Mutating arrays is not supported
4142
```
@@ -50,7 +51,7 @@ function ChainRules.rrule(::typeof(addone), a)
5051
end
5152
```
5253
the gradient can be evaluated:
53-
```julia
54+
```julia-repl
5455
julia> gradient(addone, a)
5556
([1.0, 1.0, 1.0],)
5657
```
@@ -86,7 +87,7 @@ function exception(x)
8687
end
8788
```
8889
does not work
89-
```julia
90+
```julia-repl
9091
julia> gradient(exception, 3.0)
9192
ERROR: Compiling Tuple{typeof(exception),Int64}: try/catch is not supported.
9293
```
@@ -101,7 +102,7 @@ function ChainRulesCore.rrule(::typeof(exception), x)
101102
end
102103
```
103104

104-
```julia
105+
```julia-repl
105106
julia> gradient(exception, 3.0)
106107
(6.0,)
107108
```
@@ -123,9 +124,11 @@ function mse(y, ŷ)
123124
end
124125
```
125126
takes a lot longer to AD through
126-
```julia
127-
julia> y = rand(30)
128-
julia> ŷ = rand(30)
127+
```julia-repl
128+
julia> y = rand(30);
129+
130+
julia> ŷ = rand(30);
131+
129132
julia> @btime gradient(mse, $y, $ŷ)
130133
38.180 μs (993 allocations: 65.00 KiB)
131134
```
@@ -142,7 +145,7 @@ function ChainRules.rrule(::typeof(mse), x, x̂)
142145
end
143146
```
144147
which is much faster
145-
```julia
148+
```julia-repl
146149
julia> @btime gradient(mse, $y, $ŷ)
147150
143.697 ns (2 allocations: 672 bytes)
148151
```
@@ -159,7 +162,7 @@ function sum3(array)
159162
return x+y+z
160163
end
161164
```
162-
```julia
165+
```julia-repl
163166
julia> @btime gradient(sum3, rand(30))
164167
424.510 ns (9 allocations: 2.06 KiB)
165168
```
@@ -176,7 +179,7 @@ function ChainRulesCore.rrule(::typeof(sum3), a)
176179
end
177180
```
178181
turns out to be significantly faster
179-
```julia
182+
```julia-repl
180183
julia> @btime gradient(sum3, rand(30))
181184
192.818 ns (3 allocations: 784 bytes)
182185
```

docs/src/rule_author/writing_good_rules.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ Because `typeof(Bar)` is `DataType`, using this to define an `rrule`/`frule` wil
110110

111111
You can check which to use with `Core.Typeof`:
112112

113-
```julia
113+
```julia-repl
114114
julia> function foo end
115115
foo (generic function with 0 methods)
116116
@@ -254,7 +254,7 @@ function ChainRulesCore.rrule(::typeof(double_it), x)
254254
end
255255
```
256256
Ends up infering a return type of `Any`
257-
```julia
257+
```julia-repl
258258
julia> _, pullback = rrule(double_it, [2.0, 3.0])
259259
([4.0, 6.0], var"#double_it_pullback#8"(Core.Box(var"#double_it_pullback#8"(#= circular reference @-2 =#))))
260260
@@ -289,7 +289,7 @@ function ChainRulesCore.rrule(::typeof(double_it), x)
289289
end
290290
```
291291
This infers just fine:
292-
```julia
292+
```julia-repl
293293
julia> _, pullback = rrule(double_it, [2.0, 3.0])
294294
([4.0, 6.0], _double_it_pullback)
295295

src/debug_mode.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Defaults to `false`, but if the user redefines it to return `true` then extra
66
information will be shown when errors occur.
77
88
Enable via:
9-
```
9+
```julia
1010
ChainRulesCore.debug_mode() = true
1111
```
1212
"""

src/rule_definition_tools.jl

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,28 @@ A convenience macro that generates simple scalar forward or reverse rules using
1919
the provided partial derivatives. Specifically, generates the corresponding
2020
methods for `frule` and `rrule`:
2121
22-
function ChainRulesCore.frule((NoTangent(), Δx₁, Δx₂, ...), ::typeof(f), x₁::Number, x₂::Number, ...)
23-
Ω = f(x₁, x₂, ...)
24-
\$(statement₁, statement₂, ...)
25-
return Ω, (
26-
(∂f₁_∂x₁ * Δx₁ + ∂f₁_∂x₂ * Δx₂ + ...),
27-
(∂f₂_∂x₁ * Δx₁ + ∂f₂_∂x₂ * Δx₂ + ...),
28-
...
29-
)
30-
end
22+
```julia
23+
function ChainRulesCore.frule((NoTangent(), Δx₁, Δx₂, ...), ::typeof(f), x₁::Number, x₂::Number, ...)
24+
Ω = f(x₁, x₂, ...)
25+
\$(statement₁, statement₂, ...)
26+
return Ω, (
27+
(∂f₁_∂x₁ * Δx₁ + ∂f₁_∂x₂ * Δx₂ + ...),
28+
(∂f₂_∂x₁ * Δx₁ + ∂f₂_∂x₂ * Δx₂ + ...),
29+
...
30+
)
31+
end
3132
32-
function ChainRulesCore.rrule(::typeof(f), x₁::Number, x₂::Number, ...)
33-
Ω = f(x₁, x₂, ...)
34-
\$(statement₁, statement₂, ...)
35-
return Ω, ((ΔΩ₁, ΔΩ₂, ...)) -> (
36-
NoTangent(),
37-
∂f₁_∂x₁ * ΔΩ₁ + ∂f₂_∂x₁ * ΔΩ₂ + ...),
38-
∂f₁_∂x₂ * ΔΩ₁ + ∂f₂_∂x₂ * ΔΩ₂ + ...),
39-
...
40-
)
41-
end
33+
function ChainRulesCore.rrule(::typeof(f), x₁::Number, x₂::Number, ...)
34+
Ω = f(x₁, x₂, ...)
35+
\$(statement₁, statement₂, ...)
36+
return Ω, ((ΔΩ₁, ΔΩ₂, ...)) -> (
37+
NoTangent(),
38+
∂f₁_∂x₁ * ΔΩ₁ + ∂f₂_∂x₁ * ΔΩ₂ + ...),
39+
∂f₁_∂x₂ * ΔΩ₁ + ∂f₂_∂x₂ * ΔΩ₂ + ...),
40+
...
41+
)
42+
end
43+
```
4244
4345
If no type constraints in `f(x₁, x₂, ...)` within the call to `@scalar_rule` are
4446
provided, each parameter in the resulting `frule`/`rrule` definition is given a
@@ -65,18 +67,22 @@ general multiplicative identity.
6567
The `@setup` argument can be elided if no setup code is need. In other
6668
words:
6769
68-
@scalar_rule(f(x₁, x₂, ...),
69-
(∂f₁_∂x₁, ∂f₁_∂x₂, ...),
70-
(∂f₂_∂x₁, ∂f₂_∂x₂, ...),
71-
...)
70+
```julia
71+
@scalar_rule(f(x₁, x₂, ...),
72+
(∂f₁_∂x₁, ∂f₁_∂x₂, ...),
73+
(∂f₂_∂x₁, ∂f₂_∂x₂, ...),
74+
...)
75+
```
7276
7377
is equivalent to:
7478
75-
@scalar_rule(f(x₁, x₂, ...),
76-
@setup(nothing),
77-
(∂f₁_∂x₁, ∂f₁_∂x₂, ...),
78-
(∂f₂_∂x₁, ∂f₂_∂x₂, ...),
79-
...)
79+
```julia
80+
@scalar_rule(f(x₁, x₂, ...),
81+
@setup(nothing),
82+
(∂f₁_∂x₁, ∂f₁_∂x₂, ...),
83+
(∂f₂_∂x₁, ∂f₂_∂x₂, ...),
84+
...)
85+
```
8086
8187
For examples, see ChainRules' `rulesets` directory.
8288
@@ -313,11 +319,13 @@ by `frule` or `rrule`, but a good name make debugging easier.
313319
314320
This is able to deal with fairly complex expressions for `f`:
315321
316-
julia> propagator_name(:bar, :pushforward)
317-
:bar_pushforward
322+
```jldoctest; setup = :(using ChainRulesCore: propagator_name)
323+
julia> propagator_name(:bar, :pushforward)
324+
:bar_pushforward
318325
319-
julia> propagator_name(esc(:(Base.Random.foo)), :pullback)
320-
:foo_pullback
326+
julia> propagator_name(esc(:(Base.Random.foo)), :pullback)
327+
:foo_pullback
328+
```
321329
"""
322330
propagator_name(f::Expr, propname::Symbol) = propagator_name(f.args[end], propname)
323331
propagator_name(fname::Symbol, propname::Symbol) = Symbol(fname, :_, propname)

0 commit comments

Comments
 (0)