簡體   English   中英

解決 CUDA 錯誤:通過代碼修改內存不足

[英]Solving CUDA error: out of memory by code modification

在帶有 GPU 的服務器上運行此代碼時,我不斷收到以下錯誤:

RuntimeError: CUDA out of memory. Tried to allocate 10.99 GiB (GPU 0; 10.76 GiB                                                                                         total capacity; 707.86 MiB already allocated; 2.61 GiB free; 726.00 MiB reserved                                                                                         in total by PyTorch)

我添加了一個垃圾收集器。 我嘗試使批量大小非常小(從 10000 到 10),現在錯誤已更改為:

(main.py:2595652): Gdk-CRITICAL **: 11:16:04.013: gdk_cursor_new_for_display: assertion 'GDK_IS_DISPLAY (display)' failed
2022-06-07 11:16:05.909522: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library libcudart.so.11.0
Traceback (most recent call last):
  File "main.py", line 194, in <module>
    **psm = psm.cuda()**
  File "/usr/lib/python3/dist-packages/torch/nn/modules/module.py", line 637, in cuda
    return self._apply(lambda t: t.cuda(device))
  File "/usr/lib/python3/dist-packages/torch/nn/modules/module.py", line 530, in _apply
    module._apply(fn)
  File "/usr/lib/python3/dist-packages/torch/nn/modules/module.py", line 530, in _apply
    module._apply(fn)
  File "/usr/lib/python3/dist-packages/torch/nn/modules/module.py", line 552, in _apply
    param_applied = fn(param)
  File "/usr/lib/python3/dist-packages/torch/nn/modules/module.py", line 637, in <lambda>
    return self._apply(lambda t: t.cuda(device))
**RuntimeError: CUDA error: out of memory
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.

這是 PMS 的一部分。 我復制了它,因為錯誤行顯示psm = psm.cuda()

class PSM(nn.Module):
    def __init__(self, n_classes, k, fr, num_feat_map=64, p=0.3, shar_channels=3):
        super(PSM, self).__init__()
        self.shar_channels = shar_channels
        self.num_feat_map = num_feat_map
        self.encoder = Encoder(k, fr, num_feat_map, p, shar_channels)
        self.decoder = Decoder(n_classes, p)

    def __call__(self, x):
        return self.forward(x)

    def forward(self, x):
        encodes = []
        outputs = []
        for device in x:
            encode = self.encoder(device)
            outputs.append(self.decoder(encode.cuda()))
            encodes.append(encode)
        # Add shared channel
        shared_encode = torch.mean(torch.stack(encodes), 2).permute(1,0,2).cuda()
        outputs.append(self.decoder(shared_encode))
        return torch.mean(torch.stack(outputs), 0)

這對我有用:

nvidia -smi

我發現 GPU 不太忙。 然后將torch.cuda.set_device(1)添加到我的代碼中。 我還使用了減小的批量大小,否則它將不起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM