Skip to content
class

VariadicKernel

extendsNodeIKernel
VariadicKernel

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>>
Weak references to the original inputs, used for in-place mutation version checks.
input_shapes_v_std::vector<Shape>
Shapes of the original inputs, preserved for backward shape math.
out_shape_Shape
Shape of the produced output tensor.
dtype_Dtype
Effective dtype, taken from the first non-null input.
device_Device
Effective device, taken from the first non-null input.
saved_inputs_v_std::vector<Storage>
Snapshots of each input's 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

1
static

wire_autograd

→bool
bool 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>
The backward node instance to install on out.
inputsconst std::vector<TensorImplPtr>&
The forward inputs in canonical order; slots may be nullptr.
outconst TensorImplPtr&
The freshly produced output tensor.
save_insbool, default ``true``
Whether to snapshot each non-null input's storage into saved_inputs_v_ for use during backward.

Returns

bool

true 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

3
virt

release_saved

void release_saved()

Free the values saved for backward, as AutogradNode does.

virtconst

saved_kind

SavedKind saved_kind()

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.

virt

validate_versions

void validate_versions()

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.

Methods

1
fnconst

name

int name()

Return the canonical schema name of the concrete op.