# Gradient flow and device

**URL:** <https://discuss.pytorch.org/t/gradient-flow-and-device/122133>\
**Category:** autograd\
**Created:** [May 23, 2021, 3:15pm UTC](https://discuss.pytorch.org/t/gradient-flow-and-device/122133 "2021-05-23T15:15:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cbd](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/c/13edae/32.png) [@cbd](https://discuss.pytorch.org/u/cbd)\
**Post date:** [May 23, 2021, 3:15pm UTC](https://discuss.pytorch.org/t/gradient-flow-and-device/122133/1 "2021-05-23T15:15:49Z")

</div>

I used “grad1 = grad.clone()” in my hook function. Other tensors are on GPU but it shows “grad1” is on CPU as “grad.clone()” is returning value in CPU. Even when function return the value, it gives error if return tensor is on GPU so need to convert in CPU again.

Is gradient normal flow on CPU? What can i do to convert all grad on GPU so that don’t require to switch from GPU and CPU.

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [May 23, 2021, 8:00pm UTC](https://discuss.pytorch.org/t/gradient-flow-and-device/122133/2 "2021-05-23T20:00:59Z")

</div>

I don’t know how `grad` and `grad1` are calculated, but PyTorch will stick the the specified devices in your script and won’t change the device behind your back.  
Of course this doesn’t apply to any 3rd party utilities, which explicitly could push data to the CPU to save GPU memory, but I also don’t know if you are using any of these.

Could you describe your use case a bit and post a minimal code snippet, which would reproduce this issue?

---

<div class="post-metadata">

**Author:** ![cbd](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/c/13edae/32.png) [@cbd](https://discuss.pytorch.org/u/cbd)\
**Post date:** [May 24, 2021, 5:46am UTC](https://discuss.pytorch.org/t/gradient-flow-and-device/122133/3 "2021-05-24T05:46:06Z")

</div>

Sorry it was my mistake. Tensor was on CPU.
