ETP Operators#

This page documents the user-facing ETP operators — the ops you call inside a model’s update to make a weight participate in online learning — together with the small set of core types every algorithm consumes: input wrappers, the eligibility-trace state, gradient utilities, and the error hierarchy.

To add your own ETP primitive (with custom trace-propagation rules), see Custom Primitives.

ETP Primitive Operators#

These functions mark weight operations for inclusion in online learning. Use braintrace.matmul(x, w) instead of x @ w to include a weight in eligibility-trace computation; a parameter used through a regular JAX op is automatically excluded. There is no special parameter class — every brainstate.ParamState is eligible, and participation is decided purely by whether an ETP operator consumed it.

All operators accept physical-unit quantities (mantissa/unit are split, computed, and recombined) and come in batched and unbatched forms selected by input rank.

matmul

ETP-aware matrix multiplication.

grouped_matmul

ETP-aware grouped (block-diagonal) matrix multiplication.

embedding

ETP-aware embedding lookup (trainable gather).

einsum

ETP-aware two-operand einsum, linear in the trainable weight.

element_wise

ETP-aware element-wise operation.

conv

ETP-aware convolution.

sparse_matmul

ETP-aware sparse matrix multiplication.

lora_matmul

ETP-aware LoRA (Low-Rank Adaptation) matrix multiplication.

Controlling Parameter Participation#

import jax
import braintrace
import brainstate

class MyRNN(brainstate.nn.Module):
    def __init__(self):
        super().__init__()
        self.w_rec = brainstate.ParamState(...)   # want ETP
        self.w_in = brainstate.ParamState(...)     # do NOT want ETP
        self.h = brainstate.ShortTermState(...)

    def update(self, x):
        # regular matmul -> w_in excluded from ETP
        inp = x @ self.w_in.value
        # ETP matmul -> w_rec included in ETP
        self.h.value = jax.nn.tanh(inp + braintrace.matmul(self.h.value, self.w_rec.value))
        return self.h.value
Table 1 Parameter Selection Rules#

Goal

How

Include a parameter in online learning

Use a braintrace.* ETP operator (e.g. braintrace.matmul(x, w)).

Exclude a parameter from online learning

Use a regular JAX op (e.g. x @ w).

Selection mechanism

The operation’s primitive type — not the parameter’s class. Every brainstate.ParamState is eligible; participation depends solely on whether an ETP primitive consumed it.

Delay Lines and Other Non-Hidden State#

An online algorithm credits a weight in two ways. The eligibility trace of each hidden group the weight feeds carries its influence across steps within that group. The learning signal then reads, in the same step, how the loss depends on the group’s hidden values:

  • on the new values, under every algorithm;

  • on the old values along a path to the outputs that crosses no new hidden value, under a single-step algorithm (the output bypass: a readout taken before the state is updated is credited exactly);

  • on the old values along every path, under a multi-step VJP (vjp_method='multi-step') given one step per call.

Nothing else crosses a step. A value kept in any other state, such as a delay line or ring buffer held in a brainstate.ShortTermState, is a given input to the next step. For a single-step algorithm, a hidden state of another group that only a later step reads is too.

So a weight whose influence reaches the loss only through the old value of such a state has an online gradient of exactly zero, while its BPTT gradient is not zero. Each VJP algorithm reports this as DiagnosticKind.ONLINE_GRADIENT_ZERO:

  • at compile time, as an INFO record that assumes the loss reads only the model outputs (the loss is not known yet);

  • at the first gradient computation, as a warning, if the zero holds for the outputs and states the loss actually reads. A loss that reads a state after the step (a firing-rate regulariser, say) gives that state’s new value a same-step path, and then nothing is reported.

The record names the weight and the states its credit crosses (through) and lists the fixes that work for the algorithm in use:

  • a ShortTermState line: hold it in a brainstate.HiddenState. Under a multi-step VJP, and under OSTLRecurrent or SnAp, which couple the states of a group, the weight then gets credit. Under D_RTRL, EProp and pp_prop it does not: their trace stays in the weight’s own group, and the line joins that group only, so they need one of the other algorithms too;

  • a hidden line under a single-step algorithm: use D_RTRL(vjp_method='multi-step'), OSTLRecurrent or SnAp;

  • in every case: train the weight with BPTT.

