PyTorch
在PyTorch中,限制GPU内存的使用量可以通过torch.cuda.set_per_process_memory_fraction
函数来设置。
import torch # 设置GPU内存使用的最大比例(例如50%) torch.cuda.set_per_process_memory_fraction(0.5, device=torch.device('cuda:0')) # 或者,限制每个GPU设备的最大内存量(例如2GB) torch.cuda.set_per_process_memory_fraction(2 * 1024 * 1024 * 1024, device=torch.device('cuda:0'))