Hi,
Few notes to improve your code:

If you set create_graph=True, you don’t need to pass the retain_graph arg.
When playing with higher derivatives, we recommend using autograd.grad instead of backward. That way you won’t have to play with the .grad fields and potentatially making mistakes. (in particular in your code, a bunch of .grad reset are commented out which might lead to unexpected behavior when you compute v_xx for example). You can do v_x = autograd.grad(v, x, torch.ones((batch_size, 1)), create_graph=True)[0].

Does that solve your issue?