Steps across strata
The scope of a modifier's step is one stratum, one group of a fixed number of strata, or every stratum at once. The first two are written as a pure step, introduced in Writing new types; the third is written with the roles.
using ComposableRecurrences
const CR = ComposableRecurrencesComposableRecurrencesThe scope of a step
blocks declares the scope.
blocks(m) | scope | written as |
|---|---|---|
(1, 1), the default with a step method | one stratum | step(m, θ, v, s) on scalars |
(nv, ns) | one group of nv values and ns state entries | step(m, θ, v, s) on tuples |
nothing, the default without one | every stratum at once | the roles, the advanced route |
A pure step needs no other method to run serially, under Threaded, on a device array and in the reverse pass. The number of state entries, nstate, follows from blocks.
A step on groups
A blockwise step acts on one group at a time. With blocks (nv, ns) and PerStratum holds one entry per group.
The example keeps a running total and a step count per stratum, and adds their mean to the value,
where blocks is (1, 2). Its init takes the tuple of the group's histories and returns the tuple of its state entries, or one number for all of them; here both start at zero.
struct AddMean{W}
w::W
end
CR.blocks(::AddMean) = (1, 2)
CR.init(::AddMean, θ, history) = 0
function CR.step(::AddMean, θ, v, s)
total, n = s[1] + v[1], s[2] + 1
return (v[1] + θ.w * total / n,), (total, n)
end
r = Recurrence([0.5, 0.5]; modifiers = (AddMean(PerStratum([0.5, 1.0])),))
r(fill(1.0, 2, 4); history = [1.0 2.0; 3.0 1.0])2×4 Matrix{Float64}:
2.25 3.03125 3.6849 4.56104
4.0 4.75 7.33333 9.77083The rule differentiates the group step with one forward-mode pass per group, so the gradient needs no further method.
using DifferentiationInterface, ForwardDiff, Mooncake
loss(w) = sum(
Recurrence([0.5, 0.5]; modifiers = (AddMean(PerStratum(w)),))(
fill(1.0, 2, 4); history = [1.0 2.0; 3.0 1.0]
)
)
gradient(loss, AutoMooncake(; config = nothing), [0.5, 1.0]) ≈
gradient(loss, AutoForwardDiff(), [0.5, 1.0])trueThe roles, for a step on every stratum
A whole-step modifier is one whose step reads every stratum at once, such as a mean over strata or a library call on the whole vector. It has no pure form. It is written with the roles: a forward! method that updates whole vectors in place, and an optional pullback!. A role is a type with no fields that names one job of the modifier. Couplings use the same two functions (Couplings).
A whole-step modifier has three limits. It runs on host arrays only. Every stratum must reach the step before any continues, so Threaded gains less. It takes the rule only with a pullback! or when declared linear; without either, the whole operator takes plain AD.
The role is the second argument of forward! and the third of pullback!. Every argument after the role is untyped, and each method returns nothing.
| Role | forward! | pullback! | grads |
|---|---|---|---|
Step() | forward!(m, Step(), v, s, t) updates v and s | pullback!(grads, m, Step(), v, s, t) | (; piece, v, s): v and s hold the cotangents of the new values and state; overwrite them with those of the old |
Init() | forward!(m, Init(), s, history) writes s; zero without it | pullback!(grads, m, Init(), s, history) | (; piece, s, history): read s, add into history |
Trace() | forward!(m, Trace(), r, v, s, v′, s′, t) writes the column r; the state without it | pullback!(grads, m, Trace(), v, s, t) | (; piece, trace, v, s, v′, s′): read trace, add into the others |
grads.piece is the mirror of the modifier's parameters. A modifier with a forward! for Init() takes the rule only if it also has the pullback! for Init(). param_rows(m, S) declares the number of rows of its PerStratum parameters, which each call then checks. Unlike a pure step, a whole-step modifier without a method for Init() starts from a zero state, and one without a method for Trace() records its state. A method for either role that types its arguments after the role is not called, so the default applies without an error; the :step check of PieceInterface fails such a method.
The example moves each value towards the mean over strata by a share
where grads.v with the cotangent of the values before the step. The step leaves the state unchanged, so grads.s already holds its cotangent.
struct ToMean{A}
a::A
end
function CR.forward!(m::ToMean, ::CR.Step, v, s, t)
μ = sum(v) / length(v)
for k in eachindex(v)
v[k] = (1 - m.a) * v[k] + m.a * μ
end
return nothing
end
function CR.pullback!(grads, m::ToMean, ::CR.Step, v, s, t)
v̄ = grads.v
μ, μ̄ = sum(v) / length(v), sum(v̄) / length(v̄)
ā = sum(k -> v̄[k] * (μ - v[k]), eachindex(v))
for k in eachindex(v̄)
v̄[k] = (1 - m.a) * v̄[k] + m.a * μ̄
end
CR.add_cotangent!(CR.cotangent(grads.piece, :a), ā)
return nothing
end
r = Recurrence([0.5, 0.5]; modifiers = (ToMean(0.5),))
r(1.0; history = [1.0 2.0; 3.0 1.0], stop = 3)2×3 Matrix{Float64}:
1.625 1.71875 1.67969
1.875 1.53125 1.69531The :pullback check of PieceInterface compares the pullback! with the ForwardDiff Jacobian of forward!.