storyicon / comfyui_segment_anything

Based on GroundingDino and SAM, use semantic strings to segment any element in an image. The comfyui version of sd-webui-segment-anything.
Apache License 2.0
757 stars 84 forks source link

Mac arm64 MPS support? #68

Open alexcc4 opened 6 months ago

alexcc4 commented 6 months ago

platform: mac arm64 device: mps

Loads SAM model: /Users/liangbinsi/Documents/ComfyUI/models/sams/sam_vit_h_4b8939.pth (device:AUTO)
!!! Exception during processing!!! Tensor for argument #2 'mat2' is on CPU, but expected it to be on GPU (while checking arguments for mm)
Traceback (most recent call last):
  File "/Users/liangbinsi/Documents/ComfyUI/execution.py", line 151, in recursive_execute
    output_data, output_ui = get_output_data(obj, input_data_all)
  File "/Users/liangbinsi/Documents/ComfyUI/execution.py", line 81, in get_output_data
    return_values = map_node_over_list(obj, input_data_all, obj.FUNCTION, allow_interrupt=True)
  File "/Users/liangbinsi/Documents/ComfyUI/execution.py", line 74, in map_node_over_list
    results.append(getattr(obj, func)(**slice_dict(input_data_all, i)))
  File "/Users/liangbinsi/Documents/ComfyUI/custom_nodes/comfyui_segment_anything/node.py", line 325, in main
    (images, masks) = sam_segment(
  File "/Users/liangbinsi/Documents/ComfyUI/custom_nodes/comfyui_segment_anything/node.py", line 247, in sam_segment
    masks, _, _ = predictor.predict_torch(
  File "/Users/liangbinsi/.pyenv/versions/3.10.0/lib/python3.10/site-packages/torch/utils/_contextlib.py", line 115, in decorate_context
    return func(*args, **kwargs)
  File "/Users/liangbinsi/Documents/ComfyUI/custom_nodes/ComfyUI-dnl13-seg/libs/sam_hq/predictor.py", line 114, in predict_torch
    sparse_embeddings, dense_embeddings = self.model.prompt_encoder(
  File "/Users/liangbinsi/.pyenv/versions/3.10.0/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1532, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
  File "/Users/liangbinsi/.pyenv/versions/3.10.0/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1541, in _call_impl
    return forward_call(*args, **kwargs)
  File "/Users/liangbinsi/.pyenv/versions/3.10.0/lib/python3.10/site-packages/segment_anything/modeling/prompt_encoder.py", line 158, in forward
    box_embeddings = self._embed_boxes(boxes)
  File "/Users/liangbinsi/.pyenv/versions/3.10.0/lib/python3.10/site-packages/segment_anything/modeling/prompt_encoder.py", line 97, in _embed_boxes
    corner_embedding = self.pe_layer.forward_with_coords(coords, self.input_image_size)
  File "/Users/liangbinsi/.pyenv/versions/3.10.0/lib/python3.10/site-packages/segment_anything/modeling/prompt_encoder.py", line 214, in forward_with_coords
    return self._pe_encoding(coords.to(torch.float))  # B x N x C
  File "/Users/liangbinsi/.pyenv/versions/3.10.0/lib/python3.10/site-packages/segment_anything/modeling/prompt_encoder.py", line 189, in _pe_encoding
    coords = coords @ self.positional_encoding_gaussian_matrix
RuntimeError: Tensor for argument #2 'mat2' is on CPU, but expected it to be on GPU (while checking arguments for mm)
image
martintomov commented 6 months ago

I'm looking forward for mac arm support too

ynie commented 4 months ago
krummrey commented 2 months ago

Has anyone looked into this?

kaolifu commented 1 week ago
截屏2024-11-17 21 25 40

I encountered the same issue as you, but you might want to try using this node to load the SAM model. I succeeded using this method.

platform: mac arm64 平台:macarm64 device: mps 设备:mps

Loads SAM model: /Users/liangbinsi/Documents/ComfyUI/models/sams/sam_vit_h_4b8939.pth (device:AUTO)
!!! Exception during processing!!! Tensor for argument #2 'mat2' is on CPU, but expected it to be on GPU (while checking arguments for mm)
Traceback (most recent call last):
  File "/Users/liangbinsi/Documents/ComfyUI/execution.py", line 151, in recursive_execute
    output_data, output_ui = get_output_data(obj, input_data_all)
  File "/Users/liangbinsi/Documents/ComfyUI/execution.py", line 81, in get_output_data
    return_values = map_node_over_list(obj, input_data_all, obj.FUNCTION, allow_interrupt=True)
  File "/Users/liangbinsi/Documents/ComfyUI/execution.py", line 74, in map_node_over_list
    results.append(getattr(obj, func)(**slice_dict(input_data_all, i)))
  File "/Users/liangbinsi/Documents/ComfyUI/custom_nodes/comfyui_segment_anything/node.py", line 325, in main
    (images, masks) = sam_segment(
  File "/Users/liangbinsi/Documents/ComfyUI/custom_nodes/comfyui_segment_anything/node.py", line 247, in sam_segment
    masks, _, _ = predictor.predict_torch(
  File "/Users/liangbinsi/.pyenv/versions/3.10.0/lib/python3.10/site-packages/torch/utils/_contextlib.py", line 115, in decorate_context
    return func(*args, **kwargs)
  File "/Users/liangbinsi/Documents/ComfyUI/custom_nodes/ComfyUI-dnl13-seg/libs/sam_hq/predictor.py", line 114, in predict_torch
    sparse_embeddings, dense_embeddings = self.model.prompt_encoder(
  File "/Users/liangbinsi/.pyenv/versions/3.10.0/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1532, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
  File "/Users/liangbinsi/.pyenv/versions/3.10.0/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1541, in _call_impl
    return forward_call(*args, **kwargs)
  File "/Users/liangbinsi/.pyenv/versions/3.10.0/lib/python3.10/site-packages/segment_anything/modeling/prompt_encoder.py", line 158, in forward
    box_embeddings = self._embed_boxes(boxes)
  File "/Users/liangbinsi/.pyenv/versions/3.10.0/lib/python3.10/site-packages/segment_anything/modeling/prompt_encoder.py", line 97, in _embed_boxes
    corner_embedding = self.pe_layer.forward_with_coords(coords, self.input_image_size)
  File "/Users/liangbinsi/.pyenv/versions/3.10.0/lib/python3.10/site-packages/segment_anything/modeling/prompt_encoder.py", line 214, in forward_with_coords
    return self._pe_encoding(coords.to(torch.float))  # B x N x C
  File "/Users/liangbinsi/.pyenv/versions/3.10.0/lib/python3.10/site-packages/segment_anything/modeling/prompt_encoder.py", line 189, in _pe_encoding
    coords = coords @ self.positional_encoding_gaussian_matrix
RuntimeError: Tensor for argument #2 'mat2' is on CPU, but expected it to be on GPU (while checking arguments for mm)
image