Updating a leaf that autograd is watching
A leaf with requires_grad is a parameter in autograd's eyes, and mutating it mid-graph would corrupt the tape. Updates belong outside recording, under torch.no_grad(); that context is exactly how optimizer.step applies yours.
import torch
x = torch.ones(3, requires_grad=True)
x += 1 # in-place on the leaf the optimizer would ownimport torch
x = torch.ones(3, requires_grad=True)
with torch.no_grad():
x += 1 # what optimizer.step does under the hood