Expected u.is_cuda() to be true, but got false. (Could this error message be improved? If so, please report an enhancement request to PyTorch.)

username: Ph_m_Vi_t_Ti_n
cooked: <pre><code>              for cat in sem_class_to_idx.keys():
                  if cat == '__background__':
                    continue

                  car_category = sem_class_to_idx[cat]
                  car_mask = output[0, :, :, :].argmax(axis=0).detach().cpu().numpy()
                  car_mask_uint8 = 255 * np.uint8(car_mask == car_category)
                  car_mask_float = np.float32(car_mask == car_category)
                  target_layer = [layer]
                  targets = [SemanticSegmentationTarget(car_category, car_mask_float)]

                  with GradCAM(model=Attention_UNet(),
                              target_layers=target_layer,
                              # use_cuda=torch.cuda.is_available()
                              reshape_transform=True
                              # use_cuda = False
                              ) as cam:

                          grayscale_cam = cam(input_tensor=input_tensor.transpose(-1,-3),
                                              targets=targets)[0, :]
                          cam_image = show_cam_on_image( np.repeat([min_max_preprocess(img)], 3, axis=0).transpose(1,2,0), grayscale_cam, use_rgb=True)

                          list_img.append(cam_image)
</code></pre>
<p>I got error: RuntimeError                              Traceback (most recent call last)<br>
 in &lt;cell line: 18&gt;()<br>
43                                   ) as cam:<br>
44<br>
—&gt; 45                               grayscale_cam = cam(input_tensor=input_tensor.transpose(-1,-3),<br>
46                                                   targets=targets)[0, :]<br>
47                               cam_image = show_cam_on_image( np.repeat([min_max_preprocess(img)], 3, axis=0).transpose(1,2,0), grayscale_cam, use_rgb=True)</p>
<p>/usr/local/lib/python3.10/dist-packages/mamba_ssm/ops/selective_scan_interface.py in forward(ctx, u, delta, A, B, C, D, z, delta_bias, delta_softplus, return_last_state)<br>
40             C = rearrange(C, “b dstate l → b 1 dstate l”)<br>
41             ctx.squeeze_C = True<br>
—&gt; 42         out, x, *rest = selective_scan_cuda.fwd(u, delta, A, B, C, D, z, delta_bias, delta_softplus)<br>
43         ctx.delta_softplus = delta_softplus<br>
44         ctx.has_z = z is not None</p>
<p>RuntimeError: Expected u.is_cuda() to be true, but got false.  (Could this error message be improved?  If so, please report an enhancement request to PyTorch.)</p>

username: eqy
cooked: <p>Looks like your model is on cuda but one of the inputs is not? check e.g., the device of the tensors passed such as <code>input_tensor</code> and whether they are all on gpu as expected</p>

username: Ph_m_Vi_t_Ti_n
cooked: <p>Thank. I added input_tensor.cuda() and still have the above problem</p>

username: ptrblck
cooked: <p>Unsure where and how you’ve added it, but keep in mind that <code>cuda()</code> and <code>to()</code> operations are not performed inplace on tensors. You would thus need to reassign the tensor via <code>input_tensor = input_tensor.cuda()</code>.</p>