RuntimeError: CUDNN_STATUS_MAPPING_ERROR

zeng: <p>When I use the code like</p>
<pre><code class="lang-auto">class Classify(nn.Module):
    def __init__(self, opt):
        self.convs = [nn.Conv2d(1, opt.kernel_num, (k, opt.word_vec_size), padding=0) for k in opt.kernel_sizes]

    def forward(self, x):
        x = [F.relu(conv(x)).squeeze(3) for conv in self.convs]
</code></pre>
<p>I got the problem “<strong>RuntimeError: CUDNN_STATUS_MAPPING_ERROR</strong>”, and I simple use the single <strong><em>Conv2d</em></strong> function is alright.</p>
<p>Does this kind of <strong><em>model list</em> ([ conv2d, conv2d, …])</strong> have some problem with the cnn mapping?</p>

xinwei_he: <p>try below:</p>
<pre><code class="lang-auto">self.convs= nn.ModuleList([nn.Conv2d(1, opt.kernel_num, (k, opt.word_vec_size), padding=0) for k in opt.kernel_sizes]
)
</code></pre>

McLawrence: <p>I get the same error message. My network has the following architecture:</p>
<pre><code>class CNN(nn.Module):
def __init__(self, num_classes=10):
    super(CNN, self).__init__()
    self.conv1 = nn.Conv2d(3, 32, 3, padding=1)
    self.conv2 = nn.Conv2d(32, 64, 3, padding=1)
    self.conv3 = nn.Conv2d(64, 128, 3, padding=1)
    self.pool = nn.MaxPool2d(2)
    self.classifier = nn.Sequential(
        nn.Linear(128*8*8 , 1024),
        nn.ReLU(True),
        nn.Dropout(),
        nn.Linear(1024, 1024),
        nn.ReLU(True),
        nn.Dropout(),
        nn.Linear(1024, num_classes)
    )

def forward(self, x):
    x = F.relu(self.conv1(x))
    x = self.pool(F.relu(self.conv2(x)))
    x = self.pool(F.relu(self.conv3(x)))
    x = x.view(x.size(0), -1)
    x = self.classifier(x)
    return x
</code></pre>
<p>The model works fine on CPU. I am using pytorch 0.1.12_2</p>

McLawrence: <p>I have found my mistake: I just did not know you have to do <code>net.cuda()</code>, too.</p>

yuzhi_guo: <p>I think the problem is that your GPU is too old to supported by PyTorch. I got the same error before and I notice that there was an UserWarning in the middle part of my log. It was solved after I changed my GPU from K420 to GTX1050Ti.</p>

Noosh_Nabi: <p>I faced the same problem and later on figured out the pytorch version I installed does not match CUDA version on my machine. I installed newer version of pytorch and it worked.<br>
CUDA version 9.2.88<br>
pytorch 0.4.1</p>

mmisiur: <p>Just leaving this for the sake of future readers:<br>
I got this error when I’ve overclocked my GPU too much so the driver broke. Which also fits previous answers. So if you overclock and got this error, consider lowering the clock.</p>

danpak94: <p>I have the same error, but I have a different issue than everyone else. Here’s a simple code that reproduces my issue:</p>
<pre><code class="lang-auto">import torch
import torch.nn as nn
import numpy as np

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()

        self.base_n_filter = 8

        self.conv1 = nn.Conv3d(1, self.base_n_filter, 3, stride=1, padding=1)
        self.conv2 = nn.Conv3d(self.base_n_filter, self.base_n_filter*2, 3, stride=1, padding=1)
        self.conv3 = nn.Conv3d(self.base_n_filter*2, 1, 3, stride=1, padding=1)

    def forward(self, img):
        output1 = self.conv1(img)
        output2 = self.conv2(output1)
        output3 = self.conv3(output2)

        return output3


if __name__ == '__main__':
    img = torch.zeros([1,1,248,248,140]).cuda()
    label = torch.rand_like(img, device=img.device)

    model = Net().cuda()
    output = model(img)

    loss_fn = nn.MSELoss()

    loss = loss_fn(output, label)
    loss.backward()
