Skip to content
class

ModuleOutputHookNode

extendsNodeGraphBarrier
ModuleOutputHookNode

Barrier autograd node inserted on each output of a hooked nn.Module.

Plays two roles during backward:

  1. Receives output gradients from downstream nodes through accumulate_barrier_grad, parking them into the shared ModuleBackwardHookState rather than producing input gradients on the fly. This lets the engine reach the barrier from every consumer before any hook fires.
  2. When apply_barrier runs, invokes the user-registered pre-backward hooks (if any), giving them an opportunity to replace the output gradients before they continue propagating back through the module body.

See Also

ModuleInputHookNode : sibling barrier on the input side. ModuleBackwardHookState : shared state container.

Notes

Marked as a barrier (is_barrier() == true), which the engine uses to switch from the fast apply() path to the staged barrier protocol, and a GraphBarrier, so create_graph delivers to it slot by slot too. The node does not modify gradients unless a registered hook explicitly returns a replacement tuple.

Constructors

1

Virtual methods

3
virtconst

empty_grad_is_none

bool empty_grad_is_none()

A hook's None, or a slot no gradient reached, is an empty storage.

virtconst

is_barrier

bool is_barrier()

Identifies this node as a staged barrier so the engine routes gradients through the barrier protocol instead of the direct path.

Methods

8
fn

accumulate_barrier_grad

void accumulate_barrier_grad(int input_nr, int grad)

Add an incoming gradient to what its output position holds.

Parameters

input_nrstd::uint32_t
Output position the gradient belongs to. Out-of-range values are silently ignored to keep the engine robust against pruned graphs.
gradStorage
Incoming gradient buffer. Never added into in place: it may be the very buffer another holder still reads.
fn

accumulate_barrier_grad_for_graph

void accumulate_barrier_grad_for_graph(int input_nr, int grad)

accumulate_barrier_grad for a create_graph pass: the sum is an add_op, so it stays differentiable.

fn

apply

→std::vector<Storage>
int apply(int grad_out)

Convenience entry point used when the engine reaches this node via the non-barrier fast path: forwards to accumulate_barrier_grad and then immediately runs apply_barrier.

Parameters

grad_outStorage
Incoming output gradient for slot 0.

Returns

std::vector<Storage>

One Storage per outgoing edge, in output_edge_indices order.

fn

apply_barrier

→std::vector<Storage>
int apply_barrier()

Fire the pre-backward hooks and emit one Storage per outgoing edge. Called by the engine exactly once after all incoming gradients have been accumulated.

Returns

std::vector<Storage>

Gradients to push along each outgoing edge. Empty slots become empty CpuStorage placeholders.

fn

apply_barrier_for_graph

int apply_barrier_for_graph()

apply_barrier for a create_graph pass: the hooks see the gradients with their graph, and a null entry is an edge no gradient reached.

fnconst

node_name

int node_name()

Graph label — "ModuleOutputHook" — for debug printing and profiler traces. Overrides Node::node_name.

fn

run_hooks

void run_hooks(bool graph)

Fire the pre-backward hooks — and the full ones when the module has no input that takes a gradient — once per pass. graph is whether the pass is a create_graph one, whose slots keep a replacement's graph.