Skip to content
class

AutogradNode

extendsNode
AutogradNode

Static methods

2
static

argument_position

int argument_position(int j)

The forward argument slot j stands for, which the error names: Derived::kArgPositions where the node is wired on fewer inputs than its op takes (lu_solve(LU, pivots, B) wires {LU, B}), otherwise j itself.

static

grad_reads

bool grad_reads(int i, int j)

Whether the gradient of input i reads input j's values: Derived::kGradReads where the node declares it, otherwise every gradient is taken to read every input the node saved.

Virtual methods

4
virt

release_saved

void release_saved()

Drop every reference held for backward so that memory can be reclaimed immediately after the engine finishes with this node.

Resets all entries of saved_inputs_, clears saved_output_, empties the input_tensors_ weak array, releases the strong TensorImpl references in saved_impl_inputs_, and clears the weak saved_impl_output_ handle.

Notes

Called by Engine once apply returns, except when retain_graph=True. After this call the node retains only its sequence number and (now-empty) metadata; calling apply again is undefined.

virt

restore_saved_for_graph

void restore_saved_for_graph()

Put back, for graph-mode backward, every saved tensor a write since forward may have changed without validate_versions refusing.

An input is replaced by the tensor it was at forward time, rebuilt from saved_inputs_ and its edge (forward_time_input), when it no longer sits where it did — reachable past the version check only while that check is waived. The output is always rebuilt — from saved_output_, with this node as its grad_fn — by dropping the weak handle, which is the case a reader already covers for an output that is gone: no version guards it, and a write under no_grad leaves its grad_fn where it was, so nothing on the live tensor says whether it still holds the values this node saw. Either way graph-mode backward reads the values eager backward reads.

Raises

LucidError
An input was written into and its saved values cannot rebuild it.
virtconst

saved_kind

SavedKind saved_kind()

apply reads the values this node kept: those in its slots (keep_input, keep_output), and those a node keeps in members of its own, which it declares with kKeepsOwnValues (tools/check_saved_versions.py requires it of every node class holding a tensor of its own). A node that kept none frees just the tensors graph-mode backward reads, and an eager pass may run it again.

virt

validate_versions

void validate_versions()

Verify that no input whose values apply reads has been written in place since forward.

An input is guarded when its values sit in saved_inputs_ (holds_saved_values) — the slot a backward that reads them saves them in — and a gradient that is needed reads them: one whose input takes a gradient (a live edge), by grad_reads. The others are not: add, sum or a view read at most their shape, and x * c with a constant c reads x only for c's gradient, which nothing asks for. Refusing a write into one of them refused what the reference allows. Expired weak references are skipped — if the input has been destroyed, no consumer can observe a corrupted gradient.

Raises

VersionMismatch
When a guarded input's live version counter differs from the value captured at forward. The error names this op (via Derived::schema_v1.name) and the offending input index.

Methods

12
fn

advance_saved_output_version

void advance_saved_output_version(int v)
fnconst

any_needed_gradient

bool any_needed_gradient()
fn

bind_saved_output

void bind_saved_output(const int & out)
fnconst

for_each_owned_tensor

void for_each_owned_tensor(const int & fn)

The inputs kept for graph-mode backward; the output is held weakly.

fn

keep_input

void keep_input(int i, int s)

Keep s as input i's values, or the output's, for apply. The slot is recorded as kept whatever its size: an eager pass reads it even for a tensor of no elements — on Metal as the GPU buffer a release replaces — so a released one must be refused, as the reference refuses it.

fn

keep_inputs

void keep_inputs(int s)

Every input's values at once.

fn

keep_needed_input

void keep_needed_input(int i, const int & impl, const int & storage_src, bool saves)

Keep input i for backward when saves is false (recording only impl for graph-mode metadata) or when some needed gradient reads input i (saving both storage_src->storage() and impl).

fnconst

name

→std::string_view
int name()

Human-readable name of the op, sourced from Derived::schema_v1.

Returns

std::string_view

The schema name (e.g. "linear", "matmul"). Used in error messages, profiler traces, and create_graph failure reports.

fnconst

needs_input_grad

bool needs_input_grad(int i)

Whether input i has a valid outgoing autograd edge (requires grad).

fnconst

read_by_a_needed_gradient

bool read_by_a_needed_gradient(int j)

Whether some input that takes a gradient has one reading input j.

fnconst

saved_or_zeros

int saved_or_zeros(int i, const int & grad_out)

Return saved_inputs_[i] when it was kept, or a zero-filled storage of input_shapes_[i] when no needed gradient reads input i (used by fused multi-output backward kernels whose unneeded output slots are discarded).