MehdiZouitine:

I suspect that by adding the output to the buffer I am also adding all the computational graph.


Yes, you are storing the complete computation graphs in the list, if you are not detaching the tensors.



 MehdiZouitine:

Do you have any solutions?


It depends on your use case. If you need to store the computation graphs to call backward later, then you could reduce the number of iterations. Alternatively, if you don’t need to compute the gradients, you could store the tensors after calling detach() on them.