Rules for every type
These requirements apply to every step, init, step_pullback and trace method, and to every forward! and pullback! of a coupling or of a modifier written with the roles.
Untyped arguments
Every argument after the modifier is untyped in step, init, step_pullback and trace. The package calls each method on floats, on dual numbers and on device values. It decides from the type of the modifier alone whether a method exists, so a method that types an argument is not seen.
| Method with a typed argument | Effect |
|---|---|
step | an ArgumentError at the call |
init, for a modifier that keeps state | an ArgumentError at the call |
trace, for a modifier that keeps state | an ArgumentError at a call with record = Trace() |
step_pullback | not called: the forward-mode pass is used, with the same gradient |
A typed step_pullback therefore costs speed and not correctness. gradient_routes times the gradient by each route a model can take, and so shows a step_pullback that is not used. The :step check of PieceInterface fails a typed method of any of the four.
In forward! and pullback!, grads and the arguments after the role are untyped, so that the rule finds the method. A typed forward! for Step() is an ArgumentError at the call. A typed forward! for Init() or Trace() is not called, and the modifier starts from zero or records its state; the :step check fails it.
Number types
Each method is generic in the number type of its arguments. A dual number or a Float32 can arrive in v, s, θ or a field. A method therefore does not annotate Float64 or construct 0.0; it uses zero(v), oftype and fields with type parameters.
Pure steps
A pure step is called at every stratum, or group, and time, in every loop of the package.
It allocates nothing and writes nothing, and returns the new value and state. The
:stepcheck asks that its result type is inferred from its arguments.It reads its parameters from
θ, not from the modifier's fields, as the gradient is taken inθ. The:stepcheck fails a step whose result changes with the modifier's fields whenθis held.It has no time argument. A field read at each time is a parameter, such as
TimeVarying, so that it reachesθread at the time.initreads only its parameters inθ, at the call's first time, itsstart, and the stratum's history.A step that keeps state has an
init, and one that keeps none declaresisstateless.
Traced values
Under Reactant a step runs once, on values that are not yet known, and what it does is recorded as the compiled program. So a step must not choose with if, &&, || or a ternary on v, s or an entry of θ. Write the choice as ifelse, min, max or clamp: such a step traces with no method for Reactant and is as fast on every other backend.
Arguments of a pullback
In step_pullback, v and s are the values step was given, so the values before the step. It returns the cotangents of v and s, and a named tuple of the cotangents of the parameters, named as in θ; a parameter it leaves out gets no cotangent. A pure step needs no step_pullback, as the rule takes its local derivative.
In pullback!, v, s, p and history are the values forward! was given, so the values before the step. A pullback reads them and does not write to them.
Threads
Under Threaded the steps of different strata can run at once. A pure step writes nothing, so it runs there as it is. A forward! or pullback! writes only its own arguments, and never to the type's fields. A mirror with an entry that is not a float array or a Ref of a number runs the reverse loop on one task; Executors says how the others are split.