CRTP base supplying boilerplate fields and methods to concrete backward nodes with a fixed number of forward inputs.
Inherit as class FooBackward : public AutogradNode<FooBackward, N> —
or, more commonly, through the FuncOp alias — to obtain
N-slot saved-input storage, automatic version validation, and an
engine-friendly release_saved implementation. The derived class
must expose a static constexpr OpSchema schema_v1 whose .name
field identifies the op in error messages and traces, and must
implement apply returning one Storage per forward
input.
Template Parameters
Derived : class
The concrete backward node class (CRTP self-type). Must expose a
static constexpr member schema_v1 with at least a .name
std::string_view field.
N_IN : std::size_t
Number of input tensors the corresponding forward op consumes.
Determines the compile-time size of saved_inputs_,
input_shapes_, input_tensors_, and
saved_impl_inputs_.
See Also
Node — the abstract base whose virtuals this class overrides.
FuncOp — alias used by most concrete backward classes.
Attributes
kNumInputsstd::size_tN_IN for callers that need it as a value (e.g. when constructing the next_edges vector).input_tensors_std::array<std::weak_ptr<TensorImpl>, N_IN>TensorImpl objects, used by validate_versions and retainable_inputs. Weak so the backward graph never extends an input's lifetime.input_shapes_std::array<Shape, N_IN>apply without needing the live TensorImpl.out_shape_Shapeapply.dtype_DtypeDtype::F32.device_DeviceDevice::CPU.saved_inputs_std::array<Storage, N_IN>Storage values that apply reads — for example, the input activations of an element-wise multiply. Populated by the forward op builder.saved_output_StorageStorage, populated only when the backward formula needs the output activation (sigmoid, softmax, tanh, etc.). Left as an empty CpuStorage when unused.saved_impl_inputs_std::array<std::shared_ptr<TensorImpl>, N_IN>TensorImpl objects. Set in lockstep with saved_inputs_ so that Node::apply_for_graph can call into grad_fn-bearing ops when create_graph=True.saved_impl_output_std::shared_ptr<TensorImpl>TensorImpl, set only for ops whose graph-mode backward needs the output value (e.g. SigmoidBackward).Notes
Lifecycle. The forward op builder constructs an instance, fills
input_tensors_, input_shapes_, saved_inputs_,
saved_output_, dtype_, and device_, then calls
Node::set_next_edges and Node::set_saved_versions
before attaching the node to the output TensorImpl via
set_grad_fn. During backward the engine calls
validate_versions then apply. After apply
returns, the engine calls release_saved, which resets every
Storage slot to a default-constructed (zero-byte)
CpuStorage so referenced memory can be freed immediately.
Thread safety. None. All access happens on the single backward thread.
Slot ordering. saved_inputs_ and the gradients returned
from apply are ordered to match the forward op's positional
argument list — changing that order is a wire-format break.