Fixing Corrupted ComfyUI Upscale Output on Mac MPS with contiguous()
I was batch-upscaling 640px images from AntiGravity in ComfyUI when some outputs came out completely garbled. I initially suspected an upscaler model compatibility issue, but the real cause turned out to be a memory layout problem much further upstream.
What Was Happening
The conditions to reproduce it were:
- Mac (Apple Silicon)
- ComfyUI’s
Upscale Image (using Model)node - Input image coming straight from a
Load Imagenode
Oddly, images generated within ComfyUI’s own pipeline rarely got corrupted — only Load Image as the source caused the problem. Tracking down that difference made the culprit clear.
Root Cause
ComfyUI image tensors are natively in BHWC format. The Upscale node internally permutes them to BCHW before passing to ESRGAN-family models.
After this permutation, the tensor can end up non-contiguous, and passing it directly to conv2d on MPS breaks things.
The specific issue: tiled_scale_multidim was passing s_in, a narrow() slice, directly to function(s_in) without making it contiguous first.
The Fix
One line changed in comfy/utils.py:
# before
ps = function(s_in).to(output_device)
# after
ps = function(s_in.contiguous()).to(output_device)
That single line fixed the corrupted output.
Related issue — still open at the time of writing:
https://github.com/comfyanonymous/ComfyUI/issues/11851
How to Apply the Patch
cd ~/ComfyUI
source venv/bin/activate
# Back up just in case
cp comfy/utils.py comfy/utils.py.bak
# Edit the file and replace:
# ps = function(s_in).to(output_device)
# with:
# ps = function(s_in.contiguous()).to(output_device)
# Restart ComfyUI
python main.py
Notes
--force-fp32 and PYTORCH_ENABLE_MPS_FALLBACK=0 were useful for narrowing things down, but precision settings weren’t the real issue here — memory layout was.
So “MPS breaks it” isn’t quite accurate. A more precise description would be: “paths that encounter a non-contiguous input tend to break on MPS.”
Maintenance Note
Manual edits to comfy/utils.py get overwritten when you update ComfyUI. If the problem comes back after an update, reapply the same one-liner.
If you see similar corruption from Sharpen or other nodes that do BHWC -> BCHW conversion, inserting .contiguous() right after permute/movedim should prevent recurrence.