Thank you for your answer! I indeed need the backpropagation through time. But even reducing the batch will only delay the rise of memory error. Also I already tried to use
for state in states:
                    state.detach()

It doest not change the out of memory error after 5-10 batches.