【发布时间】:2021-01-22 01:20:53
【问题描述】:
我相信在使用 GPU 时每批次的推理时间与批次大小无关,但这个最小的例子告诉我这似乎不正确:
import torch
from torch import nn
from tqdm import tqdm
BATCH_SIZE = 32
N_ITER = 10000
class NN(nn.Module):
def __init__(self):
super(NN, self).__init__()
self.layer = nn.Conv2d(3, 32, kernel_size=5, stride=1, padding=3, bias=False)
def forward(self, input):
out = self.layer(input)
return out
cnn = NN().cuda()
cnn.eval()
tensor = torch.rand(BATCH_SIZE, 3, 999, 999).cuda()
with torch.no_grad():
for _ in tqdm(range(N_ITER), mininterval=0.1):
out = cnn(tensor)
增加BATCH_SIZE时,tqdm显示的“it/s”按比例增加:
Plot of inference time vs batch size
我相信 GPU 可以同时处理整个张量,只要它不使用所有内存。也许我不太了解 GPU 是如何并行处理数据的,所以在这里我希望能得到一些见解。
我使用的是 NVIDIA GeForce 2080 Ti、pytorch 1.6.0 和 CUDA 10.2。
【问题讨论】: