TypeError: can't convert cuda:0 device type tensor to numpy. 

Salwa_Mostafa: <p>When I am trying to run my code in CoLab, I receive the following error</p>
<p><strong>TypeError: can’t convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.</strong></p>
<p>which is coming from this line in my function</p>
<pre><code>def to_torch(x, use_gpu=True, dtype=np.float32):
    x = np.array(x, dtype=dtype)
    var = torch.from_numpy(x)
    return var.cuda() if use_gpu is not None else var
</code></pre>
<p>What I understand is that x the one inside np.array() is a tensor that is coming from a GPU. As a matter of fact it is a list having one element which is a GPU tensor</p>
<p>I tried replacing the x inside np.array by x[0].cpu() or x[0].detach.cpu() but for some reason it gives an error saying that these are integers</p>
<p>I tried x.cpu() it also raises an error since x is a list</p>
<p>This is the code link in <a href="https://github.com/SalwaMostafa/marl-ae-comm" rel="noopener nofollow ugc">GitHub</a>. I used the following commands to run in CoLab</p>
<pre><code>!git clone https://github.com/SalwaMostafa/marl-ae-comm.git

import os

os.chdir("/content/marl-ae-comm/marl-grid/find-goal/") 

!pwd

!pip3 install -e ../env

!pip install -r requirements.txt

import os

os.chdir("/content/marl-ae-comm/marl-grid/find-goal/") 

!python train.py --set num_workers 10 env_cfg.comm_len 10 env_cfg.num_agents 2 env_cfg.view_size 8 env_cfg.max_steps 1024 train_iter 3000000 --gpu 0
</code></pre>
<p>The error is in <a href="https://github.com/SalwaMostafa/marl-ae-comm/blob/main/marl-grid/find-goal/ops.py" rel="noopener nofollow ugc">this function</a></p>
<p>I appreciate if someone can help me debug this error. Thank you so much in advance.</p>

ptrblck: <p>I don’t fully understand the idea behind <code>to_torch</code>.<br>
If the input argument <code>x</code> is already a <code>CUDATensor</code>, you wouldn’t need to push it to the CPU first, transform it into a <code>numpy</code> array, transform it back to a PyTorch <code>CPUTensor</code>, and finally push it back to the GPU.<br>
Wouldn’t it work to check the <code>type</code> of <code>x</code> and just return it if it’s already a tensor?</p>

Salwa_Mostafa: <p>Thank you so much for your reply. x is a list and whenever I add a .cpu() it raises an error that no cpu attribute.</p>
<p>The to_tourch is called inside this <a href="https://github.com/ToruOwO/marl-ae-comm/blob/main/marl-grid/find-goal/actor_critic/worker.py" rel="noopener nofollow ugc">function</a>  line 136. I still do not understand why this error.</p>

ptrblck: <p>The <code>cpu()</code> attribute is defined on tensors and is not available in <code>list</code>s.<br>
The error is raised, since you are passing (a <code>list</code> of) CUDATensors to this method as seen here:</p>
<pre><code class="lang-python">def to_torch(x, use_gpu=True, dtype=np.float32):
    x = np.array(x, dtype=dtype)
    var = torch.from_numpy(x)
    return var.cuda() if use_gpu is not None else var


x = [1, 2]
out = to_torch(x)
print(out)
# tensor([1., 2.], device='cuda:0')

x = [torch.tensor(1), torch.tensor(2)]
out = to_torch(x)
print(out)
# tensor([1., 2.], device='cuda:0')

# your error
x = torch.tensor(1).cuda()
out = to_torch(x)
# TypeError: can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.

x = [torch.tensor(1).cuda(), torch.tensor(2).cuda()]
out = to_torch(x)
# TypeError: can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.
</code></pre>

Salwa_Mostafa: <p>Thank you so much for your help. I understand the problem now.</p>