gpt4 book ai didi

out-of-memory - 如何在 PyTorch 中节省 GPU 内存使用量

转载 作者:行者123 更新时间:2023-12-04 13:40:27 26 4
gpt4 key购买 nike

在 PyTorch 中,我编写了一个非常简单的 CNN 判别器并对其进行了训练。现在我需要部署它来进行预测。但是目标机器的GPU内存很小并且出现内存不足错误。所以我想我可以设置requires_grad = False防止 PyTorch 存储梯度值。但是我没有发现它有什么不同。

我的模型中有大约 500 万个参数。但是在预测单批输入时,它会消耗大约 1.2GB 的内存。我认为应该不需要这么大的内存。

问题是当我只想使用我的模型进行预测时,如何节省 GPU 内存使用量?

这是一个演示,我使用 discriminator.requires_grad_禁用/启用所有参数的自动分级。不过好像没什么用。

import torch
import numpy as np
import torch.nn as nn
import torch.nn.functional as functional

from pynvml.smi import nvidia_smi
nvsmi = nvidia_smi.getInstance()

def getMemoryUsage():
usage = nvsmi.DeviceQuery("memory.used")["gpu"][0]["fb_memory_usage"]
return "%d %s" % (usage["used"], usage["unit"])

print("Before GPU Memory: %s" % getMemoryUsage())

class Discriminator(nn.Module):
def __init__(self):
super().__init__()
# trainable layers
# input: 2x256x256
self.conv1 = nn.Conv2d(2, 8, 5, padding=2) # 8x256x256
self.pool1 = nn.MaxPool2d(2) # 8x128x128
self.conv2 = nn.Conv2d(8, 32, 5, padding=2) # 32x128x128
self.pool2 = nn.MaxPool2d(2) # 32x64x64
self.conv3 = nn.Conv2d(32, 96, 5, padding=2) # 96x64x64
self.pool3 = nn.MaxPool2d(4) # 96x16x16
self.conv4 = nn.Conv2d(96, 256, 5, padding=2) # 256x16x16
self.pool4 = nn.MaxPool2d(4) # 256x4x4
self.num_flat_features = 4096
self.fc1 = nn.Linear(4096, 1024)
self.fc2 = nn.Linear(1024, 256)
self.fc3 = nn.Linear(256, 1)
# loss function
self.loss = nn.MSELoss()
# other properties
self.requires_grad = True
def forward(self, x):
y = x
y = self.conv1(y)
y = self.pool1(y)
y = functional.relu(y)
y = self.conv2(y)
y = self.pool2(y)
y = functional.relu(y)
y = self.conv3(y)
y = self.pool3(y)
y = functional.relu(y)
y = self.conv4(y)
y = self.pool4(y)
y = functional.relu(y)
y = y.view((-1,self.num_flat_features))
y = self.fc1(y)
y = functional.relu(y)
y = self.fc2(y)
y = functional.relu(y)
y = self.fc3(y)
y = torch.sigmoid(y)
return y
def predict(self, x, score_th=0.5):
if len(x.shape) == 3:
singlebatch = True
x = x.view([1]+list(x.shape))
else:
singlebatch = False
y = self.forward(x)
label = (y > float(score_th))
if singlebatch:
y = y.view(list(y.shape)[1:])
return label, y
def requires_grad_(self, requires_grad=True):
for parameter in self.parameters():
parameter.requires_grad_(requires_grad)
self.requires_grad = requires_grad


x = torch.cuda.FloatTensor(np.zeros([2, 256, 256]))
discriminator = Discriminator()
discriminator.to("cuda:0")

# comment/uncomment this line to make difference
discriminator.requires_grad_(False)

discriminator.predict(x)

print("Requires grad", discriminator.requires_grad)
print("After GPU Memory: %s" % getMemoryUsage())

通过注释掉 discriminator.requires_grad_(False) ,我得到了输出:
Before GPU Memory: 6350MiB
Requires grad True
After GPU Memory: 7547MiB

通过取消注释该行,我得到了:
Before GPU Memory: 6350MiB
Requires grad False
After GPU Memory: 7543MiB

最佳答案

您可以使用 pynvml .

这个 Python 工具使 Nvidia 成为可能,因此您可以像这样进行 Python 查询:

from pynvml.smi import nvidia_smi
nvsmi = nvidia_smi.getInstance()
nvsmi.DeviceQuery('memory.free, memory.total')

您也可以随时执行:
torch.cuda.empty_cache()

清空缓存,你会发现更多的空闲内存。

调用前 torch.cuda.empty_cache()如果你有不再使用的对象,你可以调用它:
obj = None

之后你打电话
gc.collect()

关于out-of-memory - 如何在 PyTorch 中节省 GPU 内存使用量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57942507/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com