Skip to content

Commit 86c7fd0

Browse files
committed
update to say Mooncake not Tapir nor Taped
1 parent 871d940 commit 86c7fd0

File tree

1 file changed

+35
-17
lines changed

1 file changed

+35
-17
lines changed

docs/src/tutorials/gradient_zoo.md

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ also known as reverse-mode automatic differentiation.
55
Given a model, some data, and a loss function, this answers the question
66
"what direction, in the space of the model's parameters, reduces the loss fastest?"
77

8+
This page is a brief overview of ways to perform automatic differentiation in Julia,
9+
and how they relate to Flux.
10+
811
### `gradient(f, x)` interface
912

1013
Julia's ecosystem has many versions of `gradient(f, x)`, which evaluates `y = f(x)` then retuns `∂y_∂x`. The details of how they do this vary, but the interfece is similar. An incomplete list is (alphabetically):
@@ -21,11 +24,11 @@ julia> ForwardDiff.gradient(x -> sum(sqrt, x), [1 4 16.])
2124
1×3 Matrix{Float64}:
2225
0.5 0.25 0.125
2326

24-
julia> ReverseDiff.gradient(x -> sum(sqrt, x), [1 4 16.])
27+
julia> DifferentiationInterface.gradient(x -> sum(sqrt, x), AutoMooncake(; config=nothing), [1 4 16.])
2528
1×3 Matrix{Float64}:
2629
0.5 0.25 0.125
2730

28-
julia> DifferentiationInterface.gradient(x -> sum(sqrt, x), AutoTapir(), [1 4 16.])
31+
julia> ReverseDiff.gradient(x -> sum(sqrt, x), [1 4 16.])
2932
1×3 Matrix{Float64}:
3033
0.5 0.25 0.125
3134

@@ -64,7 +67,7 @@ julia> model = Chain(Embedding(reshape(1:6, 2,3) .+ 0.0), softmax)
6467
Chain(
6568
Embedding(3 => 2), # 6 parameters
6669
NNlib.softmax,
67-
)
70+
)
6871

