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.
Step()computes one step.Init()computes the starting state.Pressure()is a coupling's mixing.Trace()is a modifier's record for a call withrecord = (:trace,).Local()is a map's own derivative rule; Local rules for maps gives its arguments.
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.
| Type | forward! | pullback! | grads |
|---|---|---|---|
| modifier step | forward!(m, Step(), v, s, t) updates v and s | pullback!(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 state | forward!(m, Init(), s, history) writes s | pullback!(grads, m, Init(), s, history) | the mirror; s holds the cotangent of s; add into history, which may be nothing |
| coupling | forward!(C, Pressure(), q, p, t) writes q | pullback!(grads, C, Pressure(), q, p, t), with q === nothing | the 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:
tis the absolute time, counted from 1 asstartandstopare.vholds one value per stratum, indexed by stratum, andsis the modifier's state after the previous step, withnstateentries.historyis the call'shistoryas given: a vector for one series, else strata × time. Without anInitmethod the state starts at zero, and noInitpullback is needed.
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:
(s, history)forInit();(v, s, t), with vectors, for a modifier'sStep();(q, p, t)forPressure();(v, s, N), with a floatN, for a depletion form;(r, v, s, v′, s′, t)forTrace(), withras Traces gives. A blockwise group takesgaftert, and its arguments are tuples ofRefs.
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.
| Name | What it does | Returns |
|---|---|---|
ComposableRecurrences.uses_adjoint(m, role) | whether the rule calls m's pullback! for role | Bool |
ComposableRecurrences.Run() | the role of an operator's whole call | a 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 rule | a 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.