I am trying to apply F.conv2d() on a one-channel image with an average kernel. It takes too much time on the GPU. Can anyone explain? I finally use F.ave_pool2d(), but I am curious what’s wrong.
import torch
import torch.nn.functional as F
import time

# dummy image
img = torch.randn([1, 1, 512, 768])

# average kernel
aveKernel = torch.ones([1, 2, 2]) / 2 ** 2
aveKernel = aveKernel[:, None, :, :]

# one channel CPU
tic = time.time()
ave_img1 = F.conv2d(img, aveKernel, padding = 1, groups=1)
toc = time.time() - tic
print('CPU conv2d one channel time:', toc)

# one channel GPU
img = img.cuda()
aveKernel = aveKernel.cuda()
tic = time.time()
ave_img1 = F.conv2d(img, aveKernel, padding = 1, groups=1)
toc = time.time() - tic
print('GPU conv2d one channel time:', toc)

# Three channel GPU
img = torch.randn([1, 3, 512, 768])
aveKernel = torch.ones([3, 2, 2]) / 2 ** 2
aveKernel = aveKernel[:, None, :, :]

img = img.cuda()
aveKernel = aveKernel.cuda()
tic = time.time()
ave_img1 = F.conv2d(img, aveKernel, padding = 1, groups=3)
toc = time.time() - tic
print('GPU conv2d three channel time:', toc)

The output :
CPU conv2d one channel time: 0.0025970935821533203
GPU conv2d one channel time: 0.14447641372680664
GPU conv2d three channel time: 7.82012939453125e-05