At the beginning of the script no memory should be allocated.
import torch
print(torch.cuda.memory_allocated())
> 0
print(torch.cuda.memory_cached())
> 0

In your score function you are storing the computation graphs of each forward pass in predicted.
This will most likely cause the out of memory issue.
The usual work flow would be to calculate the loss for each batch and perform an optimization step.
Could you explain your use case a bit and why you are storing predicted just to detach it afterwards?
If you don’t need to calculate a loss and call backward on it, you should detach pred after calculating it or wrap the loop in a with torch.no_grad() block.