torch.Storage._expired

Hussein: <p>Hello</p>
<p>I got this error when using data parallel on four GPUs but the model runs fine on single gpu. Somehow the first two batches run fine on the four GPUs and after that I got this error.</p>
<pre><code class="lang-auto">File "/opt/conda/lib/python3.7/site-packages/tqdm/std.py", line 1195, in __iter__
    for obj in iterable:
  File "/opt/conda/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 652, in __next__
    data = self._next_data()
  File "/opt/conda/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 1330, in _next_data
    idx, data = self._get_data()
  File "/opt/conda/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 1296, in _get_data
    success, data = self._try_get_data()
  File "/opt/conda/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 1134, in _try_get_data
    data = self._data_queue.get(timeout=timeout)
  File "/opt/conda/lib/python3.7/multiprocessing/queues.py", line 113, in get
    return _ForkingPickler.loads(res)
  File "/opt/conda/lib/python3.7/site-packages/torch/multiprocessing/reductions.py", line 303, in rebuild_storage_fd
    shared_cache[fd_id(fd)] = StorageWeakRef(storage)
  File "/opt/conda/lib/python3.7/site-packages/torch/multiprocessing/reductions.py", line 65, in __setitem__
    self.free_dead_references()
  File "/opt/conda/lib/python3.7/site-packages/torch/multiprocessing/reductions.py", line 70, in free_dead_references
    if storage_ref.expired():
  File "/opt/conda/lib/python3.7/site-packages/torch/multiprocessing/reductions.py", line 35, in expired
    return torch.Storage._expired(self.cdata)  # type: ignore[attr-defined]
  File "/opt/conda/lib/python3.7/site-packages/torch/storage.py", line 753, in _expired
    return eval(cls.__module__)._UntypedStorage._expired(*args, **kwargs)
AttributeError: module 'torch.cuda' has no attribute '_UntypedStorage'

</code></pre>
<p>tried multiple times and the error keeps showing. Thank you for your help.</p>

Hussein: <p>The problem was solved by using distributed data parallel. In case anyone facing the same error.</p>

lnsydengyuang: <p>Can you describe in more detail how you solved this problem, I also encountered the same problem.</p>

Hussein: <p>Hello <a class="mention" href="/u/lnsydengyuang">@lnsydengyuang</a></p>
<p>When using data parallel to train the model on more than one GPU, this error appears.<br>
I followed PyTorch recommendation by using distributed data parallel instead of data parallel. Please follow the following tutorials to implement  distributed data parallel.</p>
<aside class="onebox allowlistedgeneric" data-onebox-src="https://yangkky.github.io/2019/07/08/distributed-pytorch-tutorial.html">
  <header class="source">

      <a href="https://yangkky.github.io/2019/07/08/distributed-pytorch-tutorial.html" target="_blank" rel="noopener nofollow ugc">yangkky.github.io</a>
  </header>

  <article class="onebox-body">
    

<h3><a href="https://yangkky.github.io/2019/07/08/distributed-pytorch-tutorial.html" target="_blank" rel="noopener nofollow ugc">Distributed data parallel training in Pytorch</a></h3>

  <p>Edited 18 Oct 2019: we need to set the random seed in each process so that the models are initialized with the same weights. Thanks to the anonymous emailer ...</p>


  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<aside class="onebox githubblob" data-onebox-src="https://github.com/pytorch/examples/blob/fe8abc3c810420df2856c6e668258f396b154cee/imagenet/main.py#L208">
  <header class="source">

      <a href="https://github.com/pytorch/examples/blob/fe8abc3c810420df2856c6e668258f396b154cee/imagenet/main.py#L208" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://github.com/pytorch/examples/blob/fe8abc3c810420df2856c6e668258f396b154cee/imagenet/main.py#L208" target="_blank" rel="noopener nofollow ugc">pytorch/examples/blob/fe8abc3c810420df2856c6e668258f396b154cee/imagenet/main.py#L208</a></h4>



    <pre class="onebox"><code class="lang-py">
      <ol class="start lines" start="198" style="counter-reset: li-counter 197 ;">
          <li>train_dataset = datasets.ImageFolder(</li>
          <li>    traindir,</li>
          <li>    transforms.Compose([</li>
          <li>        transforms.RandomResizedCrop(224),</li>
          <li>        transforms.RandomHorizontalFlip(),</li>
          <li>        transforms.ToTensor(),</li>
          <li>        normalize,</li>
          <li>    ]))</li>
          <li>
          </li>
<li>if args.distributed:</li>
          <li class="selected">    train_sampler = torch.utils.data.distributed.DistributedSampler(train_dataset)</li>
          <li>else:</li>
          <li>    train_sampler = None</li>
          <li>
          </li>
<li>train_loader = torch.utils.data.DataLoader(</li>
          <li>    train_dataset, batch_size=args.batch_size, shuffle=(train_sampler is None),</li>
          <li>    num_workers=args.workers, pin_memory=True, sampler=train_sampler)</li>
          <li>
          </li>
