RuntimeError: CUDA error:unknown error in async mode

egor.chil.1999: <p>I’m trying to train a segmentation model. After I’d changed dataset size from 1500 to 4500 images I started to get such an error:</p>
<pre><code class="lang-auto">RuntimeError: CUDA error: unknown error
CUDA kernel errors might be asynchronously reported at some other API call, so the stacktrace below might be incorrect.
For debugging consider passing CUDA_LAUNCH_BLOCKING=1.
Compile with `TORCH_USE_CUDA_DSA` to enable device-side assertions.
</code></pre>
<p>It points to line with loss (loss is just pytorch mse):<br>
<code>loss_sum += loss.item()</code></p>
<p>Training code wasn’t changed at all. I’m using batch_size 32 with 512x512x3 images (I have 24GB gpu so it’s not a problem with OOM). Also I have set pin_memory to True in my dataloader.<br>
Interesting thing: if I set CUDA_LAUNCH_BLOCKING=1 I don’t have any errors. Training becames 10-20% slower but it goes well.<br>
Can you help me please? It looks like a problem with async cuda mode</p>

ptrblck: <p>Could you post a minimal and executable code snippet reproducing the issue, please?</p>

egor.chil.1999: <p>It’s extremely hard to catch. Using pin_memory=True it can crash after 2h of training, setting it to False can delay crash to 10h.<br>
I hadn’t faced this problem when I had smaller dataset. It looks like magic for me</p>
<p>Simplified code:</p>
<pre><code class="lang-auto">import torch
import torchvision

def image_to_tensor(x):
    return torchvision.transforms.ToTensor()(x)

class MyDataset(torch.utils.data.Dataset):
    def __init__(self, images, masks):
        self.images = images
        self.masks = masks

    def __len__(self):
        return len(self.images)

    def __getitem__(self, idx):
        return image_to_tensor(self.images[idx]), image_to_tensor(self.masks[idx])

class MseLoss(torch.nn.Module):
    def forward(self, input, target):
        return torch.mean(torch.nn.functional.mse_loss(input, target))
 
ds = MyDataset(images=..., masks=...)
dl = torch.utils.data.DataLoader(ds, batch_size=32, num_workers=8, shuffle=True, pin_memory=True)

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
net = Unet().to(device)
loss_fn = MseLoss().to(device)
optimizer = torch.optim.Adam(net.parameters(), lr=1e-2)

for epoch in range(300):
    net.train()
    loss_sum = 0
    for image, mask in dl:
        image, mask = image.to(device), mask.to(device)
        optimizer.zero_grad()
        pred = net(image)
        loss = loss_fn(pred, mask)
        loss.backward()
        optimizer.step()
        loss_sum += loss.item()

    loss_avg = loss_sum / len(dl)
    print(f'epoch {epoch + 1} loss is {loss_avg}')
</code></pre>

egor.chil.1999: <p>I’ve got crash even with CUDA_LAUNCH_BLOCKING=1:</p>
<pre><code class="lang-auto">terminate called after throwing an instance of 'c10::Error'
  what():  CUDA error: unknown error
Compile with `TORCH_USE_CUDA_DSA` to enable device-side assertions.
Exception raised from c10_cuda_check_implementation at ../c10/cuda/CUDAException.cpp:44 (most recent call first):
frame #0: c10::Error::Error(c10::SourceLocation, std::string) + 0x57 (0x7adddd792617 in /home/qew/.local/lib/python3.10/site-packages/torch/lib/libc10.so)
frame #1: c10::detail::torchCheckFail(char const*, char const*, unsigned int, std::string const&amp;) + 0x64 (0x7adddd74d98d in /home/qew/.local/lib/python3.10/site-packages/torch/lib/libc10.so)
frame #2: c10::cuda::c10_cuda_check_implementation(int, char const*, char const*, int, bool) + 0x118 (0x7adde01cb128 in /home/qew/.local/lib/python3.10/site-packages/torch/lib/libc10_cuda.so)
frame #3: &lt;unknown function&gt; + 0xe2439d (0x7add5c02439d in /home/qew/.local/lib/python3.10/site-packages/torch/lib/libtorch_cuda.so)
frame #4: &lt;unknown function&gt; + 0x510cf6 (0x7add9e110cf6 in /home/qew/.local/lib/python3.10/site-packages/torch/lib/libtorch_python.so)
frame #5: &lt;unknown function&gt; + 0x55ca7 (0x7adddd777ca7 in /home/qew/.local/lib/python3.10/site-packages/torch/lib/libc10.so)
frame #6: c10::TensorImpl::~TensorImpl() + 0x1e3 (0x7adddd76fcb3 in /home/qew/.local/lib/python3.10/site-packages/torch/lib/libc10.so)
frame #7: c10::TensorImpl::~TensorImpl() + 0x9 (0x7adddd76fe49 in /home/qew/.local/lib/python3.10/site-packages/torch/lib/libc10.so)
frame #8: &lt;unknown function&gt; + 0x7c0dc8 (0x7add9e3c0dc8 in /home/qew/.local/lib/python3.10/site-packages/torch/lib/libtorch_python.so)
frame #9: THPVariable_subclass_dealloc(_object*) + 0x305 (0x7add9e3c1155 in /home/qew/.local/lib/python3.10/site-packages/torch/lib/libtorch_python.so)
&lt;omitting python frames&gt;
frame #27: &lt;unknown function&gt; + 0x29d90 (0x7ade23429d90 in /lib/x86_64-linux-gnu/libc.so.6)
frame #28: __libc_start_main + 0x80 (0x7ade23429e40 in /lib/x86_64-linux-gnu/libc.so.6)
</code></pre>

