It is pytorch fault for this. When you call .backward() pytorch deleted all the intermediate results, so as to preserve memory, which also results in the deletion of the computation graph. In your example you first created your computation graph, then you called lossfn.backward(), after that you don’t have the graph and so not gradients can flow.
To avoid this situation you should use retain_graph=True at the last step i.e. lossfn.backward(retain_graph=True), just remember to zero your gradients before that.