Skip to content

Extension API ​

This page is a reference for authors of new types who need the exact form of a method, a role or a check. Writing new types shows them in use.

The pure step ​

A modifier that acts on each stratum, or each group of strata, is defined by up to four pure functions.

FunctionReturnsNeeded
step(m, θ, v, s)the new value and state (v′, s′)always
init(m, θ, history)the state before the first stepby a modifier that keeps state
trace(m, θ, v, s, v′, s′)the values to record, as a tupleby a modifier that keeps state, at a call with record = Trace()
step_pullback(m, θ, v, s, v̄′, s̄′)the cotangents (v̄, s̄, θ̄)never: the forward-mode pass is used without it

A modifier keeps state unless it is declared isstateless or its blocks has no state entries. A call that needs a missing init or trace is an ArgumentError. ismodifier(m) is true for a built-in modifier and for any type with a step, so code that takes modifiers accepts a new type without listing types.

The parameters θ are a named tuple of the modifier's fields, each parameter read at the stratum, or group, and time. The method init gets θ read at the call's first time, its start, and the stratum's history as a vector. The value v and the state s are scalars, tuples for a modifier with blocks, and s is nothing for a modifier declared isstateless. θ̄ is a named tuple of the parameters' cotangents, named as in θ. Every argument after the modifier is untyped (Untyped arguments). The operator calls these functions in every loop, so the type needs no forward!. A depletion form is a step and an optional step_pullback, with the population in θ.N.

Roles ​

A role is one job of a coupling or of a whole-step modifier, a modifier whose blocks is nothing, named by a type with no fields. The type does its work in a forward! method for each role, and may add a pullback! for each. The roles gives their arguments and the fields of grads.

RoleJobPage
Step()a whole-step modifier's stepThe roles
Init()a whole-step modifier's starting stateThe roles
Trace()a whole-step modifier's recordThe roles
Pressure()a coupling's mixing, forward!(C, Pressure(), q, p, t), and pullback!(grads, C, Pressure(), nothing, p, t)Couplings

Pressure() and Trace() are also markers a call's record takes, for the pressure of each step and the modifiers' records, beside Resume() for the State to resume from.

Names ​

NameWhat it doesReturns
ntrace(m, S)how many rows a modifier's trace hasInt
trace_rows(m, S)the names of a modifier's trace rows, so a call returns them by namea named tuple of row counts, or nothing
blocks(m)a modifier's groups of strataa tuple, or nothing
nstate(m, S)the number of state entries a modifier keeps for S strataInt
param_rows(m, S)how many rows a whole-step modifier's parameters have, or nothing to skip the checkan Int or nothing
isstateless(m)whether a modifier leaves its state unchanged, so it records no traceBool
islinear(x)whether a coupling or whole-step modifier is a matrix, so a call runs that matrix in its placeBool
param(x, k, t)reads a parameter for stratum k at time ta value
add_param!(x̄, x, v, k, t)adds to a parameter's cotangentnothing
cotangent(x̄, name)the cotangent of a fielda cotangent, or nothing
add_cotangent!(x̄, v, idx...)adds to a cotangentnothing
PieceInterfacethe conformance test for a new type, built on the Interfaces.jl packagea test result

Checking your type ​

PieceInterface takes one Arguments per role the type has, each holding the type, the role and the arguments after the role, on whole vectors:

  • (s, history) for Init();

  • (v, s, t) for a modifier's Step();

  • (v, s, N), with a float N, for a depletion form's Step();

  • (q, p, t) for Pressure();

  • (r, v, s, v′, s′, t) for Trace(), with r a vector of ntrace(m, S) entries.

A modifier written as a pure step is checked through the same roles: its Step() runs step at each stratum or group, and its Init() runs init. The test also checks that the rule finds any pullback! the type has. The optional checks are :nstate, :stateless, :pullback and :step.

What each optional check does

The :nstate check compares the state length with nstate, and the :stateless check asks that a modifier declared isstateless leaves its state unchanged. The :pullback check compares the pullback the rule takes for Step() or Pressure() with the ForwardDiff Jacobian of the step on the same inputs. The :step check requires that each step, init, step_pullback and trace method takes its arguments after the modifier untyped, and each forward! for Init() and Trace() its arguments after the role. It also requires that the step's result type is inferred and that the step reads its parameters from θ, and it compares the step_pullback with the forward-mode pass.

NoAdjoint(op) is the same operator differentiated by plain automatic differentiation and not by its rule. Its gradient is the reference for the rule's gradient, and the ratio of the two times is the gain from the rule.

The names below check a rule and say which route a call takes.

NameWhat it doesReturns
ComposableRecurrences.gradient_routes(backend, r, x; history)times the gradient by each route a recurrence can take, after checking that the routes agreea vector of rows
ComposableRecurrences.test_adjoint(backend, op, args...)runs the rule tester of an automatic differentiation package on an operator's rule at its positional argumentsa test result

gradient_routes needs ADTypes and DifferentiationInterface loaded. test_adjoint needs Mooncake, or Enzyme with EnzymeTestUtils, loaded.