This works very well, thank you, but I have another follow-up question.
suppose now that y is not a vector (n), but a matrix (n,n). So now I need to calculate:
for k in range(n):
  for l in range(n):
    x.grad = None
    y[k,l].backward(retain_graph=True)
    dydx[k, l, :, :] = x.grad.clone()

It works, but it’s running really slow, even for a relatively low n (=80). I guess it’s because it is re-calculating the whole computation graph for every element k,l ?
Is there any way to make it faster?