Autograd graph node that defers backward computation to a Python callable.
When a user subclasses lucid.autograd.Function and invokes
MySubclass.apply(...), the Python layer constructs a
PythonBackwardNode, populates py_ctx and py_backward_fn,
and registers the node as the output tensor's grad_fn via
_register_python_backward_node. The Engine then treats this node like
any other backward node — when apply is called during graph
traversal, control crosses back into Python with the GIL held.
See Also
lucid.autograd.Function — Python base class users subclass.
FunctionCtx — context object carrying saved state.
Attributes
py_ctxpy::objectFunctionCtx wrapper carrying saved tensors and extras. Forwarded unchanged as the first argument to py_backward_fn.py_backward_fnpy::objectbackward(ctx, grad_output) static method. Expected signature: (ctx, grad_output: Tensor) -> Tensor | tuple[Tensor | None, ...].out_shapeShapeTensorImpl wrapper around the raw grad_out storage.out_dtypeDtypeF32).out_deviceDeviceCPU).Notes
The public py_ctx / py_backward_fn data members are exposed
directly via def_readwrite so the pybind11 binding can set them
without accessor boilerplate.
Thread safety: none. The Engine guarantees single-threaded execution of
the backward pass, so apply does not lock; calling it concurrently
from multiple threads is undefined.
Methods
2Invoke the Python backward callable and collect the resulting gradients.
Acquires the GIL, wraps grad_out in a temporary TensorImpl
matching out_shape / out_dtype / out_device,
calls py_backward_fn(py_ctx, grad_tensor), then unpacks the
returned tuple/list/tensor back into a vector of Storage
values — one per forward input.
Parameters
grad_outStorageReturns
std::vector<Storage>One Storage per input edge. Python None entries become empty CpuStorage{} values representing "no gradient for this input".
Raises
:runtime_errorbackward_fn is unset, or it raised an exception during execution (the original message is preserved).Human-readable node name for debugger / profiler output.
Returns
std::string_viewThe literal "PythonBackward".