DistributedSampler — Expected a ‘cuda’ device type for generator when generating indices
Performing distributed training, I have the following code like this:
training_sampler = DistributedSampler(training_set, num_replicas=2, rank=0)
training_generator = data.DataLoader(training_set, **params, sampler=training_sampler)
for x, y, z in training_generator:  # Error occurs here.
    ...

Overall, I get the following message:
-- Process 0 terminated with the following error:
Traceback (most recent call last):
  File &quot;/home/ubuntu/anaconda3/lib/python3.7/site-packages/torch/multiprocessing/spawn.py&quot;, line 19, in _wrap
    fn(i, *args)
  File &quot;/home/ubuntu/VC/ppg_training_extraction/ppg_training_scripts/train_ASR_trim_scp.py&quot;, line 336, in train
    for local_batch_src, local_batch_tgt, lengths in dataloaders[phase]:
  File &quot;/home/ubuntu/anaconda3/lib/python3.7/site-packages/torch/utils/data/dataloader.py&quot;, line 352, in __iter__
    return self._get_iterator()
  File &quot;/home/ubuntu/anaconda3/lib/python3.7/site-packages/torch/utils/data/dataloader.py&quot;, line 294, in _get_iterator
    return _MultiProcessingDataLoaderIter(self)
  File &quot;/home/ubuntu/anaconda3/lib/python3.7/site-packages/torch/utils/data/dataloader.py&quot;, line 827, in __init__
    self._reset(loader, first_iter=True)
  File &quot;/home/ubuntu/anaconda3/lib/python3.7/site-packages/torch/utils/data/dataloader.py&quot;, line 857, in _reset
    self._try_put_index()
  File &quot;/home/ubuntu/anaconda3/lib/python3.7/site-packages/torch/utils/data/dataloader.py&quot;, line 1091, in _try_put_index
    index = self._next_index()
  File &quot;/home/ubuntu/anaconda3/lib/python3.7/site-packages/torch/utils/data/dataloader.py&quot;, line 427, in _next_index
    return next(self._sampler_iter)  # may raise StopIteration
  File &quot;/home/ubuntu/anaconda3/lib/python3.7/site-packages/torch/utils/data/sampler.py&quot;, line 227, in __iter__
    for idx in self.sampler:
  File &quot;/home/ubuntu/anaconda3/lib/python3.7/site-packages/torch/utils/data/distributed.py&quot;, line 97, in __iter__
    indices = torch.randperm(len(self.dataset), generator=g).tolist()  # type: ignore
RuntimeError: Expected a 'cuda' device type for generator but found 'cpu'

Now at that line, I ran the following instructions in pdb:
(Pdb) g = torch.Generator()
(Pdb) g.manual_seed(0)
&lt;torch._C.Generator object at 0x7ff7f8143110&gt;
(Pdb) indices = torch.randperm(4556, generator=g).tolist()
(Pdb) indices = torch.randperm(455604, generator=g).tolist()
*** RuntimeError: Expected a 'cuda' device type for generator but found 'cpu'

Why am I getting the runtime error when the upperbound integer is high, but not when it’s low enough?
Note, I ran on a clean Python session and found
&gt;&gt;&gt; import torch
&gt;&gt;&gt; g = torch.Generator()
&gt;&gt;&gt; g.manual_seed(0)
&lt;torch._C.Generator object at 0x7f9d2dfb39f0&gt;
&gt;&gt;&gt; indices = torch.randperm(455604, generator=g).tolist()

that this worked fine. Is it some configuration in how I’m handling distributed training among multiple GPUs? Any sort of insights would be appreciated!

I just met the same problem using dataloader and I found the following helps without removing torch.set_default_tensor_type('torch.cuda.FloatTensor')
data.DataLoader(..., generator=torch.Generator(device='cuda'))
since I don't want to manually add .to('cuda') for tons of tensors in my code
