I guess the error is raised by changing the default dtype before calling the backward method in:
     31     torch.set_default_dtype(torch.float64)
---> 32     loss.backward()

I assume this could create type mismatches, since the forward pass wouldn’t match the backward pass anymore.
Could you explain, why you are changing the default dtype globally during training?