Skip to content

[Bug] MPS crash in SAM3 Text Segmentation (grid_sample) on macOS - Placeholder tensor is empty #110

@HM1579

Description

@HM1579

[Bug] MPS crash in SAM3 Text Segmentation (grid_sample) on macOS - Placeholder tensor is empty

Description

When running SAM3 Text Segmentation on macOS (Apple Silicon, MPS backend), the node crashes with a PyTorch internal assertion error:

RuntimeError: [srcBuf length] > 0 INTERNAL ASSERT FAILED ...
Placeholder tensor is empty!

The error occurs inside torch.nn.functional.grid_sample during the grounding phase.


Environment

  • OS: macOS 14.8.2 (Apple Silicon)
  • Device: MPS (Metal)
  • PyTorch: 2.10.0
  • Python: 3.12
  • ComfyUI: 0.18.2
  • comfyui-sam3: latest (as of 2026-04)

Full Error Trace

RuntimeError: [srcBuf length] > 0 INTERNAL ASSERT FAILED at ...
Placeholder tensor is empty!

File ".../sam3/model.py", line 2344:
sampled = torch.nn.functional.grid_sample(img_feats, grid, align_corners=False)

Root Cause (Analysis)

From code inspection:

# load_model.py
load_device = comfy.model_management.get_torch_device()

# utils.py
if device is None:
    device = comfy.model_management.get_torch_device()

This means:

  • The model always follows ComfyUI’s global device
  • On macOS → this becomes mps

The crash happens specifically in:

grid_sample (MPS backend)

This suggests:

PyTorch MPS backend + SAM3 grounding pipeline is unstable or unsupported in this code path.


Expected Behavior

  • SAM3 Text Segmentation should run correctly on MPS
  • OR fallback to CPU automatically if unsupported operations are used

Actual Behavior

  • Node crashes during grounding
  • No output produced
  • Error occurs consistently on MPS

Workaround (Confirmed Working)

Forcing the model to run on CPU resolves the issue completely.

Fix 1 — nodes/load_model.py

import torch
load_device = torch.device("cpu")

Fix 2 — nodes/_model_cache.py

import torch
load_device = torch.device("cpu")
offload_device = torch.device("cpu")

Fix 3 (optional, safer) — nodes/sam3/utils.py

if device is None:
    device = torch.device("cpu")

Result After Fix

  • No crash
  • Segmentation works correctly
  • Multiple instance masks are generated as expected
  • Only downside: slower inference (CPU)

Suggestion

Consider:

  1. Detecting MPS and disabling problematic ops (e.g. grid_sample)
  2. Automatically falling back to CPU when running grounding
  3. Adding a device override option in the node UI

Additional Notes

  • This issue is independent of prompt correctness
  • Once running on CPU, results are stable and correct
  • Likely related to PyTorch MPS limitations rather than model logic

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions