Your hidden carries the computation graph from last iteration. That’s why it errors without retain_graph.
Suppose that each iteration is supposed to train on an independent sample. Just always use hidden=None as your input to rnn.