Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pytorch error: Could not run 'aten::slow_conv3d_forward' with arguments from the 'CUDATensorId' backend

I am training a CNN on CUDA GPU which takes 3D medical images as input and outputs a classifier. I suspect there may be a bug in pytorch. I am running pytorch 1.4.0. The GPU is 'Tesla P100-PCIE-16GB'. When I run the model on CUDA I get the error

Traceback (most recent call last):
  File "/home/ub/miniconda3/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3331, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-55-cc0dd3d9cbb7>", line 1, in <module>
    net(cc)
  File "/home/ub/miniconda3/lib/python3.7/site-packages/torch/nn/modules/module.py", line 532, in __call__
    result = self.forward(*input, **kwargs)
  File "<ipython-input-2-19e11966d1cd>", line 181, in forward
    out = self.layer1(x)
  File "/home/ub/miniconda3/lib/python3.7/site-packages/torch/nn/modules/module.py", line 532, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/ub/miniconda3/lib/python3.7/site-packages/torch/nn/modules/container.py", line 100, in forward
    input = module(input)
  File "/home/ub/miniconda3/lib/python3.7/site-packages/torch/nn/modules/module.py", line 532, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/ub/miniconda3/lib/python3.7/site-packages/torch/nn/modules/conv.py", line 480, in forward
    self.padding, self.dilation, self.groups)
RuntimeError: Could not run 'aten::slow_conv3d_forward' with arguments from the 'CUDATensorId' backend. 'aten::slow_conv3d_forward' is only available for these backends: [CPUTensorId, VariableTensorId].

To replicate the issue:

#input is a 64,64,64 3d image batch with 2 channels
class ConvNet(nn.Module):
    def __init__(self):
        super(ConvNet, self).__init__()
        self.layer1 = nn.Sequential(
            nn.Conv3d(2, 32, kernel_size=5, stride=1, padding=2),
            nn.ReLU(),
            nn.MaxPool3d(kernel_size=2, stride=2))
        self.layer2 = nn.Sequential(
            nn.Conv3d(32, 64, kernel_size=5, stride=1, padding=2),
            nn.ReLU(),
            nn.MaxPool3d(kernel_size=2, stride=2))
        self.drop_out = nn.Dropout()
        self.fc1 = nn.Linear(16 * 16*16 * 64, 1000)
        self.fc2 = nn.Linear(1000, 2)
        # self.softmax =  nn.LogSoftmax(dim=1)

    def forward(self, x):
        # print(out.shape)
        out = self.layer1(x)
        # print(out.shape)
        out = self.layer2(out)
        # print(out.shape)
        out = out.reshape(out.size(0), -1)
        # print(out.shape)
        out = self.drop_out(out)
        # print(out.shape)
        out = self.fc1(out)
        # print(out.shape)
        out = self.fc2(out)
        # out = self.softmax(out)
        # print(out.shape)
        return out


net = Convnet()
input = torch.randn(16, 2, 64, 64, 64)
net(input)
like image 679
Maelstorm Avatar asked Mar 06 '20 11:03

Maelstorm


1 Answers

Initially, I was thinking the error message indicates that 'aten::slow_conv3d_forward' is not implemented with GPU (CUDA). But after looked at your network, it does not make sense to me, since Conv3D is a very basic op, and Pytorch team should implement this in CUDA.

Then I dived a bit about the source code, finding that the input is not a CUDA tensor, which causes the problem.

Here is a working sample:

import torch
from torch import nn

#input is a 64,64,64 3d image batch with 2 channels
class ConvNet(nn.Module):
    def __init__(self):
        super(ConvNet, self).__init__()
        self.layer1 = nn.Sequential(
            nn.Conv3d(2, 32, kernel_size=5, stride=1, padding=2),
            nn.ReLU(),
            nn.MaxPool3d(kernel_size=2, stride=2))
        self.layer2 = nn.Sequential(
            nn.Conv3d(32, 64, kernel_size=5, stride=1, padding=2),
            nn.ReLU(),
            nn.MaxPool3d(kernel_size=2, stride=2))
        self.drop_out = nn.Dropout()
        self.fc1 = nn.Linear(16 * 16*16 * 64, 1000)
        self.fc2 = nn.Linear(1000, 2)
        # self.softmax =  nn.LogSoftmax(dim=1)

    def forward(self, x):
        # print(out.shape)
        out = self.layer1(x)
        # print(out.shape)
        out = self.layer2(out)
        # print(out.shape)
        out = out.reshape(out.size(0), -1)
        # print(out.shape)
        out = self.drop_out(out)
        # print(out.shape)
        out = self.fc1(out)
        # print(out.shape)
        out = self.fc2(out)
        # out = self.softmax(out)
        # print(out.shape)
        return out


net = ConvNet()
input = torch.randn(16, 2, 64, 64, 64)
net.cuda()
input = input.cuda() # IMPORTANT to reassign your tensor
net(input)

Remember when you put a model from CPU to GPU, you can directly call .cuda(), but if you put a tensor from CPU to GPU, you will need to reassign it, such as tensor = tensor.cuda(), instead of only calling tensor.cuda(). Hope that helps.

Output:

tensor([[-0.1588,  0.0680],
        [ 0.1514,  0.2078],
        [-0.2272, -0.2835],
        [-0.1105,  0.0585],
        [-0.2300,  0.2517],
        [-0.2497, -0.1019],
        [ 0.1357, -0.0475],
        [-0.0341, -0.3267],
        [-0.0207, -0.0451],
        [-0.4821, -0.0107],
        [-0.1779,  0.1247],
        [ 0.1281,  0.1830],
        [-0.0595, -0.1259],
        [-0.0545,  0.1838],
        [-0.0033, -0.1353],
        [ 0.0098, -0.0957]], device='cuda:0', grad_fn=<AddmmBackward>)

like image 185
Xinyao Wang Avatar answered Sep 25 '22 20:09

Xinyao Wang