# Use pickle.load inside the getitem function of a dataloader is not automatically mapped to gpu

**URL:** <https://discuss.pytorch.org/t/use-pickle-load-inside-the-getitem-function-of-a-dataloader-is-not-automatically-mapped-to-gpu/205487>\
**Category:** data\
**Created:** [June 28, 2024, 9:40pm UTC](https://discuss.pytorch.org/t/use-pickle-load-inside-the-getitem-function-of-a-dataloader-is-not-automatically-mapped-to-gpu/205487 "2024-06-28T21:40:20Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![sivannavis](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/sivannavis/32/52229_2.png) [@sivannavis](https://discuss.pytorch.org/u/sivannavis)\
**Post date:** [June 28, 2024, 9:40pm UTC](https://discuss.pytorch.org/t/use-pickle-load-inside-the-getitem-function-of-a-dataloader-is-not-automatically-mapped-to-gpu/205487/1 "2024-06-28T21:40:20Z")

</div>

Hi, I build a dataloader and wrapped it with a lightning datamodule that loads precomputed features saved in pkl file. So in the getitem function, I load it with pickl.load(filename), but I got the error:

```auto
*** RuntimeError: Caught RuntimeError in DataLoader worker process 2.
Original Traceback (most recent call last):
  File "/home/szding/.conda/envs/v2sa2/lib/python3.12/site-packages/torch/utils/data/_utils/worker.py", line 308, in _worker_loop
    data = fetcher.fetch(index) # type: ignore[possibly-undefined]
           ^^^^^^^^^^^^^^^^^^^^
  File "/home/szding/.conda/envs/v2sa2/lib/python3.12/site-packages/torch/utils/data/_utils/fetch.py", line 54, in fetch
    return self.collate_fn(data)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/szding/.conda/envs/v2sa2/lib/python3.12/site-packages/torch/utils/data/_utils/collate.py", line 316, in default_collate
    return collate(batch, collate_fn_map=default_collate_fn_map)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/szding/.conda/envs/v2sa2/lib/python3.12/site-packages/torch/utils/data/_utils/collate.py", line 154, in collate
    clone.update({key: collate([d[key] for d in batch], collate_fn_map=collate_fn_map) for key in elem})
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/szding/.conda/envs/v2sa2/lib/python3.12/site-packages/torch/utils/data/_utils/collate.py", line 141, in collate
    return collate_fn_map[elem_type](batch, collate_fn_map=collate_fn_map)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/szding/.conda/envs/v2sa2/lib/python3.12/site-packages/torch/utils/data/_utils/collate.py", line 213, in collate_tensor_fn
    return torch.stack(batch, 0, out=out)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cpu and cuda:0! (when checking argument for argument tensors in method wrapper_CUDA_cat_out_out)

```

And I checked after loading the file with pickle, the location of the data is on CPU. I thought it will be automatically mapped to cuda, now what should I do?

In my code I have something like this for my getitem function:

```auto
    def __getitem__ (self, idx):
        batch = {}
        info = self.metadata[idx]
        with open(a, 'rb') as handle:
               batch['zcode'] = pickle.load(handle)
        return batch

```

If I just simply add `.cuda()` to the batch data, then another error comes: `RuntimeError: cannot pin 'torch.cuda.FloatTensor' only dense CPU tensors can be pinned`