ptrblck: <aside class="quote no-group" data-username="egor.chil.1999" data-post="3" data-topic="206943">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/egor.chil.1999/48/65032_2.png" class="avatar"> egor.chil.1999:</div>
<blockquote>
<p>It’s extremely hard to catch. Using pin_memory=True it can crash after 2h of training, setting it to False can delay crash to 10h.<br>
I hadn’t faced this problem when I had smaller dataset.</p>
</blockquote>
</aside>
<p>Is the GPU still usable afterwards or is your system not able to communicate with it anymore?<br>
Also, did you see any <code>Xid</code>s in <code>dmesg</code>?<br>
I doubt it’s a PyTorch error at this point as it sounds as if your system would drop the device. Given it also depends on the workload etc. it might be a good idea to check the thermals of your system to see if parts might be overheating.</p>

egor.chil.1999: <p>Yeah, after this my system isn’t able to communicate with GPU, so I need to reboot it.<br>
Temperature is less than 70deg during all the training, it’s totally ok.<br>
I have realised that my pytorch compiled with cuda 12.1 but I use cuda 12.0. Can it be the reason of the problem?</p>

ptrblck: <p>No, since your locally installed CUDA toolkit won’t be used as the PyTorch binaries ship with their own CUDA runtime dependencies. Even if you are mixing different CUDA libs, it won’t throw your GPU off, but raise a SW error in case of missing symbols etc.</p>

egor.chil.1999: <p>Got it. No ideas what’s wrong with it</p>

ptrblck: <p>Did you already check <code>dmesg</code> for <code>Xid</code>s?</p>

egor.chil.1999: <p>Could you please describe what is it?</p>

ptrblck: <p>A detailed description of Xid Errors can be found <a href="https://docs.nvidia.com/deploy/xid-errors/index.html">here</a>.<br>
TL;DR:</p>
<blockquote>
<p>The Xid message is an error report from the NVIDIA driver that is printed to the operating system’s kernel log or event log. Xid messages indicate that a general GPU error occurred, most often due to the driver programming the GPU incorrectly or to corruption of the commands sent to the GPU. The messages can be indicative of a hardware problem, an NVIDIA software problem, or a user application problem.<br>
These messages provide diagnostic information that can be used by both users and NVIDIA to aid in debugging reported problems.</p>
</blockquote>
<p>You can thus just run <code>dmesg | grep -i xid</code> and check if the driver or another part of your setup is running into an issue. User application errors, e.g. memory violations, are also logged there and you can check the linked docs for a description of the corresponding error (in case you are seeing Xid errors in your log).</p>

egor.chil.1999: <p>Seems to be very helpful. I will check it asap and give you a feedback. Thanks!</p>

egor.chil.1999: <p>Hi! I’ve found such an error after yet another crash:<br>
<code>[23924.128134] NVRM: Xid (PCI:0000:03:00): 79, pid='&lt;unknown&gt;', name=&lt;unknown&gt;, GPU has fallen off the bus.</code></p>

ptrblck: <p>This error often indicates a bios, power supply, or thermal issue so check if e.g. your PSU is big enough.</p>

egor.chil.1999: <p>Yeah, looks like the truth. Have already ordered more powerful block <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>