6972
julia> model.layers[1].weight # this is the wrapped parameter array
7073
2×3 Matrix{Float64}:
@@ -94,7 +97,7 @@ julia> grad_e = Enzyme.gradient(Reverse, loss, model)
9497
Chain(
9598
Embedding(3 => 2), # 6 parameters
9699
NNlib.softmax,
97-
)
100+
)
98101
```
99102

100103
While the type returned for `∂loss_∂model` varies, they all have the same nested structure, matching that of the model. This is all that Flux needs.
@@ -142,22 +145,40 @@ In this case they are all identical, but there are some caveats, explored below.
142145

143146
Both Zygote and Tracker were written for Flux, and at present, Flux loads Zygote and exports `Zygote.gradient`, and calls this within `Flux.train!`. But apart from that, there is very little coupling between Flux and the automatic differentiation package.
144147

145-
This page has very brief notes on how all these packages compare, as a guide for anyone wanting to experiment with them. We stress "experiment" since Zygote is (at present) by far the best-tested. All notes are from February 2024,
148+
This page has very brief notes on how all these packages compare, as a guide for anyone wanting to experiment with them. We stress "experiment" since Zygote is (at present) by far the best-tested. All notes are from February 2024,
146149

147150
### [Zygote.jl](https://github.com/FluxML/Zygote.jl/issues)
148151

149-
Reverse-mode source-to-source automatic differentiation, written by hooking into Julis's compiler.
152+
Reverse-mode source-to-source automatic differentiation, written by hooking into Julia's compiler.
150153

151154
* By far the best-tested option for Flux models.
152155

153156
* Long compilation times, on the first call.
154157

155158
* Allows mutation of structs, but not of arrays. This leads to the most common error... sometimes this happens because you mutate an array, often because you call some function which, internally, creates the array it wants to return & then fills it in.
156159

157-
* Custom rules via `ZygoteRules.@adjpoint` or better, `ChainRulesCore.rrule`.
160+
```julia
161+
function mysum2(x::AbstractMatrix) # implements y = vec(sum(x; dims=2))
162+
y = similar(x, size(x,1))
163+
for col in eachcol(x)
164+
y .+= col # mutates y, Zygote will not allow this
165+
end
166+
return y
167+
end
168+
169+
Zygote.jacobian(x -> sum(x; dims=2).^2, Float32[1 2 3; 4 5 6])[1] # returns a 2×6 Matrix
170+
Zygote.jacobian(x -> mysum2(x).^2, Float32[1 2 3; 4 5 6])[1] # ERROR: Mutating arrays is not supported
171+
```
172+
173+
* Custom rules via `ZygoteRules.@adjpoint` or (equivalently) `ChainRulesCore.rrule`.
174+
175+
* Returns nested NamedTuples and Tuples, and uses `nothing` to mean zero.
158176

159-
* Returns nested NamedTuples and Tuples, and uses `nothing` to mean zero. Does not track shared arrays, hence may return different contributions
177+
* Does not track shared arrays, hence may return different contributions.
160178

179+
```julia
180+
181+
```
161182

162183
!!! compat "Deprecated: Zygote's implicit mode"
163184
Flux's default used to be work like this, instead of using deeply nested trees for gradients as above:
@@ -194,7 +215,7 @@ julia> model_tracked = Flux.fmap(x -> x isa Array ? Tracker.param(x) : x, model)
194215
Chain(
195216
Embedding(3 => 2), # 6 parameters
196217
NNlib.softmax,
197-
)
218+
)
198219

199220
julia> val_tracked = loss(model_tracked)
200221
0.6067761f0 (tracked)
@@ -230,7 +251,7 @@ New package which works on the LLVM code which Julia compiles down to.
230251

231252
* Returns another struct of the same type as the model, such as `Chain` above. Non-differentiable objects are left alone, not replaced by a zero.
232253

233-
### [Tapir.jl](https://github.com/withbayes/Tapir.jl)
254+
### [Mooncake.jl](https://github.com/compintell/Mooncake.jl)
234255

235256
Another new AD to watch. Many similariries in its approach to Enzyme.jl, but operates all in Julia.
236257

@@ -262,11 +283,11 @@ Another Julia source-to-source reverse-mode AD.
262283

263284
### [ForwardDiff.jl](https://github.com/JuliaDiff/ForwardDiff.jl)
264285

265-
Forward mode is a different algorithm...
286+
Forward mode is a different algorithm...
266287

267-
* Needs a flat vector
288+
* Needs a simple array of parameters, i.e. supports only `gradient(f, x::AbstractArray{<:Real})`.
268289

269-
* Forward mode is generally not what you want!
290+
* Forward mode is generally not what you want for nerual networks! It's ideal for ``ℝ → ℝᴺ`` functions, but the wrong algorithm for ``ℝᴺ → ℝ``.
270291

271292
* `gradient(f, x)` will call `f(x)` multiple times. Layers like `BatchNorm` with state may get confused.
272293

@@ -316,7 +337,4 @@ This year's new attempt to build a simpler one?
316337

317338
Really `rrule_via_ad` is another mechanism, but only for 3 systems.
318339

319-
Sold as an attempt at unification, but its design of extensible `rrule`s turned out to be too closely tied to Zygote/Diffractor style AD, and not a good fit for Enzyme/Tapir which therefore use their own rule systems. Also not a natural fit for Tracker/ReverseDiff/ForwardDiff style of operator overloading AD.
320-
321-
322-
340+
Sold as an attempt at unification, but its design of extensible `rrule`s turned out to be too closely tied to Zygote/Diffractor style AD, and not a good fit for Enzyme/Mooncake which therefore use their own rule systems. Also not a natural fit for Tracker/ReverseDiff/ForwardDiff style of operator overloading AD.

0 commit comments

Comments
 (0)