Skip to content
fn

retain_grad

void retain_grad(const int & t)

Keep t's gradient in its .grad during backward, even though it is not a leaf. Sets the flag and, for a non-leaf, registers t at its producer's output slot — called again after an in-place op moves t to a new one. A leaf keeps its gradient anyway; only the flag changes.