Skip to content

Extension API ​

Every name for a new type, with the arguments of each role; the easy path shows them in use.

Roles ​

A role is one job a new type can do, named by a type with no fields. A new type does its work in a forward! method for each role it has, and may add a pullback! for each. The role is the second argument of forward! and the third of pullback!, and selects the method by dispatch.

Both methods write their outputs in place and return nothing, and a method that returns anything else is an error. pullback! receives the named tuple grads. Its field grads.piece is the mirror, the cotangents of the type's own fields, laid out as The gradient mirror gives. Its other fields hold the cotangents of the role's arguments.

Typeforward!pullback!grads
modifier stepforward!(m, Step(), v, s, t) updates v and spullback!(grads, m, Step(), v, s, t)the mirror; v and s hold the cotangents of v′ and s′; overwrite them with those of v and s
initial stateforward!(m, Init(), s, history) writes spullback!(grads, m, Init(), s, history)the mirror; s holds the cotangent of s; add into history, which may be nothing
couplingforward!(C, Pressure(), q, p, t) writes qpullback!(grads, C, Pressure(), q, p, t), with q === nothingthe mirror; q holds the cotangent of q; add into p

The rows for a blockwise step, a trace and a form are on their pages; the arguments mean the same in every role:

  • t is the absolute time, counted from 1 as start and stop are.

  • v holds one value per stratum, indexed by stratum, and s is the modifier's state after the previous step, with nstate entries.

  • history is the call's history as given: a vector for one series, else strata × time. Without an Init method the state starts at zero, and no Init pullback is needed.

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:

  • (s, history) for Init();

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

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

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

  • (r, v, s, v′, s′, t) for Trace(), with r as Traces gives. A blockwise group takes g after t, and its arguments are tuples of Refs.

It also checks that the rule finds any pullback! the type has. The optional checks are :pointwise and :nstate for a pointwise step and the state length, :blocks for a blockwise step and :pullback. The :pullback check compares each pullback! for Step() or Pressure() with the ForwardDiff Jacobian of forward! on the same inputs, so it needs using ForwardDiff.

NoAdjoint(op) is the same operator differentiated by automatic differentiation instead of its rule, so its gradient checks the rule's and timing both compares their cost.

The names below are internal helpers. They help check a new type, but they may change without notice.

NameWhat it doesReturns
ComposableRecurrences.uses_adjoint(m, role)whether the rule calls m's pullback! for roleBool
ComposableRecurrences.Run()the role of an operator's whole calla type with no fields
ComposableRecurrences.test_adjoint(backend, op, ComposableRecurrences.Run(), args...)runs the rule tester of an automatic differentiation package on an operator's rulea test result

For an operator, pass Run() as the role to uses_adjoint, PieceInterface and test_adjoint. test_adjoint needs Mooncake, or Enzyme with EnzymeTestUtils, loaded.