You might have a memory leak if your code runs fine for a few epochs and then runs out of memory.
Could you run it again and have a look at nvidia-smi?
Do you see an increasing memory usage?
If so, would it be possible to post a (small) executable script so that we could have a look at your code?
Make sure to avoid storing the computation graph, e.g. by doing something like:
loss = criterion(output, target)
losses.append(loss)
# instead use
losses.append(loss.item())  # detaches the computation graph

PS: I’ve formatted your post for better readability. If you would like to add code or terminal outputs you can wrap it in three backticks ```