Try to lower the batch size to a single sample. If the script still needs more memory, you might need to use torch.utils.checkpoint to trade compute for memory or you could use e.g. Google Colab, which provides GPUs with more than 4GB.