I don’t know what the methods are exactly doing, but it seems you might have introduced a dependency on the computation graph history from previous iterations.
Check, if any used tensor might have been created in a previous iteration (and would also still be attached to the graph). If this is indeed needed (and you want to backpropagate through multiple iterations), you should use retain_graph=True, if not, you should detach() this tensor before using it in the current iteration.