You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello, I am currently modifying the architecture from "ONNX-SAM2-Segment-Anything" to export it to TorchScript. However, I have encountered a device mismatch issue during the export process. Below is my code:
While trying to export the model using torch.jit.trace in a Colab environment with T4 GPU, I encountered the following error message:
cuda
An error occurred: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu! (when checking argument for argument index in method wrapper_CUDA__index_select)
/content/segment-anything-2/sam2/modeling/sam/mask_decoder.py:203: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
assert image_embeddings.shape[0] == tokens.shape[0]
/content/segment-anything-2/sam2/modeling/sam/mask_decoder.py:207: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
image_pe.size(0) == 1
Traceback (most recent call last):
File "<ipython-input-3-b6754915e605>", line 43, in main
traced_decoder = torch.jit.trace(sam2_decoder, (image_embed, high_res_feats_0, high_res_feats_1, point_coords, point_labels, mask_input, has_mask_input, orig_im_size))
File "/usr/local/lib/python3.10/dist-packages/torch/jit/_trace.py", line 1000, in trace
traced_func = _trace_impl(
File "/usr/local/lib/python3.10/dist-packages/torch/jit/_trace.py", line 695, in _trace_impl
return trace_module(
File "/usr/local/lib/python3.10/dist-packages/torch/jit/_trace.py", line 1275, in trace_module
module._c._create_method_from_trace(
File "/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py", line 1553, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
File "/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py", line 1562, in _call_impl
return forward_call(*args, **kwargs)
File "/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py", line 1543, in _slow_forward
result = self.forward(*input, **kwargs)
File "/usr/local/lib/python3.10/dist-packages/torch/utils/_contextlib.py", line 116, in decorate_context
return func(*args, **kwargs)
File "<ipython-input-2-0239f2314634>", line 58, in forward
masks, iou_predictions, _, _ = self.mask_decoder.predict_masks(
File "/content/segment-anything-2/sam2/modeling/sam/mask_decoder.py", line 209, in predict_masks
pos_src = torch.repeat_interleave(image_pe, tokens.shape[0], dim=0)
RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu! (when checking argument for argument index in method wrapper_CUDA__index_select)
It appears that some tensors are on cpu while others are on cuda:0, even though I have ensured that all input tensors are moved to cuda. I suspect that some internal layers or tensors inside the mask_decoder are not being moved to cuda correctly. Despite moving all model layers to cuda, this issue persists.
I would greatly appreciate any suggestions or advice on how to resolve this device mismatch issue.
Best regards.
The text was updated successfully, but these errors were encountered:
Hello, I am currently modifying the architecture from "ONNX-SAM2-Segment-Anything" to export it to TorchScript. However, I have encountered a device mismatch issue during the export process. Below is my code:
While trying to export the model using torch.jit.trace in a Colab environment with T4 GPU, I encountered the following error message:
It appears that some tensors are on cpu while others are on cuda:0, even though I have ensured that all input tensors are moved to cuda. I suspect that some internal layers or tensors inside the mask_decoder are not being moved to cuda correctly. Despite moving all model layers to cuda, this issue persists.
I would greatly appreciate any suggestions or advice on how to resolve this device mismatch issue.
Best regards.
The text was updated successfully, but these errors were encountered: