May I ask in which case it will go into this else
branch, i.e., p.grad.grad_fn is None and p.grad.requires_grad = True
? I understand requires_grad
will be True
if grad_fn
is not None
, e.g., create_graph
turned on in autograd.grad
.
The source snippet is optim.optimizer.zero_grad.
Thanks.