Skip to content

Traces ​

A modifier can record a series of its own at each step, building on the smoothing modifier of Writing new types.

A call with record = (:trace,) returns (; y, trace), with trace.modifiers holding one matrix per modifier, ntrace rows by the steps. By default a modifier's trace is its state after each step. A modifier whose state never changes records nothing, and declares so with isstateless(::MyModifier) = true (isstateless).

A trace of its own ​

A modifier can instead record a function of the step's values and state before and after it, v, s, v′ and s′. Its forward! for the role Trace() writes it into r.

Typeforward!pullback!grads
modifier traceforward!(m, Trace(), r, v, s, v′, s′, t) writes rpullback!(grads, m, Trace(), v, s, t)the mirror; trace holds the cotangent of r, in its shape; add into v, s, v′ and s′

For a pointwise modifier r is a tuple of vectors, one per trace value, indexed as v. For a modifier on whole vectors it is the step's column of ntrace rows.

The smoothing modifier records its smoothed value and what it leaves of the value it receives,

and the pullback sends their cotangents to the inputs and outputs of the step:

with and zero. Two values per stratum give rows.

julia
CR.ntrace(::Smooth, S) = 2S
function CR.forward!(m::Smooth, ::CR.Trace, r, v, s, v′, s′, t)
    smoothed, left = r
    for k in eachindex(v)
        smoothed[k], left[k] = s′[k], v[k] - s′[k]
    end
    return nothing
end
function CR.pullback!(grads, m::Smooth, ::CR.Trace, v, s, t)
    r̄₁, r̄₂ = grads.trace
    for k in eachindex(v)
        grads.v[k] += r̄₂[k]
        grads.s′[k] += r̄₁[k] - r̄₂[k]
    end
    return nothing
end

r = Recurrence([0.5, 0.5]; modifiers = (Smooth(0.5),))
y, trace = r(2.0; history = [1.0, 2.0], stop = 4, record = (:trace,))
only(trace.modifiers)
2×4 Matrix{Float64}:
 1.5  2.5  3.25  4.5
 1.5  1.0  0.75  1.25

Named rows ​

A modifier can name its trace's rows with trace_rows, a named tuple of row counts in row order. A call then returns its trace as a named tuple of views of its matrix, one per name, so a caller reads the rows by name rather than by position.

julia
CR.trace_rows(::Smooth, S) = (; smoothed = S, left = S)
y, trace = r(2.0; history = [1.0, 2.0], stop = 4, record = (:trace,))
only(trace.modifiers).left
1×4 view(::Matrix{Float64}, 2:2, :) with eltype Float64:
 1.5  1.0  0.75  1.25

A blockwise modifier records its trace per group, with g after t and tuples of one-entry views, as its step does. Its pullback!(grads, m, Trace(), v, s, t, g) adds grads.trace, a tuple of views, into the views in v, s, v′ and s′, and is given the values before the step. One whose trace is not linear in the step's outputs computes them with the step's forward!. PieceInterface checks that a trace leaves v, s, v′ and s′ unchanged.