Since it doesn’t look as if you want to calculate the gradients at some point, you could wrap the code into with torch.no_grad() to avoid storing the intermediate tensors, which would be needed for the gradient calculations.
If that doesn’t help, check if you are storing some output, loss, or any other tensors, which might be attached to the computation graph in another part of the code.