</code></pre>
<p>The error only occurs when I have an input image of a specific size. For example, if I change the input to shape [1,1,250,250,150], the error no longer occurs. So it doesn’t seem like an overclocking issue. Additionally, this error also only occurs when self.base_n_filter is 8 or greater. I’m not using any lists to store my convolutions, and have set my model to cuda.</p>
<p>I am also using up-to-date GPU and cuda. The error occurs on both Tesla K80 and GTX1080Ti, with pytorch 1.2 cudatoolkit=10.0, CUDA/10.0.130, and cudnn/7.6.2.24-CUDA-10.0.130. Also, this error only occurs on Linux machines.</p>
<p>Any help would be truly appreciated!</p>

BestJuly: <p>I met the same thing and I felt strange because I run my code successfully once. In my situation, I reduced the batch size and solved this error.</p>

danpak94: <p>In the code I posted, the batch size would be 1 so I don’t think that’s the problem. But it’s probably related to how only certain input shapes cause the error.</p>

BestJuly: <p>I think you are  right.<br>
When I run my codes, I found this error happened again and when I disabled cuDNN, the error disapeared. I think cuDNN may not so flexible for processing data in all shapes. That may be a bug in cuDNN. And as I posted, I found that sometimes using smaller batch_size can avoid this error. However, in these cases, the occupation percentage of GPU memory is very low.</p>
<p>My environment: cuda v10.0.130, cudnn v7.6.5, V100.</p>

Adam_Levin: <p>I got this error because I was doing accumulated gradients (because I want my effective batch size to be larger than what can fit on my gpu at any given time) and this message showed when it tried to backprop the loss… seems like just a memory issue in some cases</p>

thao: <p>Sometimes In my case, CUDA version is so high and torch version is old lead to this problem, you should upgrade to newer version.</p>

Saurabh_Kataria: <p>I also noticed some data shapes raise this cudnn error. I tried other data shape and it worked. For me, the error actually arose in batchnorm layer while forward pass.</p>

ptrblck: <p>Could you post the input shapes and the batchnorm config as well as the cudnn version and used GPU, which raised this error?</p>

Saurabh_Kataria: <p>I found out that the issue was something else. When <code>CUDA_LAUNCH_BLOCKING=1</code> flag is not used with python script, it showed incorrect error CUDNN_STATUS_MAPPING_ERROR (in my case, it said batchnorm failed). However, when the flag is used, I get “RuntimeError: CUDA error: device-side assert triggered” at line X. I manually checked that there was a index out of bounds problem at line X. Correcting it solved the problem.<br>
The real issue is that the code never tells that the problem is “index out of bounds”, but instead says CUDNN_STATUS_MAPPING_ERROR or “RuntimeError: CUDA error: device-side assert triggered”, depending on if you are using <code>CUDA_LAUNCH_BLOCKING=1</code> or not.</p>

ptrblck: <p>Yes, that’s unfortunately caused by the asynchronous CUDA kernel execution.<br>
Generally you should get a better error message by running the code on the CPU.<br>
If that’s not feasible (e.g. since it takes too long) or doesn’t create the error, you would have to rerun the code with <code>CUDA_LAUNCH_BLOCKING=1</code> to get the right line of code, which creates the error.</p>

krishnachaitanya7: <p>In my case, I had a try-catch block inside the forward function. The logic was like this: try to index the input; if the indexing was not possible, just use the array as it is. While it worked for my case, whenever the code went into the exception, after some lines of code it was throwing<br>
CUDNN_STATUS_MAPPING_ERROR. Just removed the try-catch block and there was no error. Just leaving it here in hope that it helps somebody.</p>

hundun0322: <p>I tried to use the cpu instead of gpu, found it could run with a slower speed. So I realised it must be related to the model. But as for me, the programme is based on machine vision.And when I use the ready img before, it runs smoothly. But failed when open the zed2 to capture the img then deal it. Then I upgrade the torch(1.6-1.7). Like the Metaphysics, bingo!</p>