Skip to content
class

ModuleBackwardHookState

ModuleBackwardHookState

Shared state shuttled between the input-side and output-side hook barriers of a single nn.Module invocation.

A Module with registered backward hooks is wrapped at forward time with two barriers — ModuleOutputHookNode on each output edge and ModuleInputHookNode on each gradient-receiving input edge. Both barriers hold a shared_ptr to the same ModuleBackwardHookState so that the gradients arriving at the outputs (from downstream nodes) are available to the input-side hook callbacks, and any replacement gradients returned by the Python full-backward hook flow back through the inputs.

Attributes

pre_runnerpy::object
Python generator function that runs the pre-backward hooks. Receives the output-gradient tuple; yields each replacement for it.
full_runnerpy::object
Python generator function that runs the full-backward hooks. Receives the input-gradient tuple (one entry per positional argument of the call) and the output-gradient tuple; yields each replacement for the input gradients.
n_inputsstd::size_t
Number of original Python-side input positions on the module call (some may have requires_grad=False and therefore no edge).
n_outputsstd::size_t
Number of outputs the module produced.
pre_hooks_ranbool
One-shot guard so the pre-backward hooks fire exactly once per backward pass even when the barrier is visited from multiple edges.
full_hooks_ranbool
Same guard for the full-backward hook batch.
outputs_ranbool
Whether the output barrier ran in this pass. A gradient can reach the inputs without passing through the outputs — a double backward through what the module body saved — and the full hooks then do not run, as in the reference, which has no grad_output to hand them.
The BackwardPass the per-pass members above belong to, and those an outer pass set aside while a nested one crosses the module.
input_arg_indicesstd::vector<std::uint32_t>
Maps each edge slot in grad_inputs back to its original positional argument index in the Python signature.
input_metasstd::vector<ModuleHookTensorMeta>
Metadata for each gradient-receiving input edge, parallel to grad_inputs.
grad_inputsstd::vector<TensorImplPtr>
Per-edge gradient reaching each input, summed over every use the module body made of it. Null means no gradient arrived for that slot.
output_metasstd::vector<ModuleHookTensorMeta>
Metadata for every output position (size() == n_outputs).
grad_outputsstd::vector<TensorImplPtr>
Gradient reaching each output position, summed over every consumer of that output.
output_edge_indicesstd::vector<std::uint32_t>
For each edge of the output barrier, the output-position index it corresponds to in grad_outputs.
A slot sums what reaches it, as the reference's input buffer doesa
module whose output feeds two consumers, or whose body reads an input
twice, receives one gradient per use, and the hooks — and everything
upstream — must see their sum. Overwriting kept only the last one.
The gradients are held as tensors in both modesunder ``create_graph``
they carry their graph, and the sums are graph ops.

Notes

The two py::object runners are the Python-side loops over the module's hooks of each kind. They are held as opaque py::object (the GIL is acquired only when they are actually invoked inside apply_barrier). Each is a generator: it calls the hooks in turn and yields every result that is not None, and the barrier checks that result against the slots it replaces — count, then each gradient's dtype, device and shape (autograd/TensorHooks.h) — and puts it in place before the generator goes on, so the next hook is handed only a tuple the engine accepted.

Constructors

1
ctor

ModuleBackwardHookState

void ModuleBackwardHookState(int n_inputs, int pre_runner, int full_runner)

Construct fresh hook state for a module forward call.

Parameters

n_inputsstd::size_t
Number of positional inputs in the Python module call.
pre_runnerpy::object
Python callable that will fan pre-backward hooks at the output barrier.
full_runnerpy::object
Python callable that will fan full-backward hooks at the input barrier.

Destructor

1

Methods

3
fn

clear

void clear()

Release all Python runners and saved gradients under the GIL.

fn

enter_pass

void enter_pass()

Make the per-pass members the running pass's (PassSlots): what an earlier pass left is dropped, what an outer pass still running left is set aside. Called before every read or write of the slots.

fn

reset

void reset()

Forget every gradient and hook guard of the current pass — once the module's backward is complete, so the next pass starts empty and fires the hooks again.