Skip to content
fn

refuse_on_leaf

void refuse_on_leaf(const int & a, const char * name)

Refuse to mutate a leaf that requires grad.

A leaf is where gradient accumulates, and an in-place write moves it without leaving anything behind that says so. Nor is there a right number to return: after the write x is the result, so "the gradient with respect to x" names two different tensors depending on when it is asked. The reference refuses the same setup for the same reason. Callers that mean to overwrite say so with no_grad, which is what every optimiser step already does.

is_leaf(), not !grad_fn(): a leaf that has taken part in a forward carries its gradient accumulator as grad_fn, so the old test stopped refusing the moment a parameter was first used — every in-place op on a trained parameter then went through, and the ones that rebind turned the parameter into a non-leaf that never received .grad.