Thanks. I want to make sure my understanding is correct.
1.
In cycle, the first loss calculate by loss = f(x), but the following loss calculate by
update = optimizer(x.grad)
x = x + update
loss = f(x)

Every tensor keeps a version counter. When exec loss.backward, the Function save new version counter of the tensor, and check in backward.
when exec loss.backward, calculate the grad from bottom, so x.grad change. but not yet calculate the grad about -w*grad, so the program report error.




In cycle, autograd records a new graph every time. If I change loss.backward(torch.ones_like(loss), retain_graph=True) to loss.backward(torch.ones_like(loss), retain_graph=False), the program not report error in cycle. But sum_losses rely on all subgraph that recorded in cycle. So sum_losses.backward()  will report error.