Ok thanks… I’m still confused about what’s going wrong then. Here’s a minimal example:

import torch
linear = torch.nn.Linear(10, 20)
x = torch.randn(1, 10)
L = linear(x).sum()
grad = torch.autograd.grad(L, linear.parameters(), create_graph=True)
gnorm = 0
for g in grad:
gnorm = gnorm + g.pow(2).sum()
gnorm.backward()

I could replace the last line with

grad2 = torch.autograd.grad(gnorm, linear.parameters(), create_graph=True)

and get the same error