Extension API
A new type is written with the functions, roles and checks below; 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.
| Function | Returns | Needed |
|---|---|---|
step(m, θ, v, s) | the new value and state (v′, s′) | always |
init(m, θ, history) | the state before the first step | by a modifier that keeps state |
trace(m, θ, v, s, v′, s′) | the values to record, as a tuple | by 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.
| Role | Job | Page |
|---|---|---|
Step() | a whole-step modifier's step | The roles |
Init() | a whole-step modifier's starting state | The roles |
Trace() | a whole-step modifier's record | The 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
| Name | What it does | Returns |
|---|---|---|
ntrace(m, S) | how many rows a modifier's trace has | Int |
trace_rows(m, S) | the names of a modifier's trace rows, so a call returns them by name | a named tuple of row counts, or nothing |
blocks(m) | a modifier's groups of strata | a tuple, or nothing |
nstate(m, S) | the number of state entries a modifier keeps for S strata | Int |
param_rows(m, S) | how many rows a whole-step modifier's parameters have, or nothing to skip the check | an Int or nothing |
isstateless(m) | whether a modifier leaves its state unchanged, so it records no trace | Bool |
islinear(x) | whether a coupling or whole-step modifier is a matrix, so a call runs that matrix in its place | Bool |
param(x, k, t) | reads a parameter for stratum k at time t | a value |
add_param!(x̄, x, v, k, t) | adds to a parameter's cotangent | nothing |
cotangent(x̄, name) | the cotangent of a field | a cotangent, or nothing |
add_cotangent!(x̄, v, idx...) | adds to a cotangent | nothing |
PieceInterface | the conformance test for a new type, built on the Interfaces.jl package | a 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)forInit();(v, s, t)for a modifier'sStep();(v, s, N), with a floatN, for a depletion form'sStep();(q, p, t)forPressure();(r, v, s, v′, s′, t)forTrace(), withra vector ofntrace(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. 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.
| Name | What it does | Returns |
|---|---|---|
ComposableRecurrences.gradient_routes(backend, r, x; history) | times the gradient by each route a recurrence can take, after checking that the routes agree | a 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 arguments | a test result |
gradient_routes needs ADTypes and DifferentiationInterface loaded. test_adjoint needs Mooncake, or Enzyme with EnzymeTestUtils, loaded.