GPU编程在本地测试运行python时,遇到显存不足的问题,具体报错如下图:
CUDA out of memory. Tried to allocate 1.36 GiB (GPU 0; 31.74 GiB total capacity;
538.78 MiB already allocated; 993.38 MiB free; 548.00 MiB reserved in total by PyTorch)
If reserved memory is >> allocated memory try setting max_split_size_mb to avoid
fragmentation. See documentation for Memory Management and PYTORCH_CUDA_ALLOC_CONF
出现的是 torch.cuda.OutOfMemoryError 错误,这意味着当前 GPU 显存不足,无法分配更多的内存来执行操作。
以下是可能的解决方案:
1. 减少批量大小(Batch Size)
减少模型中使用的批量大小可以显著减少 GPU 内存的占用。你可以调整批量大小的参数。
batch_size = 16 # 或更小
# 或者从输入数据读取size
batchsize = x.size(