Given MultiStepData of two or more steps per call, a multi-step VJP credits the line within each call and drops it across calls. The gradient is then not zero, and the gradient-time check skips such calls. A call of one step (the remainder of a sequence, say) has a zero gradient, but the run’s need not, so under a multi-step VJP the gradient-time record is INFO (“zero within this call”) and emits no warning.

When the weight’s own influence also reaches the loss along a credited path, its online gradient is not known to be zero but omits the credit through the line, which can bias it, even reverse its direction. This is an INFO record, DiagnosticKind.ONLINE_GRADIENT_PARTIAL, made at compile time, which emits no warning.

The check is structural. No derivative flows through stop_gradient, through primitives whose derivative is zero (round, floor, ceil, sign) or through integer and boolean values, and calls (jit, remat) are followed into their bodies; any other equation is assumed to pass a derivative from each input to each output. A derivative that vanishes only for the values taken (jnp.heaviside, a ReLU that is never active) is not told apart, so such a weight can be reported although its BPTT gradient is zero too.

Not checked: learning_signal='random_feedback' and 'modulatory', and random-projection traces (UORO), which credit weights differently. Not detected (#216): a ring that is written and then read back in the same step, such as brainstate.nn.Delay used as update(y) and then retrieve_at_step(d). The analysis treats the buffer as one array, so the old slot it reads looks like this step’s write.

Reads of a New Hidden Value in the Same Step#

A hidden update often reads another hidden state’s new value: the membrane potential reads this step’s synaptic current (g -> V), an LSTM output reads this step’s cell (c -> h). The eligibility trace of V already carries the path W -> g -> V, because its y -> hidden derivative is taken through the update of g. Under a single-step algorithm the signal of g must then hold that read fixed, or the path is counted twice. Holding it fixed also drops the history g^{t-1} -> g^t -> V^t from the signal of g, so the trace of V must carry that too, through the transition of its hidden group. The compiler therefore cuts a read only when every relation whose hidden groups include g’s group also includes V’s, g and V share a hidden group, and every equation on the read is computed by the group transition (HiddenGroup.carried_vars) and is proven position-preserving (elementwise): an allow-listed elementwise primitive on the full position shape, or a custom_jvp/checkpoint call whose body and derivative rule are made of them. A roll, a reduction, a gather, a reshape, a dot_general or a custom_vjp on the read is not proven, so the read is not cut; for now neither is a read into or out of one slot of a HiddenGroupState. It records the cut (DiagnosticKind.HIDDEN_NEW_READ_CUT, INFO); the perturbed step evaluates the read on the unperturbed g, and an equation that also feeds the outputs on the perturbed one. A read through an ETP op without gradient (a stacked layer), or one that no trace carries, keeps the total derivative. When a read cannot be cut although a trace carries it within the step (one relation traces V’s group and another does not; the read crosses hidden groups, or goes through an equation not proven position-preserving), the signal of its source stays the total derivative on every read and the graph records DiagnosticKind.HIDDEN_NEW_READ_DOUBLE_COUNT (ERROR, also warned); vjp_method='multi-step' builds no perturbation and avoids it. Such a conflict blocks only its source, and the sources whose cuts cannot then be emitted apart from the kept reads: the reads of the other sources are still cut. graph.hidden_perturb declares the outcome: new_read_cuts, new_read_conflicts and new_read_exact. new_read_exact means only that no in-step read of a new hidden value is double counted in the single-step learning signal; it does not say the single-step gradient is exact, since the learner’s own approximations (a diagonal trace of a coupled recurrence, paths across groups) are separate.

Input Data#

Wrappers that tell an online-learning algorithm whether a step receives a single time step or a whole sequence of time steps.

SingleStepData

A container marking input data as belonging to a single time step.

MultiStepData

A container marking input data as spanning multiple time steps.

Eligibility Trace State#

The state object that stores the eligibility trace carried forward across time steps during online learning is EligibilityTrace. Its canonical API page is indexed with the online-learning algorithms that create and update it.

Gradient Utilities#

Helpers used when combining per-step gradients into the running online gradient.

GradExpon

Accumulate gradients with an exponential (leaky) running sum.

Errors#

Exceptions raised by the compilation and execution machinery.

NotSupportedError

Exception raised for operations that are not supported.

CompilationError

Exception raised for errors that occur during compilation.