# Fail to use .cuda() in \_\_getitem\_\_()

**URL:** <https://discuss.pytorch.org/t/fail-to-use-cuda-in-getitem/35802>\
**Category:** Uncategorized\
**Created:** [January 28, 2019, 3:33pm UTC](https://discuss.pytorch.org/t/fail-to-use-cuda-in-getitem/35802 "2019-01-28T15:33:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jiacheng\_Li](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jiacheng_li/32/8968_2.png) [@Jiacheng\_Li](https://discuss.pytorch.org/u/Jiacheng_Li)\
**Post date:** [January 28, 2019, 3:33pm UTC](https://discuss.pytorch.org/t/fail-to-use-cuda-in-getitem/35802/1 "2019-01-28T15:33:05Z")

</div>

While using PyHeat to analyze the time cost of my code, I find that the operation of Tensor.cuda() costs a lot, even more than that of using net to generate a result. Here is the cut of my PyHeat report:

 ![train](https://discuss.pytorch.org/uploads/default/original/2X/e/e32ef7fc49678eb043fec541b50b8ff700fc225c.jpeg) ![eval](https://discuss.pytorch.org/uploads/default/original/2X/7/7b774ade25f5ef600fe9bffff106d569230aa6a0.jpeg)

I tried using Tensor.cuda() in the function of **getitem** in my dataset but failed. I also tried setting torch.multiprocessing.set\_start\_method(‘spawn’,force=True) and setting pin\_memory=True num\_worker=4 in my dataloader, all not work yet. (If num\_worker is 1, everything works fine, but it’s too slow)

So I searched in PyTorch doc and only find things with how to put my model in multi gpus rather than put my data on one gpu with multiprocess. In the official examples of PyTorch, I still can’t find any code that use .cuda() in **getitem** in dataset. I don’t know whether there is something I missed in Doc, so I create a topic here for help.

Briefly, I want to put my data on one gpu with multiprocess and train my model on that gpu too, is there any solutions?

---

<div class="post-metadata">

**Author:** ![albanD](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alband/32/215_2.png) [@albanD](https://discuss.pytorch.org/u/albanD)\
**Post date:** [January 28, 2019, 3:46pm UTC](https://discuss.pytorch.org/t/fail-to-use-cuda-in-getitem/35802/2 "2019-01-28T15:46:19Z")

</div>

Hi,

The timing associated with `.cuda()` operations can be misleading.  
CUDA api is asynchronous and calling `.cuda()` is one of the sync point. This means that if you do something GPU intensive just before, it will appear like you did it during the `.cuda()` call.  
You should run your code with `CUDA_LAUNCH_BLOCKING=1` when measuring cpu runtime of gpu operations.

---

<div class="post-metadata">

**Author:** ![Jiacheng\_Li](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jiacheng_li/32/8968_2.png) [@Jiacheng\_Li](https://discuss.pytorch.org/u/Jiacheng_Li)\
**Post date:** [January 28, 2019, 3:51pm UTC](https://discuss.pytorch.org/t/fail-to-use-cuda-in-getitem/35802/3 "2019-01-28T15:51:27Z")

</div>

Thanks, I will test again with your advice, but my problem still not solved, since dataloader can load data using multiprocess, why not let dataloader put the data on gpu using multiprocess too?

---

<div class="post-metadata">

**Author:** ![albanD](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alband/32/215_2.png) [@albanD](https://discuss.pytorch.org/u/albanD)\
**Post date:** [January 28, 2019, 3:55pm UTC](https://discuss.pytorch.org/t/fail-to-use-cuda-in-getitem/35802/4 "2019-01-28T15:55:39Z")

</div>

Hi,

Because sharing cuda tensors across processes is a nightmare and because of the async nature of cuda not needed.

---

<div class="post-metadata">

**Author:** ![Jiacheng\_Li](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jiacheng_li/32/8968_2.png) [@Jiacheng\_Li](https://discuss.pytorch.org/u/Jiacheng_Li)\
**Post date:** [January 28, 2019, 4:01pm UTC](https://discuss.pytorch.org/t/fail-to-use-cuda-in-getitem/35802/5 "2019-01-28T16:01:24Z")

</div>

Alright 😂, by the way, where should I set CUDA\_LAUNCH\_BLOCKING=1?

---

<div class="post-metadata">

**Author:** ![albanD](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alband/32/215_2.png) [@albanD](https://discuss.pytorch.org/u/albanD)\
**Post date:** [January 28, 2019, 4:03pm UTC](https://discuss.pytorch.org/t/fail-to-use-cuda-in-getitem/35802/6 "2019-01-28T16:03:42Z")

</div>

This is an environment variable that should be set before cuda initialization. I usually do `CUDA_LAUNCH_BLOCKING=1 python my_script.py`.
