Skip to content
fn

adopt_graph_position

bool adopt_graph_position(const int & a, const int & out)

Move out's place in the autograd graph onto a.

fwd_fn builds a differentiable out whose grad_fn knows how to undo this op. Taking only its storage kept the new numbers and threw the derivative away, so a still sat where it was before the call and reported the gradient of whatever produced it:

y = x * 1.0; y.exp_(); y.sum().backward() -> dx = 1 reference -> dx = exp(x)

Silent, in both families: the value was right and only the derivative was not, so a model using one trained on a wrong gradient with nothing to show for it. The views of a read the values it now holds, so they move with it.

The version moves too, as for any write. A node that saved a before the write holds a tensor whose values and grad_fn are no longer the ones it saw; with the count left alone nothing told it so. Eager backward of an engine node got away with that, reading the Storage it had saved, but graph-mode backward read the written values, and a custom Function reads its saved tensors as they are, with the count the only sign of the write: y = Fn.apply(x); y.mul_(2) gave the gradient of the written y on both devices. The op's own node is not among those that refuse: it was handed a snapshot (snapshot), a tensor of its own whose count no write to a reaches.

The hooks registered on a stay on its old slot, as the reference's do: they were asked about the values before the write. retain_grad follows the tensor to its new slot.