How about using torch.autograd.functional.hessian — PyTorch 1.9.0 documentation 1?
The error comes from the fact that loss does not depend on d1.
You probably want to do something like d2 = torch.autograd.grad(d1, model.parameters(), create_graph=True)[0] , but I haven’t tested whether that works.