Dataloader creating data which is partially on CPU and GPU

Amruta_Muthal: <p>I am running the code below to create data loaders for Graph data:<br>
“”"<br>
batch_size = 128<br>
train_list =[]<br>
for idx, batch in enumerate(zip(X_train[train_idx],<br>
class_v[train_idx],<br>
adj_train[train_idx])):<br>
edge_index, _ = dense_to_sparse(torch.from_numpy(batch[2]).float())<br>
train_list.append(Data(x = torch.from_numpy(batch[0]).float(),<br>
y = torch.from_numpy(batch[1]).float(),<br>
edge_index = edge_index<br>
)<br>
)</p>
<p>batch_train_loader = DataLoader(train_list, batch_size=batch_size)</p>
<p>val_list = []<br>
for idx, batch in enumerate(zip(X_train_val[val_idx],<br>
class_v_val[val_idx],<br>
adj_train_val[val_idx])):<br>
edge_index, _ = dense_to_sparse(torch.from_numpy(batch[2]).float())<br>
val_list.append(Data(x = torch.from_numpy(batch[0]).float(),<br>
y = torch.from_numpy(batch[1]).float(),<br>
edge_index = edge_index)<br>
)<br>
batch_val_loader = DataLoader(val_list, batch_size=batch_size)<br>
“”"</p>
<p>When I try to read this data. I get an error that data cannot be on two devices.<br>
“”"<br>
idx = 0<br>
for data in batch_train_loader:<br>
idx+=1<br>
print(data.x.shape,data.y.shape,data.edge_index.shape)<br>
if idx==3:<br>
break<br>
“”"<br>
“”"<br>
RuntimeError                              Traceback (most recent call last)<br>
~\AppData\Local\Temp/ipykernel_17752/355153217.py in <br>
1 idx = 0<br>
----&gt; 2 for data in batch_train_loader:<br>
3     idx+=1<br>
4     print(data.x.shape,data.y.shape,data.edge_index.shape)<br>
5     if idx==3:</p>
<p>~\miniconda3\lib\site-packages\torch\utils\data\dataloader.py in <strong>next</strong>(self)<br>
519             if self._sampler_iter is None:<br>
520                 self._reset()<br>
 → 521             data = self._next_data()<br>
522             self._num_yielded += 1<br>
523             if self._dataset_kind == _DatasetKind.Iterable and \</p>
<p>~\miniconda3\lib\site-packages\torch\utils\data\dataloader.py in _next_data(self)<br>
559     def _next_data(self):<br>
560         index = self._next_index()  # may raise StopIteration<br>
 → 561         data = self._dataset_fetcher.fetch(index)  # may raise StopIteration<br>
562         if self._pin_memory:<br>
563             data = _utils.pin_memory.pin_memory(data)</p>
<p>~\miniconda3\lib\site-packages\torch\utils\data_utils\fetch.py in fetch(self, possibly_batched_index)<br>
45         else:<br>
46             data = self.dataset[possibly_batched_index]<br>
—&gt; 47         return self.collate_fn(data)</p>
<p>~\miniconda3\lib\site-packages\torch_geometric\loader\dataloader.py in <strong>call</strong>(self, batch)<br>
37<br>
38     def <strong>call</strong>(self, batch):<br>
—&gt; 39         return self.collate(batch)<br>
40<br>
41</p>
<p>~\miniconda3\lib\site-packages\torch_geometric\loader\dataloader.py in collate(self, batch)<br>
17         elem = batch[0]<br>
18         if isinstance(elem, Data) or isinstance(elem, HeteroData):<br>
—&gt; 19             return Batch.from_data_list(batch, self.follow_batch,<br>
20                                         self.exclude_keys)<br>
21         elif isinstance(elem, torch.Tensor):</p>
<p>~\miniconda3\lib\site-packages\torch_geometric\data\batch.py in from_data_list(cls, data_list, follow_batch, exclude_keys)<br>
61         Will exclude any keys given in :obj:<code>exclude_keys</code>."""<br>
62<br>
—&gt; 63         batch, slice_dict, inc_dict = collate(<br>
64             cls,<br>
65             data_list=data_list,</p>
<p>~\miniconda3\lib\site-packages\torch_geometric\data\collate.py in collate(cls, data_list, increment, add_batch, follow_batch, exclude_keys)<br>
74<br>
75             # Collate attributes into a unified representation:<br>
—&gt; 76             value, slices, incs = _collate(attr, values, data_list, stores,<br>
77                                            increment)<br>
78</p>
<p>~\miniconda3\lib\site-packages\torch_geometric\data\collate.py in _collate(key, values, data_list, stores, increment)<br>
142             incs = get_incs(key, values, data_list, stores)<br>
143             if incs.dim() &gt; 1 or int(incs[-1]) != 0:<br>
 → 144                 values = [value + inc for value, inc in zip(values, incs)]<br>
145         else:<br>
146             incs = None</p>
<p>~\miniconda3\lib\site-packages\torch_geometric\data\collate.py in (.0)<br>
142             incs = get_incs(key, values, data_list, stores)<br>
143             if incs.dim() &gt; 1 or int(incs[-1]) != 0:<br>
 → 144                 values = [value + inc for value, inc in zip(values, incs)]<br>
145         else:<br>
146             incs = None</p>
<p>RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!<br>
“”"<br>
Not sure why this should happen. I am also seeing a considerable slowing in my training loop.</p>

ptrblck: <p>Are you setting the default tensor type to a <code>CUDATensor</code> or is any class pushing the data to the GPU (e.g. <code>Data</code>)?<br>
Could you check the <code>.device</code> attribute of the tensors while iterating the <code>train_list</code>?</p>