Moving data to GPU in collate_fn fails

Andreas_Bloch: <p>I have to generate a lot of randomized batches. One thing I can’t do is pre-storing all the data on the GPU (that would take too much space). So right now I’m moving the batch from the CPU to the GPU in the training loop.</p>
<p>I’d like to speed things up by pre-moving the data to the GPU in the batch worker (s.t. I can directly use it in the training loop).</p>
<p>Here’s the MWE that illustrates my plan:</p>
<pre><code class="lang-auto">import torch
from torch.utils.data import Dataset
from torch.utils.data import DataLoader
import numpy as np

class ExampleDataset(Dataset):
    def __init__(self):
        super(Dataset, self).__init__()
    def __len__(self):
        return 100000
    def __getitem__(self, idx):
        return np.random.rand(3)

def custom_collate_fn(batch):
    batch = torch.tensor(batch)
    # i'd like to pre-move the data to the GPU but i get an error here:
    batch = batch.to('cuda', non_blocking=True)
    return batch

batch_loader = DataLoader(
    ExampleDataset(),
    batch_size=100,
    shuffle=True,
    num_workers=8,
    pin_memory=True,
    collate_fn=custom_collate_fn
)

# training loop
NUM_EPOCHS = 10
for epoch in range(NUM_EPOCHS):
    for batch_num, train_batch in enumerate(batch_loader, 0):
        # usually i transfer the train_batch from CPU to GPU here
        # that causes delays (i have huge batch-sizes)
        print('training.')
</code></pre>
<p>Here’s the error that I get:</p>
<pre><code class="lang-auto">/local/home/venv/bin/python -u /local/home/BT/mwe.py
THCudaCheck FAIL file=/pytorch/aten/src/THC/THCGeneral.cpp line=55 error=3 : initialization error
THCudaCheck FAIL file=/pytorch/aten/src/THC/THCGeneral.cpp line=55 error=3 : initialization error
THCudaCheck FAIL file=/pytorch/aten/src/THC/THCGeneral.cpp line=55 error=3 : initialization error
THCudaCheck FAIL file=/pytorch/aten/src/THC/THCGeneral.cpp line=55 error=3 : initialization error
THCudaCheck FAIL file=/pytorch/aten/src/THC/THCGeneral.cpp line=55 error=3 : initialization error
THCudaCheck FAIL file=/pytorch/aten/src/THC/THCGeneral.cpp line=55 error=3 : initialization error
THCudaCheck FAIL file=/pytorch/aten/src/THC/THCGeneral.cpp line=55 error=3 : initialization error
Traceback (most recent call last):
  File "/local/home/BT/mwe.py", line 39, in &lt;module&gt;
THCudaCheck FAIL file=/pytorch/aten/src/THC/THCGeneral.cpp line=55 error=3 : initialization error
    for batch_num, train_batch in enumerate(batch_loader, 0):
  File "/local/home/venv/lib/python3.5/site-packages/torch/utils/data/dataloader.py", line 582, in __next__
    return self._process_next_batch(batch)
  File "/local/home/venv/lib/python3.5/site-packages/torch/utils/data/dataloader.py", line 608, in _process_next_batch
    raise batch.exc_type(batch.exc_msg)
RuntimeError: Traceback (most recent call last):
  File "/local/home/venv/lib/python3.5/site-packages/torch/utils/data/_utils/worker.py", line 99, in _worker_loop
    samples = collate_fn([dataset[i] for i in batch_indices])
  File "/local/home/BT/mwe.py", line 22, in custom_collate_fn
    batch = batch.to('cuda', non_blocking=True)
  File "/local/home/venv/lib/python3.5/site-packages/torch/cuda/__init__.py", line 163, in _lazy_init
    torch._C._cuda_init()
RuntimeError: cuda runtime error (3) : initialization error at /pytorch/aten/src/THC/THCGeneral.cpp:55


Process finished with exit code 1
</code></pre>
<p>Do you know how I could achieve my plan? Unfortunately I can’t get rid of this error (I’ve tried setting torch.multiprocessing modes, but that didn’t help).</p>

