CRTP base for runtime-variable-arity op kernels.
Inherits Node directly (not AutogradNode\<Derived, N\>)
so the saved-input and edge containers can grow at runtime, and
IKernel for polymorphic invocation. Derived ops own their
own forward() and apply() — only the autograd wiring helper
is shared.
Template Parameters
Derived : class The concrete CRTP self-type.
See Also
NaryKernel — fixed-arity sibling using std::array.
UnaryKernel, BinaryKernel.
IKernel — the abstract base above the CRTP layer.
Attributes
input_ tensors_ v_std::vector<std::weak_ptr<TensorImpl>>input_ shapes_ v_std::vector<Shape>out_ shape_Shapedtype_Dtypedevice_Devicesaved_ inputs_ v_std::vector<Storage>Storage at forward time, one per input slot (empty for a null input), populated when wire_autograd is called with save_ins=true.Notes
All inputs must share dtype and device — wire_autograd does
not validate this; the concrete op's forward() is expected
to have already enforced consistency before calling wire_autograd.
validate_versions walks the runtime vector and checks each
input whose values saved_inputs_v_ holds — the rule
AutogradNode::validate_versions follows.
Static methods
1wire_autograd
→boolbool wire_autograd(int bwd, const int & inputs, const int & out, bool save_ins)Attach an existing backward node to the autograd graph.
Parameters
bwdstd::shared_ptr<Derived>out.inputsconst std::vector<TensorImplPtr>&nullptr.outconst TensorImplPtr&save_ insbool, default ``true``saved_inputs_v_ for use during backward.Returns
booltrue when the autograd graph was wired; false when grad mode is off or no input requires a gradient (no-op).
Notes
Side effects on success: derives dtype/device from the first
non-null input, records out_shape_, populates the per-input
vectors, records one Edge per input via
detail::ensure_grad_fn, captures versions, and marks
out as a non-leaf requires_grad tensor.
Virtual methods
3Free the values saved for backward, as AutogradNode does.
apply reads values from the saved slots — filled for every input when the op saves them, whatever their size — and from members of the derived class's own where it declares kKeepsOwnValues, as AutogradNode reads them. No tensor is kept for graph-mode backward, so cat and stack, which save none, may run again in either mode.
Verify no input whose values apply reads was written in place between forward and backward.
Notes
Checks each input whose values saved_inputs_v_ holds and
raises a clear error (with the op name and slot index) if its version
drifted; an input saved by shape alone is not refused (see
holds_saved_values). Invoked by the autograd engine before
apply.