Skip to content
class

NaryKernel

extendsAutogradNode<Derived, N>IKernel
NaryKernel

CRTP base for fixed-arity N-input op kernels.

Inherits AutogradNode\<Derived, N\> so input metadata (input_shapes_, saved_inputs_, input_tensors_, saved_impl_inputs_) is stored in compile-time-sized std::array containers. Also inherits IKernel for polymorphic invocation.

Unlike UnaryKernel and BinaryKernel, this base does not provide a forward() trampoline — N-ary ops have too much op-specific validation to share a single template. Instead, derived ops own their forward() and call wire_autograd once the output tensor exists to attach the backward node.

Template Parameters

Derived : class The concrete CRTP self-type. N : std::size_t The fixed input arity. Typically 3 or more (use BinaryKernel for N == 2).

See Also

UnaryKernel, BinaryKernel, VariadicKernel. IKernel — the abstract base above the CRTP layer.

Notes

Slot count is exactly N. Derived ops implement apply (and optionally apply_for_graph) themselves; no grad_formula contract is enforced because N-ary backward shapes vary too widely.

Static methods

2
static

capture_inputs

void capture_inputs(Derived & bwd, const int & inputs, const int & saves)
static

wire_autograd

→bool
bool wire_autograd(int bwd, const int & inputs, const int & out, const int & saves)

Attach an existing backward node to the autograd graph.

Parameters

bwdstd::shared_ptr<Derived>
The backward node instance to install on out.
inputsconst std::array<TensorImplPtr, N>&
The forward inputs in canonical order. Slots may be nullptr (e.g. an optional bias).
outconst TensorImplPtr&
The freshly produced output tensor.
savesconst std::array<bool, N>&
Which inputs the backward reads the values of: input i's storage is snapshotted into saved_inputs_[i] when saves[i] and the input is non-null. Those are the inputs AutogradNode::validate_versions guards, so an input read by shape alone — a bias, a beta — is left out, and a write into it after forward is not refused.

Returns

bool

true when the autograd graph was wired and out had its grad_fn installed; false when grad mode is off or no input requires a gradient (no-op).

Notes

Side effects on success: populates bwd's dtype/device/output shape from the first non-null input, fills input_shapes_, input_tensors_, saved_inputs_, saved_impl_inputs_, records one Edge per input via detail::ensure_grad_fn, captures per-input versions for in-place mutation detection, and marks out as a non-leaf, requires_grad tensor.

Methods

1
fnconst

name

int name()

Return the canonical schema name of the concrete op.