Skip to content
class

ModuleInputHookNode

extendsNodeGraphBarrier
ModuleInputHookNode

Barrier autograd node inserted on each gradient-bearing input of a hooked nn.Module.

Mirrors ModuleOutputHookNode but on the other side of the module: it collects the gradients flowing back into the module's inputs and fires the full backward hooks (which receive both grad_input and grad_output tuples) before forwarding the (possibly replaced) gradients to the upstream graph.

See Also

ModuleOutputHookNode : sibling barrier on the output side.

Notes

Hooks may return None (keep current gradients) or a tuple matching the positional input signature with None entries for slots that should remain untouched. A returned gradient must have the dtype, device and shape of the one it replaces — downstream nodes read it with the metadata captured in ModuleBackwardHookState::input_metas — and a tuple of another length, an entry that is not a tensor, or a tensor for an argument that takes no gradient is refused. When a hook is refused, or raises, nothing any hook returned flows on and the state is reset.

Constructors

1

Virtual methods

4
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.

virtconst

runs_without_grad

bool runs_without_grad()

Once the module's output barrier has run in this pass, the input barrier runs even when every output gradient was dropped on the way in, so the inputs and full hooks still receive materialized zeros.

Methods

8
fn

accumulate_barrier_grad

void accumulate_barrier_grad(int input_nr, int grad)

Add an incoming input gradient to what its slot holds.

Parameters

input_nrstd::uint32_t
Edge slot index (parallel to ModuleBackwardHookState::grad_inputs). Out-of-range values are ignored.
gradStorage
Incoming gradient buffer. Never added into in place.
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.

fn

apply

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

Convenience non-barrier entry point — see ModuleOutputHookNode::apply.

Parameters

grad_outStorage
Incoming gradient for input slot 0.

Returns

std::vector<Storage>

One Storage per outgoing edge.

fn

apply_barrier

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

Fire the full-backward hooks and emit one gradient per outgoing edge. The module's backward ends here, so the shared state is reset for the next pass.

Returns

std::vector<Storage>

One Storage per input edge in declaration order. Hooks may have replaced individual entries via their return tuple; empty slots become empty CpuStorage placeholders.

fn

apply_barrier_for_graph

int apply_barrier_for_graph()

apply_barrier for a create_graph pass.

fnconst

node_name

int node_name()

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

fn

run_hooks

void run_hooks(bool graph)

Fire the full-backward hooks once per pass; graph as for ModuleOutputHookNode::run_hooks.