shubhvachher: <p>Check <a href="https://discuss.pytorch.org/t/a-error-when-using-gpu/32761/17">this</a> out. It is a copy of your error.</p>
<p>As a summary setting cudnn.benchmark to False works for some. You could try that too. (Also, you should be setting up this way if <a href="https://discuss.pytorch.org/t/what-does-torch-backends-cudnn-benchmark-do/5936/3">this</a> applies to your randomized batch sizes.) Others have fixed the error by mixing and matching CUDA and PyTorch versions.</p>
<p>Something you’re trying that’s different is to push to the GPU inside your collate_fn. (I’m sure this sparks a discussion on multiple processes copying data to GPU together. If you know of some such, do link it here)  As a test, you could try to just return batch in custom_collate_fn without the <code>.to('cuda')</code> call. and in your <code>for batch_num, train_batch...</code> call add <code>train_batch.to('cuda', non_blocking=True)</code></p>
<p>If it still fails with the same error, try sending a reply on that thread. If it does work, still send a reply on that thread; It will greatly help debug the issue <img src="https://discuss.pytorch.org/images/emoji/apple/slightly_smiling_face.png?v=9" title=":slightly_smiling_face:" class="emoji" alt=":slightly_smiling_face:"></p>

Andreas_Bloch: <p>setting cudnn.benchmark didn’t help (True and False).</p>
<p>setting CUDA_LAUNCH_BLOCKING=0 or 1 didn’t help</p>
<p>doing the .to call inside the for loop (instead of the collate function) works, but that’s what i’d like to avoid because i’d like to have the data pre-stored on the GPU already by the worker s.t. the data can be directly accessed.</p>
<p>yes i think that the child processes for the batch workers create a new cuda context. what should be done is to get the cuda context of the parent process and initialize the tensor there. but i don’t know how to do that.</p>
<p>Also I’ve tried the solutions from here:<br>
<aside class="onebox githubissue">
  <header class="source">
      <a href="https://github.com/pytorch/pytorch/issues/3492" target="_blank" rel="nofollow noopener">github.com/pytorch/pytorch</a>
  </header>
  <article class="onebox-body">
    <a href="https://github.com/zhoumingyi" rel="nofollow noopener">
<img src="https://avatars1.githubusercontent.com/u/23503239?v=2&amp;s=96" class="thumbnail onebox-avatar" width="96" height="96">
</a>

<h4><a href="https://github.com/pytorch/pytorch/issues/3492" target="_blank" rel="nofollow noopener">Issue: RuntimeError: context has already been set(multiprocessing)</a></h4>

<div class="date" style="margin-top:10px;">
	<div class="user" style="margin-top:10px;">
	opened by <a href="https://github.com/zhoumingyi" target="_blank" rel="nofollow noopener">zhoumingyi</a>
	on <a href="https://github.com/pytorch/pytorch/issues/3492" target="_blank" rel="nofollow noopener">2017-11-05</a>
	</div>
	<div class="user">
	closed by <a href="https://github.com/apaszke" target="_blank" rel="nofollow noopener">apaszke</a>
	on <a href="https://github.com/pytorch/pytorch/issues/3492" target="_blank" rel="nofollow noopener">2017-11-05</a>
	</div>
</div>

<pre class="content" style="white-space: pre-wrap;">I use a spawn start methods to share CUDA tensors between processes
import torch
torch.multiprocessing.set_start_method("spawn")
import torch.multiprocessing as mp

def sub_processes(A, B, D, i, j,...</pre>

<div class="labels">
</div>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>
<br>
However, I got the error:<br>
<code>RuntimeError: CUDA error: all CUDA-capable devices are busy or unavailable</code></p>

shubhvachher: <p>That’s interesting. I expected your error to be just the same as my previously posted link and so .to in your for loop should have failed. I’m unsure where to start debugging this and unfortunately its late where I am… Will pick this up tomorrow, if you’re still at it do tell.</p>
<p>Have you tried reading up on <a href="https://github.com/pytorch/pytorch/issues/10832#issuecomment-419341200" rel="nofollow noopener">this</a> and the discussions in commits that follow? May provide some insight if context is all you need</p>

Andreas_Bloch: <p>Thanks for your help! Yes, I’m still at solving the problem. It looks like I’d have to share the CUDA context from the main process with the spawned workers. However, I don’t know how to do that.</p>

shubhvachher: <p>I do recommend posting something on <a href="https://discuss.pytorch.org/t/a-error-when-using-gpu/32761">this thread</a>. The devs seem to be taking quite an interest in it and they’ve been active there.</p>
<p>Shubh</p>