<li>val_loader = torch.utils.data.DataLoader(</li>
          <li>    datasets.ImageFolder(valdir, transforms.Compose([</li>
          <li>        transforms.Resize(256),</li>
      </ol>
    </code></pre>



  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>I hope this helps.</p>

Xelif: <p>I am actually getting the same error and I can’t see how to use</p>
<pre><code class="lang-auto">if args.distributed:
    train_sampler = torch.utils.data.distributed.DistributedSampler(train_dataset)
else:
    train_sampler = None
</code></pre>
<p>in my case since I am not defining any instance of <code>DistributedSampler</code> in my code.<br>
I am training on google colab with torch 1.12.0+cu113</p>
<p>Might my error be connected to using <code>torch.multiprocessing.set_start_method('spawn', force=True)</code>?<br>
My error is:</p>
<pre><code class="lang-auto">---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
&lt;ipython-input-15-cba3cc81036f&gt; in &lt;module&gt;()
     38     false_positive = 0
     39     # iterate for every batch in data loader
---&gt; 40     for images, label_grids, _, maximum_anchor_grid_gt in train_loader:
     41         images, label_grids, maximum_anchor_grid_gt = images.to(device), label_grids.to(device), maximum_anchor_grid_gt.to(device)
     42         # forward and loss

9 frames
/usr/local/lib/python3.7/dist-packages/torch/utils/data/dataloader.py in __next__(self)
    650                 # TODO(https://github.com/pytorch/pytorch/issues/76750)
    651                 self._reset()  # type: ignore[call-arg]
--&gt; 652             data = self._next_data()
    653             self._num_yielded += 1
    654             if self._dataset_kind == _DatasetKind.Iterable and \

/usr/local/lib/python3.7/dist-packages/torch/utils/data/dataloader.py in _next_data(self)
   1328 
   1329             assert not self._shutdown and self._tasks_outstanding &gt; 0
-&gt; 1330             idx, data = self._get_data()
   1331             self._tasks_outstanding -= 1
   1332             if self._dataset_kind == _DatasetKind.Iterable:

/usr/local/lib/python3.7/dist-packages/torch/utils/data/dataloader.py in _get_data(self)
   1294         else:
   1295             while True:
-&gt; 1296                 success, data = self._try_get_data()
   1297                 if success:
   1298                     return data

/usr/local/lib/python3.7/dist-packages/torch/utils/data/dataloader.py in _try_get_data(self, timeout)
   1132         #   (bool: whether successfully get data, any: data if successful else None)
   1133         try:
-&gt; 1134             data = self._data_queue.get(timeout=timeout)
   1135             return (True, data)
   1136         except Exception as e:

/usr/lib/python3.7/multiprocessing/queues.py in get(self, block, timeout)
    111                 self._rlock.release()
    112         # unserialize the data after having released the lock
--&gt; 113         return _ForkingPickler.loads(res)
    114 
    115     def qsize(self):

/usr/local/lib/python3.7/dist-packages/torch/multiprocessing/reductions.py in rebuild_storage_fd(cls, df, size)
    301             return storage
    302         storage = cls._new_shared_fd_cpu(fd, size)
--&gt; 303         shared_cache[fd_id(fd)] = StorageWeakRef(storage)
    304         return storage
    305     finally:

/usr/local/lib/python3.7/dist-packages/torch/multiprocessing/reductions.py in __setitem__(self, key, storage_ref)
     63             dict.__setitem__(self, key, storage_ref)
     64             if len(self) &gt; self.limit:
---&gt; 65                 self.free_dead_references()
     66 
     67     def free_dead_references(self):

/usr/local/lib/python3.7/dist-packages/torch/multiprocessing/reductions.py in free_dead_references(self)
     68         live = 0
     69         for key, storage_ref in list(self.items()):
---&gt; 70             if storage_ref.expired():
     71                 del self[key]
     72             else:

/usr/local/lib/python3.7/dist-packages/torch/multiprocessing/reductions.py in expired(self)
     33 
     34     def expired(self):
---&gt; 35         return torch.Storage._expired(self.cdata)  # type: ignore[attr-defined]
     36 
     37     def __del__(self):

/usr/local/lib/python3.7/dist-packages/torch/storage.py in _expired(cls, *args, **kwargs)
    751     @classmethod
    752     def _expired(cls, *args, **kwargs):
--&gt; 753         return eval(cls.__module__)._UntypedStorage._expired(*args, **kwargs)
    754 
    755     def is_pinned(self):

AttributeError: module 'torch.cuda' has no attribute '_UntypedStorage'
</code></pre>

vickyr: <p>Hello <a class="mention" href="/u/xelif">@Xelif</a>,</p>
<p>I don’t have an exact solution for this issue but I will share what I did to solve this issue.</p>
<p>I got this same issue “AttributeError: module ‘torch.cuda’ has no attribute ‘_UntypedStorage’” in <strong>Colab</strong> while training Yolactedge. Previously the same notebook was working fine. I saw that the PyTorch version was 1.12 and I downgraded it to 1.8 Just to give it a try.</p>
<p>Used the Command<br>
`<br>
%cd /usr/local<br>
!rm cuda<br>
!ln -s cuda-10.0 cuda<br>
!nvcc --version</p>
<p>!pip install torch==1.8.2 torchvision==0.9.2 torchaudio==0.8.2 --extra-index-url <a href="https://download.pytorch.org/whl/lts/1.8/cu111" rel="noopener nofollow ugc">https://download.pytorch.org/whl/lts/1.8/cu111</a></p>
<p>`</p>
<p>And it solved the problem for me. Maybe it is helpful to you.</p>