The error is most likely raised, as you are not detaching the hidden state, keep the computation graph alive via retain_graph=True, and manipulate the parameters inplace via optimizer.step().
Are you sure you need to use retain_graph=True? If not, remove it and detach the hidden state via:
hidden = (hidden[0].detach(), hidden[1].detach())

before passing it to the model.