Barrier autograd node inserted on each output of a hooked nn.Module.
Plays two roles during backward:
- Receives output gradients from downstream nodes through
accumulate_barrier_grad, parking them into the sharedModuleBackwardHookStaterather than producing input gradients on the fly. This lets the engine reach the barrier from every consumer before any hook fires. - When
apply_barrierruns, 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
1ModuleOutputHookNode
void ModuleOutputHookNode(int state)Construct an output-side barrier bound to shared hook state.
Parameters
statestd::shared_ptr<ModuleBackwardHookState>Virtual methods
3A hook's None, or a slot no gradient reached, is an empty storage.
Identifies this node as a staged barrier so the engine routes gradients through the barrier protocol instead of the direct path.
Methods
8Add an incoming gradient to what its output position holds.
Parameters
input_ nrstd::uint32_tgradStorageaccumulate_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.
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_ outStorageReturns
std::vector<Storage>One Storage per outgoing edge, in output_edge_indices order.
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.
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.
Graph label — "ModuleOutputHook" — for debug printing and profiler traces. Overrides Node::node_name.
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.