Input types can't be cast to the desired output type Long

username: mendelsontau
cooked: <p>Hi, getting the following error when trying to generate a batch. I checked and all tensors to be stacked are torch.in64 as well as the tensor out to which they are to be stacked. any ideas?</p>
<p>File “/home/gamir/DER-Roei/alon/anaconda3/envs/open_clip/lib/python3.10/site-packages/torch/utils/data/dataloader.py”, line 652, in <strong>next</strong><br>
data = self._next_data()<br>
File “/home/gamir/DER-Roei/alon/anaconda3/envs/open_clip/lib/python3.10/site-packages/torch/utils/data/dataloader.py”, line 1347, in _next_data<br>
return self._process_data(data)<br>
File “/home/gamir/DER-Roei/alon/anaconda3/envs/open_clip/lib/python3.10/site-packages/torch/utils/data/dataloader.py”, line 1373, in _process_data<br>
data.reraise()<br>
File “/home/gamir/DER-Roei/alon/anaconda3/envs/open_clip/lib/python3.10/site-packages/torch/_utils.py”, line 461, in reraise<br>
raise exception<br>
RuntimeError: Caught RuntimeError in DataLoader worker process 0.<br>
Original Traceback (most recent call last):<br>
File “/home/gamir/DER-Roei/alon/anaconda3/envs/open_clip/lib/python3.10/site-packages/torch/utils/data/_utils/worker.py”, line 302, in _worker_loop<br>
data = fetcher.fetch(index)<br>
File “/home/gamir/DER-Roei/alon/anaconda3/envs/open_clip/lib/python3.10/site-packages/torch/utils/data/_utils/fetch.py”, line 52, in fetch<br>
return self.collate_fn(data)<br>
File “/home/gamir/DER-Roei/alon/anaconda3/envs/open_clip/lib/python3.10/site-packages/torch/utils/data/_utils/collate.py”, line 180, in default_collate<br>
return [default_collate(samples) for samples in transposed]  # Backwards compatibility.<br>
File “/home/gamir/DER-Roei/alon/anaconda3/envs/open_clip/lib/python3.10/site-packages/torch/utils/data/_utils/collate.py”, line 180, in <br>
return [default_collate(samples) for samples in transposed]  # Backwards compatibility.<br>
File “/home/gamir/DER-Roei/alon/anaconda3/envs/open_clip/lib/python3.10/site-packages/torch/utils/data/_utils/collate.py”, line 146, in default_collate<br>
return torch.stack(batch, 0, out=out)<br>
RuntimeError: torch.cat(): input types can’t be cast to the desired output type Long</p>

username: mendelsontau
cooked: <p>Just to add that it appears to be happening only when the dataloader is multiprocess (i.e. num_workers &gt; 0) and when the dataloader is single process everything works fine</p>

username: dhpark
cooked: <p>Did you solve the problem? I get the same error when I use torch-geometric.</p>

username: zyu
cooked: <p>Did you solve the problem?  I met the same problem.</p>

username: pengyang7881187
cooked: <p>I met the same problem with pyg, and solved it by input edge_index as FloatTensor, and I transform it to Long type after the batch is feteched</p>

username: BSchneider
cooked: <p>I had this issue when I was modifying the labels of certain samples by overriding the dataloader’s <strong>getitem</strong> method. It seems like with multiple workers pytorch wasn’t able to cast tensors the dataloader was returning anymore. The fix was to ensure all the label tensors the dataloader returned had been cast to long with .type(torch.LongTensor)  [at the end of the <strong>getitem</strong> method].</p>

username: AliAbbasi
cooked: <p>I encountered this error while modifying labels in a custom Dataset class. I was incorporating mixup augmentation and randomly softening some labels. The problem arose because the class was inconsistently returning Long labels and Float labels (due to the softening from mixup).</p>
<p>To resolve this, I manually changed the label type before returning it from the <code>__getitem__</code> method:</p>
<pre><code class="lang-auto">return data, label.float() 
</code></pre>