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::objectfull_runnerpy::objectn_inputsstd::size_trequires_grad=False and therefore no edge).n_outputsstd::size_tpre_hooks_ranboolfull_hooks_ranboolinput_arg_indicesstd::vector<std::uint32_t>grad_inputs back to its original positional argument index in the Python signature.input_metasstd::vector<ModuleHookTensorMeta>grad_inputs.grad_inputsstd::vector<std::optional<Storage>>std::nullopt means no gradient arrived for that slot.output_metasstd::vector<ModuleHookTensorMeta>size() == n_outputs).grad_outputsstd::vector<std::optional<Storage>>output_edge_indicesstd::vector<std::uint32_t>grad_outputs.Notes
The two py::object runners are the Python-side dispatchers that fan
each barrier event out to every registered hook on the module. They are
held as opaque py::object (the GIL is acquired only when they are
actually invoked inside apply_barrier).
Constructors
1ModuleBackwardHookState
void ModuleBackwardHookState(int n_inputs, int pre_runner, int full_runner)Construct fresh hook state for a module forward call.
Parameters
n_inputsstd::size_tpre_runnerpy::objectfull_runnerpy::object