Fix device selection on Apple Silicon (MPS)#205
Open
FirasAguel wants to merge 1 commit into
Open
Conversation
Several nodes selected their device with a module-level `device = "cuda" if torch.cuda.is_available() else "cpu"`. On Apple Silicon `torch.cuda.is_available()` is False, so they silently ran on CPU even when ComfyUI had selected MPS. The model still runs, just far slower, so there is no error -- only a job that sits at 0% for minutes (reads as a hang). A single BiRefNet image took ~18 min on CPU; on MPS a 21s video processed in ~9 min (~1s/frame). Defer to ComfyUI's own device management via a shared helper `get_device()` in AILab_utils (wraps `comfy.model_management.get_torch_device()`), so nodes follow whatever backend ComfyUI chose -- matching what the SAM2/SAM3/ Florence2/Segment/SDMatte nodes already do. Centralizing it keeps the check from drifting again. Applied to BiRefNet, RMBG, ClothSegment, BodySegment, FashionSegment, FaceSegment, and SegmentV2. LamaRemover already uses get_torch_device() and is left unchanged. Fixes 1038lab#200. Related to 1038lab#91, 1038lab#135.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
On Apple Silicon (Mac, MPS), several nodes selected their device with a module-level
Since
torch.cuda.is_available()isFalseon a Mac, they silently fell back to CPU even when ComfyUI itself had selected MPS. The model still runs, just far slower, so there's no error — only a job that sits at 0% for minutes, which reads as a hang.This defers device selection to ComfyUI's own device management via a small shared helper, so the nodes follow whatever backend ComfyUI chose (CUDA, MPS, or CPU) — the same approach the SAM2 / SAM3 / Florence2 / Segment / SDMatte nodes already use.
Changes
get_device()topy/AILab_utils.py, wrappingcomfy.model_management.get_torch_device().LamaRemoveralready usesget_torch_device()and is left unchanged.deviceis only ever used as.to(device)/map_location=device/ a hashable cache key, so returning atorch.deviceobject instead of a string is safe at every call site.Evidence (before/after, Apple Silicon)
Environment: Apple Silicon, macOS 15.7.4, 48 GB unified memory, ComfyUI 0.20.1 (Desktop), PyTorch 2.10.0, Python 3.13, ComfyUI-RMBG v3.0.0. ComfyUI startup correctly reports
Device: mps.Notes
PYTORCH_ENABLE_MPS_FALLBACK=1so those fall back to CPU instead of erroring.Related issues
device = "cuda" if torch.cuda.is_available() else "cpu"pattern. A device dropdown was later added to the SAM2 node, but these image-segmentation nodes (BiRefNet, RMBG, Cloth/Body/Fashion/FaceSegment, SegmentV2) still hard-coded the CUDA-or-CPU check until now.get_torch_device()and is unchanged here. Also closed for inactivity.)If you'd prefer a user-facing device dropdown (Auto/CUDA/CPU/MPS) on these nodes — matching the YoloV8/SAM3 pattern — rather than this automatic deferral, happy to follow up with that.